Repository navigation
Make INSERT responsive to cancellation when a block creates many partitions - #108062
alexey-milovidov wants to merge 1 commit into
Conversation
…itions When a single INSERT block is scattered into a large number of single-partition parts (for example, a high-cardinality `partition by` expression together with `throw_on_max_partitions_per_insert_block = 0`), `MergeTreeSink::consume` and `ReplicatedMergeTreeSink::consume` wrote every part in one loop without ever returning to the pipeline executor. The executor enforces `max_execution_time` and `KILL QUERY` only between `consume` calls, so such an INSERT could not be interrupted and ran until all parts were written. The AST fuzzer in the stress test turned `numbers_mt(10)` into roughly `numbers_mt(1048577)` for an insert into a `partition by key` table, producing about 1048577 single-row parts on object storage. The query ran for over 1400 seconds, the hung check failed, the server could not shut down (the thread was stuck inside `consume`), and the subsequent restart failed with "Address already in use". Check the query time limit via `QueryStatus::checkTimeLimit` at the start of each per-partition iteration, so the INSERT honors `max_execution_time` and `KILL QUERY` instead of running an uninterruptible loop. This matches the existing pattern used in `StorageMergeTree` and `StorageReplicatedMergeTree`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=a006dbd8a1e92a14b7404c4bfdcd90b6345ab6ee&name_0=MasterCI&name_1=Stress%20test%20%28amd_tsan%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [ce1958b] Summary: ❌
AI ReviewSummaryThe PR makes many-partition Missing context / blind spots
Findings
Tests
Final VerdictStatus: Minimum required actions: cover cancellation inside the delayed finalization/commit phase for both sinks, and add replicated regression coverage for the changed replicated path. |
| /// cancellation only between `consume` calls. Check it here so such a query stays responsive | ||
| /// to `max_execution_time` and `KILL QUERY` instead of running an uninterruptible loop. | ||
| if (auto process_list_element = context->getProcessListElement()) | ||
| process_list_element->checkTimeLimit(); |
There was a problem hiding this comment.
The new check covers the temporary-part creation loop, but the same consume call can still spend a long time in the delayed finalization/commit phase without any cancellation check. finishDelayedChunk iterates delayed_chunk->partitions and finalizes/commits each part without consulting the ProcessList, and the batch can contain many partitions when max_insert_delayed_streams_for_parallel_write is raised (object-storage writes also use delayed batches by default). A KILL QUERY or throw-mode max_execution_time that arrives during that phase is still ignored until the whole delayed batch is done, so the many-partition INSERT can remain stuck inside consume/onFinish.
Please add the same per-partition check inside finishDelayedChunk and the replicated finishDelayed loop, or otherwise make the delayed batch size bounded independently of that setting.
| -- the time limit, hanging the query (and the server shutdown) for many minutes. | ||
|
|
||
| drop table if exists t_many_partitions_cancel; | ||
| create table t_many_partitions_cancel (key Int) engine = MergeTree order by key partition by key; |
There was a problem hiding this comment.
This PR also changes ReplicatedMergeTreeSink::consume, but the regression test only exercises the plain MergeTree sink. If the replicated check is removed, 04358_insert_many_partitions_cancellable still passes, so the replicated half of the promised fix has no regression coverage.
Please add a small ReplicatedMergeTree variant, either in this test with the appropriate tags or as a separate stateless test, using the same high-cardinality partition by block and max_execution_time assertion.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 10/10 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 18 line(s) · Uncovered code |
|
Btw imo the right place to check it is in the low-level data writing functions. Somewhere in the write buffers or in the keeper client. This looks like an ad-hoc single-place fix. I tried ~ this #89286 |
|
Closing as a duplicate. |
A single
INSERTblock can be scattered into a very large number of single-partition parts — for example, a high-cardinalitypartition byexpression together withthrow_on_max_partitions_per_insert_block = 0. In that caseMergeTreeSink::consumeandReplicatedMergeTreeSink::consumewrote every part in one loop without ever returning to the pipeline executor. Because the executor enforcesmax_execution_timeandKILL QUERYonly betweenconsumecalls, such anINSERTcould not be interrupted and ran until all parts were written.This was found by the AST fuzzer in the stress test: it turned
numbers_mt(10)into roughlynumbers_mt(1048577)for an insert into apartition by keytable, producing about 1048577 single-row parts on object storage. The query ran for over 1400 seconds, the hung check failed, the server could not shut down (the thread was stuck insideconsume), and the subsequent restart failed withAddress already in use— surfacing asHung check failed, possible deadlock found,Cannot start clickhouse-server, andCheck failed.The fix checks the query time limit via
QueryStatus::checkTimeLimitat the start of each per-partition iteration, so theINSERThonorsmax_execution_timeandKILL QUERYinstead of running an uninterruptible loop. This mirrors the pattern already used inStorageMergeTreeandStorageReplicatedMergeTree. Normal inserts (one or a few partitions) are unaffected: it is a single cheap check per partition that only throws when a limit is actually reached.Added a regression test
04358_insert_many_partitions_cancellablethat asserts such anINSERTreturnsTIMEOUT_EXCEEDEDinstead of hanging.CI report (master): https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=a006dbd8a1e92a14b7404c4bfdcd90b6345ab6ee&name_0=MasterCI&name_1=Stress%20test%20%28amd_tsan%29
Related: #107941
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an
INSERTthat creates a large number of partitions in a single block (for example, a high-cardinalitypartition bytogether withthrow_on_max_partitions_per_insert_block = 0) being uninterruptible: it now respectsmax_execution_timeandKILL QUERY.🤖 Generated with Claude Code