Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Related: #28627 (IVF read/decompression amplification), #28898 and #28918 (overlapping reads/allocations for the same key). This report isolates two independently reproduced MemCache accounting/reclamation defects. Reproducer A uses different keys and different backing allocations; reproducer B is serial and requires no duplicate read.
Recent open/closed searches since 2026-09-11 for reservedBytes, evictBatch, EvictToTargetWithWait, MemCache, and FIFO did not find a report covering these specific counterexamples. #28898 was read and compared, not excluded solely by its title.
Branch Name
main
Commit ID
- Executed diagnostics:
6690f1af97977ad5671235e9204296cd2d504ad5. - Latest main rechecked on 2026-09-18:
480b91764a94cf9ab49d3f2b43a60cb63ce677c6. git diff 6690f1af97977ad5671235e9204296cd2d504ad5 480b91764a94cf9ab49d3f2b43a60cb63ce677c6 -- pkg/fileservice pkg/common/mallocis empty. Thus the tested cache/allocation implementation remains unchanged on this main HEAD. This is code-equivalence verification, not a claim that a second complete MO image was deployed and tested.
Other Environment Information
- Linux/amd64 test host, 20 CPUs and approximately 62 GiB RAM.
- Go 1.26.4, repository
mo-cgo-testwrapper,GOMAXPROCS=4,GOMEMLIMIT=1GiB. - Real
pkg/fileserviceMemCache, DataCache, FIFO and native allocator. Existing native build artifacts were reused; no full MO image rebuild or SQL-cluster test was performed. - Each data block is 1 MiB. Actual allocator backing size was checked to be exactly 1,048,576 bytes, larger than the 64 KiB idle-buffer-pool threshold.
- Cache capacity: 4 MiB for A and 3 MiB for B. No production-source changes, remote object-store dependency, random sleep, OOM, external cache pressure, or cluster reconfiguration.
GOMEMLIMITis only a Go runtime soft limit, not a hard bound on CGo/process RSS.
Actual Behavior
There are two separate defects in the capacity-accounting/reclamation path. They should not be conflated with normal FIFO policy decisions or assumed to share one root cause.
A. Reservation-to-FIFO handoff double-counts one allocation and evicts data that fits
The real MemCache can evict an existing entry even though all four distinct backing allocations fit its capacity. A deterministic channel barrier selects the interval after the real DataCache.Set has returned but before MemCache.Update commits that same buffer's reservation.
| State/result | Handoff paused | Control: handoff completes before next allocation |
|---|---|---|
| Cache capacity | 4 MiB | 4 MiB |
| Used after C's real Set | 3 MiB | 3 MiB |
| Reserved after C's real Set | 1 MiB | 0 |
| Used after inserting D | 3 MiB | 4 MiB |
| Resident keys | B, C, D | A, B, C, D |
| Eviction callback | A, 1 MiB | none |
| Used/reserved after final cleanup | 0 / 0 | 0 / 0 |
Raw diagnostic output:
blocked=true unit=1048576 capacity=4194304
set_return_used=3145728 set_return_reserved=1048576
after_D_used=3145728 after_D_reserved=1048576
resident=map[A:false B:true C:true D:true]
postEvict=[fifo-validation/double-charge/1/1048576]
final_used=0 final_reserved=0
The remaining reservation in this snapshot belongs to paused C and is expected to remain pending until C resumes. The incorrect outcome is evicting A and retaining only 3 MiB of four otherwise fitting blocks, not merely the existence of a pending reservation.
Executed validation:
TestMemCacheReservationOverlapDoesNotEvictFitEntries: fails at the no-unnecessary-eviction oracle on the unmodified implementation.TestMemCacheReservationOverlapNoBlockControl: passes with all four keys resident.-count=20: 20/20 failing counterexamples and 20/20 passing controls.-race: reproduces the same logical failure without aDATA RACEreport.
The test gate delegates to the real Set first and then pauses only C. It does not override Used, reservation values, backing sizes, eviction results or allocator behavior. It enlarges a reachable scheduling interval; it does not measure that interval's duration or frequency in production.
B. Synchronous eviction mistakes a deleted main-queue record for no progress
After deletion has released an entry's value but left its lazy queue record, EvictToTarget(ctx, 0) can return while live entries remain, even with a live context and no concurrent insertions.
deleteHead=true unit=1048576 capacity=3145728
used_after_sync=2097152
resident_after_sync=map[A:false B:true C:true]
postEvict_after_sync=[]
Only A was deleted. B and C are separate file paths and remain valid cached allocations. The trace resets its eviction recorder after A's DeletePaths callback so the empty callback list refers to the subsequent synchronous eviction.
The control without DeletePaths reaches zero and evicts all three values. A second reclamation call after the counterexample clears B/C; final Used and reserved bytes are zero. This proves premature termination of one reclamation call, not an unconditional permanent hang.
TestMemCacheDeletedMainHeadDoesNotShortCircuitSynchronousEvict: fails at the zero-target oracle.TestMemCacheDeletedMainNormalEvictControl: passes.
Normal-policy control
The production FIFO was also tested with 100 equal-sized capacity units initially occupied by 100 old hot entries, then a repeated 60-entry new working set:
| Round | New-working-set hits | Old resident entries | Used units |
|---|---|---|---|
| 1 | 0/60 | 90 | 100 |
| 2 | 10/60 | 40 | 100 |
| 3 through 20 | 60/60 each round | 40 | 100 |
TestFIFOValidationNormalWorkingSetFitsAfterOldHotPrefill passes. Old hot entries do not indefinitely prevent this fitting working set from warming. This control is not a claim that FIFO must behave as LRU or that every workload must fit after one scan.
Expected Behavior
- Transferring one backing allocation from pending reservation to FIFO ownership should not cause a competing allocation to count that same backing twice and unnecessarily evict otherwise fitting data. Avoiding double-counting must not introduce an under-accounted allocation interval.
- Synchronous eviction should continue when removing a lazy deleted queue record has made progress and additional reclaimable live entries remain above target. Whether a new value-release callback was produced is not equivalent to whether queue/accounting progress was made.
These expectations do not forbid normal capacity eviction, guarantee a minimum residency time, require a different replacement policy, or justify exceeding memory budgets.
Steps to Reproduce
The complete executed diagnostic source is supplied in a follow-up comment, so reproduction does not depend on access to the test host or private logs.
A: distinct-key handoff interleaving
- Create a 4 MiB MemCache; AllocateCacheData/Update A and B, each 1 MiB. Release caller references. Used=2 MiB, reserved=0.
- Allocate C, which creates a 1 MiB reservation.
- Run C's Update through a test-only DataCache wrapper that delegates the real Set, signals a channel, then pauses before returning to Update.
- At that boundary, verify Used=3 MiB and reserved=1 MiB, although only A/B/C have been allocated.
- Allocate and Update distinct key D, also 1 MiB. All four unique allocations would fit the 4 MiB capacity, but A is evicted.
- Release the gate, join C's Update, release vectors exactly once and flush. Verify final Used/reserved=0.
- Repeat without the pause. There is no eviction and all four values are present.
B: serial lazy-deletion trace
- Create a 3 MiB MemCache. Insert A/B/C, each 1 MiB and each at a different file path.
- Get each entry twice to make it eligible for promotion to the main queue.
- Delete only A via MemCache.DeletePaths. Its value is released, but its queue record still carries the access count and size.
- Call MemCache.EvictToTarget with a non-canceled context and target=0.
- The small-queue records are promoted. Main-queue eviction processes already-deleted A and returns early; B/C remain cached and Used=2 MiB.
- Compare the same trace without step 3: target=0 is reached in one call.
Commands after installing the diagnostic files and preparing the repository's Go/CGo dependencies:
GOMAXPROCS=4 GOMEMLIMIT=1GiB .agents/skills/mo-dev/scripts/mo-cgo-test \
-v -count=20 -timeout=120s \
-run '^TestMemCacheReservationOverlap' ./pkg/fileservice
GOMAXPROCS=4 GOMEMLIMIT=1GiB .agents/skills/mo-dev/scripts/mo-cgo-test \
-v -race -count=1 -timeout=120s \
-run '^TestMemCacheReservationOverlapDoesNotEvictFitEntries$' ./pkg/fileservice
GOMAXPROCS=4 GOMEMLIMIT=1GiB .agents/skills/mo-dev/scripts/mo-cgo-test \
-v -count=1 -timeout=120s \
-run '^TestMemCacheDeletedMain' ./pkg/fileserviceThese commands intentionally exit nonzero on the affected code because the tests assert the desired invariant. They are bug reproductions, not passing fix-validation results.
Additional information
Proven root cause A: non-atomic accounting handoff
Exact code on the rechecked main commit:
- MemCache.reserveCacheData, lines 439-474 admits using
capacity - Used - reservedBytes; its eviction target iscapacity - reservedBytes - want. - MemCache.Update, lines 790-803 calls Set first and CommitCacheReservation only after it returns.
- FIFO.Set, lines 271-281 charges the item to queue Used or pendingBytes before returning.
Therefore, the C allocation is simultaneously included in Used and reservedBytes during that interval. D's decision sees 4 - 3 - 1 = 0 MiB available instead of 1 MiB. Its requested eviction target becomes 4 - 1 - 1 = 2 MiB; the real FIFO removes A to reduce Used from 3 MiB to 2 MiB. This directly explains the observed callback, missing A and 3 MiB final residency. The unpaused control removes this exact precondition and preserves A.
This is synchronized operations composing into a logical accounting defect, not an unsynchronized-memory data race. The existing TestMemCacheCommitsReservationAfterFIFOUsageIsCharged protects against the opposite, under-counting window; it does not cover a concurrent allocation before reservation commit.
Proven root cause B: release-result is confused with progress
- DataCache.deletePath removes map membership and purges the value but intentionally leaves the queue record/accounting for lazy cleanup.
- FIFO.evict1 can promote this previously hot record to queue2.
- FIFO.evict2 dequeues the dead record and subtracts its size from used2, but returns
evicted=falsebecause the value was already purged. - FIFO.evictBatch, lines 763-778 recognizes progress only through the recorded used1 change. It does not recognize this used2 decrease and returns a batch with
evicted=false. - FIFO.evictWithWait, lines 718-732 stops on
!evicted, even though Used=2 MiB exceeds target=0 and B/C are still available to reclaim.
No queue fields were forged. The production S3FS.Delete -> MemCache.DeletePaths path can create these lazy records. MemCache.Flush uses the same synchronous zero-target primitive. Also, reserveCacheData rejects a reservation when this primitive returns above target; the caller then uses a non-cache-admitted transient buffer. Those are real consumers of the faulty progress decision.
Impact and evidence boundary
Confirmed: unnecessary eviction for otherwise fitting distinct allocations; incomplete synchronous reclamation with valid cached data remaining; controls and cleanup behavior described above.
Potential workload consequences: additional memory-cache misses, disk reads/decompression and transient non-cached allocations, or incomplete reclamation at a requested boundary. Their production frequency and scale have not been measured by these UTs.
Not established: data corruption, the entire cause of an IVF Daily's multi-fold slowdown, the fraction of production evictions caused by A, whether B was exercised by the affected read-only IVF objects, or a specific regression-introducing PR. Neither full IVF 10M nor a production-image A/B was executed for this report.
In particular, repeated reads of the same column every few seconds are insufficient by themselves to attribute each eviction to A or B: ordinary working-set turnover remains a valid alternative. No claim is made that fixing these two defects alone guarantees a particular QPS.
No production fix is included. The two root causes and their regression oracles are recorded separately to support a bounded repair without changing general FIFO policy, removing reservation safeguards, or expanding memory budgets.
Activity
LeftHandCold commentedon Sep 18, 2026
Complete executed diagnostic sources
These are temporary package-level diagnostics, not a production patch. They were executed on
6690f1af97977ad5671235e9204296cd2d504ad5;pkg/fileserviceandpkg/common/mallocare unchanged at rechecked main480b91764a94cf9ab49d3f2b43a60cb63ce677c6.A and B assert the intended invariants and fail on the affected code. Their corresponding controls pass. No FIFO counters/queues are modified by the harness. The handoff gate only pauses after the real Set for one chosen key and allows other Set calls to continue; cleanup joins the gated Update before releasing its caller reference.
pkg/fileservice/mem_cache_reservation_overlap_test.gopkg/fileservice/fifocache/fifo_normal_working_set_test.goThis passing control uses capacity=100, a 60-entry new working set and repeated access. It does not incorrectly require one-pass LRU behavior or use a degenerate zero-sized probation target.
Commands and expected results are in the issue body. The normal policy control is selected with
-run '^TestFIFOValidationNormalWorkingSetFitsAfterOldHotPrefill$' ./pkg/fileservice/fifocache.LeftHandCold commentedon Sep 18, 2026
Fix submitted to main: #29092, commit
ce6176bb93f35b9248a5d4a3d8e890af471a9b07.Reproduction and fix verification
The two implementation defects are confirmed independently of the Daily performance attribution. On server 50, the final patch passes the original production-path cache reproductions and controls:
DataCache.SetreturnsDeletePaths(A), then synchronous target-zero evictionAll final scenarios finish with both usage and reservation equal to zero. The harness uses the actual MemCache/native backing and Set/Get/DeletePaths/eviction paths, not fabricated production counters. Adapting the original harness required the private counter's new
.Load()accessor and changing the old diagnostic expectation for C's reservation from 1 MiB to zero; the residency, no-unnecessary-eviction, target, and cleanup oracles are unchanged.The fix makes reservation handoff atomic with capacity admission and recognizes progress in both FIFO queues. It does not change the FIFO replacement policy, cache sizes, SQL semantics, or S3/DiskCache read policy. External callbacks remain outside the accounting guard. Values without the reservation protocol do not acquire that guard; a dedicated UT protects this fast path.
Validation on the final sources:
-race -count=100: pass.go vet, formatting, and diff checks: pass.Same-host/same-command five-run microbenchmark medians (baseline -> final patch): Update 3150 -> 3191 ns/op; Read 248.2 -> 240.6; sequential Set 874.8 -> 878.4; parallel Set 244.3 -> 247.6. Allocs/op are unchanged. An earlier local prototype unnecessarily serialized non-reservation Set and regressed that microbenchmark by about 2.2x; it was rejected and is not the submitted patch. These measurements are not a blanket zero-overhead guarantee.
Introduction history
These are not defects present since the original FIFO introduction (#14019, January 2024):
31adf54ab1, August 18, 2026).8c6e37f8c8, May 27, 2026).69aaf6d689, May 28, 2026).This attribution is based on the introducing commits and their parent code, not just PR titles. It is not a complete old-version deployment bisect.
Live telemetry: evidence boundary
Read-only Grafana/Loki checks of ONLY/PRE round 2 showed:
Loki also confirms the same approximately 25 MB physical key being read from disk and set into memory repeatedly on one AP, at 04:12:01, 04:12:17, and 04:12:31 CST. However, telemetry does not export the reservation handoff, per-key FIFO eviction reasons, or deleted queue records. The above ratios aggregate AP traffic in the selected windows, not an exclusive per-query counter; the isolated round also has only four 30-second samples.
Therefore: poor reuse is observed, and the two code defects are reproduced and fixed, but their contribution to the complete Daily slowdown is not yet proven. Zero RSS-pressure counters do not mean zero ordinary capacity eviction. No full SQL/IVF production-image A/B was performed, and this PR must not be presented as a guarantee of restoring a particular IVF QPS.
fix(fileservice): correct cache accounting and reclamation (#29092)