Skip to content

fix: keep disk cache file state consistent - #24494

Merged
mergify[bot] merged 13 commits into
matrixorigin:3.0-devfrom
LeftHandCold:fix/disk-cache-stale-index-3.0
May 21, 2026
Merged

mergify[bot] merged 13 commits into
matrixorigin:3.0-devfrom
LeftHandCold:fix/disk-cache-stale-index-3.0

Conversation

@LeftHandCold

@LeftHandCold LeftHandCold commented May 20, 2026 •

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:

issue #24493

What this PR does / why we need it:

This PR fixes a disk cache stale-index race exposed after FIFO cache eviction callbacks were moved outside queueLock.

For disk cache, the eviction callback removes the physical cache file. After an item is moved to ghost state, the same key can be inserted again before the old callback runs. The old callback can then delete the physical file for the newly inserted cache entry, leaving the in-memory index present while the file is gone. Later SetFile calls trusted the index and returned without rewriting the file, so repeated reads missed disk cache and fetched the full object from S3 again.

Changes:

  • Make disk cache eviction skip deleting a path that has already been reinserted.
  • Make DiskCache.writeFile verify the physical file exists before trusting an index hit; stale index entries are repaired and rewritten.
  • Clean up physical files whose cache index is evicted while writeFile still holds the per-path update guard.
  • Move postEvict execution out of Delete / DataCache.DeletePaths shard locks so callbacks can safely inspect cache state.
  • Order same-key mem-cache remote callbacks by generation so stale evict callbacks cannot delete a newer set.
  • Finish the fifocache.New callback signature migration for object metadata cache.
  • Add regression tests for stale disk cache index repair, update cleanup, FIFO accounting, and mem-cache callback ordering.

Validation:

source /Users/shenjiangwei/Work/code/matrixone/setup_env.sh && go test -mod=mod ./pkg/fileservice -run 'TestDiskCacheSetFileRepairsStaleIndex|TestDiskCacheEvictSkipsPathBeingUpdated|TestDiskCacheUpdateCleanupRemovesUnindexedFile|TestDiskCacheStaleRepairReplacesCacheEntrySize|TestDiskCacheRemovesFileEvictedDuringWrite|TestDiskCacheFileCache|TestMemCacheSkipsStalePostEvictAfterReinsert|TestMemCacheSerializesSameKeyCallbacks' -count=1 -timeout=90s
source /Users/shenjiangwei/Work/code/matrixone/setup_env.sh && go test -mod=mod ./pkg/objectio ./pkg/fileservice ./pkg/fileservice/fifocache -count=1 -timeout=180s

@LeftHandCold
LeftHandCold requested a review from XuPeng-SH as a code owner May 20, 2026 07:20
Copilot AI review requested due to automatic review settings May 20, 2026 07:20
@LeftHandCold
LeftHandCold requested a review from fengttt as a code owner May 20, 2026 07:20
@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 →

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label May 20, 2026
@mergify mergify Bot added kind/bug Something isn't working kind/test-ci labels May 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a disk cache index/file inconsistency race introduced by running FIFO cache eviction callbacks asynchronously (outside queueLock). The fix prevents stale eviction callbacks from deleting newly reinserted cache files and makes DiskCache.SetFile self-heal when the on-disk file is missing despite an in-memory index hit.

Changes:

  • Add Cache.Contains and adjust FIFO cache deletion paths so postEvict callbacks run outside shard locks.
  • In disk cache eviction callback, skip os.Remove if the path has been reinserted into the cache.
  • In DiskCache.writeFile, verify the physical file exists before trusting an index hit; delete stale index entries and rewrite.
  • Add a regression test covering stale index repair.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
pkg/fileservice/fifocache/fifo.go Adds Contains, refactors value purging + runs postEvict outside locks for Delete/Evict paths.
pkg/fileservice/fifocache/data_cache.go Ensures DeletePaths executes postEvict callbacks after unlocking shard.
pkg/fileservice/disk_cache.go Prevents stale evict callback from deleting reinserted files; repairs stale index on SetFile.
pkg/fileservice/disk_cache_test.go Adds regression test for stale index repair behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/fileservice/fifocache/fifo.go Resolved
@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

