Skip to content

Start executor threads for CPU slots granted after the request - #125901

Merged
serxa merged 18 commits into
masterfrom
pipeline-executor-spawn-without-mutex
Nov 5, 2026
Merged

serxa merged 18 commits into
masterfrom
pipeline-executor-spawn-without-mutex

Conversation

@serxa

@serxa serxa commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

A query that starts while CPU slots are busy gets fewer threads than it asked for. When other queries release their slots, the slots are granted to this query, but it did not start threads for them: PipelineExecutor acquired slots only right after a step pushed tasks to the shared queue, and a thread that keeps running its own chain of tasks does that only after 128 local tasks in a row. Until then the granted slots stayed unused, by this query and by every other one.

ExecutorTasks now keeps the requested threads that found no slot (threads_to_spawn, at most the pipeline width). Every thread checks it after each task, without a lock, and starts a thread for each slot granted since. Requests come from pushTasks (unchanged demand, so setMax grows as before), from the initial tasks, and in place of a thread downscaled because its preempted CPU lease was not granted back in time, so that thread is started again once the slot is.

spawn_mutex is removed with the pending_demand counter and the spin loop around it. tryAcquire, ExecutorTasks::upscale and the thread pool are thread-safe, so any thread can start threads for the slots it acquired; ExecutorTasks now raises the setMax target itself as it requests threads, so pushTasks returns nothing; the target only grows, and setMax is called with the latest target under a separate mutex, so concurrent calls can't leave the allocation below it.

Tests: gtests for a slot granted while the pipeline runs (lazy and eager allocation) and for a downscaled thread, integration tests where another query releases the slots under concurrent_threads_soft_limit_num and where a worker downscaled by CPU preemption is replaced once the competing query finishes, and a performance test where a workload gives a query fewer CPU slots than threads.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A query that started while CPU slots were busy, or whose threads were stopped by CPU preemption, now starts threads for the slots granted to it later as soon as they are granted. Previously such slots could stay unused for seconds, and other queries could not use them either.


Workflow [PR]
Sync PR [sync-upstream/pr/125901]

Version info

  • Merged into: 26.11.1.1421-master (included in 26.11 and later)

A thread requested by `ExecutorTasks::pushTasks` was dropped if no CPU slot
was granted at that moment, so a slot granted later stayed unused until the
pipeline pushed new tasks again. `ExecutorTasks` now keeps such requests in
`threads_to_spawn`, and every thread starts threads for granted slots after
each task while it is non-zero.

Remove `spawn_mutex` with the `pending_demand` spin loop: spawning is safe
from any thread, and `desired_threads` is an atomic that only grows.

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

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [016fb17]

Summary: ✅


AI Review

Summary

PipelineExecutor now retains requests for unavailable CPU slots, retries acquisition after each task, and requests replacements for downscaled workers. ExecutorTasks owns the growing allocation target and orders setMax calls separately from task scheduling. No new actionable findings remain at 016fb17739a4ee21d8cd8195adfb6862aa2faae5; the previous bot findings are addressed in the current code.

Missing context / blind spots
  • ⚠️ Current-head unit and integration jobs remain pending in the workflow index. No local build was available; validation relies on the regression tests and the author's recorded fixed-versus-baseline runs. Completed current-head CI would close this execution-validation gap.
Final Verdict

Status: ✅ Approve. No additional code changes requested; current-head CI results remain pending.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 14, 2026
After the slots are released, every step waits until all threads are in it,
instead of counting steps until they all ran one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Oct 15, 2026
A thread stops when its preempted CPU lease is not granted back in time.
It was started again only when new tasks were pushed; now `ExecutorTasks::downscale`
adds a requested thread, so it starts as soon as the slot is granted.

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

serxa commented Oct 15, 2026

Copy link
Copy Markdown
Member Author

Related: #125394 fixes the same problem, CPU slots granted to a running query that no thread picks up. It keeps the current spawning code and retries tryAcquire after every task while the last spawnThreads call ran out of slots.

This PR deals with it where the requests are made instead. ExecutorTasks keeps the requested threads that found no slot, next to the other slot accounting, so the executor needs no separate flag and no spawn_mutex. The same counter covers lazy and eager allocation and the threads downscaled by CPU preemption, which are not covered there, and it drops the remaining requests once the pipeline has no capacity left, so the per-task check stops when nothing more can be started. It comes with a regression test that fails without the change.

serxa and others added 2 commits November 3, 2026 19:10
The test is taken from #129072 by Melvyn Peignon.

Co-Authored-By: Melvyn Peignon <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@serxa

serxa commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

