Repository navigation
Cancel a mutation's IN-subquery set build on shutdown / KILL MUTATION - #110198
Conversation
A mutation whose `x IN (subquery)` right-hand side is materialized during primary-key / skip-index analysis (`KeyCondition::buildOrderedSetInplace`) used to run that nested set-building pipeline without observing mutation cancellation. A background mutation's query context has no process list element, no progress callback, and no interactive-cancel callback on this speculative build, so a large or slow subquery blocked server shutdown, `SYSTEM STOP MERGES`, and `KILL MUTATION` until the whole subquery had finished. `mutatePartToTemporaryPart` now installs an interactive-cancel callback on the mutation's query context that mirrors `MutationContext::checkOperationIsNotCanceled` (`merges_blocker` / `is_cancelled`). The reading context resolves `getQueryContext` back to this query context, so the nested `CompletedPipelineExecutor` in `FutureSetFromSubquery::buildOrderedSetInplace` polls the callback and aborts the set build promptly on cancellation. Closes: #51586 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [8fb3108] Summary: ✅
AI ReviewSummaryThis PR wires the mutation query context's interactive-cancel callback into nested Final VerdictStatus: ✅ Approve |
…tation-in-subquery-set-build
tiandiwonder
left a comment
There was a problem hiding this comment.
optional: the description says the new test "also strengthens the existing 03644_cancel_huge_mutation_subquery", but the diff doesn't touch 03644 — "complements" would avoid sending readers looking for a change that isn't there.
| /// Reading and analysis performed while building the mutation pipeline can run nested, blocking | ||
| /// pipelines of their own via `CompletedPipelineExecutor` - most notably `buildOrderedSetInplace` | ||
| /// in `KeyCondition`, which materializes the right-hand side of `x IN (subquery)` to use it for | ||
| /// primary-key / skip-index analysis. Such a build has no way to observe mutation cancellation, so | ||
| /// a large set could block server shutdown or `KILL MUTATION` for a long time (see issue #51586). | ||
| /// The nested `CompletedPipelineExecutor` polls the query context's interactive-cancel callback, so | ||
| /// install one here that mirrors `MutationContext::checkOperationIsNotCanceled`. `context` is the | ||
| /// mutation's query context (`makeQueryContextForMutate`), so the callback is reachable from the | ||
| /// reading context via `getQueryContext`. |
There was a problem hiding this comment.
optional: this comment could be tighter — the analysis-mechanism detail reads better in the PR description, and "mirrors MutationContext::checkOperationIsNotCanceled" is subtly off (that function's ?:/|| precedence skips is_cancelled when new_data_part is set, so this lambda is actually stronger).
| /// Reading and analysis performed while building the mutation pipeline can run nested, blocking | |
| /// pipelines of their own via `CompletedPipelineExecutor` - most notably `buildOrderedSetInplace` | |
| /// in `KeyCondition`, which materializes the right-hand side of `x IN (subquery)` to use it for | |
| /// primary-key / skip-index analysis. Such a build has no way to observe mutation cancellation, so | |
| /// a large set could block server shutdown or `KILL MUTATION` for a long time (see issue #51586). | |
| /// The nested `CompletedPipelineExecutor` polls the query context's interactive-cancel callback, so | |
| /// install one here that mirrors `MutationContext::checkOperationIsNotCanceled`. `context` is the | |
| /// mutation's query context (`makeQueryContextForMutate`), so the callback is reachable from the | |
| /// reading context via `getQueryContext`. | |
| /// Building the mutation pipeline can run nested blocking pipelines via `CompletedPipelineExecutor` - | |
| /// most notably `KeyCondition::buildOrderedSetInplace` materializing the right side of `x IN (subquery)`. | |
| /// Such a build observes cancellation only through the query context's interactive-cancel callback, so | |
| /// without one it blocks server shutdown and `KILL MUTATION` until the subquery finishes (issue #51586). | |
| /// `context` is this mutation's query context, which the reading context resolves via `getQueryContext`. |
There was a problem hiding this comment.
Good catch — you're right that the ?:/|| precedence in checkOperationIsNotCanceled skips the is_cancelled check when new_data_part is set, so this lambda is stronger, not a mirror. I've rewritten the comment along the lines of your suggestion, describing the mechanism directly and dropping the mirrors reference. Also changed "strengthens" to "complements" in the PR description for 03644. Thank you!
The reviewer (tiandiwonder) noted that describing the callback as mirroring `MutationContext::checkOperationIsNotCanceled` is subtly off: because `?:` has lower precedence than `||`, that function skips the `is_cancelled` check whenever `new_data_part` is set, so this lambda (which always checks both the partition blocker and `is_cancelled`) is actually stronger. Rewrite the comment to describe the mechanism directly and drop the inaccurate "mirrors" reference. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… state The regression test `04516_cancel_mutation_in_subquery_set_build.sh` waited for the mutation to be running (present in `system.merges` with `elapsed > 2`) before issuing `KILL MUTATION`, but on timeout it only logged a message and `break`-ed out of the loop, continuing the test. On a busy worker that left a false-pass path: `KILL MUTATION` would cancel a still-queued mutation, `system.merges` would already be empty, and the final `0` / `100` output would match the reference without ever exercising the in-flight `CompletedPipelineExecutor` cancellation path this PR fixes. Make the timeout fail hard with `exit 1` (as `03644_cancel_huge_mutation_subquery.sh` already does), so the test cannot pass unless the mutation actually reached the running `system.merges` state that the fix is meant to cover. Addresses the AI Review "Request changes" verdict on PR #110198. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…tation-in-subquery-set-build
|
The two red checks on
AI Review is ✅ Approve on the current head. |
…tation-in-subquery-set-build
|
The The only other red, |
|
The only red on This is the known race tracked in #110133 (fails on master itself, independent of any PR). @groeneai already has a fix open: #109747. AI Review is ✅ Approve on the current head. |
|
The |
…tation-in-subquery-set-build
|
Merged Rationale: the Remaining known-unrelated flaky reds, both with open fixes not yet on master:
|
…hared PreparedSetsCache The AI review of #110198 flagged that installing an interactive-cancel callback on a mutation's query context lets a nested `IN (subquery)` set build be stopped mid-flight by `KILL MUTATION` / shutdown - but also by a per-partition cancellation (`stopMergesAndWaitForPartition` / `cancelForPartition`, used by `REPLACE`/`MOVE`/`DROP PARTITION`). When such a build is cancelled, `CreatingSetsTransform`'s destructor stores a "Failed to build set, most likely pipeline executor was stopped" exception into the mutation-scoped `PreparedSetsCache` (shared across the part tasks of one plain `MergeTree` mutation when `enable_sharing_sets_for_mutations` is on, the default). `PreparedSetsCache::findOrPromiseToBuild` then rethrew that sticky exception to every later user of the same key, so cancelling one partition's task could break sibling tasks in partitions that were not cancelled. Make a poisoned cache entry rebuildable instead of a permanent failure: `findOrPromiseToBuild` now treats a ready future that holds an exception the same way it already treats a ready future holding a null set - it drops the entry and hands out a fresh promise so the set is rebuilt. This matches the existing intent documented in `FutureSetFromSubquery::buildOrderedSetInplace`, where the non-destructive speculative build already avoids sharing through the cache for exactly this reason. A cancelled sibling part task now fails at most once and rebuilds the set on its next attempt. Add `gtest_prepared_sets_cache` covering reuse of a successful set, rebuild of a null result, and - the regression - rebuild after a builder stored an exception (both for a later caller and for a concurrent waiter's retry). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI Review "Request changes" finding on The reviewer correctly noted that the new interactive-cancel callback also fires on per-partition cancellation ( The |
…romiseToBuild The `check_catch_all` style check flagged the `catch (...)` added in `ebbb745196c` (`PreparedSets.cpp:774`) as silently swallowing exceptions. The catch is intentional — a cached set build that finished by throwing (e.g. a cancelled `CreatingSetsTransform`) is dropped and rebuilt rather than rethrown to an unrelated caller — so annotate it with an `Ok` comment as the check expects, keeping the existing `NOLINT` for `bugprone-empty-catch`. This was the sole real failure on `ebbb745196c`: the `catch_all` style violation dropped the whole PR workflow, which in turn dropped the per-arch `Bugfix validation (functional tests, ...)` jobs and made the `new_tests_check.py` gate (`Finish Workflow`) fail. Both jobs passed on the prior head `f8577171081`, so fixing the style check clears both reds. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Fixed the two reds on
The AI Review |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 54/54 (100.00%) · Uncovered code |
… state The regression test `04516_cancel_mutation_in_subquery_set_build.sh` waited for the mutation to be running (present in `system.merges` with `elapsed > 2`) before issuing `KILL MUTATION`, but on timeout it only logged a message and `break`-ed out of the loop, continuing the test. On a busy worker that left a false-pass path: `KILL MUTATION` would cancel a still-queued mutation, `system.merges` would already be empty, and the final `0` / `100` output would match the reference without ever exercising the in-flight `CompletedPipelineExecutor` cancellation path this PR fixes. Make the timeout fail hard with `exit 1` (as `03644_cancel_huge_mutation_subquery.sh` already does), so the test cannot pass unless the mutation actually reached the running `system.merges` state that the fix is meant to cover. Addresses the AI Review "Request changes" verdict on PR ClickHouse#110198. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…hared PreparedSetsCache The AI review of ClickHouse#110198 flagged that installing an interactive-cancel callback on a mutation's query context lets a nested `IN (subquery)` set build be stopped mid-flight by `KILL MUTATION` / shutdown - but also by a per-partition cancellation (`stopMergesAndWaitForPartition` / `cancelForPartition`, used by `REPLACE`/`MOVE`/`DROP PARTITION`). When such a build is cancelled, `CreatingSetsTransform`'s destructor stores a "Failed to build set, most likely pipeline executor was stopped" exception into the mutation-scoped `PreparedSetsCache` (shared across the part tasks of one plain `MergeTree` mutation when `enable_sharing_sets_for_mutations` is on, the default). `PreparedSetsCache::findOrPromiseToBuild` then rethrew that sticky exception to every later user of the same key, so cancelling one partition's task could break sibling tasks in partitions that were not cancelled. Make a poisoned cache entry rebuildable instead of a permanent failure: `findOrPromiseToBuild` now treats a ready future that holds an exception the same way it already treats a ready future holding a null set - it drops the entry and hands out a fresh promise so the set is rebuilt. This matches the existing intent documented in `FutureSetFromSubquery::buildOrderedSetInplace`, where the non-destructive speculative build already avoids sharing through the cache for exactly this reason. A cancelled sibling part task now fails at most once and rebuilds the set on its next attempt. Add `gtest_prepared_sets_cache` covering reuse of a successful set, rebuild of a null result, and - the regression - rebuild after a builder stored an exception (both for a later caller and for a concurrent waiter's retry). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…-mutation-in-subquery-set-build Cancel a mutation's IN-subquery set build on shutdown / KILL MUTATION
A mutation whose
x IN (subquery)right-hand side is materialized during primary-key / skip-index analysis (KeyCondition::buildOrderedSetInplace→FutureSetFromSubquery::buildOrderedSetInplace) runs a nested, blockingCompletedPipelineExecutorto build the set. A background mutation's query context has no process-list element, no progress callback, and no interactive-cancel callback on that speculative build, so a large or slow subquery blocked server shutdown,SYSTEM STOP MERGES, andKILL MUTATIONuntil the whole subquery had finished. (Theuse_index_for_in_with_subqueries_max_valuescap only bounds the retained set elements, not the subquery runtime.)MergeTreeDataMergerMutator::mutatePartToTemporaryPartnow installs an interactive-cancel callback on the mutation's query context that mirrorsMutationContext::checkOperationIsNotCanceled(merges_blocker/is_cancelled). The reading context resolvesgetQueryContextback to this query context (set up bymakeQueryContextForMutate), so the nestedCompletedPipelineExecutorpolls the callback and aborts the set build promptly on cancellation. This covers both plain (MutatePlainMergeTreeTask) and replicated (MutateFromLogEntryTask) mutations.Verified locally with an A/B build: with the fix
KILL MUTATIONon an in-flightx IN (slow subquery)set build clears the merge-list entry in ~0.8s; without it the entry lingers for the whole subquery duration. New test04516_cancel_mutation_in_subquery_set_buildreproduces this and complements the existing03644_cancel_huge_mutation_subquery.Closes: #51586
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed slow server shutdown and unresponsive
KILL MUTATIONwhen a mutation withx IN (subquery)was building the subquery set during primary-key analysis; the set build is now cancelled promptly.