I think this PR fixes the stale-index repair path, but there is still one race window in the core fix.

postEvict now skips os.Remove(path) only when ret.cache.Contains(path) is already true. That does not cover an in-progress SetFile/writeFile for the same disk path before the new index is installed:

  1. old FIFO item is moved to ghost and its postEvict is delayed;
  2. another goroutine starts writing/repairing the same diskPath and holds startUpdate(diskPath);
  3. it observes an existing file and is about to d.cache.Set(...), or has just renamed the temp file and is about to d.cache.Set(...);
  4. the old postEvict runs, Contains(path) is still false, so it removes the physical file;
  5. writeFile then installs the index, recreating the same index-present/file-missing state.

So Contains reduces the blast radius, and the new os.Stat repair handles the next SetFile, but an old eviction callback can still delete a newer physical file because it does not participate in the DiskCache per-path update serialization.

A conservative fix would be to make eviction-time file removal coordinate with the same per-path update marker, e.g. a tryStartUpdate(path) style helper: if the path is currently being updated, skip the stale eviction removal; otherwise hold the marker while doing Contains + os.Remove. This avoids the TOCTOU between the cache-index check and the physical delete. Please be careful with removeOnePath, since it already removes the file while holding startUpdate and then calls d.cache.Delete; the eviction callback must not self-deadlock in that path.

@LeftHandCold
LeftHandCold force-pushed the fix/disk-cache-stale-index-3.0 branch from 4e6e32f to 71f9bc3 Compare May 20, 2026 07:58
@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Addressed review comments in the latest force-push:

  • Coordinated eviction-time physical file removal with the DiskCache per-path update marker via tryStartUpdate, so a stale postEvict cannot remove a file while the same path is being rewritten/repaired.
  • Removed the stale-index Delete + Set path to avoid FIFO size double-counting. SetFile now rewrites the missing physical file while preserving the existing index, and only re-Sets if the index is evicted during the rewrite.
  • Strengthened tests with an explicit stale-index assertion and a new in-progress-update eviction test.
  • Restored the rationale comment around purgeItemValue ordering/semantics.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Reviewed the latest update. I did not find additional blocking issues after the final cleanup change.

The pushed fix now also handles the edge case where the just-written disk cache file is synchronously evicted while the same path is still in the write/update critical section: if the cache index no longer contains the path after Set/Replace, the physical file is removed immediately, avoiding an untracked .mofscache file.

Local validation passed:

  • go test -mod=mod ./pkg/fileservice -run 'TestDiskCacheSetFileRepairsStaleIndex|TestDiskCacheEvictSkipsPathBeingUpdated|TestDiskCacheStaleRepairReplacesCacheEntrySize|TestDiskCacheRemovesFileEvictedDuringWrite|TestDiskCacheFileCache' -count=1 -timeout=60s
  • go test -mod=mod ./pkg/fileservice ./pkg/fileservice/fifocache -count=1 -timeout=120s

Tried to approve, but GitHub rejected it because this is my own PR: Review Can not approve your own pull request.

LeftHandCold and others added 8 commits May 20, 2026 19:44
Retain memory cache values before publishing them in FIFO cache so delete/evict paths cannot release cache ownership before it is acquired. Also drain pending FIFO enqueue jobs before accounting-sensitive 3.0 replace/evict paths and skip deleted pending items.

Co-authored-by: Copilot <[email protected]>
Use the same shard-locked validation for immediate enqueue as pending enqueue so a slow post-set callback cannot enqueue an item deleted before enqueue. Add coverage for enqueue-after-delete accounting.

Co-authored-by: Copilot <[email protected]>
@mergify

mergify Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-05-21 07:32 UTC · Rule: release-3.0
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-05-21 08:36 UTC · at 2ddf1983b60806d10abda4e3d47d6d6bd6ac86bc · squash

This pull request spent 1 hour 3 minutes 19 seconds in the queue, including 1 hour 2 minutes 52 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • github-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI (3.0) / Coverage
    • check-neutral = Matrixone Utils CI (3.0) / Coverage
    • check-skipped = Matrixone Utils CI (3.0) / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI (3.0) / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI (3.0) / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI (3.0) / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)

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 kind/test-ci size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants