Skip to content

fix(fileservice): serialize disk cache directory cleanup - #27840

Merged
mergify[bot] merged 1 commit into
matrixorigin:mainfrom
gouhongshen:agent/issue-27832-f108ea9f
Aug 29, 2026
Merged

mergify[bot] merged 1 commit into
matrixorigin:mainfrom
gouhongshen:agent/issue-27832-f108ea9f

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Aug 29, 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:

Related to #27832

What this PR does / why we need it:

Root cause

DiskCache.loadCache runs asynchronously during file-service initialization. It
removed every empty non-root directory while walking the cache tree. A concurrent
full-object cache fill creates its parent with MkdirAll and then calls
CreateTemp. If the loader removed that parent in between, the optional cache
write failed open and the S3 read fell back to a full-object buffer; a small
range then retained that entire backing allocation.

The historical #27520 issue is independent: it synchronized the later
post-publication cache-hit assertion. This failure is on the first range read
before that barrier.

Changes

  • Serialize startup empty-directory cleanup with the MkdirAll plus
    CreateTemp critical section using a per-DiskCache RWMutex.
  • Keep concurrent cache writers compatible with the shared lock. The loader
    takes the exclusive lock only while attempting one empty-directory removal.
  • Keep stale empty-directory cleanup; no new cache-directory retention path is
    introduced.
  • Add deterministic lifecycle coverage with instance-local barriers. It forces
    the loader to reach cleanup while a foreground cache writer owns its parent
    directory, then verifies temp-file creation succeeds. A stale empty-directory
    control remains removable.

Issue-to-test proof

  • TestDiskCacheLoadCacheKeepsForegroundDirectory proves the exact
    MkdirAll -> loader cleanup -> CreateTemp ownership boundary without sleeps:
    the writer reaches the temporary-file phase under the shared lock, the loader
    reaches directory cleanup, and only then is the writer released. The cache
    temp file and its parent survive; a separate stale directory is removed.
  • TestS3FSFullObjectDiskCacheFillDoesNotRetainWholeObjectBuffer covers the
    consumer contract: the first seven-byte range from a 1 MiB object has bounded
    capacity and one S3 Get; after FlushCache, a later range hits disk cache.

Tests run

The branch was rebased without conflict onto origin/main
7bb89155a2c43a3a97da0f0fb4d865fdf4855235; the following were rerun on
2232f3852c09cd11191fb047cb131e9bb7b3af6e:

  • Both issue-proving tests in CI-style coverage mode:
    -short -tags matrixone_test -covermode=set.
  • Both directly affected tests under -race -count=100.
  • The full TestDiskCache|TestS3FSFullObject group with
    -short -tags matrixone_test.
  • GOWORK=off go vet -mod=readonly -tags matrixone_test ./pkg/fileservice.
  • git diff --check.

The earlier CI-style coverage mode with the repository-wide -coverpkg scope
passed both selected tests and generated a profile with 89,738 lines. The
base-side commits incorporated by the rebases change only pkg/fulltext2 and
pkg/sql/plan, not the fileservice code, dependencies, fixtures, or test modes
exercised here.

The normal and race pkg/fileservice package runs reached completion but failed
only at TestMinioSDK/file_service with Access Denied from the local MinIO
service. The same selected MinIO test fails with the same signature in an
isolated origin/main worktree, so it is unrelated to this diff.

Residual risks

The loader can briefly wait for an in-progress parent-directory creation, but
the shared critical section contains only MkdirAll and CreateTemp; it does
not cover streaming, fsync, or publication. Cache writes retain their existing
fail-open behavior for real filesystem errors.

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Aug 29, 2026
@mergify mergify Bot added kind/bug Something isn't working kind/test-ci labels Aug 29, 2026
@gouhongshen
gouhongshen force-pushed the agent/issue-27832-f108ea9f branch from 2872f2a to 26afc6a Compare August 29, 2026 10:58
@gouhongshen
gouhongshen marked this pull request as ready for review August 29, 2026 11:50
@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 →

@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 completed on exact head 2232f3852c09cd11191fb047cb131e9bb7b3af6e (base 7bb89155a2c43a3a97da0f0fb4d865fdf4855235).

No blocking issue found. I reviewed every production and test hunk and traced the directory lifecycle and lock order. All DiskCache writes reach the new helper, whose shared critical section covers exactly MkdirAll through successful CreateTemp; startup cleanup takes the exclusive lock for one os.Remove. Therefore cleanup cannot remove the parent in the only vulnerable interval, a created temp file makes the later remove fail safely, writers remain mutually concurrent, and streaming/fsync/publication stay outside the lock. The loader generation is already canceled and joined by Close, so the instance-local mutex matches directory ownership and introduces no cross-generation wait edge.

Evidence: exact-head required CI is green; git diff --check is clean; and the two issue-proving tests passed locally under -race -count=20 via the repository CGo test harness. This is a focused bug fix with no public protocol or persistent-format change, so no separate design document is required.

@mergify mergify Bot added the queued label Aug 29, 2026
@mergify

mergify Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-29 13:16 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-08-29 13:16 UTC · at 2232f3852c09cd11191fb047cb131e9bb7b3af6e · squash

This pull request spent 14 seconds in the queue, including 1 second running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

@mergify
mergify Bot merged commit 1cacf73 into matrixorigin:main Aug 29, 2026
26 of 27 checks passed
@mergify mergify Bot removed the queued label Aug 29, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…n#27840)

### Root cause

`DiskCache.loadCache` runs asynchronously during file-service initialization. It
removed every empty non-root directory while walking the cache tree. A concurrent
full-object cache fill creates its parent with `MkdirAll` and then calls
`CreateTemp`. If the loader removed that parent in between, the optional cache
write failed open and the S3 read fell back to a full-object buffer; a small
range then retained that entire backing allocation.

The historical matrixorigin#27520 issue is independent: it synchronized the later
post-publication cache-hit assertion. This failure is on the first range read
before that barrier.

### Changes

- Serialize startup empty-directory cleanup with the `MkdirAll` plus
`CreateTemp` critical section using a per-`DiskCache` `RWMutex`.
- Keep concurrent cache writers compatible with the shared lock. The loader
takes the exclusive lock only while attempting one empty-directory removal.
- Keep stale empty-directory cleanup; no new cache-directory retention path is
introduced.
- Add deterministic lifecycle coverage with instance-local barriers. It forces
the loader to reach cleanup while a foreground cache writer owns its parent
directory, then verifies temp-file creation succeeds. A stale empty-directory
control remains removable.

### Issue-to-test proof

- `TestDiskCacheLoadCacheKeepsForegroundDirectory` proves the exact
`MkdirAll -> loader cleanup -> CreateTemp` ownership boundary without sleeps:
the writer reaches the temporary-file phase under the shared lock, the loader
reaches directory cleanup, and only then is the writer released. The cache
temp file and its parent survive; a separate stale directory is removed.
- `TestS3FSFullObjectDiskCacheFillDoesNotRetainWholeObjectBuffer` covers the
consumer contract: the first seven-byte range from a 1 MiB object has bounded
capacity and one S3 Get; after `FlushCache`, a later range hits disk cache.

### Tests run

The branch was rebased without conflict onto `origin/main`
`7bb89155a2c43a3a97da0f0fb4d865fdf4855235`; the following were rerun on
`2232f3852c09cd11191fb047cb131e9bb7b3af6e`:

- Both issue-proving tests in CI-style coverage mode:
`-short -tags matrixone_test -covermode=set`.
- Both directly affected tests under `-race -count=100`.
- The full `TestDiskCache|TestS3FSFullObject` group with
`-short -tags matrixone_test`.
- `GOWORK=off go vet -mod=readonly -tags matrixone_test ./pkg/fileservice`.
- `git diff --check`.

The earlier CI-style coverage mode with the repository-wide `-coverpkg` scope
passed both selected tests and generated a profile with 89,738 lines. The
base-side commits incorporated by the rebases change only `pkg/fulltext2` and
`pkg/sql/plan`, not the fileservice code, dependencies, fixtures, or test modes
exercised here.

The normal and race `pkg/fileservice` package runs reached completion but failed
only at `TestMinioSDK/file_service` with `Access Denied` from the local MinIO
service. The same selected MinIO test fails with the same signature in an
isolated `origin/main` worktree, so it is unrelated to this diff.

### Residual risks

The loader can briefly wait for an in-progress parent-directory creation, but
the shared critical section contains only `MkdirAll` and `CreateTemp`; it does
not cover streaming, fsync, or publication. Cache writes retain their existing
fail-open behavior for real filesystem errors.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…n#27840)

### Root cause

