Repository navigation
Fix ssd_cache dictionaries with the same PATH returning each other's values - #122465
Conversation
…values SSDCacheFileBuffer opened <PATH>.bin with O_TRUNC. When a second storage was created on a path whose file another live storage still had open, it truncated that file and wrote its own blocks from block 0. The first storage's index still pointed at those offsets, and the block checksum was valid because the blocks were well-formed blocks of the other storage, so dictGet returned the other dictionary's values without an error. Two instances of one dictionary overlap the same way during SYSTEM RELOAD DICTIONARY, where queries still reading the previous instance then failed with CORRUPTED_DATA. Create the file under a unique temporary name with O_EXCL and rename it over <PATH>.bin instead. A storage that still has the previous file open keeps reading its own inode, which is freed when its last descriptor closes. Every storage still starts with an empty cache. Rejecting a PATH that is already in use was not chosen: it needs an owner identity that survives SYSTEM RELOAD and CREATE OR REPLACE, where two instances of one dictionary briefly share the path, and the new instance would still truncate the file under the old one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The cache file was created under a temporary name next to <PATH>.bin and renamed over <PATH>.bin. rename(2) does not follow a symlink at its target, so a link at <PATH>.bin (for example to a file on another disk) was replaced by a regular file, while the old in-place open followed it. The link chain is now resolved first (at most 40 links, then ELOOP, as open does), and the new file is created in the directory of the file the chain points to and renamed over that file, so the link stays and its target becomes the cache. The temporary name is now a fixed-length ssd_cache_<seed>.tmp instead of <PATH>.bin.<seed>.tmp, so a <PATH>.bin that fits NAME_MAX no longer fails with ENAMETOOLONG. The test also covers a dangling relative link at the cache file. Co-Authored-By: Claude Opus 5.5 <[email protected]>
rename(2) checks only the permissions of the directory, so an existing <PATH>.bin, or the file a symlink there points to, was replaced even when the server could not open it for reading and writing, where the old in-place O_RDWR open failed with EACCES and left the file intact. The constructor now checks read and write access to that file before creating anything, and any error other than ENOENT fails with CANNOT_OPEN_FILE as before. The symlink loop now stops at a path that names a directory (a link whose target ends in '/'), so such a link fails as it did before instead of resolving one level too deep. It also uses FS::isSymlinkNoThrow, so an lstat error such as ENAMETOOLONG reaches the access check and keeps CANNOT_OPEN_FILE instead of surfacing as a std::filesystem error. The error for a failed open of the temporary file now also names the file it was going to replace. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The symlink comment now states why the link is resolved (rename(2) overwrites a symlink) instead of describing the loop, and the access comment no longer says rename(2) checks only the directory's permissions, which is not true in a sticky directory. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)An independent model reviewed this before publication (engine: codex; 5 findings over 3 rounds, with my own cold review each round) and again after the AI review (2 findings).
Severity: ❌ blocker / Session ids: cron:clickhouse-review-slot-49:20260926-192500, cron:clickhouse-maint-slot-13:20260926-205300 |
|
Workflow [PR], commit [dead399] Summary: ✅
AI ReviewSummaryThe PR isolates Final VerdictStatus: ✅ Approve |
The new cache file was renamed over <PATH>.bin before the last constructor steps that can fail (F_NOCACHE on macOS and the preallocation). A SYSTEM RELOAD DICTIONARY without FILE_SIZE of free space therefore failed after <PATH>.bin already named the new file. The old instance kept working on its unlinked inode, but <PATH>.bin was left holding whatever the failed preallocation had reserved: with a 40 MiB cache on a 64 MiB ext4 file system that was all remaining space, so the file system stayed at 0 bytes free and an unrelated 8 MiB write failed with ENOSPC. The new file is now renamed last and removed if any step before or at the rename fails, so a failed reload leaves <PATH>.bin and the free space as they were. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+17.89 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units7 translation units recompiled, 16 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 4072d8bEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 169 check runs completed (129 pass, 39 not selected, 1 red), Session id: cron:our-pr-ci-monitor:20260927-023024 |
Backport #122465 to 26.10: Fix `ssd_cache` dictionaries with the same `PATH` returning each other's values
Backport #122465 to 26.9: Fix `ssd_cache` dictionaries with the same `PATH` returning each other's values
Backport #122465 to 26.8: Fix `ssd_cache` dictionaries with the same `PATH` returning each other's values
… the same `PATH` returning each other's values
… the same `PATH` returning each other's values
…h the same `PATH` returning each other's values
Cherry pick #122465 to 26.3: Fix `ssd_cache` dictionaries with the same `PATH` returning each other's values
…`PATH` returning each other's values
…`PATH` returning each other's values
…`PATH` returning each other's values
… `PATH` returning each other's values
…`PATH` returning each other's values
Backport #122465 to 26.3: Fix `ssd_cache` dictionaries with the same `PATH` returning each other's values
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
ssd_cacheandcomplex_key_ssd_cachedictionaries that share aPATHreturning each other's values fromdictGet, andCache data corruptederrors in queries that run duringSYSTEM RELOAD DICTIONARYof an SSD cache dictionary. Each dictionary instance now creates its own cache file instead of truncating the file another instance may still be reading.Description
On 26.8.10.6 and 26.3.33.24, two
ssd_cachedictionaries created with the samePATHsilently return each other's values fromdictGet. The documented layout example uses a fixedPATH, so copies of it share one file. No public issue exists; reproduction below.Root cause: the
SSDCacheFileBufferconstructor opens<PATH>.binwithO_TRUNC. A second storage on that path truncates the file the first one still reads and writes its own blocks from block 0. The first storage's index still points at those offsets, and a well-formed foreign block has a valid checksum, so the lookup returns the other dictionary's value.SYSTEM RELOAD DICTIONARYdoes the same to running queries that still use the previous instance.Change: the constructor creates and preallocates a new file under a unique temporary name (
O_EXCL), then renames it over<PATH>.bin, or over the file a symlink there points to. A storage that still has the previous file open keeps its own inode, and a new storage still starts empty. This one constructor opens the file for both key types. The previous file stays allocated while its instance is alive, so a reload needsFILE_SIZEof free disk space, and the file's directory must be writable by the server. Rejecting aPATHalready in use would need an owner identity that survivesSYSTEM RELOADandCREATE OR REPLACE, where two instances of one dictionary share the path.Validation: the new test (simple and complex keys) fails on master with 435 and 419 of 500 values wrong and passes with the fix in 50 of 50 randomized runs; the existing SSD cache tests pass. With 12 concurrent readers and
SYSTEM RELOAD DICTIONARYin a loop for 180 s, 102 of 679 queries failed withCORRUPTED_DATAon master; on the fix, none failed with it or returned a wrong value.Reproduction
Workflow [PR]
Sync PR [sync-upstream/pr/122465]
Version info
26.11.1.1921-master(included in26.11and later)26.3.64.2