#129072 fixes the same problem, found from a production case (an object storage scan that kept 3 threads after the other queries released their slots). Its integration test is now part of this PR, in b35bff9: it passes here and fails on master (the query finishes 2.6 s after the release, the limit is 1.5 s).

The signature of `init` differs on release branches, so this keeps backports free of conflicts in `ExecutorTasks`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@serxa
serxa marked this pull request as ready for review November 3, 2026 19:49
serxa and others added 2 commits November 3, 2026 20:07
Wait for the released slots to be acquired instead of checking how long the query takes,
bound every wait, and kill both queries on any failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`ExecutorTasks::downscale` keeps its meaning; `executeStepImpl` requests the thread where it stops.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@serxa serxa added the pr-performance Pull request with some performance improvements label Nov 3, 2026
A workload limits the query to fewer CPU slots than threads, so every thread checks for
granted slots after each step.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... and removed comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Nov 3, 2026
@serxa

serxa commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

A known limitation, unchanged from master: the setMax target of a query never shrinks. Requested threads are dropped when the pipeline width is reached, or when a thread goes idle with no queued or asynchronous task left. If more CPU slots are granted after that (up to the target the query already asked for), no thread is started for them until the pipeline pushes new tasks to the shared queue; then threads are started for all of them at once. Giving such slots back would need the slot allocations to support reclaiming granted slots, which setMax does not do now.

@serxa

serxa commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

@groeneai, please investigate the failure and provide a fix in a separate PR, or link an existing one if already in progress.

Performance Comparison (amd_release, master_head, 4/6): server crashed during the test run (Connection refused (localhost:9001) after a brief window — not caused by this PR's changes). Job log

@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

The server did not crash. Each Connection refused (localhost:9001) line comes from wait_for_server polling right after confirm_changes restarted both servers on purpose (pkill -f clickhouse-serv, 6 restarts); every time select 1 answered within 2 s, and run-errors.tsv is empty.

The check failed on window_functions_sliding_frames query #9 (groupArray(v) OVER (PARTITION BY p ORDER BY i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)): +23.8%, confirmed_regression, reproduced at all 3 stack placements. It is a code-layout effect, not the logic of this PR:

  • With the two CI binaries of this run (PR merge 2411a4ca = 016fb177 into 6e1a5e4e, and the 6e1a5e4e reference), on 8 pinned cores of a Xeon 6975P, 6 alternating rounds of 10 runs with a restart each: median 1.206 s vs 1.031 s (+17%). With max_threads = 1, so the executor never starts a second thread: 3.10 s vs 2.61 s (+19%).
  • The extra CPU is all inside WindowTransform::work: Frame::advance gets 3075 vs 1733 profiler samples. ExecutorTasks and PipelineExecutor do not show up in either profile.
  • WindowTransform::work and Frame::advance have the same instructions in both binaries (they differ only in addresses). They come after the executor code in .text, so the code this PR adds there moves them by 960 bytes, while their hot callees (memcpy, scatter, countColumnsSizeInSelector) stay at the same addresses.
  • On master this query moves by +21% to +34% between a commit and its parent on amd (dashboard tier suspect, 4 runs from 2026-11-02 to 2026-11-04). The arm shard of this run passed.

This is the class from #124574, which was closed as not planned today, so there is no fix PR to link and nothing to fix in the C++ code. cc @egor-click: the placement reruns change only the stack/TLS placement, so a code-placement artifact passes them, and the tier stayed confirmed_regression although the master history of this query has these swings.

@serxa
serxa added this pull request to the merge queue Nov 5, 2026
Merged via the queue into master with commit 3822703 Nov 5, 2026
366 of 367 checks passed
@serxa
serxa deleted the pipeline-executor-spawn-without-mutex branch November 5, 2026 15:34
@robot-clickhouse robot-clickhouse added pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo labels Nov 5, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 5, 2026
Backport #125901 to 26.10: Start executor threads for CPU slots granted after the request
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 8, 2026
Cherry pick #125901 to 26.8: Start executor threads for CPU slots granted after the request
robot-clickhouse added a commit that referenced this pull request Nov 8, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 8, 2026
Cherry pick #125901 to 26.9: Start executor threads for CPU slots granted after the request
robot-clickhouse added a commit that referenced this pull request Nov 8, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 8, 2026
Backport #125901 to 26.8: Start executor threads for CPU slots granted after the request
clickhouse-gh Bot added a commit that referenced this pull request Nov 8, 2026
Backport #125901 to 26.9: Start executor threads for CPU slots granted after the request
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 9, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport v26.7-must-backport v26.8-must-backport v26.9-must-backport v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants