Repository navigation
Fix the userspace page cache of local disks returning a removed file's data for a new file at the same path - #128738
Conversation
`computeFileCacheVersionToken` and `isFileCacheVersionTokenSettled` move from `StorageFile.cpp` to `Common/filesystemHelpers` unchanged, so that the local disk can build a page cache file version from them in the next commit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`DiskLocal::prepareRead` registered the page cache with the path only, so the file version was empty. `PageCache.h` requires a new version whenever a file's contents change. A MergeTree part written again under the same name (block numbers restart after `TRUNCATE` + `DETACH`/`ATTACH TABLE`, or after `DETACH PART`) produces files at the same paths, and every cached block of the removed file was returned for the new one: wrong sums, or `CHECKSUM_DOESNT_MATCH` when old and new blocks mix. The version is now built from the `stat` that `prepareRead` already makes (`computeFileCacheVersionToken`: mtime with nanoseconds, inode, size). A same-size rewrite in the same timestamp tick (or the same second on filesystems with whole-second timestamps) can keep that token, so a read of a file modified in the last 3 seconds (`isFileCacheVersionTokenSettled`) gets a process-unique version: it still goes through the page cache, but its blocks are never shared with another read. Once a file has settled its token is stable, so the hit rate for immutable MergeTree files is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the unsettled-token comment in DiskLocal::prepareRead to one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings) plus my own review (4 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261102-175700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261102-165400 |
|
Workflow [PR], commit [73f5dd2] Summary: ✅
AI ReviewSummaryThis PR versions local-disk userspace page-cache entries by file metadata, with unique versions for recently modified files, preventing stale reads when a path is reused. No actionable code findings at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested; CI validation remains pending. |
|
cc @Michicosun @kssenii, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units414 translation units recompiled, 3300 s compile time in total, 414 of them have a recent master baseline. |
bc3a3c0
Related: #113617
Related: #128715
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the userspace page cache of local disks (
use_page_cache_for_local_disks) returning the data of a removed file for a new file written at the same path, for example aMergeTreepart written again afterTRUNCATEandDETACH/ATTACH TABLE.Description
On 26.3.33.24 (LTS), 26.8.21.12 and 26.10.2.103, with
page_cache_max_size> 0,use_page_cache_for_local_disks = 1andlocal_filesystem_read_method = 'pread', aMergeTreepart written again at the same path afterTRUNCATE+DETACH/ATTACH TABLE(orDETACH PART) reads the previous part's data:sum(x)returns the old part's sum, or the query fails withCHECKSUM_DOESNT_MATCHwhen old and new 1 MiB blocks mix. No related open issue found.PageCache.hrequires a newfile_versionwhenever a file's contents change, butDiskLocal::prepareReadkeyed the cache by the path only.The fix passes a version built from the
statthatprepareReadalready makes: modification time with nanoseconds, inode and size (the tokenStorageFileandLocalObjectStoragealready use; the two helpers move fromStorageFile.cpptoCommon/filesystemHelpersunchanged in the first commit). A same-size rewrite within one timestamp tick, or within one second on filesystems with whole-second timestamps, can keep that token, so a file modified in the last 3 seconds gets a version of its own: the read still goes through the page cache, but its blocks are not shared. Files older than 3 seconds keep a stable version, so their hit rates are unchanged.The mmap cache is not affected: every removal of old parts clears it. The uncompressed and text index caches have the same path-reuse problem; #128715 fixes them.
Validation: the new test fails on master (5050 three times) and passes with the fix, 50/50 runs. A size-only version makes it fail again; dropping the 3-second rule reproduces the stale read 10/10 on an ext4 filesystem with whole-second timestamps, while the fix is correct 10/10.
Workflow [PR]
Sync PR [sync-upstream/pr/128738]
Version info
26.11.1.1201-master(included in26.11and later)