Repository navigation
Conversation
A metadata file of a disk with local metadata stores the reference count of its blobs, and that count lives in the inode every hard link shares. Unlinking decremented the count and then moved the file aside; its undo moved the file back and then restored the count. When the restore failed (a memory limit on the 1 MiB write buffer in stress tests), `MetadataOperationsHolder::rollback` stopped there and left a live link counted as unshared. The next removal of that link queued its blobs, and a part that still used them through a hard link, such as the copy DETACH puts into `detached/`, lost them: ATTACH failed with `The specified key does not exist`. `RemoveRecursiveOperation` had the same order, and `CreateHardlinkOperation::undo` restored the count before removing the new link. `RemoveRecursiveOperation::undo` also restored its writes first to last, so two links to one inode inside the removed directory ended one count too low even after a complete rollback. Every operation now changes the link first on the way in and the count first on the way back: unlink and recursive removal move the file or tree aside before decrementing (queued blobs keep their original `local_path`, and the predicate still gets the path relative to the removed root), undo restores the counts, in reverse order, before moving back, and the hard-link undo removes the new link before restoring the count. A rollback that stops part way now leaves a count that is too high, so the worst outcome of a double failure is a leaked blob and an orphan metadata file instead of a deleted blob that is still referenced. Without failures the resulting files, counts and removal queue are unchanged. The tests use a test-local `DiskLocal` that fails chosen calls, so they build against the previous code and six of them fail there. Closes ClickHouse#53905 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`WriteFileOperation::execute` set `file_existed` only after `readFileIfExists` returned, so a throw while opening the read buffer (a memory limit, `EMFILE`) left it false and `undo` removed a file that existed before the transaction. With the unlink order of the previous commit this deleted the temporary file of an unlink, the move back then threw and the rollback stopped with the earlier unlinks of the transaction still executed. It also made `CreateHardlinkOperation::undo` delete the source file of a failed hard link. `execute` now checks `existsFile` first and reads with `readFile`, the same two calls `readFileIfExists` made. Tests: read faults in `FaultInjectingDiskLocal` (`prepareRead`), a failed hard link that keeps its source, an unlink rollback that completes after a read fault, and two faults in the single-file branch of `removeRecursive` (the rollback restore and the move aside), which pin its undo order and its execute order. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`DiskLocal::existsFile` can throw (a memory limit on the path allocation), and `file_existed` defaulted to false, so `WriteFileOperation::undo` then removed a file that existed before the transaction: the source file of a failed hard link, or the temporary file of an unlink. `file_existed` is now unset until the check completes, and `undo` removes the file only when the check reported it missing. Tests: an `existsFile` fault in `FaultInjectingDiskLocal` and a failed hard link that keeps its source when the check throws. Every fault case now asserts how many injected faults fired, and the two move-fault cases that no count was written before the move failed, so a drifted call index cannot turn a case into one that passes without its fault. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`RemoveRecursiveOperation::traverseFile` computed the path of a released file inside the moved-aside tree with `fs::relative`, which runs `weakly_canonical` on both arguments: it stat()s the relative metadata path against the process working directory and resolves that directory. A removed or unsearchable working directory made it throw `std::filesystem_error`, which `MetadataOperationsHolder::commit` does not roll back, so the tree stayed under its temporary name and its blobs leaked; it also cost several syscalls per released file under `metadata_mutex`. `lexically_relative` returns the same path without touching the filesystem. `UnlinkFileOperation::execute` copied the temporary name into `tmp_file_path` after moving the file aside; the copy allocates, and if it threw, `undo` could not move the file back. Move the string instead. The unlink move-failure test now also checks the `local_path` of the queued blob. 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; 4 rounds, 9 findings, 0 in the last round) plus own cold reviews.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20260930-190900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260930-185300 |
|
Workflow [PR], commit [48835f4] Summary: ✅
AI ReviewSummaryThis PR reorders the local metadata unlink and recursive-remove steps so interrupted rollbacks over-count hard links instead of under-counting them, which matches the bug it is trying to fix. I found one remaining exception-safety gap in the new move-before-decrement flow, so I cannot approve it as-is. Findings
Final VerdictStatus: LLVM Coverage ReportMeasured on commit 48835f4.
Changed lines: Changed C/C++ lines covered: 56/57 (98.25%) · Uncovered code |
|
|
||
| /// We need to move file to the random name for the possible undo and to save the fs hardlink count | ||
| auto tmp_path = getRandomASCIIString(32); | ||
| disk.moveFile(path, tmp_path); |
There was a problem hiding this comment.
MetadataOperationsHolder::commit only rolls back DB::Exception, so moving the file aside before the remaining allocations are done is not exception-safe for std::bad_alloc and other non-DB::Exception failures. A concrete trace here is: moveFile(path, tmp_path) succeeds, then object_metadata->serializeToString() or std::make_unique<WriteFileOperation> throws, execute() unwinds past the holder's catch (Exception &), and the failed transaction leaves the metadata only under the random temporary name because undo() is never called. Before this patch those allocations happened before the rename for unlinkFile, and the same new window now exists in RemoveRecursiveOperation::execute after moveFile / moveDirectory.
Can we either make the post-move section allocation-free for all exception types, or broaden the holder to roll back on any exception instead of only DB::Exception?
|
Agreed on #53905: #87358 fixed the new-inode problem it describes (unlink now moves the file aside, and undo moves it back). I removed the This PR is about a different gap in the current Timed log from one of the stress runs (
Outside the stress harness: 11 of the 12 new |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 19 s compile time in total, 9 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR changes only the rollback and ordering logic for transactions in the local metadata storage of object-storage disks: unlink, recursive remove, hard-link undo and the WriteFileOperation existence check. It also adds unit tests. None of this code runs on the read path of a SELECT. The only flagged query is clickbench Q33, at +5.2% (1138 ms to 1197 ms). That is inside master's current normal range of 1097–1350 ms, so the automated checks correctly left it as not_sure, and nothing in the diff gives a reason to reopen that. No performance impact is expected from this change. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
This will not fix anything; subsequent write operations can fail the same way as move. undo here should be atomic, otherwise the logic does not stand. |
|
@Michicosun understood, I will leave it closed. You are right that this does not make undo atomic. The rollback still stops part way: in the 10-minute DETACH/ATTACH loop the partial-rollback message is logged 335 times with the fix (350 on master). What the PR changes is the state it stops in. The steps of unlink, recursive removal and hard-link undo are ordered so that a failed write or move leaves the reference count too high. The result is a leaked blob and an orphan metadata file, never a deleted blob that Do you want me to work on an atomic undo in a new PR, or would you rather handle it yourself? |
Related: #87358
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed data loss on disks that keep metadata locally (S3, Azure, HDFS, local object storage): when removing a data part failed and rolling the removal back failed too, for example on a memory limit, a blob still used through a hard link could be deleted, so a part detached with
ALTER TABLE ... DETACH PARTITIONcould no longer be attached (The specified key does not exist); a hard link that failed, for example duringFREEZEorDETACH, could also delete a metadata file of the source part.Description
On 26.3-26.9,
ALTER TABLE ... DETACH PARTITIONon an S3 disk whose part removal and rollback both fail (for example on a memory limit) deletes a blob that thedetached/copy still uses, and the followingATTACH PARTITIONfails withThe specified key does not exist. Seen in 3 stress-test runs since 2026-09-20 (example).A metadata file shares its inode, and the reference count stored in it, with its hard links.
UnlinkFileOperationdecremented the count before moving the file aside and its undo moved the file back before restoring the count, so a rollback that stopped in between left a linked file counted as unshared, and the next removal queued its blob.RemoveRecursiveOperationhad the same order,CreateHardlinkOperation::undorestored the count before removing the new link, andRemoveRecursiveOperation::undorestored its writes first to last (two links to one inode in the removed directory ended one count too low even after a complete rollback).Now each step removes a link before decrementing its count and adds one after incrementing it, in both execute and undo; undo writes run in reverse. A rollback that stops part way now over-counts: at worst a leaked blob and an orphan metadata file, never a deleted blob still referenced. Without failures the files, counts and removal queue are unchanged.
WriteFileOperationundo now deletes a file only if its check found none, so a failed check or read no longer deletes the file being unlinked or the source of a hard link.Twelve new cases in
gtest_metadata_local_disk.cppuse a test-localDiskLocalthat fails chosen calls; eleven fail on master and pass with the fix, and each changed step is pinned by one. A DETACH/ATTACH loop with injected memory faults lost a blob 105 times in 10 minutes on master and 0 with the fix.Workflow [PR]
Sync PR [sync-upstream/pr/123129]