Repository navigation
Reject REPLACE/MOVE/DETACH PARTITION with pending lightweight-update patches in plain MergeTree - #107386
Conversation
…n MergeTree
A lightweight UPDATE records, per row, the source part's data version in a
separate patch part. Plain StorageMergeTree partition operations clone or keep
only the base data parts and renumber them to new, higher block numbers, while
the pending patch part is left behind. On the next read the renamed base part's
version is above the patch's recorded version, so the patch is dropped and the
committed update is silently reverted (data loss).
Reproducer (returns [5,1] instead of [11,15] before this change):
SET enable_lightweight_update = 1;
CREATE TABLE t (c0 Int) ENGINE = MergeTree ORDER BY tuple()
SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1;
INSERT INTO t VALUES (1);
INSERT INTO t VALUES (5);
UPDATE t SET c0 = c0 + 10 WHERE TRUE;
ALTER TABLE t REPLACE PARTITION ID 'all' FROM t;
SELECT groupArray(c0) FROM t;
The same silent revert happens for REPLACE/ATTACH PARTITION FROM another table
whose source partition has pending patches, MOVE PARTITION TO TABLE, DETACH
PARTITION, and DETACH PART.
StorageReplicatedMergeTree already guards exactly these operations with
getPatchPartsVectorForPartition + assertNoPatchesForParts, rejecting them with
an actionable SUPPORT_IS_DISABLED message that points at APPLY PATCHES. This
brings plain StorageMergeTree to the same behavior. DROP PARTITION stays
allowed, matching the Replicated engine (DROP destroys data by design).
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-5:20260613-101000 |
|
cc @CurtizJ @alesapin — could you review this? Plain (non-replicated) MergeTree silently reverts a committed lightweight UPDATE when REPLACE/MOVE/DETACH PARTITION (or DETACH PART) runs on a partition with pending patch parts: the base parts are cloned/renumbered and the orphaned patch is dropped on the next read. This adds the same |
|
Workflow [PR], commit [e7d2a45] Summary: ✅
AI ReviewSummary
Missing context / blind spots
Final Verdict
|
…ES race In replacePartitionFrom and movePartitionToTable the base parts to clone and the patch parts were read under two separate parts-lock acquisitions. A concurrent source-side APPLY PATCHES could materialize and remove the patch between the two reads, so assertNoPatchesForParts would pass while the code went on to clone the already-captured pre-patch base parts, reopening the silent revert the guard is meant to prevent. Take both the visible source parts and the patch parts under a single source-side parts lock before deciding whether to reject, matching StorageReplicatedMergeTree::replacePartitionFromImpl. APPLY PATCHES swaps parts under the exclusive parts lock, so holding it (exclusive for replacePartitionFrom, shared for movePartitionToTable) across both reads makes the two snapshots consistent. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (re-validation after the lock-snapshot fix)
Build ID c2e22ddad2ab12eea9538efd3f193f94722d3154 (changed from ff412c710c3a..., confirmed in the running test server). Session id: cron:clickhouse-worker-slot-2:20260613-165300 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 66/67 (98.51%) | Lost baseline coverage: none · Uncovered code |
|
CI fully finished and clean on HEAD |
|
Hi — this looks like a backport candidate for |
|
Confirmed pre-existing, not a recent regression: we independently reproduced this silent data loss on a clean master HEAD (commit |
…rtition adoption Cross-table partition adoption (REPLACE/ATTACH/MOVE PARTITION FROM, CLONE AS, cross-shard/foreign FETCH) hardlink-clones parts and keeps their content unchanged, including the persisted _block_number/_block_offset columns that a source table with enable_block_number_column/enable_block_offset_column materializes at merge time. Those pairs are the unique row identity Join-mode patch application depends on (PatchPartInfo.h). Every table numbers blocks from 1, so imported identities collide with the destination's own. A later merge gathers the duplicates into one part; applying a lightweight-update patch in Join mode then hits the sorted/unique assertion in applyPatchJoin (applyPatches.cpp:578, an abort under debug/sanitizer builds) or silently patches the wrong rows in a release build. Add MergeTreeData::assertNoPersistedBlockIdentityInAdoptedParts, which throws SUPPORT_IS_DISABLED when a part being adopted physically stores _block_number or _block_offset, and call it at every cross-table adoption door in both the MergeTree and ReplicatedMergeTree engines (replacePartitionFrom, movePartitionToTable, the to_detached path of fetchPart, movePartitionToShard). CLONE AS is covered via its lowering to ATTACH PARTITION ALL FROM. The check is content-based, not settings-based, because destination settings are mutable and a part without the persisted files serves only name-derived virtuals. Self REPLACE is exempt because it removes the originals it clones; self ATTACH is not. This is the same layer and policy as PR ClickHouse#107386, which rejected the sibling patch-orphaning case. The allocator-vs-content class (RESTORE, detach/restart/ attach, non-empty restore) is a distinct bug left for a follow-up. Also make a failing FETCH PARTITION clean up after itself. fetchPartition fetches the parts of a partition concurrently and rethrows only after every worker has finished, so for a partition holding both a merged identity-bearing part and a later level-0 part the level-0 worker renames its part into detached before the merged one is rejected. The statement then failed while leaving that part behind, which made it attachable and made a retry fail earlier and differently, on checkIfDetachedPartitionExists with PARTITION_ALREADY_EXISTS. Record the parts this query itself renamed into detached and, unless the whole partition was fetched, remove exactly those on the way out. With to_detached a true result from fetchPart means that call performed the rename under that exact name, which is what establishes ownership: the entry check is explicitly racy, so a name alone would not, and a part detached concurrently would have taken a _tryN name instead. Each part is renamed out of the way and removed on its own, as DROP DETACHED PARTITION does, so failing on one part neither rolls back nor skips the others, and a failure after the rename leaves at worst an inert directory. The staging name carries a unique suffix so a leftover from an earlier attempt can never collide with, and thereby block, a later one. This non-atomicity predates the new guard, which is simply the first common way to reach it. Adds 03100_lwu_54_attach_foreign_block_ids, covering the REPLACE/ATTACH/MOVE and FETCH rejections, both single-column sources (_block_offset only and _block_number only, so each half of the check is exercised on its own), the allowed column-less and self-REPLACE cases on both engines, the absence of any surviving part after a rejection (no active part in the rejected CLONE AS target, no detached part after the rejected FETCH, which pins the ordering the fetchPart guard relies on), the mixed-partition FETCH leaving behind only a part that was detached before it ran and staying retryable with the same error, and a lightweight-update correctness postlude. Pins the two settings off in five existing tests whose merged sources newly hit the guard under randomization: 00975_move_partition_merge_tree, 02998_projection_after_attach_partition, 03008_s3_plain_rewritable, 03008_s3_plain_rewritable_alter_partition and 03008_azure_plain_rewritable_alter_partition. Found by the Stress test (arm_tsan) job on PR ClickHouse#108715: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108715&sha=870fe2bdb1cfda4dbd20fd6fff4f788e074f0736&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29 ClickHouse#108715 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…rtition adoption Cross-table partition adoption (REPLACE/ATTACH/MOVE PARTITION FROM, CLONE AS, cross-shard/foreign FETCH) hardlink-clones parts and keeps their content unchanged, including the persisted _block_number/_block_offset columns that a source table with enable_block_number_column/enable_block_offset_column materializes at merge time. Those pairs are the unique row identity Join-mode patch application depends on (PatchPartInfo.h). Every table numbers blocks from 1, so imported identities collide with the destination's own. A later merge gathers the duplicates into one part; applying a lightweight-update patch in Join mode then hits the sorted/unique assertion in applyPatchJoin (applyPatches.cpp:578, an abort under debug/sanitizer builds) or silently patches the wrong rows in a release build. Add MergeTreeData::assertNoPersistedBlockIdentityInAdoptedParts, which throws SUPPORT_IS_DISABLED when a part being adopted physically stores _block_number or _block_offset, and call it at every cross-table adoption door in both the MergeTree and ReplicatedMergeTree engines (replacePartitionFrom, movePartitionToTable, the to_detached path of fetchPart, movePartitionToShard). CLONE AS is covered via its lowering to ATTACH PARTITION ALL FROM. The check is content-based, not settings-based, because destination settings are mutable and a part without the persisted files serves only name-derived virtuals. Self REPLACE is exempt because it removes the originals it clones; self ATTACH is not. This is the same layer and policy as PR ClickHouse#107386, which rejected the sibling patch-orphaning case. The allocator-vs-content class (RESTORE, detach/restart/ attach, non-empty restore) is a distinct bug left for a follow-up. Also make a failing FETCH PARTITION clean up after itself. fetchPartition fetches the parts of a partition concurrently and rethrows only after every worker has finished, so for a partition holding both a merged identity-bearing part and a later level-0 part the level-0 worker renames its part into detached before the merged one is rejected. The statement then failed while leaving that part behind, which made it attachable and made a retry fail earlier and differently, on checkIfDetachedPartitionExists with PARTITION_ALREADY_EXISTS. Record the parts this query itself renamed into detached and, unless the whole partition was fetched, remove exactly those on the way out. With to_detached a true result from fetchPart means that call performed the rename under that exact name, which is what establishes ownership: the entry check is explicitly racy, so a name alone would not, and a part detached concurrently would have taken a _tryN name instead. Each part is renamed out of the way and removed on its own, as DROP DETACHED PARTITION does, so failing on one part neither rolls back nor skips the others, and a failure after the rename leaves at worst an inert directory. The staging name carries a unique suffix so a leftover from an earlier attempt can never collide with, and thereby block, a later one. This non-atomicity predates the new guard, which is simply the first common way to reach it. Adds 03100_lwu_54_attach_foreign_block_ids and its ReplicatedMergeTree half 03100_lwu_55_attach_foreign_block_ids_replicated, covering the REPLACE/ATTACH/MOVE and FETCH rejections, both single-column sources (_block_offset only and _block_number only, so each half of the check is exercised on its own), the allowed column-less and self-REPLACE cases on both engines, the absence of any surviving part after a rejection (no active part in the rejected CLONE AS target, no detached part after the rejected FETCH, which pins the ordering the fetchPart guard relies on), the mixed-partition FETCH leaving behind only a part that was detached before it ran and staying retryable with the same error, and a lightweight-update correctness postlude. Pins the two settings off in five existing tests whose merged sources newly hit the guard under randomization: 00975_move_partition_merge_tree, 02998_projection_after_attach_partition, 03008_s3_plain_rewritable, 03008_s3_plain_rewritable_alter_partition and 03008_azure_plain_rewritable_alter_partition. The new coverage is split across two files, and four of the five pinned tests carry no-flaky-check, because the flaky check runs each changed test 50 times with the thread fuzzer enabled and fails any single run over 180s. The fuzzer adds a per-statement cost that dominates these tests: the combined adoption test measured 0.4s without it and 130s with it, on a 93-statement file whose queries are individually trivial, and the four object-storage and 10M-row tests were already 14-21s each without it. Neither the assertions nor the fixtures change: the split preserves all 68 non-boilerplate statements, all 12 expected-error markers and all 14 reference lines in order, and the two halves measure 60s and 62s under the fuzzer. no-flaky-check is used rather than the long tag because it narrows only the flaky check, leaving the Fast test and coverage jobs untouched. Found by the Stress test (arm_tsan) job on PR ClickHouse#108715: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108715&sha=870fe2bdb1cfda4dbd20fd6fff4f788e074f0736&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29 ClickHouse#108715 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…-tree-replace-partition-drops-patch-parts Reject REPLACE/MOVE/DETACH PARTITION with pending lightweight-update patches in plain MergeTree
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix silent data loss on plain (non-replicated)
MergeTreewhenREPLACE PARTITION,MOVE PARTITION,DETACH PARTITION, orDETACH PARTis run on a partition that still has unapplied lightweightUPDATEpatches. These operations now reject the command (asReplicatedMergeTreealready does), pointing toALTER TABLE ... APPLY PATCHES, instead of silently reverting the committed update.Description
A lightweight
UPDATErecords, per row, the source part's data version in aseparate patch part. Plain
StorageMergeTreepartition operations clone or keeponly the base data parts and renumber them to new, higher block numbers, while
the pending patch part is left behind. On the next read the renamed base part's
version is above the patch's recorded version, so the patch is dropped and the
committed update is silently reverted (data loss).
Minimal reproducer (returns
[5,1]instead of[11,15]before this change):The same silent revert happened for
REPLACE/ATTACH PARTITION FROManothertable whose source partition has pending patches,
MOVE PARTITION TO TABLE,DETACH PARTITION, andDETACH PART.StorageReplicatedMergeTreealready guards exactly these operations withgetPatchPartsVectorForPartition+assertNoPatchesForParts, rejecting themwith an actionable
SUPPORT_IS_DISABLEDmessage that points atAPPLY PATCHES.This brings plain
StorageMergeTreeto the same behavior.DROP PARTITIONstays allowed, matching the Replicated engine (it destroys data by design).
No existing GitHub issue found.
Tested locally on a debug build: new test
03100_lwu_51_replace_partition_pending_patchfails on master HEAD (the operations silently succeed and the data reverts) and
passes with this change; the existing
03100_lwu_*patch-parts suite (including03100_lwu_22_detach_attach_patches) and the00626/00975/01818/02271/02731partition-op tests are unaffected.
Version info
26.6.1.1157