`DiskCache.loadCache` runs asynchronously during file-service initialization. It
removed every empty non-root directory while walking the cache tree. A concurrent
full-object cache fill creates its parent with `MkdirAll` and then calls
`CreateTemp`. If the loader removed that parent in between, the optional cache
write failed open and the S3 read fell back to a full-object buffer; a small
range then retained that entire backing allocation.

The historical matrixorigin#27520 issue is independent: it synchronized the later
post-publication cache-hit assertion. This failure is on the first range read
before that barrier.

### Changes

- Serialize startup empty-directory cleanup with the `MkdirAll` plus
`CreateTemp` critical section using a per-`DiskCache` `RWMutex`.
- Keep concurrent cache writers compatible with the shared lock. The loader
takes the exclusive lock only while attempting one empty-directory removal.
- Keep stale empty-directory cleanup; no new cache-directory retention path is
introduced.
- Add deterministic lifecycle coverage with instance-local barriers. It forces
the loader to reach cleanup while a foreground cache writer owns its parent
directory, then verifies temp-file creation succeeds. A stale empty-directory
control remains removable.

### Issue-to-test proof

- `TestDiskCacheLoadCacheKeepsForegroundDirectory` proves the exact
`MkdirAll -> loader cleanup -> CreateTemp` ownership boundary without sleeps:
the writer reaches the temporary-file phase under the shared lock, the loader
reaches directory cleanup, and only then is the writer released. The cache
temp file and its parent survive; a separate stale directory is removed.
- `TestS3FSFullObjectDiskCacheFillDoesNotRetainWholeObjectBuffer` covers the
consumer contract: the first seven-byte range from a 1 MiB object has bounded
capacity and one S3 Get; after `FlushCache`, a later range hits disk cache.

### Tests run

The branch was rebased without conflict onto `origin/main`
`7bb89155a2c43a3a97da0f0fb4d865fdf4855235`; the following were rerun on
`2232f3852c09cd11191fb047cb131e9bb7b3af6e`:

- Both issue-proving tests in CI-style coverage mode:
`-short -tags matrixone_test -covermode=set`.
- Both directly affected tests under `-race -count=100`.
- The full `TestDiskCache|TestS3FSFullObject` group with
`-short -tags matrixone_test`.
- `GOWORK=off go vet -mod=readonly -tags matrixone_test ./pkg/fileservice`.
- `git diff --check`.

The earlier CI-style coverage mode with the repository-wide `-coverpkg` scope
passed both selected tests and generated a profile with 89,738 lines. The
base-side commits incorporated by the rebases change only `pkg/fulltext2` and
`pkg/sql/plan`, not the fileservice code, dependencies, fixtures, or test modes
exercised here.

The normal and race `pkg/fileservice` package runs reached completion but failed
only at `TestMinioSDK/file_service` with `Access Denied` from the local MinIO
service. The same selected MinIO test fails with the same signature in an
isolated `origin/main` worktree, so it is unrelated to this diff.

### Residual risks

The loader can briefly wait for an in-progress parent-directory creation, but
the shared critical section contains only `MkdirAll` and `CreateTemp`; it does
not cover streaming, fsync, or publication. Cache writes retain their existing
fail-open behavior for real filesystem errors.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…n#27840)

### Root cause

`DiskCache.loadCache` runs asynchronously during file-service initialization. It
removed every empty non-root directory while walking the cache tree. A concurrent
full-object cache fill creates its parent with `MkdirAll` and then calls
`CreateTemp`. If the loader removed that parent in between, the optional cache
write failed open and the S3 read fell back to a full-object buffer; a small
range then retained that entire backing allocation.

The historical matrixorigin#27520 issue is independent: it synchronized the later
post-publication cache-hit assertion. This failure is on the first range read
before that barrier.

### Changes

- Serialize startup empty-directory cleanup with the `MkdirAll` plus
`CreateTemp` critical section using a per-`DiskCache` `RWMutex`.
- Keep concurrent cache writers compatible with the shared lock. The loader
takes the exclusive lock only while attempting one empty-directory removal.
- Keep stale empty-directory cleanup; no new cache-directory retention path is
introduced.
- Add deterministic lifecycle coverage with instance-local barriers. It forces
the loader to reach cleanup while a foreground cache writer owns its parent
directory, then verifies temp-file creation succeeds. A stale empty-directory
control remains removable.

### Issue-to-test proof

- `TestDiskCacheLoadCacheKeepsForegroundDirectory` proves the exact
`MkdirAll -> loader cleanup -> CreateTemp` ownership boundary without sleeps:
the writer reaches the temporary-file phase under the shared lock, the loader
reaches directory cleanup, and only then is the writer released. The cache
temp file and its parent survive; a separate stale directory is removed.
- `TestS3FSFullObjectDiskCacheFillDoesNotRetainWholeObjectBuffer` covers the
consumer contract: the first seven-byte range from a 1 MiB object has bounded
capacity and one S3 Get; after `FlushCache`, a later range hits disk cache.

### Tests run

The branch was rebased without conflict onto `origin/main`
`7bb89155a2c43a3a97da0f0fb4d865fdf4855235`; the following were rerun on
`2232f3852c09cd11191fb047cb131e9bb7b3af6e`:

- Both issue-proving tests in CI-style coverage mode:
`-short -tags matrixone_test -covermode=set`.
- Both directly affected tests under `-race -count=100`.
- The full `TestDiskCache|TestS3FSFullObject` group with
`-short -tags matrixone_test`.
- `GOWORK=off go vet -mod=readonly -tags matrixone_test ./pkg/fileservice`.
- `git diff --check`.

The earlier CI-style coverage mode with the repository-wide `-coverpkg` scope
passed both selected tests and generated a profile with 89,738 lines. The
base-side commits incorporated by the rebases change only `pkg/fulltext2` and
`pkg/sql/plan`, not the fileservice code, dependencies, fixtures, or test modes
exercised here.

The normal and race `pkg/fileservice` package runs reached completion but failed
only at `TestMinioSDK/file_service` with `Access Denied` from the local MinIO
service. The same selected MinIO test fails with the same signature in an
isolated `origin/main` worktree, so it is unrelated to this diff.

### Residual risks

The loader can briefly wait for an in-progress parent-directory creation, but
the shared critical section contains only `MkdirAll` and `CreateTemp`; it does
not cover streaming, fsync, or publication. Cache writes retain their existing
fail-open behavior for real filesystem errors.

Approved by: @XuPeng-SH
mergify Bot pushed a commit that referenced this pull request Nov 27, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Related to #33274.

## What this PR does / why we need it:

After a bulk update or delete, `COUNT(*)` consults tombstones to determine which rows are visible. Two changes reduce the cost of that read on 4.2-dev.

- Size regular UPDATE and DELETE flushes by the bytes retained for writing. Reserve the block-level deletion workspace once per contiguous block-ID run. In the scaled workload, the resulting layout reduced the number of tombstone objects visible to COUNT from 220 to 9.
- Backport bounded asynchronous disk cache publication from #27368 and the directory cleanup fix from #27840. The cache owns a copy of each queued range. For a whole file, the caller finishes the source read before handing sync and publication to the worker. This takes disk sync off the foreground read path while preserving source errors and handling cancellation, deletion and shutdown.

**Cold COUNT on the same snapshots.** Each cell shows median elapsed time and object-store GET requests across three fresh MatrixOne instances with empty local caches. The main binary is the revision used in the issue, `f5fa451732`.

| Snapshot | 4.2-dev reader before backport | 4.2-dev reader with backport | nightly main reader |
| --- | ---: | ---: | ---: |
| 220 tombstone objects | 6.441 s / 458 GETs | 4.007 s / 458 GETs | 3.088 s / 554 GETs |
| 9 tombstone objects | 1.071 s / 30 GETs | 0.595 s / 30 GETs | 0.409 s / 27 GETs |

On the original layout, the cache backport cut cold COUNT latency by 38% at the same median GET count. With the original reader, changing the layout reduced median GETs from 458 to 30. Small-layout timings varied across batches on the shared host. Validation on the billion-row nightly workload remains pending.

**Validation**

- All 54 COUNT queries returned 5,237,637. Each of the 18 runs returned the same SUM of 2,621,524,317. A separate SQL transaction verified read-after-update, read-after-delete and rollback.
- Ran the full `multi_update` package tests. For the cache backport, ran full tests with and without `-race` for `fileservice`, `fileservice/fifocache` and `util/metric/v2`.
- Changed-statement coverage was 22/23 for the writer change and 695/735 for the cache change. Targeted `go vet` and `golangci-lint` checks passed.


Approved-by: XuPeng-SH <[email protected]>

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/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants