Repository navigation
Retry every stage of a plain-rewritable undo until it succeeds - #119235
Conversation
Reversing a failed `plain_rewritable` metadata transaction is not atomic. Each object storage write is separate, and `MetadataOperationsHolder::rollback` stops at the first `undo` that throws. The writes the reversal never reached stay in object storage, while the in-memory filesystem still describes the state before the transaction, because a failed commit never publishes its snapshot. Nothing reconciles the two until the filesystem is rebuilt from object storage, so a `MergeTree` part directory can turn up under a name the server never committed. The state of object storage is unknown at that point, so no decision taken from it can be trusted, including the decision to accept it. Every stage of an `undo` now repeats until object storage accepts it, and a stage that succeeded never repeats. `MetadataStorageFromPlainObjectStorageMoveFileOperation` needs the second half of that: it drops a temporary copy once the blob the copy holds is back under its own key, so a retry that started the reversal over would copy from an object an earlier stage has already removed. A commit holds `metadata_mutex`, and so do `refresh` and `dropCache`, so no other transaction and no reload can read the filesystem of this disk while a reversal is still in progress. A shutdown is the only exit. The retries stop, the transaction reports its failure, and the next start loads the filesystem from object storage. The retries live in the plain-rewritable operations, so `MetadataOperationsHolder` and the other metadata storages keep their current behaviour. Related: #118623 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [8bd3311] Summary: ✅
AI ReviewSummaryThis PR replaces one-shot Findings
Final Verdict
LLVM Coverage ReportMeasured on commit 8bd3311.
Changed lines: Changed C/C++ lines covered: 276/300 (92.00%) · Uncovered code |
An object storage call can write and then report a failure, so `execute` cannot learn from its own return values what it has already changed. `MetadataStorageFromPlainObjectStorageMoveFileOperation` recorded its progress after each `copyObject` and `removeObjectIfExists` returned, so a failure raised once the call had landed left the matching step out of the reversal: a blob published under its new key stayed there, and a directory with the implicit file list reports whatever blobs sit under its prefix, so a restart showed a file the transaction never committed. Every operation now captures what its reversal needs, sets one flag, and only then writes. A false flag proves that nothing was written; a true flag makes the reversal run without asking whether any single write landed. For the two operations that destroy a blob the process does not hold in memory - the file move and the unlink of a file whose blob is not shared - each stage of the reversal states where one key has to end up and asks object storage whether it is already there, which holds whether the matching step never ran, ran, or ran and lost its answer. A blob that exists neither under its own key nor under the temporary key it was copied to is gone, and that is reported as a logical error rather than retried, because no number of retries brings it back. `MetadataStorageFromPlainObjectStorageMoveDirectoryOperation` no longer tracks which markers it rewrote. The old logical path of every marker of the subtree is known in memory, so the reversal rewrites all of them: rewriting one the forward pass never reached costs a single write and changes nothing. Adds `MetadataTransactionRollbacks` and `MetadataTransactionRollbacksFailed`, so a rollback that does not run to completion is visible, and `DiskPlainRewritableUndoStageRetries`, which is the counter that moves while a reversal is repeating a step. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ect storage The reversal of a failed transaction ends on its own only once object storage accepts it, so a test of the shutdown exit has to observe a reversal that is really repeating a step. The previous test called `shutdown` before the transaction, so the retry loop was never entered and the concurrent path went untested. The test now holds the move at a `PAUSEABLE_ONCE` failpoint once one `prefix.path` marker carries its new path, tells the object storage of the disk to reject every request that changes something, and releases the move. The rest of the move then fails, and the reversal of that marker cannot succeed either. The test waits until the same step has been repeated, shuts the disk down, and reads back from the log that the reversal stopped because of the shutdown rather than by succeeding or by giving up on its own. `FailingLocalObjectStorage` is a `LocalObjectStorage` that can be told to reject every request that changes something, while reads keep working so the test can still look at what is stored. It keeps this out of the object storage itself. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Master reverted "Support real hard links for plain_rewritable metadata" (#110939) in 79745e1, so the operations this branch touches lost their explicit file lists. The reversal work is re-applied on the simpler code: - `MetadataStorageFromPlainObjectStorageHardLinkOperation` is gone, because `createHardLink` copies the blob again, which the copy operation covers. - `MetadataStorageFromPlainObjectStorageWriteFileOperation` no longer writes to object storage, so it has nothing to reverse and does not take `UndoRetries`. - The move of a file has a single form again, so its reversal no longer has a branch for a move that only rewrites file lists. - `prefix.path` holds a logical path again, so the tests read it directly. Everything the branch claims is unchanged: every stage of an `undo` repeats until object storage accepts it, each stage converges on the state recorded before the first write rather than on what `execute` saw succeed, and a blob that exists under neither its own key nor its temporary key is reported as a logical error. When #119304 restores the hard links, the reversals of the hard link operation, of `WriteFileOperation` and of the file move that only rewrites file lists need the same treatment again. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Merged latest master ( Master reverted
When Verified on the new base: Debug build with |
…k leaves `MetadataStorageFromPlainObjectStorageMoveFileOperation::undo` no longer checks that the target blob is under its own key when the copy that puts it aside is absent. The copy runs before anything overwrites or removes the target, so without it the target is still the blob the transaction found, and the check could only fire on a disagreement this operation did not produce - as a `LOGICAL_ERROR`, which aborts a debug build. The sequencing that makes the check unnecessary is stated in a comment instead. The same shape of check stays on the source blob, where the move really does remove it and the reversal has to put it back. The pause between the retries of a stage now grows to 5 seconds instead of 1, and each pause is spread by up to 100 milliseconds, so that the transactions a failing object storage holds up do not all come back at the same moment. The comment that justified the cap claimed it kept a shutdown responsive, which was never true: a shutdown wakes the wait instead of waiting for it. `MetadataOperationsHolder::rollback` now states in the exception what a rollback that did not complete leaves behind. The message reached only the log, so the error recorded for the query or the merge named the object storage failure without saying that object storage may keep a part of a transaction that is reported as failed. This covers every partial rollback, including one where an `undo` throws without any retry. Renames `UndoRetries` to `UndoWithRetries`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A reversal that a shutdown abandons releases the metadata lock, and the transactions that were waiting for it resume at once. They decide what to write from a filesystem that object storage no longer matches, which is the state this branch exists to prevent - so the disk now takes no further transaction after that happens. The mark is set while the commit still holds the lock, and it is checked right after the lock is taken, because a transaction that was already waiting is past the point where it was created. Reads are still served: the filesystem in memory is the state that was committed, and a file resolves to the blob it did before. `refresh` and `dropCache` stop reloading, since reloading would adopt a state no transaction ever committed. Nothing here can make the two agree again, so this lasts until the next start, which loads the filesystem from object storage. `MetadataOperationsHolder::isPartiallyRolledBack` reports the state that decides this. It is the one piece worth keeping from #118623, which detected the same thing and then reloaded the filesystem to match object storage. Also fixes what CI reported on the tests: an override must not repeat the default arguments of the method it overrides, and a `catch (...)` needs to say that swallowing is intended. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`dropCache` and `refresh` asked whether the disk was left with a reversal abandoned before they took `metadata_mutex`, so a reload that passed the question resumed on the other side of the lock and loaded anyway - adopting a state no transaction ever committed, in the process rather than at the next start. `SYSTEM DROP FILESYSTEM CACHE` reaches the first and the background refresh of parts reaches the second. `load` asks the question itself now, and both callers hold the lock when they call it, so a reload that was waiting finds the answer that was set while it waited. The initial load runs before anything can be reversed. This is the same mistake the check in `commit` was written to avoid, one call site away: a question asked before the lock says nothing about the state behind it. The test that abandons a reversal now reloads afterwards and expects the filesystem not to move. One marker is left naming the new path, so a reload changes what the disk reports whichever marker was rewritten first. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The comment on the broken state claimed that a file still resolves to the blob it did before. That holds for a directory marker and not for a blob: when the abandoned reversal was putting a blob back, the blob is left only under the temporary key the operation copied it to, while the filesystem still names the original one, so that one file fails to read. Reads stay served rather than rejected, because refusing them takes a whole disk down over one broken transaction, and the shutdown path reads too. The comment now says which reads are wrong instead of claiming that none of them are. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Build profile diff (arm_release)Comparing Binary sizes
|
| Binary | Master | PR | Δ |
|---|---|---|---|
programs/clickhouse-stripped |
718.85 MiB | 747.22 MiB | +28.37 MiB (+3.95%) |
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.
The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.
Object file sizes
7 object files changed (+32.57 KiB total), 1 added.
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Disks/DiskObjectStorage/MetadataStorages/PlainRewritable/MetadataStorageFro… |
351.89 KiB | 372.80 KiB | +20.91 KiB (+5.94%) |
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
12 translation units recompiled, 27 s compile time in total, 11 of them have a recent master baseline.
Symbol sizes ⚠️
| Binary | Symbol | Master | PR | Δ |
|---|---|---|---|---|
programs/clickhouse |
_ZZN24TopLevelDomainLookupHash7isValidEPKcmE8wordlist |
new | 864.07 KiB | +864.07 KiB |
programs/clickhouse-keeper |
_ZZN24TopLevelDomainLookupHash7isValidEPKcmE8wordlist |
new | 864.07 KiB | +864.07 KiB |
programs/clickhouse-keeper |
wordlist |
new | 242.38 KiB | +242.38 KiB |
programs/clickhouse |
wordlist |
new | 242.38 KiB | +242.38 KiB |
programs/clickhouse |
rd_kafka_properties |
new | 131.25 KiB | +131.25 KiB |
programs/clickhouse-keeper |
_ZGRN2DB12_GLOBAL__N_15nounsE_ |
new | 102.33 KiB | +102.33 KiB |
programs/clickhouse |
_ZGRN2DB12_GLOBAL__N_15nounsE_ |
new | 102.33 KiB | +102.33 KiB |
programs/clickhouse |
a_2 |
new | 82.50 KiB | +82.50 KiB |
programs/clickhouse-keeper |
a_2 |
new | 82.50 KiB | +82.50 KiB |
…g up The retries stopped on a shutdown, which needed a way to stop them and a way to contain what an abandoned reversal left behind. Neither is needed: a reversal that never returns without success cannot release the metadata lock to a transaction that would then write from a filesystem object storage no longer matches. Michicosun also pointed out that the exit was not reachable anyway - tables are shut down before disks, so a background operation inside the retry loop would block `DatabaseCatalog::shutdown` long before the `disk->shutdown` that was supposed to release it. So the retries have no exit other than a stage succeeding, and the following is gone with the one that existed: - `UndoWithRetries` was a class with a mutex, a condition variable and a flag, and had to be owned by the metadata storage and handed to every operation. It holds no state now, so it is the free function `undoWithRetries`, and the member, the constructor parameter of seven operations, all ten arguments at the construction sites and the pointer alias are gone with it. - The broken state of the disk, which existed to keep transactions out of the window the exit opened: `markBroken`, `throwIfBroken`, the guard in `load`, the checks in `commit`, and `MetadataOperationsHolder::isPartiallyRolledBack`, which was added to drive it. - The tests of both, and the object storage that rejects requests and the log capture that only they used. The pause between the retries comes from `DynamicDelay`, which grows it by a factor between a minimum and a maximum and adds the jitter, so the arithmetic for both is no longer written here. The message about a rollback that did not complete no longer says that the filesystem in memory does not have the part that is left, or that the next start loads the filesystem from object storage. `MetadataOperationsHolder` is shared, and neither holds for every metadata storage. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… from two events A stage that finds a blob under neither its own key nor its temporary key throws a logical error rather than asking again, and that is now the only way out of the retries. What it leaves was not written down: the blob is gone, so nothing brings back the file it belonged to, and `MetadataOperationsHolder::rollback` stops at the operation that threw, so the operations below it keep the writes they have already made. A debug or sanitizer build aborts on the logical error before any of that. `MetadataTransactionRollbacks` and `MetadataTransactionRollbacksFailed` are incremented in `MetadataOperationsHolder`, which the local-disk metadata transactions use as well, so they counted more than their descriptions claimed. The counters are right to cover every metadata transaction; the word "object storage" is what has to go. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The reversal of a file move falls through when the copy that put the target aside is not there, and the comment explained that by ordering alone: the copy comes before anything destroys the target. That leaves out what matters, which is the two outcomes of the copy. Either it reported success, and its result is there to restore from, or it threw, and the target was never destroyed. A copy that reports success and writes nothing would break the branch, and it would break every other call these reversals depend on, so it is not what this code defends against. What it does defend against is a call that reports a failure after it has already written, which is why the stages converge on a state rather than trusting what `execute` saw. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`03008_s3_plain_rewritable_fault` enables `plain_object_storage_write_fail_on_directory_move` and expects the removal of a part to be rolled back and retried. The failpoint sat in `rewriteSingleDirectory`, which the reversal of a move uses as well, and a fault that never stops firing cannot be retried to an end - so the reversal spun and the removal never came back. The test timed out after 600 seconds instead of passing in 6. The fault is injected at the call in `execute` now. Nothing was flushed at the old point either, so the forward pass fails exactly as before, while the reversal can finish. `plain_object_storage_fail_on_directory_move_undo` stays for faults on the reversal, and it fires once so that the retry can succeed. The other faults of these operations are already confined to `execute`, except `local_object_storage_network_error_during_remove`, which the reversals do reach but which fires once. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…orage The tests of this work leaned on failpoints for two different things, and only one of them needs a fault at all. Whether an `undo` puts object storage back needs no fault. A transaction that ends with an operation failing on its own merits rolls back everything before it, which is all the reversal needs to run. Four tests do that now - a moved directory tree, an unlinked file, a moved file and a replaced file - and each one asserts that every object of the disk comes back with the contents it had, not only with the same keys, and that a restart agrees. The reversals of the unlink and of the plain file move had no test of their own before this. Whether a stage is repeated needs no object storage. `undoWithRetries` takes the stage as a callable, so a test hands it one that fails twice and counts the calls. `LOGICAL_ERROR` is the one path left uncovered, and the reason is written where the test would be: `DB::Exception` aborts the process for that code in a debug or sanitizer build, which is every build these tests run in. So the two failpoints inside the reversals are gone, and this leaves one that this work adds: a fault after the copy that publishes a blob, which is the state no failing operation can produce - `execute` has changed object storage and does not know it. `plain_object_storage_copy_fail_on_file_move` stops the move of a file once both blobs have been put aside and the target has been removed, so the reversal of a replaced file has real work to do. It already existed and had no test using it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Four comments went that carried nothing the code or another comment did not already carry: the one above the rollback counter, which repeated the message the same block adds to the exception; the one over a loop that said there is one stage per directory; and the two that explained the flags whose declarations explain them. The rest are shorter. The description of `undoWithRetries` kept why a reversal does not give up, what it costs, and what a logical error leaves, and dropped the part rename it used to illustrate and the argument about deciding from an unknown state - that argument belongs in the pull request, not at the function. Three stayed as they were, because the code cannot show any of them: that the directory move fault is injected on the forward pass because the reversal shares the same helper, that the temporary copies are dropped last so a failing stage never strands a restore, and that an absent copy of the target means the target was never touched. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Master (#119235) made every stage of a plain-rewritable undo repeat until object storage accepts it, because an undo that stops halfway leaves object storage describing a filesystem this process does not have. The undo stages that this branch adds - restoring the `prefix.path` of a directory whose explicit file list an operation rewrote - now go through `undoWithRetries` as well. Co-Authored-By: Claude Fable 5.1 <[email protected]>
`MetadataOperationsHolder::rollback` stops at the first `undo` that throws and leaves every operation below it executed. On an object storage disk with `local` metadata that loses data: a removal that decremented the hard link counts of a part's files and then failed keeps the counts decremented when restoring one of them fails, and the next removal of another link of those files deletes their blobs. Seen in `Stress test (arm_tsan)` on ClickHouse#110626, where `DROP DETACHED PARTITION` failed on an injected memory limit, its rollback failed on the same fault while restoring a count, and a concurrent `DETACH` then deleted the blobs of the detached part that `ATTACH PARTITION` read next. Following ClickHouse#119235 (`plain_rewritable`), the rollback now runs to the end: - `MetadataOperationsHolder::rollback` blocks memory-limit exceptions and their fault injection. A rollback runs inside the `catch` of `commit`, where the memory tracker may throw, and a query at its own limit cannot free memory before its rollback ends. - Every step of an `undo` of a local operation repeats until it succeeds. The retry loop of ClickHouse#119235 moves to `MetadataStorages/UndoWithRetries` with the profile event as a parameter; the plain-rewritable entry point forwards to it with its own event, and `local` metadata counts `MetadataFromDiskUndoStageRetries`. - Each operation records what its `execute` did, so its `undo` reverts only that and each step can fail only on a real error. Before, a move onto an existing file, `createDirectory` of an existing non-empty directory, `chmod` of a missing file or a failed read of the old modification time made the `undo` throw, which stopped the rollback with no fault at all; `createHardLink` deleted a destination it did not create (also one that already was a link to the same file), and `WriteFileOperation` deleted an existing file whose read failed. - `RemoveRecursiveOperation` restores its count writes in reverse order: two links to one file inside the removed tree ended one count too low after a complete rollback. - `DiskLocal::removeDirectoryIfExists` checked a path that already carried the disk path, so for a relative disk path it never removed anything and the rollback of `createDirectoryRecursive` left the directories behind. A step that keeps failing on an I/O error now holds the rollback, and the metadata lock with it, instead of returning with the metadata half rolled back, as for `plain_rewritable`. The order of the steps is unchanged; a crash during a rollback is still not covered. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… end An encrypted disk with a `path` over a `readonly` local plain_rewritable disk creates that path on the wrapped disk. LocalObjectStorage refuses the write with READONLY before any I/O, the metadata transaction rolls back, and the undo stage (removing the directory metadata) is refused with READONLY too. undoWithRetries, since ClickHouse#119235, repeats every code except LOGICAL_ERROR and CORRUPTED_DATA, so the stage was retried forever while holding the metadata lock: CREATE TABLE never returned, and a server with such disks in its configuration never finished starting (master La Casa Del Dolor (amd_debug), 64 retries in 5 minutes). BACKUP TO Disk on such a disk hung the same way. A read-only object storage refuses every write for its whole life: the flag is fixed when the storage is constructed, and Local checks it before any I/O in every mutation (writeObject, removeObject[s], removeObject[s]IfExist[s], copyObject). So no stage of the transaction wrote anything there to reverse, and READONLY joins the codes that are not repeated. The rollback stops the same way it does for CORRUPTED_DATA and the original READONLY reaches the caller, as on 26.9. Tests: a stateless test bounded by `timeout` (exit code 164 with the fix, 124 after 60 s without it) and the unit test UndoWithRetries.DoesNotRepeatReadonly, a copy of DoesNotRepeatCorruptedData whose stage goes through from the fourth attempt, so a repeat shows as a missing exception rather than a hang. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #118623
Related: #118615
MetadataOperationsHolder::rollbackstops at the firstundothat throws, so the writes it never reached stay in object storage while the in-memory filesystem still describes the state before the transaction. Nothing reconciles the two until the filesystem is rebuilt from object storage, so aMergeTreepart directory can turn up under a name the server never committed.Every stage of a
plain_rewritableundonow repeats until object storage accepts it. Each stage states where a key has to end up and checks whether it is already there, so it holds whether the matching step never ran, ran, or ran and lost its answer; a stage that succeeded never repeats. The retries have no other way out than succeeding.#118623 fixed this by reloading the filesystem to match object storage, and is closed: the state there is unknown, so adopting it is as likely to be wrong as anything else. Still open, both of them older than this change and neither made worse in kind by it. A stage that finds a blob under neither its own key nor its temporary key throws a logical error instead of asking again, and the reversal stops there, so the operations below it keep their writes; closing that needs an intent marker, so that
loadcan finish or reverse a move itself. And a move destroys the blob of the source and overwrites the blob of the target before the commit publishes the snapshot, while reads take no lock and keep resolving the old one, so during a move the source reads as missing and the target reads as the source; retrying a reversal holds that window open for as long as object storage rejects writes.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
plain_rewritabledisks leaving object storage and the in-memory filesystem in disagreement when a failed metadata transaction could not be reversed completely. Every step of the reversal is now retried until object storage accepts it. The disagreement previously surfaced only after a restart, when a directory could appear under a name the server never committed.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/119235]
Version info
26.10.1.46(included in26.10and later)