Repository navigation
refactor(fileservice): S3-FIFO memory cache that owns its references, LRU disk cache - #32275
Conversation
Replace fifocache with pkg/fileservice/s3fifo for the memory cache, the objectio meta cache and the disk cache index. - The cache retains a value when it inserts it and releases it once the value has left the cache, outside its locks; notifications are optional and no longer carry reference counting. - Keys are indexed in 256 shards and queued in 16 policy shards; every push is ordered globally and eviction takes the oldest member, so the shards evict as one S3-FIFO. One goroutine evicts at a time. - A Set inserts, makes its value evictable once its insertion is notified, then evicts until Used is within the capacity, waiting for the evictor: Used no longer stays over the capacity after Sets return. - The ghost queue counts only the entries it remembers; groups are an intrusive list, so DeletePaths needs no scan. - A panic committing a reservation rolls back the charge and the cache's reference. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A Set that finds another goroutine evicting returns, unless Used is over the ceiling, a sixteenth over the capacity, where it waits to evict too. The goroutine evicting checks Used again each time it lets go of the evictor, so it evicts the bytes of the Sets that left eviction to it. A Set whose key is cached returns after one index check, a value is published under the index shard lock only, and a Set evicts in small batches. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ghost entries keep counting their bytes until they leave the ghost queue, as the cache it replaces did, which restores the warm hit ratio. A read retries its frequency bump until it lands. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The data disk cache now evicts least recently used files, as the index-file cache does. With TPC-H and wiki_all 1M alternating on one server, TPC-H reads about half as many objects from S3 and runs about 27% faster; wiki_all is unchanged. The LRU cache now runs eviction callbacks outside its lock, so a callback may use the cache. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keys are spread over 16 shards with their own locks, and a global stamp of each item's last use keeps eviction least recently used across the shards. Set and Replace leave eviction to a running evictor, which evicts in batches and checks again when it is done. Parallel Get-or-Set at 12 threads drops from about 706 to 143 ns/op. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each contract of docs/design/20261106-fileservice-cache-ownership.md checked through the public API: cache-owned references, publication, the used-byte bound, notifications calling into the cache and ghost re-insertion for the S3-FIFO caches; LRU order, held entries, oldest inserts and callbacks outside locks for the disk caches; and an object being downloaded fetched once, with a read far behind the download reading its range. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tarted it The read that starts a whole-object download waited for either the download to start or the fill to end. When the source failed right after the read's bytes were written, both were ready and the select could pick the fill's error, failing a read whose bytes were downloaded; TestS3FSReadFullObjectToDiskCacheStreamingErrorAfterCapture failed about half the time. The read now prefers a started download, keeping the fill's result for later. Readers also reopened the download's file by name, which a publication or a failed download's cleanup could remove first. A download now keeps one read handle, closed once it is retired and the last reader is done, and the read that starts a download holds it before any byte is fetched. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…gain A read beyond the max extra wait read its range at once. When its bytes are more than an eighth of what the download still writes before them, a range read fetches again most of what the download is about to write, competing with it; such a read now waits for the download instead, bounded by the predicted time plus the margin. The new mo_fs_disk_cache_download_declined_bytes_total metric counts the bytes range reads fetch again. disk-download-max-extra-wait defaults to 200 ms, the budget of the previous fixed wait for a downloading object. On TPC-H SF10, two cold rounds at 1 GB memory and 4 GB disk, 200 ms took 47.6 s and 2,552 requests against 53.4 s and 4,428 at 50 ms on local object storage, and 49.3 s and 2,193 against 67.7 s and 3,199 behind an emulated remote store. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…loads Design-only revision for review: a read beyond the margin waits when its bytes are most of what the download writes before them; the margin defaults to 200 ms; measurements of the 50 ms margin and of the duplication bound alone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
mo-bot review · Commit: Review linked issue or PR description changed before publication; a fresh contextual review is queued. |
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 4deacbe75086cf12d280d384bf6102bb4db4c982
Assessment: request_changes
Cache-owned references and shared LRU reuse are justified, and the design gate is resolved. The new benchmarks do not resolve three implementation defects.
Model: gpt-6.1-sol / max
Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0
-
P1
pkg/fileservice/s3_fs.go:1560: A read captures its bytes while all eight background-fill slots are occupied, then its context is cancelled during waitSet.
Impact: The foreground read and storage copy ignore cancellation until the whole object finishes.
Evidence: fillCtx strips cancellation at line 1494; waitSet receives setDone before calling cancelFill. The source-extracted probe fails while its cancellation-forwarding control passes (download-cancellation-probe.log). Forward cancellation until successful background handoff and test cancellation after capture with saturated slots. -
P2
pkg/fileservice/s3fifo/cache.go:605: Delete or Replace removes the selected shard's head between oldest(small) and step acquiring its lock.
Impact: Eviction can remove a newer entry ahead of an older entry in another shard, violating the global FIFO contract.
Evidence: For insertion order A1, B2, A3, deleting A1 after selection makes step evict A3 while B2 remains. The actual-package witness fails; reselecting passes (s3fifo-oldest-probe.log). Carry and revalidate the selected head order under the policy lock, retrying when it changes. -
P2
pkg/fileservice/disk_cache.go:234: During asynchronous startup loading, a live file is indexed/read before older ordinary cache files are enumerated.
Impact: Old files receive fresh LRU priority and can displace the live working set, causing avoidable object-storage reads.
Evidence: The new LRU owner is installed here, but loadCache still calls Set at line 315. indexLoadedDirs already uses SetOldest. The loader-equivalent probe evicts the hot entry; its SetOldest control retains it (lru-startup-probe.log). Use SetOldest for ordinary startup files too.
Validation:
-
Fresh race runs passed 28 S3-FIFO and 13 LRU tests without skips; isolation preflight passed.
-
Three candidate/control probes reproduced the findings; every correction control passed.
-
Static audit passed patch correspondence, whitespace, formatting of 40 Go files and seven-item discussion coverage. All six receipts, terminal exits and logs are reconciled in validation-evidence.json.
-
Frozen CI reports 13 successful, 14 skipped and two neutral checks. Updated benchmarks were assessed as author-reported evidence.
Limitations:
-
Native fileservice/objectio tests and live S3 cancellation were not rerun locally; cancellation uses source tracing and an extracted-method harness.
-
Raw author runtime/benchmark logs were unavailable; remote-store and multi-CN performance were not independently measured.
…oad files as oldest - A read waiting for its download, with no background slot free, now cancels the download when its context is cancelled; cancellation stops forwarding only once the download is handed to a background slot. - S3-FIFO eviction checks under the shard lock that the queue head it chose is unchanged, and chooses again otherwise, so a concurrent delete cannot make it evict a newer entry ahead of an older one in another shard. - The disk cache's startup load indexes the files it finds as least recently used, as the design states, so a file read during the load is not evicted ahead of them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 491cd699b7c95b0e7b88bab2c7100de788548aef
Assessment: approve
Cache-owned references and shared LRU reuse remain justified. This head resolves the three prior cancellation, FIFO ordering and startup-recency findings.
Model: gpt-6.1-sol / max
Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0
Validation:
-
Reviewed the six-file follow-up and affected ownership/liveness paths; verified 46 unchanged files or deletions against the frozen prior review.
-
Assessed all nine discussion items and reconciled the prior findings.
-
Reused frozen successful CI results and qualified prior race, model and benchmark reports.
-
Isolation preflight and source/provenance/format audit passed. Receipts and retained logs are reconciled in validation-ledger.json.
Limitations:
-
The fresh local race attempt executed zero tests: downloading Go 1.27.1 timed out before compilation.
-
Runtime and performance evidence is attributed to supplied reports; raw author artifacts and live remote-S3 validation were unavailable locally.
✅ Merged into
|
What type of PR is this?
Which issue(s) this PR fixes:
issue #32274
What this PR does / why we need it:
Design: docs/design/20261106-fileservice-cache-ownership.md v2 at 4deacbe750 (design-only commit, for design review before implementation review; v1 was 279858c).
The memory and meta caches move to a new
fileservice/s3fifopackage, and the data disk cache now uses a shardedlrucache.fifocacheis deleted. The diff is +3288 / −3204 lines.s3fifo(memory and meta caches)Setleaves eviction to an evictor that is already running, unless the cache is more than 1/16 over capacity. Then it waits, so memory stays bounded.Replacelarger than the capacity deletes the key.fifocache.lrucache(data disk cache and index-file cache)SetOldest, while an eviction scans the shards can be passed over by that eviction.SetandReplaceleave eviction to an evictor that is already running. That evictor checks again when it is done, and evicts in batches of 64.Whole-object prefetch and downloads
disk-download-max-extra-wait(default 200 ms) later than a range read could fetch them, or if they are more than an eighth of what the download still writes before them.mo_fs_disk_cache_download_read_total{type}andmo_fs_disk_cache_download_declined_bytes_total.Tests
s3fifo); LRU order, held entries, oldest inserts and callbacks outside locks (lrucache); an object being downloaded fetched from storage once, and a read far behind a download reading its range (S3FS.Read). 12 tests, each ×30 under-race.s3fifomodel: checked against a sequential model of the algorithm. Used bytes, small/main/ghost queue bytes and key membership are compared after every one of 20k random get/set/replace/delete ops.lrucachemodel: checked against a single-list LRU model. The full recency order and used bytes are compared after every one of 10k random set/SetOldest/get/delete/replace/hold ops.TestScanEvictsOldestAfterConcurrentGettouches the picked shard's oldest item while the eviction scan reads another shard, and checks that the globally oldest item is evicted.lrucachemodel test also fails if eviction picks any shard but the oldest, or ifSetOldestis stamped as newest.s3fifo: 37 focused tests repeated under-race(count 100, 50, 30 or 2 depending on test length).lrucacheand the disk cache: the LRU tests ×50,TestDiskCacheReindexBeforeEvictionStartKeepsFile×50, and allTestDiskCache*and index-file-cache tests ×5, all under-race.s3fifo,lrucache,fileserviceandobjectioeach run once under-race.main: everything builds, andfileserviceandobjectiopass.Benchmarks
Setup: one CN, MinIO. TPC-H is SF10 (5.0 GB in MO). wiki_all 1M is 768-D vectors with an IVF-Flat index (lists=1000, probe_limit=8): 2.9 GB of table plus 2.9 GB of index. TPC-C is 10 warehouses (about 1 GB), 50 terminals.
Partly cached: 1 GB memory cache, 4 GB disk cache
Neither workload fits in the caches, so each round evicts the other's data. Each benchmark ran twice.
Final check, TPC-H → wiki → TPC-C. Against main, one run each, with
tpccrestored from the same snapshot (and left 2 minutes for the restore's merges) before each run; cold start; 2 cycles of one TPC-H round, one wiki round and 5 min of TPC-C (10 warehouses, 50 terminals). Emulated S3 is a proxy in front of MinIO adding 25 ms per request and capping each response at 100 MB/s.disk-download-max-extra-wait= 200 ms, before the duplication bound; the emulated-S3 PR column is 4deacbe with defaults.Swap test (measured at 76ddc0f, before the prefetch and download changes). Cold start, then 4 cycles of one TPC-H round (22 queries) followed by one wiki round (filter, nofilter and proj, 100 queries each). Results drifted by up to 50% over the evening, so each table compares only runs that alternated in one session.
main vs this PR at 76ddc0f (sharded LRU disk)
Disk policy, with this PR's memory cache (S3-FIFO disk at 1107dd6, single-lock LRU disk at d62e924)
Sharding, single-lock (d62e924) vs 16-shard LRU (76ddc0f)
TPC-H, cold start, 2 rounds (earlier build, before 1107dd6, S3-FIFO in memory and on disk; total seconds for both rounds)
The memory hit ratio is equal or higher than main's, and S3 reads are similar or lower.
wiki_all 1M, warm (same earlier build): memory hit ratio .689 vs main's .685–.690; latency about 22.2 ms on both.
Fully cached: 14 GB memory cache, 20 GB disk cache
All the data fits on disk, so nothing is evicted. This measures the cost of a cache hit rather than the eviction policy. Runs alternated main and this PR.
Final check, TPC-H → wiki → TPC-C, fully cached. Head 4deacbe with defaults against main, one run each, same procedure as the partly cached final check.
Even on every measure; TPC-C differs by −3.2% and +2.9%.
TPC-H and wiki (measured at 76ddc0f, before the prefetch and download changes). Cold start, then 2 cycles of one TPC-H round and one wiki round. Cycle 1 fills the caches and cycle 2 is measured.
Both read the same items. Even with 14 GB, TPC-H hits memory for 283k of 688k reads, and the other 405k all hit the disk cache.
TPC-C (measured at 76ddc0f). Restored from the same snapshot before each run, then cold start and 5 minutes of TPC-C.
No errors in any run.
Disk-cache hit (measured at 76ddc0f; 4 KB random read from 4,096 cached 64 KB files, ns/op)
Opening and reading the file dominates each hit. Both make 26 allocations per read.
Micro-benchmarks
Micro, memory cache (ns/op, main → this PR at 1107dd6)
MemCacheSetParallellooks faster on main only because main stops evicting and stays at about 200% of capacity in this benchmark. ItsSets do no work; this PR stays within its bound.Set: about 650 → 677 ns/op.Set: 3 → 2.Micro,
lrucache(parallel Get-or-Set, ns/op; single lock at d62e924, 16 shards at 44eadb1)The extra cost on one thread is small next to the file I/O behind each disk cache operation.
Limits
disk-download-max-extra-waittrades latency for fewer requests; the metrics show how reads of downloading objects are served.🤖 Generated with Claude Code