Skip to content

[Review suggestion] Do not abort direct SELECT from S3Queue on ALTER that re-creates streaming tasks (for ClickHouse#130904) - #1

Closed
ch-samuel-reviewer-1 wants to merge 1 commit into
azat:s3queue-alter-parallel-insertsfrom
ch-samuel-reviewer-1:address-review-130904
Closed

ch-samuel-reviewer-1 wants to merge 1 commit into
azat:s3queue-alter-parallel-insertsfrom
ch-samuel-reviewer-1:address-review-130904

Conversation

@ch-samuel-reviewer-1

Copy link
Copy Markdown

Suggested changes for ClickHouse#130904, from the review by @NIKTONIKTO717. @azat, could you take a look?

What changed

  • StorageObjectStorageQueue::alter no longer calls stream_control.requestCancel when it re-creates the streaming tasks. It sets the new restarting_streaming_tasks flag before deactivating the old tasks, and resets it once all of them are deactivated.
  • The new StorageObjectStorageQueue::isStreamingCycleCancelled (the epoch moved, or the flag is set) replaces stream_control.isCancelRequested in the streamToViews loop and in the deduplication_v2 cancel callback. Direct SELECT sources keep checking only the epoch, which now moves only on SYSTEM STOP and SYSTEM CANCEL.
  • New test 05619_s3queue_alter_does_not_cancel_direct_select.

Why

Addresses ClickHouse#130904 (comment) from ClickHouse#130904 (review). On this branch the test's direct SELECT returns count() = 50 and sum(x) = 1225 while ALTER ... processing_threads_num = 4 re-creates the tasks; at c83c685 it fails with Consumption aborted by SYSTEM STOP or SYSTEM CANCEL. (QUERY_WAS_CANCELLED).

How to accept

  • Merge this PR into s3queue-alter-parallel-inserts, or
  • Approve it, and @NIKTONIKTO717 will push these commits to your branch.

Merging adds the changes to the upstream PR automatically. If you disagree with anything, comment here; nothing changes on your branch until this PR is merged.


Opened by ch-samuel-reviewer-1, an automated review assistant run by @NIKTONIKTO717.

🤖 Generated with Claude Code

…s streaming tasks

`ALTER ... MODIFY SETTING processing_threads_num` (with `parallel_inserts`)
called `stream_control.requestCancel`, which advances the same epoch as
`SYSTEM CANCEL`, so a concurrent direct `SELECT` failed with
`QUERY_WAS_CANCELLED`. Stop only the streaming cycles with a flag that stays
set until the old tasks are deactivated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@azat

azat commented Nov 13, 2026

Copy link
Copy Markdown
Owner

@NIKTONIKTO717 please take a look at your bot

azat pushed a commit that referenced this pull request Nov 13, 2026
9 of the 11 queries of sparse_grams and sparse_grams_index_build ran
1-8.3 s on the CI amd runner (14-day MasterCI medians). Following the
disabled-marker rule for performance tests (no existing query text or
number changes), the 9 long queries get disabled="1" and the same 9
queries are appended on fewer rows:

- sparse_grams: #1/ClickHouse#2 10M -> 1M rows (ClickHouse#6/ClickHouse#7), ClickHouse#4 10M -> 700k (ClickHouse#8),
  ClickHouse#5 4M -> 250k (ClickHouse#9).
- sparse_grams_index_build: #0-ClickHouse#4 run again as ClickHouse#5-ClickHouse#9 on a 60_000-row
  sg_src_small (same randomPrintableASCII(200) generator) instead of the
  150_000-row sg_src; the text-index INSERT takes LIMIT 2_500 instead of
  30_000. sg_src was read only by the disabled queries, so its create,
  fill and drop are replaced by sg_src_small's.

Function, string generator and length, index definitions, granularity and
settings are unchanged; only the row counts change.

Related: ClickHouse#129258

Co-Authored-By: Claude Opus 5.5 <[email protected]>
azat pushed a commit that referenced this pull request Nov 13, 2026
…up window

`NegativeLimitBySortedStreamTransform::consume` sent every group, even one
that starts and ends inside the current chunk, through the sliding window:
`append_run` pushed a `ChunkSlice` into the empty window deque (allocating the
deque map and a block, and copying the chunk `shared_ptr`), `finalizeWindow`
copied it into `pending` (a second `shared_ptr` copy) and then `window = {}`
(libc++: `clear(); shrink_to_fit();`) freed the block and the map. So each
one-row group cost two memory-tracked allocations, two frees and two atomic
increment/decrement pairs, plus one `pending` entry.

The window is only needed for a group that touches a chunk boundary. A group
that ends inside the chunk while the window is empty also started inside this
chunk, so its output is known when its end is found: the last
`min(L, length + offset)` rows of the run without the trailing `offset` rows,
the same rows `append_run` + `dropExcessRows` + `dropOffsetRows` keep for a
one-slice window. Such groups are now staged into `pending` directly, and
`appendToPending` extends the last pending slice when the new rows continue
it in the same chunk, so the one-row groups of a chunk become one slice.
`generate()` produces the same chunk for a merged slice (same rows, first
start and last end). Groups touching a chunk boundary still use the window.

Evidence that this path is what moves the `negative_limit_by_in_order`
queries #0, #1 and ClickHouse#8 (millions of groups): ClickHouse#125000, which changed only the
allocator of these deques, moved them by -19/-16/-17% on master d3ee313 while
ClickHouse#2-ClickHouse#7 stayed flat; and the ClickHouse#125013 perf job's prewarm counters for #0 show
equal instructions on both sides (10301M vs 10333M, ~1030 per group) and +15%
cycles on one build.

Local A/B (release build, `CMAKE_BUILD_TYPE=None`, no ThinLTO, the perf
test's tables and queries, 12 rotating rounds, geomean of per-round ratios
new/base with bootstrap 95% CI):

  #0 10M one-row groups      0.764 s -> 0.096 s  0.123 [0.113, 0.135]
  #1 1M groups of 10         0.098 s -> 0.049 s  0.457 [0.413, 0.510]
  ClickHouse#2 100k groups of 100      0.025 s -> 0.019 s  0.707 [0.642, 0.785]
  ClickHouse#3-ClickHouse#7 100 groups           0.86-1.03, no CI lower bound above 1.0 (4-15 ms)
  ClickHouse#8 5M two-key groups       0.447 s -> 0.116 s  0.250 [0.236, 0.265]

User-space instructions of 5 runs of #0: 60.3G -> 10.2G (~1190 -> ~190 per
group after subtracting 5 runs of ClickHouse#3), cycles 16.2G -> 3.1G; ClickHouse#8: 31.4G ->
6.4G instructions. In the base profile of #0 about half of the samples are
in the deque allocation, `shrink_to_fit`, `CurrentMemoryTracker` and
jemalloc; none of them is in the top 15 after the change.

Replacing only `window = {}` by `clear()` (keeps the deque blocks) gives
0.526 / 0.594 / 0.583 on #0 / #1 / ClickHouse#8, so the direct staging is 4.3x and
2.3x faster than that on #0 and ClickHouse#8.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
azat pushed a commit that referenced this pull request Nov 14, 2026
…oop over batches of rows

The previous commit replaced the per-row `isCancelled` load with a mask
test on the row index. On AArch64 clang-23 keeps that test and its branch
in the row loop (`tst x23, #0x3ff; b.ne` per row, the only difference
from the previous build of this branch), and `limit_by` #0/ClickHouse#3/ClickHouse#5/ClickHouse#6 and
`optimize_limit_by_keys` #0/#1 are 1.18-1.22x slower than master there,
while the two previous runs had them at 1.00x.

Make the batching explicit instead: an outer loop over batches of 1024
rows checks for cancellation, and the inner row loop contains the hash
lookup only. Measured with the CI binaries on a Graviton 4 host against
the clang-22 master build: 0.99-1.05x (PR build: 1.18-1.22x).

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117346&sha=d847a368b13c19355fbfac4bbcec67663d20ec9f&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%201%2F6%29
PR: ClickHouse#117346

Co-Authored-By: Claude Fable 5.1 <[email protected]>
azat pushed a commit that referenced this pull request Nov 14, 2026
…registers

After the outer batch loop of c23127a, clang 23 on AArch64 keeps two values
in `consumeImpl` that depend on the current row's hash table load in callee-saved
registers. The out-of-line `processRunInsideWindow` saves and restores them in
`stp`/`ldp` pairs together with registers on the next row's key load path, so the
next lookup waits for the previous row's cache miss: `limit_by_in_pk_order` ClickHouse#3
+80%, `limit_by_with_order` #1 +77%, `limit_by` #1 +42% on ARM, with user time
x1.91 and the same page faults. This is the mechanism of e16e062, one call
level down.

Move the growth of `output_slices` into a separate function reached by a tail
call, so `processRunInsideWindow` makes no calls and saves only `x29`/`x30`
(checked in an AArch64 ThinLTO build of the translation unit with clang 23.1.4;
on x86 it saves nothing either).

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117346&sha=8675465598566b9a2f459432b9831f33b2876b86&name_0=PR
PR: ClickHouse#117346

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants