Repository navigation
Give back fully consumed CPU slots when the last running thread is preempted - #123640
Conversation
…eempted `CPULeaseAllocation::consume` finishes at most one request per report. When a report covers several quanta, the remaining requests are fully consumed but still hold their slots. If the last running thread is then preempted, nothing reports consumption until a grant resumes a thread, so these slots stay held while the query waits. When the grant needs one of them, the query waits for itself forever. This happens, for example, when only worker threads compete for slots (`CREATE RESOURCE cpu (WORKER THREAD)`): finishing the free master request returns no slot. Now, when no thread is left running after a preemption, all requests in consumption are finished. Preemption with no running thread implies `consumed_ns >= requested_ns`, so this returns only slots that were already used. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [3c69c08] Summary: ✅
AI ReviewSummaryThis PR fixes a real deadlock in Final Verdict
LLVM Coverage ReportMeasured on commit 3c69c08.
Changed lines: Changed C/C++ lines covered: 17/19 (89.47%) · Uncovered code |
`clang-diagnostic-missing-designated-field-initializers` rejects a partial designated initializer of `CPULeaseSettings`. Report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=123640&sha=9b1f26b4a8605fa78f977bc6e6c09816f6da4524&name_0=PR&name_1=Build%20%28arm_tidy%29 PR: #123640 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 Compile time of recompiled translation units8 translation units recompiled, 12 s compile time in total, 8 of them have a recent master baseline. |
Backport #123640 to 26.9: Give back fully consumed CPU slots when the last running thread is preempted
Backport #123640 to 26.8: Give back fully consumed CPU slots when the last running thread is preempted
Backport #123640 to 26.7: Give back fully consumed CPU slots when the last running thread is preempted
…s when the last running thread is preempted
…s when the last running thread is preempted
…s when the last running thread is preempted
`effectiveMaxSlots()` is `current_max_slots - parked_count`, and `current_max_slots` is the executor's desired thread count. When the workload grants a query fewer slots than it wants (`max_concurrent_threads` below `max_threads`), parking all of its threads leaves `allocated` equal to `effectiveMaxSlots()`, so `parkLease` keeps every slot. With no running thread nothing reports consumption or spawns a thread, so the slots stay idle. A `remote()` query whose shard runs on the same server under the same workload then hangs: the coordinator waits for the shard, and the shard's thread waits in `renew` for a slot the coordinator holds. Make `effectiveMaxSlots()` zero while no thread is running or preempted and at least one is parked, so the last park gives back every held slot. A worker that times out in preemption while the others are parked enters the same state with a request still pending; `grantImpl` now returns a grant that arrives then (the surplus loop of `parkLease` is moved to `finishSurplusRequests` and called from both). This is the parking counterpart of ClickHouse#123640. Tests: a stateless test with a one-slot workload and a loopback `remote()` GROUP BY (times out at 60 s without the change), and a unit test for the late grant after a preemption timeout. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With
cpu_slot_preemptionenabled,CPULeaseAllocation::consumefinishes at most one resource request per report. A report that covers several quanta leaves the other requests fully consumed while they still hold their slots. If the last running thread of the query is then preempted, no thread reports consumption until a grant resumes one, so the query keeps these slots while it waits. If the grant needs one of them, the query never resumes.One way to hit it: only worker threads compete for slots (
CREATE RESOURCE cpu (WORKER THREAD)), so the master request is free and finishing it returns no slot. With a workload'smax_concurrent_threadsslots held this way, the preempted master thread waits forever. It has no preemption timeout.The fix: when a preemption leaves no running thread,
renewfinishes all requests in consumption and enqueues the next one if needed. Preemption with no running thread impliesconsumed_ns >= requested_ns, so this returns only slots that were already used (checked by achassert). The one-request-per-report limit inconsumestill applies while threads are running, so the pipeline does not stall.The new unit test
SchedulerCPULeaseAllocation.FullPreemptionReturnsConsumedSlotsreproduces the deadlock with one query,max_concurrent_threads = 1and a free master slot. Without the fix the master thread never resumes.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a possible hang of a query under workload CPU scheduling with
cpu_slot_preemptionenabled: when all its threads were preempted, the query could keep CPU slots it had already used and wait forever for one of them.Workflow [PR]
Sync PR [sync-upstream/pr/123640]
Version info
26.10.1.1431-master(included in26.10and later)26.9.12.3,26.8.19.5,26.7.23.10