Skip to content

Do not keep a rolled back empty part until the parts in its range are removed - #122375

Merged
alexey-milovidov merged 12 commits into
masterfrom
fix-rolled-back-empty-part-removal
Oct 9, 2026
Merged

alexey-milovidov merged 12 commits into
masterfrom
fix-rolled-back-empty-part-removal

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

grabOldParts keeps an outdated empty part until every outdated part it covers is removed, so that a restart does not resurrect them. It did the same for the empty parts of a rolled back transaction — e.g. a TRUNCATE whose commit failed after its empty covering parts had already been renamed on disk. Such a part never covered anything, yet it stayed on disk for old_parts_lifetime (the EMPTY_PART_COVERS_OTHER_PARTS hold also overrode remove_rolled_back_parts_immediately), and a merge could meanwhile write a part intersecting it. When the transaction was implicit, nothing on disk marks the part as rolled back (the RolledBackCSN of a non-transactional part is never persisted), so PartLoadingTree cannot skip it and the table fails to load after a restart:

Logical error: 'Part all_12_26_2 intersects previous part all_12_16_2. It is a bug or a result of manual intervention'

Found by Stress test (arm_release) on master: an injected memory fault failed TRUNCATE TABLE dst of 03755_insert_select_duduplication.sh while committing the metadata of its third empty part; the rollback left all_12_16_2 on disk, a merge 0.7 s later wrote all_12_26_2, and the server could not start after the restart 4 minutes later:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=8d281dface090dddfd7d6a87be150c0d779941e9&name_0=MasterCI&name_1=Stress%20test%20%28arm_release%29

Now, when the operation fails after the empty parts were renamed and there is no transaction, renameAndCommitEmptyParts rolls back and removes them from disk right away (tryRemovePartImmediately), so they never stay on disk to be loaded after a restart. (Exempting all rolled back parts from the hold in grabOldParts is not possible: the empty covering part of DROP_RANGE / REPLACE_RANGE is deliberately rolled back right after it is added, and must stay until the parts it covers are removed.) A new fail point mt_throw_after_renaming_empty_parts reproduces the failed commit; the test 05258_rolled_back_empty_part_not_held_by_covered_parts stops the background cleanup and checks that the rolled back parts are gone right after the failed TRUNCATE, and that the table loads after DETACH / ATTACH both before and after a merge. The source side of MOVE PARTITION TO TABLE covers the moved parts with empty parts in the same way, so it uses the same removal (removeRolledBackEmptyPartsAndRethrow), covered by 05325_rolled_back_empty_part_of_move_partition_removed.

Related: #120980 (same symptom, different cause: leftovers of REPLACE PARTITION)

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix Part ... intersects previous part ... when loading a MergeTree table after a TRUNCATE, DROP PARTITION or MOVE PARTITION TO TABLE failed to commit (e.g. because of a memory limit or a disk error) and a merge ran before the restart. The server could fail to start because of it.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/122375]

Version info

  • Merged into: 26.10.1.1904-master (included in 26.10 and later)

… removed

`grabOldParts` keeps an outdated empty part until all outdated parts it
covers are removed, so that they are not resurrected on restart. It did so
also for the empty parts of a rolled back transaction, e.g. a `TRUNCATE`
whose commit failed after the parts had been renamed on disk. Such a part
never covered anything, but it stayed on disk for `old_parts_lifetime`,
and a merge could meanwhile write a part intersecting it. When the
transaction was implicit, nothing on disk marks the part as rolled back,
so on restart the table failed to load with `Part all_12_26_2 intersects
previous part all_12_16_2. It is a bug or a result of manual intervention`,
and the server did not start.

Found by `Stress test (arm_release)`, where an injected memory fault
failed `TRUNCATE TABLE dst` of `03755_insert_select_duduplication.sh`:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=8d281dface090dddfd7d6a87be150c0d779941e9&name_0=MasterCI&name_1=Stress%20test%20%28arm_release%29

Add the fail point `mt_throw_after_renaming_empty_parts` to reproduce it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [410ba89]

Summary: ✅


AI Review

Summary

This PR closes the non-transactional rollback hole for empty covering parts by removing renamed empty parts synchronously on the failure path and by reusing the same cleanup for MOVE PARTITION TO TABLE. I re-checked the updated rollback helper, the MOVE PARTITION TO TABLE caller, the prior inline threads, and the new stateless tests; the previously reported restart, object-storage cleanup, and PreActive working-set leaks now look addressed, and I did not find any new correctness or safety issues in the current diff.

Final Verdict

✅ No new findings in the current code. The existing clickhouse-gh threads are resolved correctly, so I am not posting additional inline comments.

LLVM Coverage Report

Measured on commit 410ba89.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 56/81 (69.14%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Sep 26, 2026
alexey-milovidov and others added 2 commits October 3, 2026 03:41
# Conflicts:
#	src/Storages/MergeTree/MergeTreeData.cpp
The style check `failpoints_disabled_in_tests` requires every test that enables a fail point
to disable it on every exit path, and tests that arm fail points must be tagged `no-fasttest`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122375&sha=1fc640aabe52bd0ce2a93754eed5e6648bfcc7ea&name_0=PR&name_1=Style%20check

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/StorageMergeTree.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 410ba8979a6441d8ff8cdc13bbcc08b1459ba7a8 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception; stopped retrying after the 1250 s read budget).

