Repository navigation
Wait for distributed plan stages without holding an execution thread - #116017
Conversation
`ReadFromDistributedPlanSource` dispatches the stages of a distributed
plan and waits for them to finish; the query result arrives through a
second source of the same pipeline, which reads the `final_result`
exchange. With a streaming exchange nothing runs until that second
source connects, because `StreamingExchangeSink` has no socket until its
consumer shows up and therefore never asks its input for data: every
worker task builds its pipeline and then sits idle.
The driver used to wait inside `work`, which keeps an execution thread
busy and leaves the source immediately runnable again. A pipeline that
has a single execution thread then keeps re-running the driver and never
reaches the result source, so the whole query makes no progress until
`max_execution_time` fires. That is not a corner case: on a loaded
server concurrency control hands a query only its guaranteed master
slot, which is exactly one execution thread.
Park the source in the executor's async queue between polls instead, so
the thread is free while the stages run.
Reproduced with a single-CPU-slot server and a background query holding
the slot, using the query of
`04891_distributed_plan_limit_early_stop`, which is the test this made
flaky (with the query's own `max_threads = 2`, 8 runs each):
before: 17.01 15.97 16.97 16.97 16.97 17.97 17.10 16.97
after: 4.28 4.26 4.26 4.26 5.26 4.26 4.27 4.26
CI report of the flaky run:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=30024ffcc4bbaa402c58248515477da98cd18621&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29
|
Workflow [PR], commit [b070170] Summary: ✅
AI ReviewSummaryThis PR changes PR Metadata
Findings
Tests
Final VerdictNot ready. The Linux/macOS wakeup path is an improvement, but the fix is still partial across supported platforms and it still lacks deterministic automated proof for the one-slot execution path it claims to fix. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 82/85 (96.47%) · Uncovered code |
| } | ||
|
|
||
| if (distributed_query_executor->execute()) | ||
| #if defined(OS_LINUX) || defined(OS_DARWIN) |
There was a problem hiding this comment.
On non-OS_LINUX/OS_DARWIN builds this still falls back to waitForStage(100) inside work, so the original starvation remains for distributed_plan_execute_locally: the initiator can still spend its only execution thread inside ReadFromDistributedPlanSource while the second source that reads final_result never gets scheduled. FreeBSD is a supported build target here, so the fix is only partial unless we either add a non-blocking wait path there too or explicitly reject this execution mode on platforms without async executor support.
| { | ||
| executor->start(); | ||
| while (!executor->execute()); | ||
| while (!executor->execute(/*poll_timeout_ms=*/ 100)); |
There was a problem hiding this comment.
This only adapts the helper to the new execute signature; it still does not prove the one-slot pipeline path that motivated the fix. 04891_distributed_plan_limit_early_stop runs with max_threads = 2 and only flakes when server-level concurrency control happens to squeeze the initiator down to one slot, so CI can regress back to the old starvation without reliably noticing. Please add a focused reproducer that forces a single-slot initiator (or a unit/integration equivalent) and asserts the query still makes progress.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units16 translation units recompiled, 62 s compile time in total, 16 of them have a recent master baseline. |
…poll interval Co-Authored-By: Claude Fable 5 <[email protected]>
| #if defined(OS_LINUX) || defined(OS_DARWIN) | ||
| /// `Ready` means `work` would be called next, but the stages are still running and there is | ||
| /// nothing for it to do until the poll interval elapses. Give the execution thread back. | ||
| if (status == Status::Ready && waiting_for_stages) |
There was a problem hiding this comment.
AFAIU this will result in increased latency: if all tasks finish in 10ms we will still wait for the stage_poll_timer timer for stage_poll_interval_us (100ms) until we detect this.
I'll replace this timer with event.
A distributed plan could make no progress at all until
max_execution_timefired, whenever its initiator pipeline ended up with a single execution thread.ReadFromDistributedPlanSourcedispatches the plan's stages and waits for them; the query result arrives through a second source of the same pipeline, which reads thefinal_resultexchange. With a streaming exchange nothing runs until that second source connects, becauseStreamingExchangeSinkhas no socket until its consumer shows up and therefore never asks its input for data — every worker task builds its pipeline and then sits idle.The driver used to wait inside
work, which keeps an execution thread busy and leaves the source immediately runnable again, so a single-threaded pipeline kept re-running the driver and never reached the result source. This is not a corner case: on a loaded server concurrency control hands a query only its guaranteed master slot, i.e. exactly one execution thread. Now the source parks itself in the executor's async queue between polls, so the thread is free while the stages run.This is what made
04891_distributed_plan_limit_early_stopflaky on master (the test times out because the query never starts, not because theLIMITearly stop is broken). CI report of a failing run: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=30024ffcc4bbaa402c58248515477da98cd18621&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29In that run all 14 worker tasks built their pipelines and then did nothing for 25 s, no exchange source ever connected, and the initiator's only pipeline worker thread spent 25 of 25 profiler samples inside
ReadFromDistributedPlanSource::tryGenerate→waitForStage.Reproduced locally with a single-CPU-slot server (
concurrent_threads_soft_limit_num = 1) plus a background query holding the slot, running the query of04891_distributed_plan_limit_early_stopwith its ownmax_threads = 2, 8 runs each:With
max_threads = 1the same setup gives 19.5-25.6 s before and 4.3-5.6 s after. All 45distributed_planstateless tests were run locally against the patched build; the only ones that did not pass are thes3test04824_parquet_lazy_materialization_distributed_plan, which needs a fixture the ad-hoc local server does not have.No new test:
04891_distributed_plan_limit_early_stopalready covers this — it is the test the bug was breaking, and it only passes reliably with the fix.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Workflow [PR]
Sync PR [sync-upstream/pr/116017]