Repository navigation
fix(fileservice): serialize disk cache directory cleanup - #27840
Conversation
2872f2a to
26afc6a
Compare
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
26afc6a to
2232f38
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 14 seconds in the queue, including 1 second running CI. Required conditions to merge
|
…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
…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
…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
## 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]>
What type of PR is this?
Which issue(s) this PR fixes:
Related to #27832
What this PR does / why we need it:
Root cause
DiskCache.loadCacheruns asynchronously during file-service initialization. Itremoved every empty non-root directory while walking the cache tree. A concurrent
full-object cache fill creates its parent with
MkdirAlland then callsCreateTemp. If the loader removed that parent in between, the optional cachewrite 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
MkdirAllplusCreateTempcritical section using a per-DiskCacheRWMutex.takes the exclusive lock only while attempting one empty-directory removal.
introduced.
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
TestDiskCacheLoadCacheKeepsForegroundDirectoryproves the exactMkdirAll -> loader cleanup -> CreateTempownership 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.
TestS3FSFullObjectDiskCacheFillDoesNotRetainWholeObjectBuffercovers theconsumer 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/main7bb89155a2c43a3a97da0f0fb4d865fdf4855235; the following were rerun on2232f3852c09cd11191fb047cb131e9bb7b3af6e:-short -tags matrixone_test -covermode=set.-race -count=100.TestDiskCache|TestS3FSFullObjectgroup 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
-coverpkgscopepassed both selected tests and generated a profile with 89,738 lines. The
base-side commits incorporated by the rebases change only
pkg/fulltext2andpkg/sql/plan, not the fileservice code, dependencies, fixtures, or test modesexercised here.
The normal and race
pkg/fileservicepackage runs reached completion but failedonly at
TestMinioSDK/file_servicewithAccess Deniedfrom the local MinIOservice. The same selected MinIO test fails with the same signature in an
isolated
origin/mainworktree, 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
MkdirAllandCreateTemp; it doesnot cover streaming, fsync, or publication. Cache writes retain their existing
fail-open behavior for real filesystem errors.