Skip to content

fix(fileservice): harden disk cache async update lifecycle - #27368

Merged
XuPeng-SH merged 10 commits into
matrixorigin:mainfrom
XuPeng-SH:agent/fix-diskcache-update-lifecycle
Aug 21, 2026
Merged

XuPeng-SH merged 10 commits into
matrixorigin:mainfrom
XuPeng-SH:agent/fix-diskcache-update-lifecycle

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Aug 20, 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:

Fixes #27358
Fixes #27359
Fixes #27360
Fixes #27363
Fixes #27366

What this PR does / why we need it:

Disk-cache operations had several coupled lifecycle problems under multi-CN load and slow or failing local storage:

  • per-path waits ignored caller cancellation;
  • the async update flag still finalized cache writes synchronously;
  • cleanup filesystem I/O ran while holding the global path-update mutex;
  • local cache write failures were swallowed with unbounded warning logs;
  • S3 full-object fills and prefetch exposed disk-cache fsync tail latency to SQL;
  • exact-range cache followers could inherit an unbounded async fsync wait;
  • async OnWritten callbacks could deadlock by reentering Flush or Close;
  • Close could execute queued filesystem cleanup in the caller, miss a finalizer still copying its source, or race final publication after shutdown cancellation.

This change closes those paths around one ownership model:

  • use context-aware, generation-safe per-path ownership and keep same-path exclusion while running cleanup outside the global mutex;
  • add one bounded async worker per disk cache, with 64 admission slots, duplicate-path coalescing, and a 64 MiB cap for copied range-entry data;
  • transfer full-file temp-file/fd/path ownership to the worker before fsync/stat/close/rename, keeping the final cache path invisible until publication;
  • make S3 write, buffered full read, streaming full read, and prefetch honor SetAsyncUpdate;
  • make FlushCache an explicit disk-cache completion barrier;
  • make Close seal and cancel synchronously but drain queued cleanup off the caller, so the caller context is an actual upper bound;
  • reserve full-file finalizers before source copy so Close observes every admitted operation and cleanup ownership transfers off the submitting read path;
  • linearize final publication and Close sealing under one mutex: Close-winning work discards its temp file, while publication-winning work remains part of the generation Close drains;
  • cap both exact-range and full-file follower waits before falling back to object storage;
  • run OnWritten callbacks as an ordered, admission-bounded post-publication chain outside Flush/Close completion barriers, allowing safe reentry without unbounded callback data growth;
  • define IOMerger completion at caller-visible cache admission: async disk publication is deliberately outside the merge generation, and later disk-cache reads retain their bounded wait/fallback behavior;
  • propagate backend/source and cancellation errors, while keeping local cache filesystem failures fail-open;
  • add a disk-cache error counter path, rate-limited WARN/recovery events, and mo_fs_disk_cache_async_update_dropped_total.

Synchronous behavior remains available through SetAsyncUpdate(false).

Validation

Local, after the final semantic edit:

  • pkg/fileservice passed its complete normal CGo package test;
  • pkg/fileservice passed its complete race package test;
  • pkg/fileservice passed go build;
  • pkg/fileservice passed go vet;
  • pkg/util/metric/v2 passed its package test;
  • nine focused wait/callback/Close/publication regressions each passed 100 consecutive race runs.

On the isolated 2-CN + MinIO harness at 10.222.1.55:

  • injected 21-second disk-cache fsync latency;
  • the fixed CN returned the exact aggregate result in 1.444 s while the 21.05 s fsync was still active; a cold control CN took 9.590 s;
  • 16 concurrent aggregate queries all returned the exact result, with no restart/OOM and stable FD/goroutine counts;
  • replacing the cache directory with a regular file during 16 cold reads preserved query correctness, emitted one rate-limited WARN, and emitted one recovery INFO after restoring the directory;
  • the final image was built with the repository Dockerfile and passed the mo-service -h smoke test.

Known follow-ups

@aptend aptend 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 re-review completed on exact head 09f82a241581e21e5b2832408f24d3b4fa455862. I read the complete review/thread history, audited the increment since my changes-requested head 048d78f1c6549dfa0bcefdcc5d4983ae3698ce8f, and rechecked the full PR diff. My open-reader eviction blocker is closed: opening a cache file now acquires a path read generation; mutation/eviction invalidates that generation before FIFO membership becomes externally invisible; and reindexing must atomically claim the still-current generation. This closes both Delete and capacity-eviction orderings without waiting for readers holding safe open descriptors. I also audited the new FIFO prepare/finish-eviction lifecycle, including update-owner transfer, callback panic cleanup, replacement behavior, and lock ordering. The checked-in deterministic Delete/ForceEvict regression passes. Full fileservice/fifocache/metric suites pass, and the focused reader-generation plus prepare-evict tests pass under -race -count=20 with the required CGo setup. No blocking findings remain.

Comment thread pkg/fileservice/disk_cache_async.go Outdated
defer d.releaseAsyncUpdateResources(job)
<-previous
for _, fn := range job.callbacks {
fn(job.filePath, job.entry)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[P1] Contain panics at the library-owned async callback boundary

Async updates now invoke OnDiskCacheWritten callbacks from the goroutine created here. If a callback panics, the caller can no longer recover around DiskCache.Update; an unrecovered panic in this goroutine terminates the entire Go process/CN. The existing close(done) and resource-release defers run during unwinding, but they do not contain the panic.

I verified this as a PR regression on the exact revisions with one deterministic counterexample: wrap cache.Update(..., true) in a goroutine with defer recover(), register an OnDiskCacheWritten callback that signals entry and panics, then wait for the recovery result. On base f331bb966e3e5ec5d0acf6e187b5366b0487294e the test passes because the callback runs on the invoking stack; on head 09f82a241581e21e5b2832408f24d3b4fa455862 the test process exits at this line with panic: callback panic.

Please supervise callback invocation at the library-owned goroutine boundary (with observable logging/metrics), define whether later callbacks continue after one callback fails, and add a regression proving that the process plus the ordered callback/resource-release chain survive a callback panic.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep re-review completed on exact head ca9c9b3382d30145aa71ad74d7e9490bee5373bd, independent of the still-running CI.

The previous async-callback panic blocker is closed. Each async callback now has an independent recovery boundary; a hostile panic value is not stringified; later callbacks on the same update and callbacks on subsequent updates continue; and the existing resource defer still releases the slot, pending-byte accounting, and callback-retained data exactly once. Panic observability uses an unlabeled counter and a fixed, globally budgeted event, so it does not introduce metric cardinality or unbounded log-key growth.

I also re-audited the complete PR through the ownership, state-transition, wait-for, restart/Close, and bounded-growth models: range-update admission and copied-data accounting; full-file temp/fd/path publication and cleanup; FIFO prepare/finish eviction; path read generations and stale-reader reindex exclusion; Close sealing/drain behavior; S3 buffered, streaming, prefetch, and IOMerger integrations; cancellation; callback ordering; and failure/panic paths. The intentionally deferred stale-range invalidation and physical temporary/inflight-byte quota remain explicitly tracked by #27367 and #25764.

Validation on this exact head: git diff --check; build and vet for all changed Go packages; full normal and full -race suites for pkg/fileservice, pkg/fileservice/fifocache, and pkg/util/metric/v2; plus each of the four lifecycle/IOMerger focus tests independently under -race -count=100 using the repository CGo test wrapper. All passed. No blocking findings remain on this head.

This branch had an error being deployed

1 failed deployment
ci — df15982f Deployed Aug 22, 2026 by XuPeng-SH via Matrixone Utils CI / Coverage #47553
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/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

4 participants