Skip to content

fix(fileservice): correct cache accounting and reclamation - #29092

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/memcache-accounting-reclamation-29088
Sep 18, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
LeftHandCold:fix/memcache-accounting-reclamation-29088

Conversation

@LeftHandCold

Copy link
Copy Markdown
Collaborator

What type of PR is this?

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

Which issue(s) this PR fixes:

Fixes #29088

What this PR does / why we need it:

This fixes two independently reproduced accounting/reclamation defects in FileService MemCache:

  1. A successful insertion could charge one backing allocation to FIFO usage before releasing its allocation reservation. A competing allocation could count the same backing twice and evict data even when the distinct allocations fit the configured capacity.
  2. Synchronous eviction could stop after cleaning an already-deleted main-queue record. That operation reduced used2, but the progress check only recognized used1 changes or a newly released value, leaving reclaimable live entries above the requested target.

The reservation handoff now uses the same accounting guard as allocation admission. FIFO used/pending bytes are charged before committing the reservation, with no intermediate state visible to another admission. Reservation release uses an atomic decrement rather than reacquiring the guard. Values without the reservation protocol do not acquire this guard.

External Set/eviction callbacks remain outside the guard. The pressure-admission path publishes the insertion ownership before commit/eviction callbacks can panic, so deferred cleanup cannot release an already-admitted value as a rejected insertion. Both synchronous-eviction loops now recognize changes to either queue counter, including promotion that changes their distribution without changing their sum.

The scope excludes FIFO replacement-policy changes, larger cache budgets, S3/DiskCache read-policy changes, and SQL or persisted-format changes. Cache-hit reads do not acquire the accounting guard. The regression tests target the internal cache contract, so no new SQL BVT or production fault hook is added.

Validation

Validated on an isolated Linux/amd64 source checkout on server 50, using Go 1.26.4. SHA-256 checks match all 11 changed source/test files to the submitted worktree. No running cluster was replaced.

  • Original issue reproductions and their controls: four 1 MiB allocations fit a 4 MiB cache without eviction; deleting a hot head no longer prevents synchronous eviction to zero. All four entries and final zero usage/reservations are checked.
  • Added focused UTs for reservation handoff, pending enqueue, callback-panic cleanup, target-zero/nonzero reclamation, pressure admission, and the non-reservation fast path. Existing duplicate/admission/release tests are retained.
  • Full pkg/fileservice and pkg/fileservice/fifocache tests, both normally and with -race: pass.
  • Focused FIFO, MemCache/original-repro, and six ObjectIO consumer tests with -race -count=100: pass. ObjectIO coverage includes validated-vector wrappers and memory/remote cache admission.
  • go vet -mod=readonly ./pkg/fileservice/fifocache ./pkg/fileservice ./pkg/objectio: pass, using the repository CGo header/library setup. ObjectIO was executed with mo-cgo-test.
  • gofmt and git diff --check: pass.

Same-host, same-command microbenchmarks (-benchmem -count=5, medians in ns/op):

Benchmark Main baseline Final patch
MemoryCacheUpdate 3150 3191
MemoryCacheRead 248.2 240.6
MemCacheSetSequential 874.8 878.4
MemCacheSetParallel 244.3 247.6

Allocs/op are unchanged. An earlier local prototype unnecessarily locked non-reservation values and regressed parallel Set; that prototype is not included. The final UT explicitly requires a non-reservation Set to finish even while the accounting guard is held. These are package/microbenchmark checks, not an end-to-end IVF throughput guarantee.

Evidence boundary

The issue reproductions prove implementation defects, not the fraction of an IVF Daily slowdown caused by them. This PR must not be presented as a guarantee of a specific IVF QPS or as a complete production-image performance A/B.

Make allocation-reservation handoff atomic with FIFO charging, while preserving the unlocked path for values without reservations. Continue synchronous reclamation after deleted main-queue records and cover ownership, panic cleanup, and pending enqueue boundaries.

Fixes matrixorigin#29088
@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 →

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Code review of ce6176b against base/merge-base 480b917: no blocking findings identified. This is a focused correction of the two independently reproduced #29088 defects, not a FIFO-policy redesign.

Reviewed all 11 changed files and traced reservation creation/commit/release, MemCache.Update, DataCache adapters, Bytes and validatedVectorCacheData forwarding, normal/pending/pressure enqueue, duplicate/rejected admission, callback panic cleanup, and synchronous eviction consumers.

Audit Conclusion
Q1 ownership Reservation CAS remains one-shot; duplicate/rejected inserts do not prematurely commit; pressure admission publishes insertion ownership before fallible callbacks
Q2 locking/progress Accounting guard precedes queue/shard acquisition; production commit only performs CAS/atomic decrement, without reacquiring capacityMu; external post callbacks execute outside that guard. Both eviction loops recognize changes to either queue
Q3 accounting Used/pending charge and reservation handoff are protected from competing allocation admission; no cache budget increase or new background resource

Validation independently run on this exact head with GOMAXPROCS=2, GOMEMLIMIT=1GiB and -p 1: full fifocache package -race -count=1 passed; each of TestDataCacheSetWithoutReservationDoesNotTakeAccountingGuard, TestPressureAccountingCommitPanicCleansPendingEviction, TestAccountingReservationIsHeldThroughPreEnqueuePostSet and TestAccountingCommitChargesPendingEnqueueBeforeSetReturn separately passed -race -count=100. Diff check passed; review worktree is clean. Wider fileservice/ObjectIO tests, original reproductions and benchmarks are documented by the author in the PR; I did not rerun those Linux/native validations or an IVF workload. CI was still running when inspected, so this is not a claim that all CI has passed or that a particular IVF speedup is guaranteed.

Code-level approval recommendation. The authenticated account is also the PR author (LeftHandCold), so GitHub disallows a formal self-APPROVE; recording the review here instead.

@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.

Deep review (Astra medium, exact head ce6176b)

APPROVE.

Reviewed all changed hunks across 11 files against base 480b917.

Ownership: reservation commit/release remains one-shot; duplicate/rejected insertion preserves cleanup ownership; pressure admission records ownership before fallible callbacks.

Locking/liveness: accounting handoff shares the allocation-admission guard. Production commit uses CAS/atomic decrement; post callbacks execute outside that guard.

Accounting/reclamation: direct and pending enqueue precede reservation commit. Both synchronous eviction loops recognize progress in either queue, including deleted records and promotion. Consumers and deterministic accounting/reclamation tests were traced.

This focused bug fix is exempt from the large-feature design gate. No SQL, protocol, persisted-format, or platform contract changes require additional artifacts.

Exact-head CI workflow success was verified, including required CI. Raw CI log retrieval failed due network errors; no local Linux/native validation was rerun. No required fixes identified.

@mergify mergify Bot added the queued label Sep 18, 2026
@mergify

mergify Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-18 09:02 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-18 09:03 UTC · at e4475de3ef329ce8663495bc37f960b64141c579

This pull request spent 1 minute 5 seconds in the queue, with no time running CI.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Reason

Pull request #29092 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

1 inactive deployment
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 size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: FileService MemCache double-counts reservation handoff and stops synchronous reclamation early

3 participants