Repository navigation
Fix query not upscaling to CPU slots released by other queries (granted slots stayed unused) - #129072
melvynator wants to merge 1 commit into
Conversation
…ries Since ClickHouse#79570 the executor only calls spawnThreads() (the sole consumer of CPUSlotsAllocation::tryAcquire) when a step pushed new tasks into the global queue. Pipelines made of per-stream ISimpleTransform chains (e.g. object storage scans) yield exactly one Ready task per step, which is executed as a local task, so pushTasks() reports zero demand on every step and tryAcquire() is never retried. A query that started while CPU slots were exhausted therefore stays on the threads it got at start even after other queries release their slots; the slots granted to its allocation stay unused and are unavailable to every other query until it finishes. Keep a lock-free hint in ExecutorTasks of how many queued tasks could run on additional threads and, on steps that pushed nothing, retry spawnThreads() for that many threads so granted slots are picked up immediately. Adds an integration test that fails without the fix.
|
Workflow [PR], commit [d3c63af] Summary: ❌
AI ReviewSummaryPR #129072 retries CPU slot acquisition when queued work remains, allowing queries to use slots released by other queries. Changes are required because the retry also repeatedly increases allocation demand for the same work, and the new regression test can stall until its timeout. Findings
Tests
Final Verdict |
| /// CPU slots granted to this allocation after other queries released theirs are only consumed by | ||
| /// `spawnThreads()`, so without this retry they would stay granted-but-unused until the query ends. | ||
| if (pool && spawn_count == 0) | ||
| spawn_count = tasks.getUncoveredHint(); |
There was a problem hiding this comment.
[P1] Retry acquisition without adding the same queued work to demand repeatedly
getUncoveredHint measures outstanding work, but assigning it to spawn_count feeds it into the additive pending_demand / desired_threads calculation below. For example, with an eight-thread pipeline ceiling, two initially ready chains, one acquired slot, and the other chain queued, the hint stays at one while slots are exhausted. Six local processor steps then grow desired_threads from two to eight without any new parallel work. When slots become available, setMax has already requested all eight: spawnThreads can start idle workers, and any unacquired grants still count against other queries. This defeats lazy allocation for narrow stages of wider pipelines and recreates slot hoarding.
Keep the acquisition retry separate from additive demand, or derive an idempotent allocation target that accounts for slots already requested. Please cover a narrow stage with a wider pipeline ceiling under slot contention; the added test starts with demand already at its full ceiling, so it cannot expose this inflation.
| == "1" | ||
| ) | ||
|
|
||
| while not foreground_started(): |
There was a problem hiding this comment.
[P2] Bound the foreground-start wait and clean up on failure
This loop requires observing a transient entry in system.processes. If the foreground query completes before the polling thread observes it, or raises before registration, every subsequent check returns zero and the loop cannot terminate. The background query is killed only after the loop, so this consumes the integration test's 900-second timeout while leaving the CPU load running. Executing the actual test function with foreground completion forced before its first poll confirms that polling continues and no cleanup runs.
Use a bounded wait that also detects foreground completion/errors, and put cancellation and thread cleanup in finally so a missed observation fails promptly.
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]>
|
#125901 fixes the same problem. The difference is in how the missing demand is tracked. Here the number of uncovered queued tasks is added to |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units10 translation units recompiled, 20 s compile time in total, 10 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.93 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
#125901 covers the issue |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a query that started while CPU slots (
concurrent_threads_soft_limit_*) were exhausted never using the slots freed later by other queries: it stayed on the few threads it got at start, and the slots granted to it stayed unused and blocked other queries until it finished. Regression since 25.6 (#79570).Documentation entry for user-facing changes
No user-facing changes other than the bug fix.
The problem
Reproduced on 26.7.2.59, 26.3 LTS and current master; 25.3 LTS behaves correctly.
concurrent_threads_soft_limit_ratio_to_cores=2).ConcurrencyControlgrants them to Q3's allocation (ConcurrencyControlSlotsGrantedgoes up,cur_concurrencyis charged), but Q3 keeps running on 3 threads until it ends.Timings from the repro (c6a.4xlarge, 16 vCPU, 100 Parquet files behind a slow fake S3 with 250 ms/request):
Root cause
CPUSlotsAllocation::tryAcquire()is only called fromPipelineExecutor::spawnThreads(), and since #79570 ("Optimize thread spawning in PipelineExecutor", 25.6)spawnThreads()is only called whenExecutorTasks::pushTasks()reports a positive demand, i.e. when the step that just finished pushed new tasks into the global queue. #102928 (lazy allocation, 26.7) kept that gate.Per-stream
ISimpleTransformchains (which is what an object storage scan is: source → transforms → resize) produce exactly oneReadytask per step, and that task is executed as a local task by the same thread.pushTasks()therefore reports a demand of 0 on every step,spawnThreads()is never called again, and slots that were granted to the allocation after the query started are never acquired. The other source tasks sit in the globaltask_queuewith nobody to run them.The only thing that eventually breaks this is the
max_scheduled_local_tasks = 128valve, which forces a step through the global queue every 128 local steps; with I/O-bound sources that takes seconds, or longer than the query itself.The fix
ExecutorTaskskeeps a lock-free hint (uncovered_hint) ofmin(queued tasks − idle threads, num_threads − total_slots), updated wherever the queues /threads_queue/total_slotschange (already undermutex). InexecuteStepImpl, when the step pushed nothing itself (spawn_count == 0), the executor uses that hint as the demand, sospawnThreads()→tryAcquire()is retried on the next step and granted slots are picked up immediately.The hint is zero in the steady state (all queued tasks covered by threads, or no headroom), so the hot path stays a relaxed atomic load; no behaviour change when slots are not constrained.
Test
test_concurrent_threads_soft_limit/test.py::test_concurrent_threads_soft_limit_upscale_after_release: a background query takes all slots on a node withconcurrent_threads_soft_limit_num=10, a sleep-bound 8-stream query starts (gets 1 thread), the background query is killed, and the test checks the victim finishes within 1.5 s of the release (fixed: ~0.5 s; unpatched: ~2.6 s because the 128-step valve only fires at ~3.2 s).Verified with the integration runner: fails on unpatched master, passes (3/3) with the fix, the rest of the file still passes.
Workflow [PR]
Sync PR [sync-upstream/pr/129072]