Repository navigation
Choose the number of streams of a query from the fair share of CPU slots - #125085
Conversation
The number of streams (and threads) a query is planned with was always the number of CPU cores. The concurrency control limits how many threads run, but not how many streams the query is planned with. With many concurrent queries, every query still built a pipeline for all cores: one partial aggregation state per stream, merging all of them, more memory, and oversubscription of the threads that it was granted. This is why the concurrent QPS of ClickBench is worse than of systems that share one pool of workers between queries. Now, when `max_threads` is `auto`, it is lowered to the fair share of CPU slots of `ConcurrencyControl`: the slot limit divided evenly between the live allocations and the new query. A query on an idle server uses all cores as before. It can be disabled with the new setting `use_concurrency_control_for_max_threads`. The helper `getEffectiveMaxThreads` replaces the direct calls to `getMaxThreadsForAvailableMemory` for `max_threads`, so the limit applies everywhere the planner and the pipeline builder pick the parallelism. Related: https://benchmark.clickhouse.com/#system=-&type=-&machine=-6t|ca2|6ax|g4e|6ale|3al&cluster_size=-&opensource=-&hardware=+c&tuned=+n&storage=-&metric=qps&queries=- Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Planner/Planner.cpp
|
Workflow [PR], commit [a185638] Summary: ✅
AI ReviewSummaryThis PR lowers auto Findings
Tests
Final Verdict
|
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
…share of CPU slots The test checks the number of sinks equal to `max_insert_threads`, which is capped by `max_threads`. With `max_threads` being `auto`, it is now lowered to the fair share of CPU slots, which depends on the concurrently running tests (4 instead of 8 in the Fast test). Disable `use_concurrency_control_for_max_threads` in the test. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125085&sha=767bc99f807d398d54b3562195dc939b730039be&name_0=PR&name_1=Fast%20test #125085 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes16 object files changed (+29.14 KiB total), 1 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units20 translation units recompiled, 149 s compile time in total, 19 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
Since ClickHouse#125085 an auto `max_threads` is lowered to the fair share of CPU slots (`getEffectiveMaxThreads`). Under the test runner's concurrent load the two `--max_threads=0` EXPLAIN PIPELINE checks of `algorithm: Thread` can get a single stream; `ReadFromMergeTree` then reads in order (`readInOrder` instead of `readFromPool`) and the line disappears. The failing arm_binary job's server log shows `fair share of CPU slots 1 (32 requested)` for exactly the query that lost its line. Pin `use_concurrency_control_for_max_threads = 0` on those two invocations, as ClickHouse#125715 does for `02149_read_in_order_fixed_prefix`. The other invocations do not depend on the number of streams. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since ClickHouse#125085, `max_threads = auto` is lowered to the fair share of CPU slots of `ConcurrencyControl` when other queries are running. Under the concurrent load of the test runner the read in order can be planned with a single stream, so both parts are read by one `readInOrder` call and `EXPLAIN PIPELINE` shows `ReverseTransform × 2` instead of two separate chains. Disable `use_concurrency_control_for_max_threads` in the test, as its output depends on the number of streams. Failed in the merge queue of ClickHouse#118315: https://github.com/ClickHouse/ClickHouse/actions/runs/38491344800/job/115617405459 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…trol The two `all > 1` checks need the `LIMIT 1` read to log more than one non-final file segment of `b.bin`. With the prefetched pool disabled (as in CI), each stream of `MergeTreeReadPool` starts at a different mark range, so the number of logged segments is bounded by the streams and threads that start reading before the limit closes the query. Since ClickHouse#125085 an auto `max_threads` is lowered to the fair share of CPU slots. In the failing job (PR ClickHouse#109455, Stateless tests (arm_asan_ubsan, azure, sequential, 1/2)) background pipelines held the slots and the read got `Lower number of threads for query to the fair share of CPU slots 1 (8 requested)` and `Reading approx. 1000000 rows with 1 streams`, so it read one granule, one segment, and printed FAIL twice. Disable `use_concurrency_control` for the read: `getEffectiveMaxThreads` then keeps the auto value and the executor spawns threads without slots. Validated with the CI test config and a MinIO-backed `s3_disk`: with `concurrent_threads_soft_limit_num = 1` the unfixed test fails 5/5 with CI's diff and log lines, the fixed one passes 20/20 there (7-8 streams) and 50/50 on the default config. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The concurrent QPS of ClickHouse in ClickBench is worse than Firebolt's, even on machines where single-query times are the same. On
c8g.metal-48xl, the sum of hot query times is 2.64 s vs 2.66 s, but QPS is 32.5 vs 42.3. Onc7a.metal-48xlit is 28.5 vs 33.5, and onc6a.metal15.8 vs 24.1.Related: https://benchmark.clickhouse.com/#system=-&type=-&machine=-6t|ca2|6ax|g4e|6ale|3al&cluster_size=-&opensource=-&hardware=+c&tuned=+n&storage=-&metric=qps&queries=-
The reason: a query is always planned with one stream per CPU core. The concurrency control limits how many threads run, but not how many streams the query is planned with. With 10 concurrent queries, every query still builds a pipeline for all cores. That means one partial aggregation state per stream, the cost of merging all of them, more memory, and oversubscription. With the ClickBench queries run sequentially,
max_threads = 96costs 1.58x the CPU time ofmax_threads = 8for the same work. LargeGROUP BYqueries cost 1.3-1.6x more, andORDER BY ... LIMITqueries 3-6x more.This PR extends the concurrency control to the choice of the number of streams. When
max_threadsisauto, it is lowered to the fair share of CPU slots ofConcurrencyControl: the slot limit divided evenly between the live allocations and the new query (at least one). A query on an idle server is planned with all cores, as before. An explicitly setmax_threadsis respected. The new settinguse_concurrency_control_for_max_threads(enabled by default) disables it.ConcurrencyControlnow counts its live allocations and has agetFairSharemethod. The new helpergetEffectiveMaxThreads(settings)combines the existing memory-based limit (max_threads_min_free_memory_per_thread) with the fair share. It replaces the direct calls togetMaxThreadsForAvailableMemoryformax_threads, in the planner, the old interpreters,BuildQueryPipelineSettingsandIStorage.Local check on a 96-core Neoverse-V2 machine, 100M-row
hits, with 10 clients cycling through the ClickBench queries as the ClickBench QPS test does. The machine was shared and heavily loaded, so throughput is too noisy to quote. Per-query metrics fromsystem.query_log:This needs confirmation by the ClickBench QPS test on dedicated machines.
The fair share depends on the other queries running on the server, so a functional test cannot assert stream counts deterministically under parallel CI. The counting is covered by a gtest (
ConcurrencyControl.FairShare).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
When
max_threadsis not set explicitly, a query is planned with the fair share of CPU slots of the concurrency control instead of all CPU cores. This increases the throughput and lowers the memory usage under many concurrent queries, while a query on an idle server still uses all cores. It can be disabled with the settinguse_concurrency_control_for_max_threads.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125085]
Version info
26.10.1.2340-master(included in26.10and later)