Repository navigation
Conversation
…ITION fails to commit StorageMergeTree covers the parts a TRUNCATE, DROP/DETACH PART, DROP/DETACH PARTITION or MOVE PARTITION TO TABLE removes with empty parts one level above them. renameAndCommitEmptyParts renames those parts to their final names before committing the transaction, because renaming is IO bound and must not hold the parts lock. MergeTreeData::Transaction::commit() is not atomic across parts: it commits each part's storage transaction in a loop, and that loop, getActivePartsToReplace, addNewPartAndRemoveCovered and removal_locks.store() can all throw. rollback() then moved the parts out of the working set but never renamed them back, so a commit that failed left a part that was never committed on disk under a permanent name, with no txn_version.txt marker (the tid is non-transactional, so the RolledBackCSN store is deferred and never reaches the disk). A later merge of the still active level 0 parts produces a part of the same level over a wider block range. MergeTreePartInfo::contains requires a strictly greater level when the block ranges differ, so the two parts neither contain one another nor are disjoint, and the next part load throws Part all_1_8_1 intersects previous part all_1_1_1 out of PartLoadingTree::add. The table cannot be loaded and the server aborts on startup. Record the directory each part had before renameParts() moved it, and rename those parts out of it in rollback(). The record and the undo are opt-in per caller (setUndoRenamesOnRollback), because a caller is only allowed to undo a rename while its parts are still private to the transaction. On the replicated INSERT path the part is published in Keeper before Transaction::commit() runs and commit() can still throw after that, so there the permanent directory is the recovery path a restart needs: renaming it back would leave Keeper referencing a part this replica no longer has. Every replicated caller, every merge and every INSERT therefore keeps today's behaviour byte for byte and allocates nothing new. By the time ~Transaction runs the undo, the callers have already released the temporary directory holder and the operation mutex: all three callers of renameAndCommitEmptyParts declare the transaction before they take the operation mutex and before they bind the holders, and reverse order destruction frees both of those first. A statement that was waiting on the operation mutex can cover the same parts, derive the same block range and level, and legitimately own the recorded tmp_empty_<name> directory. The undo therefore does not target the recorded name at all: it renames the part to a free tmp_rollback_ directory, which nothing else in the tree creates or claims. Reordering the declarations is not an alternative, because the comment above each of them forbids it: rollback() takes the parts lock itself and could deadlock in a destructor. The invariant the fix needs is only that the part does not keep a permanent name: loadDataParts skips any directory whose name starts with tmp, and clearOldTemporaryDirectories reaps it. remove_new_dir_if_exists stays false, so the undo can never delete a directory another statement owns, and a failure of any kind falls through to the per-part log and continue. The same leak exists on the merge, mutation and replicated-commit paths and is not addressed here. Co-Authored-By: Claude Opus 5 <[email protected]>
Condense the failpoint and setUndoRenamesOnRollback() declaration docs, drop the design rationale above the tmp_rollback rename (it is in the commit message) and the self-evident member comment.
Internal second-model reviewBefore opening this PR I ran an independent cold review of the resulting code plus a second-model
Both directions were measured on Build-ID-distinct binaries rather than argued: with the opt-ins intact |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260923-041200 |
|
Workflow [PR], commit [0ec39fb] Summary: ❌
LLVM Coverage ReportMeasured on commit 0ec39fb.
Changed lines: Changed C/C++ lines covered: 47/51 (92.16%) · Uncovered code |
CI finish ledger - 74fb1f1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Shards 1/6, 2/6, 3/6, 5/6 and 6/6 all passed on this head, so this is a single-shard Session id: cron:our-pr-ci-monitor:20260923-190314 |
…ck-leaves-empty-part-120910 # Conflicts: # src/Common/FailPoint.cpp # src/Storages/MergeTree/MergeTreeData.cpp
The test arms a failpoint, which the failpoint_tests_are_isolated style check now requires to be tagged no-parallel and no-fasttest. Co-Authored-By: Claude Opus 5.5 <[email protected]>
At that point the part is under its permanent name, not under the directory renameParts() recorded, so the message no longer names the recorded directory. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| /// A part storage transaction that has not been committed still holds its `moveDirectory`, | ||
| /// so on an object storage disk the new directory is not there; on a local disk the move | ||
| /// already ran. | ||
| if (!part->getDataPartStorage().exists()) |
There was a problem hiding this comment.
On object-storage disks, exists()==false here is the normal "rename is still buffered in the part-storage transaction" case, not proof that rollback has nothing left to clean up. rollback() then drops the part from the working set without ever calling undoTransaction(), even though the empty-part path already documents at src/Storages/MergeTree/MergeTreeData.cpp:7926 that skipping undoTransaction() strands uploaded blobs when the final directory never materializes.
That means a multi-part TRUNCATE / DROP PARTITION / source half of MOVE PARTITION TO TABLE can still leak the later empty parts on object storage once the first commitTransaction() succeeds and a later one throws. I think rollback needs to undoTransaction() for parts whose storage is still active, and reserve the tmp_rollback_* rename path for parts whose storage transaction has already committed.
There was a problem hiding this comment.
Fixed in 0ec39fb: a rolled back part with no directory yet now has its storage transaction undone (keyed on the directory, since a local disk's transaction stays active after the move), and 05238_rollback_removes_uncommitted_part_blobs checks through system.blob_storage_log that no uploaded blob is left behind.
…k yet On an object storage disk a part's directory and metadata only appear when its storage transaction commits, while its blobs are uploaded as the files are written. When `Transaction::commit()` fails after committing the storage of some parts, the remaining parts were dropped from the working set with their storage transactions neither committed nor undone, so the blobs they had already uploaded stayed in the object storage with no metadata pointing at them. `rollback()` now undoes the storage transaction of such a part (only for the callers that opted into undoing renames, whose parts are still private to the transaction). Measured with a `local_blob_storage` disk, a 3-part TRUNCATE and the failpoint firing after the first commit: before, 14 non-empty blobs (two empty compact parts) remained after `DROP TABLE SYNC`; after, none. The new test checks this through `system.blob_storage_log`: every non-empty uploaded blob of its own disk must get a successful delete event. With the `undoTransaction()` call removed it reports 2 leaked blobs (packed parts) in 3 of 3 runs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 0ec39fbEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The other 148 check runs completed, Session id: cron:our-pr-ci-monitor:20261007-230450 |
Related: #120910
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
MergeTreetable failing to load withLogical error: Part ... intersects previous part ...after aTRUNCATE TABLE,DROP PARTITION,DROP PARTorMOVE PARTITION TO TABLEthat failed while committing. Such a statement left its empty covering part on disk under its permanent name, and a later merge then produced a same level part over a wider block range, which the part loader rejects. AMOVE PARTITION TO TABLEwhose destination commit failed also left the moved parts there, so reloading that table brought back rows the statement had not moved.Description
On 26.9, a
TRUNCATE TABLEon aMergeTreetable that fails while committing leaves its empty covering part on disk under its final name, and the table then fails to attach withLogical error: Part all_1_8_1 intersects previous part all_1_1_1, so the server does not start.MOVE PARTITION TO TABLEleaks the same way: in the destination if its commit fails, in the source if a later one does.Root cause:
StorageMergeTreecovers the parts it removes with empty parts one level above them, renamed to their final names before committing.MergeTreeData::Transaction::commit()is not atomic across parts and can throw;rollback()moved the parts out of the working set but never renamed them back, leaving no rollback marker on disk. A later merge then produces a same level part over a wider block range, whichPartLoadingTree::addrejects.The change records each part's directory before the rename and, in
rollback(), renames the part out of its permanent name into a freetmp_rollback_directory, which the loader skips. It does not rename back to the recorded directory, which another statement may own by then. On object storage, a part whose storage transaction is not committed has no directory yet; that transaction is undone instead, removing its uploaded blobs. The undo is opt-in per caller (setUndoRenamesOnRollback), because a rename may only be undone while the parts are still private to the transaction. On the replicated INSERT path the part is published in Keeper beforecommit()runs, which can still throw, so there the permanent directory is what a restart recovers from. Replicated callers, merges and mutations are therefore unchanged and keep that leak, as does the non-atomicity noted inStorageMergeTree.cppsince 2020: a source-side failure leaves the destination committed.Reported by @ alexey-milovidov in #120910 (comment), failing job https://github.com/ClickHouse/ClickHouse/actions/runs/35705130932/job/106713077937. Issue: #122212.
Workflow [PR]
Sync PR [sync-upstream/pr/121708]