Repository navigation
Honor a break-mode timeout while building child plans of a Merge table - #113642
Conversation
Follow-up to ClickHouse#113415, which added a cancellation checkpoint to the per-child loop in ReadFromMerge::createChildrenPlans but discarded checkTimeLimit's return value. checkTimeLimit signals a timeout two different ways. For KILL QUERY and timeout_overflow_mode = 'throw' it throws from throwProperExceptionIfNeeded, which is gated on the killed flag. For 'break' it instead returns false: ExecutionSpeedLimits::handleOverflowMode returns false for OverflowMode::BREAK, and CancellationChecker::cancelTask calls cancelQuery only for THROW, so a break-mode deadline never sets that flag. The checkpoint therefore could not alter control flow in that mode and the loop kept running full query analysis for every remaining matched table past the deadline. The stop is a break rather than a throw because timeout_overflow_mode declares break as "stop executing the query and return the partial result, as if the source data ran out". A throw would contradict that, and the note above max_execution_time already concedes that a query "currently cannot stop during query analysis", which is exactly this loop, so this makes the documented break semantics reachable in a place the docs exempt rather than adding a new error. Leaving the loop early breaks the 1:1 alignment between getSelectedTables() and the child plans that ReadFromMerge documents, and initializePipeline is the sole positional consumer of it (child_plans->at(i) bounded by selected_tables.size()), so the non-const caller truncates selected_tables to the plans actually built. A one-sided shrink is not representable: reverting only the truncation aborts in initializePipeline with std::out_of_range from vector::at. Per-child resources are pushed only for children that built, so dropping the trailing tables drops nothing still referenced, and truncating to empty reuses the NullSource path a Merge table matching nothing already takes. The other plan-time sites that also discard the false return (JoinOrderOptimizer::checkLimits, and part selection in MergeTreeDataSelectExecutor) are deliberately left alone: they belong to other subsystems, and truncating part selection would be wrong results rather than a smaller result. The test paces the loop with a PAUSEABLE failpoint instead of timing it, which is what makes a deterministic stateless test possible here. The KILL QUERY arm is a control for the half ClickHouse#113415 already fixed and stays green without this change. The row count cannot serve as the oracle, because the same elapsed budget that stops the plan build has also stopped the pipeline, so both outcomes return zero rows; the test counts how many children were planned instead. Co-Authored-By: Claude Fable 5 <[email protected]>
The test synchronises on a PAUSEABLE failpoint against its own two-second max_execution_time budget, and the cancellation checkpoint is evaluated before the pause on every iteration including the first. If server-side startup has not reached the first child within that budget, the guard fires with zero children planned, selected_tables truncates to empty, initializePipeline takes its NullSource path, and the first SYSTEM WAIT FAILPOINT PAUSE times out, so the test reports a failure on a correct binary. The runner randomizes dozens of settings that move analysis and planning cost, among them max_threads and optimize_read_in_order, and the file pinned none of them. Add no-random-settings. No coverage is lost: the changed code path is reached only with timeout_overflow_mode = 'break', which the runner never randomizes, and the test pins both timeout settings in its own query, so randomization was never exercising the behaviour under test. Also relax the control assertion from an exact count to a lower bound and prune the system.text_log read with the usual event_date and event_time predicate. The witness is unchanged: it is the inequality between the children planned by the timed-out query and by the control. Co-Authored-By: Claude Fable 5 <[email protected]>
The break-mode assertions only observe that the loop stopped, not how much of selected_tables survived. Truncating it to empty instead of to the plans built keeps both containers aligned, so nothing aborts: ReadFromMerge::initializePipeline takes its early NullSource return, and the wait still times out, the client still exits 0 with empty stderr, and the child counter still reads one, because that counter is logged after the break. So discarding the planned child shipped green. Assert ProfileEvents['SelectedParts'] instead. ReadFromMergeTree::initializePipeline increments it while building a child's pipeline, before any row is read, which is the only observable that separates the two truncation widths here: the retained prefix cannot be observed through rows, since the same deadline has already stopped the pipeline and both outcomes read zero rows. Measured on the shipping binary: break arm 1, control 4, deterministic over repeats. A lower bound is asserted rather than an equality because the number of parts is not the property under test. The mutant that over-truncates reddens this assertion alone and leaves the four existing break-arm assertions green. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Internal second-model review (click to expand)Before publishing I reviewed this change cold (independently of the notes written while
Two claims raised during review were withdrawn after I re-derived them, rather than acted on:
The two-sided truncation was checked against every reader of the two containers, not just the Session id: cron:clickhouse-review-slot-10:20260806-092700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20260806-081400 |
|
cc @KochetovNicolai @tavplubix, could you review this? It is a follow-up to #113415: |
|
Workflow [PR], commit [764c8db] Summary: ❌
AI ReviewSummaryThis PR makes Final Verdict✅ Looks good to merge. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 15/15 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units22 translation units recompiled, 33 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - 764c8dbCI is fully finished on this head: 175 check-runs, 0 queued or in progress,
Not caused by this pull request. The diff touches
Session id: cron:our-pr-ci-monitor:20260809-003000 |
af10c1c
Pulls in the merged fix ClickHouse#113642 for the hung `Stress test (arm_asan_ubsan)` check (uncancellable `ReadFromMerge` child-plan analyzer loop), which was the sole red on this PR.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
SELECTfrom aMergetable that exceedsmax_execution_timewithtimeout_overflow_mode = 'break'now stops building query plans for the remaining matched tables, instead of running full query analysis for every matched table before stopping. The query still finishes without an error, as that mode documents.Description
Follow-up to #113415, which added a cancellation checkpoint to the per-child loop in
ReadFromMerge::createChildrenPlansbut discardedcheckTimeLimit's return.checkTimeLimitsignals a timeout two ways. ForKILL QUERYandtimeout_overflow_mode = 'throw'it throws, gated on the killed flag. For'break'it returnsfalseinstead, and abreakdeadline never sets that flag, so the existing checkpoint cannot alter control flow and the loop keeps analysing every remaining matched table.This stops the loop on the
falsereturn and truncatesselected_tablesto the plans built, keeping the two aligned 1:1 asReadFromMergedocuments. The stop is abreakrather than a throw because that is what the setting declares (Settings.cpp: "stop executing the query and return the partial result, as if the source data ran out"), so the query stays silent and successful. No new error code, andKILL QUERYandthrowmode are unaffected.The behaviour change is confined to that mode. The deadline is shared with the pipeline, so such a query has already exceeded its execution-time budget: the saving is analysis work, not rows.
Validated with a new stateless test that paces the loop with a
PAUSEABLEfailpoint instead of timing it. Pre-fix thebreakarm fails: all 4 children are planned and the loop pauses at a second child. With the fix it stops after the first, and the client still exits 0 with empty stderr. Four mutants each redden only their own arm: reverting the guard, deleting the failpoint, and either direction of the truncation. Reverting it aborts ininitializePipelinewithstd::out_of_range; over-truncating leaves the pipeline empty, which every other assertion accepts, so both directions are pinned. 50/50 runs pass (p50 26.1 s); the test opts out of settings randomization, so that is the only meaningful arm.