Skip to content

fix(fileservice): bound background downloads, keep cost estimates and read share honest - #32345

Merged
mergify[bot] merged 7 commits into
matrixorigin:mainfrom
cpegeric:fileservice_download_fixes
Nov 9, 2026
Merged

mergify[bot] merged 7 commits into
matrixorigin:mainfrom
cpegeric:fileservice_download_fixes

Conversation

@cpegeric

@cpegeric cpegeric commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #32274

What this PR does / why we need it:

Follow-up to #32275. Review of the merged code found seven defects in the object-download layer and the LRU disk cache. Each fix comes with a regression test that fails without it. The design (docs/design/20261106-fileservice-cache-ownership.md) moves to v3 to record the contracts these fixes add.

Defect Fix
A download that moved to the background ignored the request's deadline and DiskCache.Close, so a stalled stream held its background slot forever. The download keeps the reader's deadline and ends when the disk cache closes.
With all 8 background slots taken, a read that already had its bytes failed when the download failed later. That read returns its bytes; only its own cancellation fails it.
An item larger than the LRU disk cache's capacity was inserted as newest, and eviction deleted every other file first. Such an item is inserted as oldest and evicted first.
Bytes read from an object while it downloaded never counted toward its read share, so the thrash signal could switch a fully used table to range reads. Reads served by a download count toward the cached file once it is published.
The download-rate estimate also took in disk-cache writes copied from memory (the write path and content reads), at GB/s. Copies from memory are not tracked as downloads; only downloads from object storage feed the rate.
Range-read cost was timed from open to close, including the time a caller spent consuming a streamed reader and reads that failed. Only range reads the file service drains itself, to the end, are timed.
An LRU item could be evicted before its insert notification ran, which leaked a read-share entry. An item is not evicted until its insert notification returns.

A second commit (1e0a2ecc1a) answers the review of the first and a self-review of it:

Defect Fix
Untracking copies from memory also stopped them recording their size for the room check, and let other readers fetch and cache ranges of an object being written. Every whole-object fill is tracked as a download again; only downloads from object storage feed the download rate.
Bytes read from a download were credited only when it finished, so they were lost if the file was evicted at publication or the read was still running then. Bytes are counted before the read and credited when the file is published.
While an item larger than the capacity waited for its insert notification, other inserts evicted everything else for it. Items whose insert notification has not returned are neither evicted nor counted toward eviction.

The prefetch design also corrects one sentence: a full LRU disk cache evicts only to make room, so the thrash signal turns off after the cache grows or files are deleted, not when files "age out".

Validation

  • Reverting each fix: its regression test fails (a timeout for the close test).
  • Race stress: the new tests, plus the existing download and streaming tests, pass 100 runs each under -race.
  • Full packages: pkg/fileservice/... passes in a normal run, and fileservice, lrucache and s3fifo each pass one run under -race.

🤖 Generated with Claude Code

… read share honest

- A background download keeps its reader's deadline and ends when the disk
  cache closes.
- A read that captured its bytes does not fail when its download fails later,
  also when no background slot was free.
- The download rate is fed only by downloads from object storage; content
  already in memory is cached without tracking it as a download.
- Range-read cost is timed only for reads the file service drains to the end.
- Bytes read from a downloading object count toward its cached file's read share.
- The LRU disk cache inserts an item larger than its capacity as oldest, and
  does not evict an item before its insert notification returns.
- Design v3 records these contracts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 0b6dd9b1c488c6bd966d8e1881da29ce0dda4fd0

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 0b6dd9b1c488c6bd966d8e1881da29ce0dda4fd0

Assessment: request_changes

The focused reuse of existing owners is appropriate, but two accounting defects remain: content fills lose object-size observations, and download reads can lose read-share credit during publication.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

  • P2 pkg/fileservice/disk_cache.go:1433: Populate a fresh disk cache through S3FS.write or the full-object content-read path, which now uses setFileContent.
    Impact: wholeObjectFits retains its 128MiB default or a stale maximum, incorrectly keeping narrow reads in range mode or reporting room for objects that do not fit.
    Evidence: trackDownload=false skips creating objectDownload at line 901, which also suppresses noteWholeFile(n) at lines 943–945. Previously these callers recorded the whole-file size. For example, an 8KiB content fill in a fresh 1MiB cache leaves the fit calculation using 128MiB. Preserve whole-file size observations independently of throughput sampling and cover content fills in the fit-policy tests.

  • P2 pkg/fileservice/disk_cache_download.go:198: A download publishes while an acquired reader remains inside IOEntry.readFrom, for example blocked in WriterForRead.
    Impact: Successful download reads receive no preload read-share credit, leaving the claimed accounting fix incomplete and allowing false thrashing signals.
    Evidence: finishDownload samples served only once, without waiting for acquired readers. served.Add occurs after readFrom returns at line 248; later release never transfers that increment. Publication-time eviction can also consume/delete the preload record before this transfer. Reconcile counts across publication, active readers and eviction; add overlap and immediate-eviction regression cases.

Validation:

  • Reviewed all nine changed files, relevant owners/consumers, closing issue and complete supplied discussion.

  • Preflight and frozen-source/Go formatting-parser audits passed; receipts and logs are matched in validation-ledger.json.

  • Frozen current-head CI reports successful build, UT, SCA, coverage and proxy/pessimistic multi-CN BVT.

  • Author reports focused race repetitions and affected-package passes; these do not cover the identified boundaries.

Limitations:

  • Local targeted race testing timed out downloading Go 1.27.1 before compilation; no product tests ran locally.

  • Regression overlay probes were retained but not executed. Findings are established by source traces; raw author/CI execution logs were not supplied.

…s at publication

- A whole-object fill from memory is tracked as a download again, so readers
  share it, Update skips its ranges and the room check records its size; only
  downloads from object storage feed the download rate.
- Bytes read from a download are counted before the read and credited to the
  file when it is published, so a file evicted at once, or a read still running
  at publication, keeps them.
- The LRU disk cache neither evicts an item nor counts it toward eviction until
  its insert notification returns, so an oversized item no longer empties the
  cache while it is inserted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@XuPeng-SH

XuPeng-SH commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 1e0a2ecc1aeae97919a907d9383719fafd6feb9e

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 1e0a2ecc1aeae97919a907d9383719fafd6feb9e

Assessment: request_changes

The existing owners are reused appropriately, and both prior accounting findings are resolved at their cited boundaries. Two advertised fixes remain incomplete: initiating streaming reads lack read-share credit, and concurrent hits can defeat oversized-first eviction. These are remaining preexisting cases, not new regressions.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

  • P2 pkg/fileservice/disk_cache_download.go:266: A cold S3FS.Read starts a whole-object streaming download and receives its bytes through fullObjectDiskCacheReader.capture, without a follower entering DiskCache.Read.
    Impact: The initiating read bypasses this accounting call, leaving its read-share contribution at zero. Subsequent evictions can classify useful objects as unread and switch their group to range reads. Include initiating captures in the publication-safe accounting and add starter-path coverage.
    Evidence: s3_fs.go:1564 calls capture; readRange at :1664-1672 copies bytes directly without calling serve or preloads.read. disk_cache.go:203-205 transfers only takeServed. The source-extracted race probe captured 4096 bytes but transferred 0; its follower control passed (host-download-probe-race.log).

  • P2 pkg/fileservice/lrucache/lru.go:139: A concurrent Get accesses an oversized item while its Set is waiting for postSet to finish.
    Impact: Get promotes the oversized item, so eviction removes older warm entries before removing it. Preserve oversized eviction priority across concurrent hits and cover this overlap.
    Evidence: set releases the shard lock during postSet (:159-162), while Get unconditionally replaces the stamp and moves the item to the front (:181-186). With capacity 100, two warm entries of 40 and an oversized entry of 150, the deterministic race probe loses both warm entries and finishes at Used()==0; the no-hit control retains 80 (host-probes-race.log). Startup file admission permits this reader overlap.

