Repository navigation
Conversation
|
Workflow [PR], commit [88536fd] Summary: ✅
AI ReviewSummaryReviewed #122934 at Missing context / blind spots
Final VerdictStatus: ✅ Approve for the code review. No additional code changes requested; merge remains subject to the pending CI results. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units260 translation units recompiled, 2947 s compile time in total, 260 of them have a recent master baseline. |
Plain `StorageMergeTree` decided what a mutation, a merge or `OPTIMIZE FINAL` may touch from the registered mutations only, while a version can exist before it is registered: a mutation between its file write and its registration, a lightweight `UPDATE` between block allocation and patch commit. Four races followed: two mutations registered out of order because `mutate` released the alter lock before `startMutation` (ClickHouse#122083); a mutation was applied to a part while a lightweight `UPDATE` of it was uncommitted (ClickHouse#116686); a merge spanned an allocated version or recorded a metadata mutation's version past it, so the update's patch was skipped for the merged part; `OPTIMIZE FINAL` retried after waiting for running merges with the predicate and the metadata snapshot built before the wait. `committing_blocks` is the registry of allocated but not yet visible versions. Selectors take a `CommittingBlocksSnapshot` (its `Mutation`/`Update` blocks plus a watermark: the block counter at the copy, read under the allocation mutex) while holding `currently_processing_in_background_mutex` and pass it to the merge predicate. The first such block above a part's data version bounds mutation selection, refuses merges across it, caps the merged part's recorded version and patch materialization; parts (by `min_block`) and patches above the watermark do not take part in that selection, so nothing allocated after the copy can lie between two things a selection compares. `mutate` keeps the alter lock through `startMutation`; `alter` keeps its rename mutation's block reserved until the metadata is published or rolled back and finds the mutation by version on rollback; `OPTIMIZE FINAL` rebuilds the predicate, the collector and the metadata snapshot on every retry. Built on top of ClickHouse#119155 (its two commits kept) and supersedes it: the committing-block scan guarded by `supportsLightweightUpdate()` is replaced by the snapshot; its reason string and test are kept. Tests: 05291, 05292, 05294-05299, 05302, 05304 (failpoint-driven; 05294 arm 2, 05295 and the restored-part arm of 05297 are guards that also pass without the fix), 05227 from ClickHouse#119155. Closes: ClickHouse#122083 Closes: ClickHouse#116686 Signed-off-by: Mikhail Filimonov <[email protected]>
…lector Rename the snapshot's `watermark` to `last_allocated_block`. `MergeTreePartsCollector` takes that bound and drops late parts itself, so `isPartAfterSnapshot` and `getReservations` are gone: the caller owns the snapshot and passes it to the predicate and to `getMutationVersionForMergedPart`. Rename test 05295 to `05295_pending_version_cleanup_after_error`: the style check rejects test paths containing `fail`. Signed-off-by: Mikhail Filimonov <[email protected]>
18baed6 to
f6f9622
Compare
The style check (67965ab) requires `no-parallel` and `no-fasttest` on every test that arms a fail point. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
|
@murphy-4o fixed one of those bugs in #124167 slightly differently. But this one covers several more similar cases. I've already rebased that branch twice.... It's not productive. |
|
|
||
| /// Released under the mutex so that the scheduler run triggered by the registration never sees the block as pending. | ||
| if (prepared) | ||
| prepared->block_holder.reset(); |
There was a problem hiding this comment.
Why not? It should not reflect on correctness
There was a problem hiding this comment.
Reverted to the master structure in a4d8dd7 (addPreparedMutationEntry by value, mutation_registered, no explicit holder resets). Two things were behind the diff, neither worth it here:
- the by-reference form covered a throw inside
addPreparedMutationEntryafter the insert, where the caller'spreparedis already moved from; nothing there throws in practice; - the retained block holder covered the double-fault case: the in-memory commit throws, then the durable rollback throws too and the rename is re-registered, and an update committed in that window could be merged past the rename's block.
Dropped with their tests 05302 and 05298; can be a follow-up.
| if (prepared) | ||
| { | ||
| auto it = current_mutations_by_version.find(mutation_version); | ||
| if (it != current_mutations_by_version.end()) | ||
| /// `prepared->entry` is intact unless `addPreparedMutationEntry` got past its insert. | ||
| held_version = prepared->version; | ||
| if (auto it = current_mutations_by_version.find(held_version); it != current_mutations_by_version.end()) | ||
| { | ||
| decrementMutationsCounters(mutation_counters, *it->second.commands); | ||
| held_version = it->first; | ||
| held_entry.emplace(std::move(it->second)); | ||
| current_mutations_by_version.erase(it); | ||
| } | ||
| } | ||
| else if (prepared) | ||
| { | ||
| held_version = prepared->version; | ||
| held_entry.emplace(std::move(prepared->entry)); | ||
| else | ||
| { | ||
| held_entry.emplace(std::move(prepared->entry)); | ||
| } |
There was a problem hiding this comment.
Same as the reply on StorageMergeTree.cpp:955: reverted to the master structure in a4d8dd7.
| { | ||
| if (held_entry) | ||
| { | ||
| chassert(held_entry->commands); |
There was a problem hiding this comment.
Same as the reply on StorageMergeTree.cpp:955: reverted to the master structure in a4d8dd7.
| if (prepared) | ||
| prepared->block_holder.reset(); |
There was a problem hiding this comment.
Same as the reply on StorageMergeTree.cpp:955: reverted to the master structure in a4d8dd7.
| extern const char mt_throw_after_mutation_entry_inserted[]; | ||
| extern const char mt_throw_after_mutation_commit[]; | ||
| extern const char mt_pause_before_register_mutation[]; | ||
| extern const char mt_alter_throw_in_durable_rollback[]; | ||
| extern const char mt_alter_pause_before_durable_rollback[]; | ||
| extern const char mt_optimize_pause_after_reservation_snapshot[]; | ||
| extern const char mt_optimize_pause_before_reading_patches[]; | ||
| extern const char mt_mutation_pause_before_block_allocation[]; |
There was a problem hiding this comment.
Too many, it complicates the code.
There was a problem hiding this comment.
Down to two new ones in a4d8dd7: mt_mutation_pause_before_block_allocation (05297) and mt_optimize_pause_after_reservation_snapshot (05297, 05304; the second optimize pause is folded into it). The other two went with 05298 and 05302.
|
|
||
| std::expected<MergeMutateSelectedEntryPtr, SelectMergeFailure> StorageMergeTree::selectPartsToMerge( | ||
| const StorageMetadataPtr & metadata_snapshot, | ||
| StorageMetadataPtr & metadata_snapshot, |
There was a problem hiding this comment.
Why does it need to be solved in this pr?
There was a problem hiding this comment.
It does not need to be. Removed together with 05299; separate PR.
| /// The wait released the mutex: reservations, last allocated block and metadata are stale. A stale bound | ||
| /// would drop parts inserted meanwhile, and a rename published meanwhile would be missed. | ||
| if (attempt > 0) | ||
| { | ||
| auto metadata_snapshot_handle = getInMemoryMetadataPtr(getContext(), false); | ||
| metadata_snapshot = metadata_snapshot_handle; | ||
| reservations = getCommittingBlocksSnapshot(); | ||
| merge_predicate = std::make_shared<MergeTreeMergePredicate>(*this, txn, lock, reservations); | ||
| parts_collector = std::make_shared<MergeTreePartsCollector>(*this, txn, merge_predicate, reservations.lastAllocatedBlock()); | ||
| } | ||
| ++attempt; | ||
|
|
There was a problem hiding this comment.
Let's do that unconditionally on every attempt and move the previous predicate/collector pair construction inside lambdas.
There was a problem hiding this comment.
Done in a4d8dd7: build_predicate_and_collector is called once in select_without_hint and at the start of every attempt in select_in_partition.
| { | ||
| auto metadata_snapshot_handle = getInMemoryMetadataPtr(getContext(), false); | ||
| metadata_snapshot = metadata_snapshot_handle; | ||
| reservations = getCommittingBlocksSnapshot(); |
There was a problem hiding this comment.
This looks like it should be obtained inside the predicate instead of patching its constructor arguments.
There was a problem hiding this comment.
Done in a4d8dd7: MergeTreeMergePredicate takes the snapshot in its constructor; selectPartsToMerge no longer builds it.
There was a problem hiding this comment.
Let's use the approach from DistributedMergePredicate instead here.
- Filter by min block number in the constructor, not min update block number.
- Check committing blocks between parts instead of checking updates only.
- check that merges can be chosen only for parts that are less than
last_allocated_block(the same guarantee asprev_virtual_parts).
IMO we need to unite the predicates instead of rewriting this logic once again.
There was a problem hiding this comment.
I tried to write it that way. The block-gap rule of DistributedMergePredicate is not equivalent to the version rule here, because in MergeTree mutation and the block number diverge:
- A new patch part is named with a fresh commit block but carries
mutation= the update's version (MergeTreeDataWriter::writeTempPatchPart,MergeTreeSink::commitPart). Updates 2 and 4 can commit as blocks 6 and 5 while update 3 is still pending: the version rule refuses merging those two patches across 3, a block-gap check between 5 and 6 sees nothing. - A restored part keeps a foreign
mutation(RESTORE keeps the backed-up mutation version of parts, later mutations on the restored table are silently skipped #122940):all_1_1_0_100+all_2_2_0with reservation 3. The block rule allows the merge, the merged part records 100, and mutation 3 is skipped on that data. REPLACE PARTITIONallocates its drop-range block beforelockParts. MergeTree has one counter for all partitions, so a check over all committing blocks between two parts would block merges in every other partition for the duration of the replace.
(3) is already there: the collector drops parts above last_allocated_block, the same guarantee as prev_virtual_parts.
What can be shared without changing behaviour is the structural part (partition/patch/volume checks, mutation-version equality, getPatchesToApplyOnMerge through a MutationsState adapter). The reservation checks would stay engine-specific. I'd rather do that extraction as a follow-up than in this PR.
|
|
||
| /// Committing blocks and the block counter copied together: every later block is above `last_allocated_block`. A reservation | ||
| /// is an `Update` or `Mutation` block (a version allocated but not visible yet), never a `NewPart` block. | ||
| class CommittingBlocksSnapshot |
There was a problem hiding this comment.
IMO, if we rewrite to a distributed merge predicate approach, this new abstraction will not be needed and getCommittingBlocks can just return a pair of committing blocks and last allocated block.
There was a problem hiding this comment.
Kept it, but it is now internal to the predicate: the constructor takes it, the collector and getMutationVersionForMergedPart read it through getReservations(). A pair would work too; firstReservationAfter over the sorted vector is used in four places, so the small class stays.
Keep the bounded mutation range from this branch in `selectPartsToMutate` and take `patch_versions_by_partition` from master. The unregistered-mutation loop of ClickHouse#124167 is subsumed: a `Mutation` reservation bounds the range the same way and now reports `UNREGISTERED_MUTATION`. Test 05331 (from ClickHouse#124167) assumed an ALTER can register above a paused mutation. `mutate` holds the alter lock until registration, so the ALTER waits instead; the test now checks that and keeps the merge arm. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
…ter-rollback changes Review follow-ups for ClickHouse#122934: - `MergeTreeMergePredicate` takes the committing-blocks snapshot in its constructor and exposes it; `MergeTreePartsCollector` and `getMutationVersionForMergedPart` read it from there. - `selectPartsToMerge` builds the predicate and the collector through one lambda: once for background selection, on every attempt of the `OPTIMIZE FINAL` retry loop. - `alter`, `addPreparedMutationEntry` and `startMutation` are back to the master text. The retained block holder covered only the case where the in-memory commit and the durable rollback both throw; tests 05298 and 05302 go with it. - `OPTIMIZE FINAL` no longer re-reads the metadata on retry; test 05299 goes with it (separate PR). - Two test fail points remain: `mt_mutation_pause_before_block_allocation` and `mt_optimize_pause_after_reservation_snapshot`; 05304 uses the latter for its second arm. - Test 05331 checks that an `ALTER` times out on the alter lock while a mutation is paused before registration, instead of waiting for it in the background. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
…snapshot and the reads With the snapshot taken by the predicate, a pause after construction is too late for the second arm of 05304: the patch attached meanwhile is never read, so the `lastAllocatedBlock` filter on patches is not exercised. The hook now sits in the constructor and fires only on a thread with a query context, so the background scheduler cannot consume the one-shot pause. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
The style check rejects `while true` in stateless tests: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122934&sha=f1ebbd8a8a42670529aef329a4d3d7dea54d34a3&name_0=PR&name_1=Style%20check Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
…s not apply `getMutationVersionForMergedPart` stopped only at reservations. A patch part that is committed but not applied by the merge, because a mutation is pending below it or `apply_patches_on_merge` is off, let the merged part record a version above the patch; the patch then never applied to it and the update was lost (ClickHouse#125315, second reproducer). The merge predicate reports the first such patch above the sources' data version and the recorded version stays below it. Test 05493 covers a patch between two pending `DROP COLUMN` mutations and `apply_patches_on_merge = 0`. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
… once A `Mutation` reservation now gates merges, so a mutation start that fails after block allocation left refused merges waiting for the scheduler backoff. `removeCommittingBlock` triggers the assignee for `Mutation` blocks as it does for `Update`. Patches above the snapshot watermark are dropped once in the predicate constructor instead of being copied out on every `getPatchesToApplyOnMerge` call; `firstPatchNotAppliedAfter` sees the same set. Tests 05292, 05294, 05296, 05493 set `max_bytes_to_merge_at_max_space_in_pool = 1` so a background merge cannot join the two parts before the scenario starts. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
…d an unapplied patch `getMinUpdateBlockNumber` counts an `Unknown` block as an update, so the snapshot's reservation list does the same instead of ignoring it. Plain MergeTree never allocates one; the two views of the snapshot now agree. 05493 gains the refusal arm: a `RENAME COLUMN` registered above a patch the merge does not apply makes `OPTIMIZE FINAL` report `CANNOT_ASSIGN_OPTIMIZE`. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
…art name `ATTACH PART` gives a patch part a fresh block number and drops the version from its name; the index inside keeps the version readers apply it at. With the bound taken from the name, a merge recorded a pending `DROP COLUMN` above the index version and the merged part never got the patch (reproduces on master). `firstPatchNotAppliedAfter` now reads the index and matches applied patches by name. Test 05304 gains the reattach arm. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
The cutoff that keeps a merge from applying a patch above its snapshot also kept that patch out of the version bound, so a patch reattached between the snapshot and the parts read did not stop the merge from recording a pending `DROP COLUMN` above the patch's index version. The cutoff now applies to materialization only. Test 05304 gains the combined arm. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
The merged part takes the version of the last patch the merge applied, so a patch left out below that version would never apply to it. Only a reattached patch can be left out below an applied one: its block is above the snapshot or the next mutation while its index keeps the version it applies at. The first such patch now ends the prefix, and the newer patches wait for a merge that sees it. Test 05304 gains the arm with an older reattached patch and a newer applicable one. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
Reverts 4041460, 2b2a665 and 1ab6805. They made the merge-side bounds read a patch part's version from `PatchPartIndex` instead of the part name, because `ATTACH PART` drops the version from the name and keeps the index. Every merge-side consumer of the name had to be revisited one by one (`firstPatchNotAppliedAfter`, the predicate's patch map, the applied-patch prefix), and a merged patch part whose index spans the reattached version still broke the last variant. The name/index mismatch is an `ATTACH PART` defect present on master and is tracked separately; this change stays with the block reservations. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
…ther The merge predicate left a patch part above `last_allocated_block` for read time. Only `ATTACH PART` puts a patch there, and leaving it out is safe only while the merged part's version stays below the version the patch applies at, which its name no longer says. Master applies such a patch by the version in its name; do the same, and keep the cutoff at the first pending update only. The reservation bound in `getPatchesToApplyOnMerge` is unchanged. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
…tation A part inserted while `OPTIMIZE FINAL` waits for running merges or mutations is above the snapshot of allocated block numbers taken before the wait. The retry rebuilds the predicate and the collector, so the part is merged; with the snapshot built once before the loop the part stays out and two parts remain active. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
The style check `failpoint_tests_are_isolated` requires `no-parallel` and `no-fasttest` on a test that enables a failpoint. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=122934&sha=a4434ad1e567bd40945e69b4505224ac709d60fd&name_0=PR&name_1=Style%20check PR: ClickHouse#122934 Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
|
Dear @Michicosun, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Closes: #122083
Closes: #125314
Closes: #125315
Closes: #125319
Related: #116686
Related: #122704
Related: #119155
Related: #124167
Related: #113459
Related: #129058
Plain
MergeTreedecided what a mutation, a merge orOPTIMIZE FINALmay touch from the registered mutations only, while a version can exist before it is registered: a mutation between its file write and its registration, a lightweightUPDATEbetween block allocation and patch commit. Selection that crosses such a version loses the mutation or the update for the affected rows. The fix makes those versions boundaries for selection.Changes
mutatekeeps the alter lock throughstartMutation, so two mutations cannot register out of order (Concurrent lightweight DELETE can mark an earlier mutation done without applying it #122083).MergeTreeMergePredicatetakes one snapshot ofcommitting_blocks(theMutationandUpdateblocks pluslast_allocated_block, the block counter read under the allocation mutex) whilecurrently_processing_in_background_mutexis held;selectPartsToMutatetakes the same snapshot. The first such block above a part's data version bounds mutation selection (Plain MergeTree: a concurrent heavyweight ALTER UPDATE/DELETE silently drops an acknowledged in-flight lightweight UPDATE (no havePendingPatchPartsForMutation guard on the mutation-selection path) #116686, generalizing Leave a part alone while a lightweight update of it is uncommitted #119155 and Fix a concurrent ALTER UPDATE being skipped and reported done in MergeTree #124167 without thesupportsLightweightUpdate()guard), refuses merges across it, caps the version recorded for a merged part, and bounds which patches a merge materializes. Parts allocated abovelast_allocated_block(bymin_block) do not take part in that selection, so nothing allocated after the copy can lie between two things a selection compares.apply_patches_on_merge = 0) caps the recorded version the same way: at or above the patch's version the merged part would never get the patch applied (Merge records a version above an unapplied lightweightUPDATE: update lost #125315, second trigger). ARENAME COLUMNregistered above such a patch refuses the merge. The version is the one in the patch's name, as everywhere in merge selection.ATTACH PARTof a patch part drops that version from the name and keeps it inPatchPartIndex, which readers use; a merge then records a version above the reattached patch and hides it. That mismatch is present on master and is out of scope here:ATTACH PARTof a patch part drops its version from the name: a merge records a version above it and the update is lost #129398.OPTIMIZE FINALrebuilds the merge predicate and the parts collector on every retry after waiting for running merges; the snapshot built before the wait was stale.removeCommittingBlockwakes the background scheduler forMutationblocks too (a failed mutation start no longer leaves refused merges waiting for the backoff) and notifies every waiter: waiters wait for different block numbers, sonotify_onecould wake one that cannot proceed.Behaviour
lock_acquire_timeoutapplies to it as to anyALTER. ConcurrentALTER ... UPDATE/DELETEsubmissions serialize on that write; anALTERthat waits past the timeout fails withDEADLOCK_AVOIDED(as for any alter-lock wait), a mutation withTIMEOUT_EXCEEDED.DROP COLUMNit materialized when a version between them is not visible yet;system.mutationsthen lists it as pending until the mutation re-applies the drop as a no-op.RENAME COLUMNcannot be re-applied, so such a merge is refused instead.OPTIMIZE FINALnever merges across a version that is still being committed. It leaves those parts for the next attempt (withoptimize_throw_if_noop = 1it reportsCANNOT_ASSIGN_OPTIMIZE) and merges them as usual once the version is visible.OPTIMIZE FINALreading its metadata snapshot before the wait for running merges (Silent data loss: OPTIMIZE FINAL that waits for a running merge keeps a stale metadata snapshot, so a concurrent RENAME COLUMN is stamped as applied but written with the old name #122704);alterrollback holes (Failed ALTER RENAME COLUMN rollback on MergeTree leaves the rename mutation applied against mismatched metadata #129058).Testing
OPTIMIZE FINALretry: with the snapshot taken once before the wait, a part inserted during the wait stays out of the merge.05331_mutation_not_skipped_before_registrationfrom Fix a concurrent ALTER UPDATE being skipped and reported done in MergeTree #124167 is adjusted: anALTERissued while a mutation is paused before registration times out on the alter lock instead of registering above it.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes lost mutations and lost lightweight updates on
MergeTreetables when a mutation or a merge runs concurrently with a mutation or a lightweightUPDATEthat is already issued but not yet visible: mutations can no longer register out of order, and merges no longer cross a version that is still being committed. Also fixesOPTIMIZE FINALreusing a stale merge plan after waiting for running merges.Workflow [PR]
Sync PR [sync-upstream/pr/122934]