Repository navigation
Conversation
A patch part is applied to a part either wholly or not at all, so a merge of patch parts must not span a data version at which a mutations snapshot can still be built; patchHasHigherDataVersion throws LOGICAL_ERROR otherwise. MergeTreeMergePredicate::canMergeParts guards that with two version sets: the mutations still registered in current_mutations_by_version, and the data versions of the parts visible for the merge (the guard added by ClickHouse#115064). A mutate task that has already been selected is in neither set. No part carries its future data version yet, and KILL MUTATION erases the entry from current_mutations_by_version while the task keeps running: the kill cancels through the MergeList, and that entry is only inserted by MutatePlainMergeTreeTask::prepare, i.e. on the task's first executeStep, so a selected-but-not-started task does not observe it. Once both sets have forgotten the version, the patch parts on either side of it look mergeable, the merge produces a patch spanning it, and the task then builds its snapshot at exactly that version and throws. The mutation entry is persistent on-disk state that is re-selected after a restart, so the failure returns on every start until the patch is removed. StorageMergeTree already tracks those versions in currently_mutating_part_future_versions, written by CurrentlyMergingPartsTagger under currently_processing_in_background_mutex, which is the merge_mutate_lock the predicate holds, so the predicate adds them to its data-version set and restores the sorted-and-deduplicated order findDataVersionInRange relies on. The set only grows, so the change can only refuse more merges; a false refusal costs one skipped patch merge, retried after the tagger is finalized. The replicated predicates need no change: they build the same map from virtual_parts, which already contains the versions assigned by queue entries that have not executed. This makes the plain arm symmetric with them. Issue ClickHouse#116047 attributes the invisible version to a running merge. The CI trace shows a mutation instead: only mutate tasks pass a non-null max_mutation_versions, MergeTask passes nullptr. This does not repair a patch that already spans a version on disk; recovery stays ALTER TABLE ... APPLY PATCHES IN PARTITION. Closes ClickHouse#116047. Co-Authored-By: Claude Opus 5 <[email protected]>
The previous commit closed one of the two ways an in-flight operation can own a data version that no active part carries. This closes the other, which is the mechanism issue ClickHouse#116047 itself describes, so its last paragraph no longer holds: both the mutation and the merge case are real, and the CI trace only happened to exhibit the mutation one. FutureMergedMutatedPart::assign raises the result part's data version to the highest patch part the merge applies, so a regular part at version 1 merged with patch 3 produces a result at version 3. While that merge runs, version 3 is on no active part, getDataVersionsByPartition skips patch parts, and CurrentlyMergingPartsTagger recorded a future version only for mutations. A lightweight update then produces patch 5, canMergeParts sees equal mutation versions on both patches and finds nothing between 3 and 5, and the patch parts merge into [3, 5]. The merge commits its result at version 3, and the merged patch then neither wholly applies to that part nor wholly does not, so MergeTreeData::assertNoPatchesForParts throws for it and DETACH PART, DETACH PARTITION, MOVE PARTITION and REPLACE PARTITION ... FROM abort. A mutations snapshot does not throw here: a mutate task builds it at its own mutation version, which is drawn after both patch versions. StorageMergeTree therefore tracks these versions too, in a map of its own rather than in currently_mutating_part_future_versions: that one is also read to fill system.mutations.parts_in_progress_names, and merge sources there would be reported as parts a mutation is working on. Patch merges are skipped, because their result would be keyed under the patch partition id, which no lookup uses, and their version is the maximum of their sources' anyway. This is the plain-MergeTree counterpart of two protections the replicated queue already has: its predicate takes future versions from virtual_parts, which holds queue entries by name, and isMergeOfPatchPartsBlocked separately refuses a merge of patch parts whose source is scheduled to be applied in a merge. The regression test needs a merge held with its future version already registered, so mt_merge_task_pause_in_prepare_with_patches is added. It pauses only a merge that applies patch parts, which keeps unrelated merges out of it, and it is placed after the merge list entry so that a held merge is observable in system.merges instead of being waited for. Co-Authored-By: Claude Opus 5 <[email protected]>
…notes clickhouse-test rewrites a bare ENGINE = MergeTree into the shared engine under --cloud, and neither test can measure anything there: 03100_lwu_52 waits for mt_merge_task_pause_in_prepare_with_patches, which only MergePlainMergeTreeTask::prepare reaches, and 03100_lwu_36 asserts the behaviour of MergeTreeMergePredicate. Both now carry no-shared-merge-tree, as the rest of this family does. 03100_lwu_52's header named the wrong consumer of the version the merge commits at. A mutations snapshot is built at a mutate task's own mutation version, which is drawn after both patch versions, so it does not throw for a merge result. The reachable consumer is MergeTreeData::assertNoPatchesForParts, which takes its bound from the parts' data versions, so DETACH PART, DETACH PARTITION, MOVE PARTITION and REPLACE PARTITION ... FROM abort instead. 03100_lwu_36 gets the cleanup trap its sibling already has: mt_mutate_task_pause_in_prepare is PAUSEABLE_ONCE and server wide, so an early exit before the final client call would park the next mutation in that server for the rest of the run. Co-Authored-By: Claude Opus 5 <[email protected]>
03100_lwu_36 waits for the mutation with wait_for_mutation_in_progress, which polls parts_in_progress_names. That column is computed from currently_mutating_part_future_versions, which CurrentlyMergingPartsTagger fills in its constructor, at the moment the task is SELECTED. The state the test actually depends on is a thread parked inside MutateTask::prepare, where the failpoint sits, and the background pool enters that function later. So the helper returned while the park was still only promised, and the test then ran an UPDATE, an INSERT and a KILL MUTATION against it. On an idle box that gap measures 250 ms; a loaded sanitizer or stress job widens it, and the flaky check runs a brand-new test many times. Keep the helper, because the tagger entry is the state the merge predicate reads and the reference output's timing depends on it, and assert the park separately with a bounded SYSTEM WAIT FAILPOINT, so a regression is a test failure rather than a hung job. The comment above the helper claimed it waited for "exactly the state under test", which it never did; it now says which of the two states it observes. Also restate the contract of MergeTreeMergePredicate::data_versions_by_partition: it no longer holds only the data versions of the regular parts, since the constructor appends the future versions of an in-flight mutate task and of an in-flight merge, which no part carries.
Internal second-model reviewIndependent reviews of this branch (a cold read of the diff, and a separate model gate, over four ❌ Cloud engine rewrite made the new tests moot - ❌ A failure mode described that cannot happen - the merge case was documented as failing in a ❌ The running-mutation test observed the wrong state - it synchronised on
💡 Not acted on, recorded here instead. 💡 One wording nit left in, deliberately. The last round's reviews both landed on the same line: 💡 Also verified and left alone: the appended versions are a superset that cannot refuse more merges |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260910-113200 |
|
Workflow [PR], commit [73e26c3] Summary: ❌
AI ReviewSummaryThe PR prevents patch-part merges on plain Final VerdictStatus: ✅ Approve. |
|
cc @Michicosun @KochetovNicolai, could you review this? |
| /// carries yet: the merge takes its result version from those patches, and the mutation's version need not | ||
| /// still be in 'current_mutations_by_version', because KILL MUTATION erases it while the task keeps running. | ||
| chassert(merge_mutate_lock.owns_lock()); /// guards both maps read below | ||
| for (const auto & [part, future_version] : storage.currently_mutating_part_future_versions) |
There was a problem hiding this comment.
data_versions_by_partition is now snapshotting in-flight versions once in the constructor, but OPTIMIZE ... FINAL reuses the same MergeTreeMergePredicate across the wait/retry loop in StorageMergeTree::selectPartsToMerge (src/Storages/StorageMergeTree.cpp:1662, 1791-1813). If the conflicting merge/mutation exits without committing a part, CurrentlyMergingPartsTagger::finalize() erases the map entry and wakes the waiter, yet this snapshot still keeps that version for the rest of the query.
That turns a transient conflict into a permanent CANNOT_SELECT / silent no-op. A killed mutation is the concrete case here: once the paused task observes cancellation, no part ever gets that data version, so the patch parts should become mergeable again after the wake, but the reused predicate still blocks them.
Please either rebuild the predicate (and the collector that owns it) after each wait_for, or make these future versions a live lookup instead of constructor state.
There was a problem hiding this comment.
The two structural premises hold. The predicate is built once (StorageMergeTree.cpp:1706), select_in_partition reuses it across the wait_for at :1851, and CurrentlyMergingPartsTagger::finalize() does erase the entry and notify. It is also called from the destructor (:949), so a task that exits without committing always releases it.
The consequence does not follow, because that snapshot is read only inside if (left.info.isPatch()) (MergeTreeMergePredicate.cpp:151-170): it can refuse a merge of patch parts and nothing else. The loop's candidate range comes from MergeTreePartsCollector::grabAllPartsInsidePartition (:139) through filterByPartitions, which filters on part->info.getPartitionId() (Compaction/PartsCollectors/Common.cpp:37), and a patch part's own partition id is patch-<hash>-<original>, not the original (MergeTreePartInfo.cpp:60). Both callers of merge() are in optimize(), and the FINAL one enumerates ids from getVisibleDataPartsVector (StorageMergeTree.cpp:2655), which is {DataPartKind::Regular} (MergeTreeData.cpp:10195), so a patch-... id is never generated.
Measured on 52e8366, two patch parts in one patch partition, apply_patches_on_merge = 0 so nothing else consumes them:
| step | active parts |
|---|---|
| before | all_1_1_0, all_2_2_0, patch-359a..-all_4_4_0_3, patch-359a..-all_6_6_0_5 |
OPTIMIZE TABLE t FINAL |
all_1_2_1, patch-359a..-all_4_4_0_3, patch-359a..-all_6_6_0_5 |
OPTIMIZE TABLE t PARTITION ID 'patch-359a..-all' FINAL |
all_1_2_1, patch-359a..-all_4_6_1_5 |
The third row is the control: patch merges are selectable through this very path, so the second row is not a vacuous pass, and naming the internal partition id is the only way to reach the snapshot from a reused predicate.
The reuse is also pre-existing, and stale in the opposite direction: on master the part-derived half of the same map already misses versions committed during the loop, which is the direction that lets a patch merge through. Appending versions can only refuse more. The loop also stops iterating once currently_merging_mutating_parts is empty (:1846), so the query returns CANNOT_SELECT instead of spinning, and the next OPTIMIZE builds a fresh predicate.
So I am leaving it here: rebuilding the predicate and the collector inside the loop changes snapshot lifetime for every merge selection in this storage, not just patch merges, and the stale direction that can produce the logical error is not introduced by this change. I recorded the same reasoning before this thread was opened, in #119312 (comment), and that offer stands: happy to do it as a separate PR with its own test if a reviewer would prefer it addressed.
Build profile diff (arm_release)Commit See the job log for details. |
CI finish ledger - 52e8366Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Not caused by this pull request. The boundary is measurable: A master merge is therefore owed here to re-run CI on a tree that contains the revert; nothing else Session id: cron:our-pr-ci-monitor:20260911-010040 |
…tation Picks up the revert of "Unify the spill trigger for hash joins" (c46c19c), without which 05141_time_series_select_join_settings fails on this branch for a reason that has nothing to do with its change: the head was dispatched at 17:37:13Z, before the revert landed at 19:22:08Z, and praktika pins the merge ref when Config Workflow starts.
CI finish ledger - 500f057Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Attribution note, since this branch does work in the patch-parts area and the coincidence deserves an explicit answer rather than an assumption: the master commit Session id: cron:our-pr-ci-monitor:20260911-083121 |
Master's style check now requires a stateless test that enables a fail point to be tagged both `no-parallel` and `no-fasttest`. Add `no-fasttest` to 03100_lwu_36 and 03100_lwu_52, which already carry `no-parallel`. Make the tagging in the `CurrentlyMergingPartsTagger` constructor all-or-nothing. The inserts into `currently_merging_mutating_parts` and the future data version maps may allocate and throw `MEMORY_LIMIT_EXCEEDED`; the destructor does not run for a constructor that throws, so the parts stayed tagged as merging until restart. Roll the inserts back and rethrow. Also shorten the comments this PR added in MergeTreeMergePredicate.cpp and MergePlainMergeTreeTask.cpp. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rge-spanning-running-mutation
Closes: #116047
Related: #115064
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
Found patch part ... that intersects mutation with version ...(LOGICAL_ERROR) on plainMergeTreetables with lightweight updates. A merge of patch parts could span a data version owned by an operation still in flight: a mutate task that had already been selected, or a merge that takes its result version from the patch parts it applies. Resolving patches against that version then failed for good: the mutation kept aborting, and again after every restart, and once the merge had committed its result so didDETACH PART,DETACH PARTITION,MOVE PARTITIONandREPLACE PARTITION ... FROM; debug and sanitizer builds terminated. Such merges are no longer assigned. Closes #116047.Description
A patch part applies to a part wholly or not at all, so a merge of patch parts must not span a data version that is later resolved against it (
patchHasHigherDataVersion).MergeTreeMergePredicate::canMergePartsguarded that with two sets: the mutations still incurrent_mutations_by_version, and the data versions of the parts visible for the merge (#115064). Two in-flight operations are in neither, since no part carries their version yet:KILL MUTATIONerases fromcurrent_mutations_by_versionwhile the task itself keeps running;FutureMergedMutatedPart::assignraises the result's data version to the highest patch it applies, andgetDataVersionsByPartitionskips patch parts.The patch parts on both sides then look mergeable, and the union of their ranges is unresolvable: the mutate task throws when it builds its snapshot at its own version, and the merge result, once committed at a patch's version, makes
assertNoPatchesForPartsthrow, soDETACH/MOVE/REPLACE PARTITIONcannot run. The issue's CI trace exhibits the mutation case and its text describes the merge case; both are fixed.StorageMergeTreenow tracks the merge case besidecurrently_mutating_part_future_versions; both maps are written byCurrentlyMergingPartsTaggerunder the mutex the predicate holds and read into its set. The set only grows, so a false refusal costs one skipped patch merge, retried once the tagger is finalized.ReplicatedMergeTreeneeds no change: its predicate takes future versions fromvirtual_parts, which holds queue entries by name, and separately refuses these merges inisMergeOfPatchPartsBlocked.A patch that already spans a version on disk is not repaired here; recovery stays
ALTER TABLE ... APPLY PATCHES IN PARTITION.Workflow [PR]
Sync PR [sync-upstream/pr/119312]