See the job log for details.

alexey-milovidov and others added 2 commits October 4, 2026 23:40
The previous approach let `grabOldParts` remove any rolled back empty part
without waiting for the outdated parts in its range. But the empty covering
part written by `removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper`
for `DROP_RANGE` / `REPLACE_RANGE` is deliberately rolled back right after it
is added, so it got `RolledBackCSN` too and was removed before the parts it
covers. After `DETACH` / `ATTACH` those parts came back as unexpected parts,
and `ReplicatedMergeTree` stayed read-only: `02486_truncate_and_unexpected_parts`
failed in https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122375&sha=d64b4d9f93423b046530f3903f0b607c60989ec6&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20azure%2C%20parallel%29

Revert that, and instead remove the empty parts of `renameAndCommitEmptyParts`
right away when the operation fails after they were renamed and there is no
transaction. This also closes the restart window pointed out in review: the
parts no longer stay on disk until the next cleanup, so a restart cannot load
them as covering parts. The test now stops cleanup and does `DETACH` / `ATTACH`
right after the failed `TRUNCATE`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/StorageMergeTree.cpp Resolved
alexey-milovidov and others added 2 commits October 5, 2026 22:01
The removal renames the part directory to `delete_tmp_` first, so a partial
failure is already durable; only a failure of that rename leaves the part to
be loaded after a restart, and then no on-disk marker could be written either.
Log such parts at error level instead of silently proceeding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lient error

When the rolled back empty parts of a failed `TRUNCATE` / `DROP PARTITION` cannot be removed,
add the warning that they must be detached manually to the exception returned to the client,
not only to the server log.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/StorageMergeTree.cpp Resolved
…back` throws

`MergeTreeData::Transaction::rollback` stores `RolledBackCSN` before it moves
the parts out of `PreActive`, so if it throws, the renamed empty parts stay in
the working set, and `tryRemovePartImmediately` cannot remove them. Move such
parts to `Temporary` directly, clear the transaction so its destructor does not
roll them back again, and notify `preactive_parts_cv` waiters.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/StorageMergeTree.cpp Resolved
…ty parts

`createEmptyPart` opens a part storage transaction for each empty part. When the
commit fails before reaching `commitTransaction`, the transaction stays active, and
on object storage its staged operations and uploaded blobs are invisible to
`tryRemovePartImmediately`, so they would be stranded. Undo it before removing the
part, as `MergeTreeData::removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper`
does for its empty covering part.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/StorageMergeTree.cpp Resolved
alexey-milovidov and others added 3 commits October 8, 2026 06:33
…PARTITION TO TABLE` right away

The source side of `MOVE PARTITION TO TABLE` covers the moved parts with empty parts, the same way as `TRUNCATE`
and `DROP PART`. If committing them failed after the rename, a non-transactional move left them on disk until
the background cleanup, so a restart could load them as covering parts and hide the source partition.
Move the immediate removal from `renameAndCommitEmptyParts` into `removeRolledBackEmptyPartsAndRethrow` and
call it from `movePartitionToTable` as well.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…or the style check

The `catch` block calls `removeRolledBackEmptyPartsAndRethrow`, which always rethrows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic might look complicated. However, it (1) applies to empty parts, and (2) has enough logging. So it is safe and introspectable. The result looks good to me.

@filimonov

Copy link
Copy Markdown
Contributor

The catch block in removeRolledBackEmptyPartsAndRethrow aborts the server: it moves the PreActive parts to Temporary via removePartsFromWorkingSetImmediatelyAndSetTemporaryState, then hands them to tryRemovePartImmediately, whose Temporary branch (MergeTreeData.cpp:8135) skips modifyPartState(..., Deleting), so asMutableDeletingPart raises a logical error at MergeTreeData.cpp:4520. Before this PR that branch was unreachable — every other caller passes an Outdated part.

<Fatal> : Logical error: 'Cannot remove part all_1_1_1, because it has state: Temporary'
(from thread 17) (query: ALTER TABLE t_rollback_mark DROP PARTITION tuple()) Received signal 6, Aborted

The block runs whenever rollback itself throws, i.e. when the store of the rolled-back creation_csn fails — the same class of fault that found this PR. Reproduced on an ASan+UBSan build with the failpoints from #124485, failing both the removal store and the creation_csn store of one DROP PARTITION. Setting the state to Deleting in that branch should be the whole fix.

filimonov added a commit to filimonov/ClickHouse that referenced this pull request Oct 12, 2026
ClickHouse#122375 removes the rolled-back empty covering parts
inside the failing statement, so a failed `DROP PARTITION` or `TRUNCATE` no longer empties the
table after a reload. The two reproductions assert that instead of expecting it to fail: the parts
must leave the disk (`leftover_part_dirs`), and the rows must survive `DETACH`/`ATTACH`.

`test_failed_drop_partition_survives_reload_when_rollback_mark_fails` is skipped. When the
rollback itself throws, the handler added by ClickHouse#122375 hands a part in state `Temporary` to
`tryRemovePartImmediately`, which raises `Cannot remove part ..., because it has state:
Temporary`; a debug or sanitizer build aborts on it and the node is gone for the rest of the
module. The reload it checks still loses the rows, so the test stays as written.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Mikhail Filimonov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants