Repository navigation
Conversation
The reference count persisted in a `local` metadata file is a redundant copy of that file's OS hard-link count, and the code that decrements it is not crash-atomic with the unlink it pairs with. `UnlinkFileOperation::execute` persists the decrement, renames the file aside for undo, and only unlinks in `finalize()`. An exception is safe, because the transaction rolls back and `undo()` restores the count, but a kill in that window leaves the count one too low while every directory entry survives. The next removal that walks such a file reads zero, treats the blob as unreferenced and hands it to the object storage deleter, although a live part still points at it, so reading that part then fails with `The specified key does not exist`. `RemoveRecursiveOperation` has the same shape. Parts come to share an inode through ClickHouse's own hardlink flow: `FREEZE` hardlinks a part into `shadow/`, and a partial mutation carries over the columns it does not rewrite. Stop letting the persisted counter authorize destruction. Each removal operation now decides in its own `finalize()`, immediately before its own unlink, and releases a blob only when that unlink drops the last hard link. Because finalizers run sequentially and each actually unlinks, aliases split across operations resolve without any shared state, and an operation whose unlink fails leaves the count high, so the decision is fail-closed for free. `RemoveRecursiveOperation` removes a whole subtree in one call, so it counts links per inode within its own renamed subtree; the `should_remove_objects` verdicts are combined with AND across an inode's links, so a retention-listed alias vetoes the release rather than being outvoted by link counting alone. Both operations now enqueue blobs only after the unlink succeeds. Previously the enqueue came first, and since `finalize()` exceptions are swallowed per operation, a failing unlink could enqueue a blob whose metadata link survived, an independent second route to the same loss. A dedicated mutex serializes the finalization phase, which `commit()` runs after releasing `metadata_mutex`. Without it two transactions each removing one of an inode's two links could both observe two links and neither would release the blob, leaking it. `metadata_mutex` is deliberately not widened: that would block metadata reads for the whole of a potentially long recursive deletion, and since `SharedMutex` is not recursive it would add a deadlock hazard. The loss direction needs no lock, because before any finalizer runs the target has been renamed to a transaction-private random name that no concurrent `CreateHardlinkOperation` can resolve, and any other public alias is counted by the `stat`. A failed `stat` is treated as "do not release": leaking a blob is recoverable, deleting one a live part points to is not. `finalize()` is non-throwing by contract, so this is handled in place. The reference count keeps its meaning and serialization, so `getHardlinkCount`, its zero-copy consumer, the zeroing for zero-copy sends and the on-disk metadata format are all unchanged. Only the destruction predicate moves. Where a decrement was already lost to an earlier crash, such a blob is now leaked instead of deleted; reclaiming those would need a startup scan comparing the counter against `st_nlink` and is not part of this change. The new integration test reproduces the loss deterministically from `FREEZE`, `UNFREEZE` parked on a new pauseable failpoint, and a kill in that window. It uses only supported operations, with no out-of-band part manipulation. The unit tests cover both directions, including the leak direction, which had no coverage: the two existing hardlink cases use inline data with zero blobs and never assert the released set, so they could not detect either fault. Closes ClickHouse#111188
A retention instruction was attached to a NAME but the release decision is a property of the INODE, and each `UnlinkFileOperation` only ever sees its own link. So for two hard links to one metadata file removed by one transaction, where one link is retention-listed, the retention-listed operation still dropped its own link unconditionally and the sibling then found a single remaining link, concluded it was the last one, and released the very blob the retention listing existed to protect. Reverse the order and the blob survived, so the outcome rode on `NameSet` hash order. `RemoveRecursiveOperation` already combined those verdicts per inode within its own subtree; the asymmetry was an omission, and it is reachable because `removeSharedFiles` builds an independent operation per entry, each with its own `should_remove_objects`, from a set of base names that cannot distinguish two names sharing an inode. The transaction now carries a per-inode veto that the unlink operations share. Each operation records its own verdict during `execute()`, where every operation runs before any finalizer, so by the time one decides, the map already reflects every link the transaction will remove. A release needs both the per-operation last-link test and that shared verdict. The map holds no counts and can only turn a release off, so it cannot authorize an early release the way a shared tally of link counts could; a missing entry vetoes nothing. Give the finalization mutex a real test rather than an argument. A new pauseable fail point between counting an inode's links and unlinking one opens the losing window on demand, so two threads each removing one of two links reproduce the leak deterministically enough to reject an implementation without the mutex, which they do in every run, while every run passes with it. The two stranded-reference-count cases asserted only that the blob is kept, so they also passed an implementation that never releases anything for a stranded inode, which is exactly the over-broad direction this change's leak-rather-than- delete trade-off makes plausible. Each now also removes the surviving alias and asserts the release, pinning that the decision reads `st_nlink` and not the counter. The recursive retention case picked an alias while directory iteration order is unspecified, so it is parameterized over both aliases, and a case where no link is retention-listed pins that the veto has not become a blanket suppression.
…ve enqueue ordering Two properties this change asserts as contract had no test that could reject their removal. The finalization mutex was covered by a test that asserted a released blob, which only follows from an interleaving each round has to hit by chance: a single round reddened 2 times out of 5, so it took 200 rounds to be reliable. The pauseable fail point between counting an inode's links and unlinking one blocks every thread that reaches it, so while one transaction is parked there a second park is reachable exactly when finalization is not serialized, and impossible when it is. Counting parks discriminates by construction: removing the mutex now reddens on a single round, 5 runs out of 5. Reading the count needs an accessor for the pause epoch the channel already tracks. RemoveRecursiveOperation had no arm for enqueueing only after the unlink succeeds, which the unlink operation does have, so hoisting its enqueue loop above the removals left every case green. A fail point before the removals plus a case on a single-link file, where the link count cannot mask the ordering, rejects that. Its sibling arm requires the same scenario to release when nothing fails, so the pair cannot be satisfied by an implementation that never releases there. No change to the removal logic itself.
The unit case asserting that two transactions cannot finalize concurrently observed the second transaction through a flag set before commit(), which only says it entered commit() - unlinkFile queues the operation, and both execute() and finalize() run inside commit(). Arrival at the finalization gate was therefore assumed rather than proven, and the assertion still depended on the second thread traversing execute() within the deadline. The same gap made every successful run pay the full deadline: with the mutex present the poll loop's only exit is unreachable by construction, which is the property under test, so the case cost 15.0 s of the suite's 48.3 s. A pauseable failpoint immediately in front of the mutex acquisition, outside the lock, makes "the second transaction is at the gate" directly observable. The test now waits for that state and releases the thread from there, so what remains before the assertion is a single mutex acquisition rather than a thread start and a whole execute() pass, and the deadline drops to 500 ms. The failpoint is outside the lock deliberately: parking inside it would serialize the exclusion under test and deadlock the fixture. Removing the mutex reddens the case on a single round in 5 of 5 runs, in 3-5 ms rather than by waiting the deadline out. With the mutex intact, parking an extra thread on a second metadata storage makes the assertion fire, so it genuinely observes parks. The case is 1510 ms, and the suite 48.3 s -> 35.0 s, with 265/265 green and 20/20 under --gtest_repeat. No change to the removal logic.
Internal second-model review - 4 rounds, final round clean (0 findings)An independent model reviewed this change cold in each of 4 rounds, and a separate reviewer ❌ Blockers / majors fixed
💡 Verified, no change Fail-closed in every direction (a failed link count keeps the blobs), lock order acyclic, on-disk |
Pre-PR validation gate
Mutation arms, one per part of the diff: reverting the unlink decision, the recursive decision, either operation's append ordering, the retention veto, or the cross-operation inode veto each reddens at least one case. The finalization mutex has a structural oracle rather than a probabilistic one, on both sides. The inner fail point, between counting an inode's links and unlinking one, blocks every thread that reaches it, so while one transaction is parked there a second park is reachable only if finalization is not serialized. A second fail point immediately in front of the mutex acquisition, outside the lock, makes the other thread's arrival at the gate an observed state rather than an assumed one, so nothing is left to a deadline: removing the mutex reddens 5 runs out of 5 on a single round, in 3 to 5 ms. With the mutex intact, parking an extra thread on a second metadata storage makes the assertion fire, which shows it genuinely counts parks. The case costs 1.5 s, down from 15.0 s. A blob leak check was also run: after |
|
cc @kssenii @Michicosun, could you review this? On a disk with |
|
not needed |
|
@Michicosun which of the two do you mean, so that I do not act on the wrong one?
If it is 1: this is a deleted object, not a benign cache line. On a disk with I am not reopening it, the disposition is yours. If it is 1, what should happen to #111188? |
Closes: #111188
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a loss of object storage blobs after an unclean shutdown. On a disk with
localmetadata, a part file removed while the server was killed could leave its persisted reference count one lower than the number of hard links to it, and the next removal then deleted blobs that a live part still pointed to, so reading that part failed withThe specified key does not exist. A blob is now released only when the removal actually drops its last hard link. Closes #111188.Description
The reference count in a
localmetadata file is a redundant copy of that file's hard-link count, and its decrement is not crash-atomic with the unlink it pairs with:UnlinkFileOperation::executepersists the decrement and renames the file aside, and onlyfinalize()unlinks. An exception is safe, since the transaction rolls back, but a kill in that window leaves the count one too low while every directory entry survives. The next removal reads zero and deletes a blob a live part still references.RemoveRecursiveOperationhas the same shape. Parts share an inode throughFREEZEand partial mutations.The persisted counter no longer authorizes destruction. Each removal operation decides in its own
finalize(), immediately before its own unlink, and releases a blob only when that unlink drops the last hard link.RemoveRecursiveOperationcounts links per inode within the subtree it removes. A retention-listed alias vetoes the release for its whole inode, so "keep this file's blobs" cannot be defeated by unlink order. Both operations now enqueue only after the unlink succeeds; the enqueue came first before, so a failing unlink could enqueue a blob whose link survived. A dedicated mutex serializes finalization so two transactions dropping an inode's last two links cannot both see two and both decline; wideningmetadata_mutexinstead would block metadata reads for a whole recursive deletion.The counter keeps its meaning and serialization, so
getHardlinkCount, the zero-copy consumer and the on-disk format are unchanged. A decrement already lost to an earlier crash now leaks its blob rather than deleting it; reclaiming those needs a startup repair scan, out of scope.A new integration test reproduces the loss from
FREEZE/UNFREEZEplus a kill, using only supported operations: 5/5 fail without the fix, 25/25 pass with it. Sixteen new unit cases cover both directions, including the previously uncovered leak direction.