Validation:

  • Reviewed all nine changed files, four discussion entries, the linked issue and both comments, and affected owners/consumers. Saved review-plan.md, source-proof.md and complete discussion-assessment.json.

  • Host/internal race probes demonstrated both findings with passing adjacent controls. Exact source copies/extractions and input hashes are retained in host-probe-manifest.json.

  • Isolation preflight, frozen-source/diff/gofmt/overlay-syntax and probe-provenance audits passed. Eight check receipts are reconciled with retained logs and probe versions in validation-evidence.json.

  • The supplied exact-head snapshot reports successful build, UT, SCA, coverage and active multi-CN BVT. Author-reported focused race and package results were assessed separately.

  • Repository testing stopped before compilation because compatible libmo.so was unavailable. An initial host extraction omitted a shared constant; the corrected missing component was rerun. These setup failures are excluded from product-test results.

Limitations:

  • Host probes use Go 1.26.6 and extracted source, not the repository Go 1.27.1/native build. No local full file-service or S3 service execution is claimed.

  • local_validation is empty, and raw author/CI logs were not supplied. A repository-fixture regression overlay is retained but unexecuted.

…zed items oldest

- The read that starts a whole-object download counts the bytes it captures
  toward the file's read share, as other readers of the download do, so an
  object read only by its starter is not taken as unread.
- A use of an item larger than the LRU capacity does not make it newer, so a
  read while it is inserted no longer evicts warm items before it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cpegeric

cpegeric commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the finding of the review of 564066484e in 2cb7435332.

lrucache/lru.go:152: a startup file admitted through SetOldest while an oversized insertion is pending is evicted first. An oversized item was stamped with oldest.Add(-1), so a later SetOldest took a lower stamp and was evicted before it. Now:

  • An item larger than the capacity takes a stamp below every SetOldest stamp (oldest.Add(-1) + math.MinInt64/2).
  • SetOldest places its item ahead of the oversized items at the back of its shard, so each shard's order still matches its stamps, which removeOldest relies on through the shard tail.

Test: TestOversizedItemStaysOldestAfterSetOldest, the case from the review. Capacity is 100, with entries of 40 + 40 and a 150-byte insertion pending in postSet; then SetOldest(10). All three small entries stay, the oversized item is evicted, and Used() is 90. The test fails without the fix.

Validation:

  • The new test and TestEvictionDoesNotCountPendingInsertion each pass 100 runs under -race.
  • pkg/fileservice/... passes; fileservice and lrucache each pass one -race run.
  • vet and gofmt are clean.

@XuPeng-SH

XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 2cb74353326543ee4cba549e41797892c8c450c9

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 2cb74353326543ee4cba549e41797892c8c450c9

Assessment: approve

The fixes reuse existing cache owners and resolve all prior findings. No actionable PR defects remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0

Validation:

  • Reviewed all nine changed paths, affected consumers, complete discussion and linked issue context; source unchanged.

  • Preflight, source/format audits, LRU normal/race suites and full fileservice race suite passed.

  • All 38 focused race checks passed 43–100 repetitions each.

  • Baseline normal suite and deterministic deadline-fallback controls on both revisions passed. All 55 check receipts are reconciled in validation-ledger.json.

Limitations:

  • Candidate normal fileservice suite failed unchanged TestDesignDownloadFetchedOnce after a test-installed one-second deadline caused an allowed second range request. Controls confirmed this behavior on both revisions; the candidate normal suite is not recorded as passing.

  • Raw author/CI logs were unavailable. No additional SQL, real remote-storage or performance campaign was run.

@mergify mergify Bot added the queued label Nov 9, 2026
@mergify

mergify Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ Merged into main as b19ef50 at 01:39 UTC

1 h 19 min in the queue, 1 h 9 min of it CI.

So far

  • ✅ Entered the queue Mon 9 Nov, 00:20 · rule main · triggered by rule Automatic queue on approval for main
  • ✅ Passed · in place
  • ✅ Merged 01:39 · b19ef50 · squash
3 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = CI Required
    • check-neutral = CI Required
    • check-skipped = CI Required

Times in UTC · dashboard · how the merge queue works

This branch was previously deployed

1 inactive deployment
ci — b2cd98ed Deployed Nov 9, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/documentation Improvements or additions to documentation size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants