Skip to content

Wait for distributed plan stages without holding an execution thread - #116017

Merged
davenger merged 2 commits into
masterfrom
fix-distributed-plan-driver-starvation
Aug 26, 2026
Merged

davenger merged 2 commits into
masterfrom
fix-distributed-plan-driver-starvation

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

A distributed plan could make no progress at all until max_execution_time fired, whenever its initiator pipeline ended up with a single execution thread.

ReadFromDistributedPlanSource dispatches the plan's stages and waits for them; 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, 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_stop flaky on master (the test times out because the query never starts, not because the LIMIT early 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%29

In 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 of 04891_distributed_plan_limit_early_stop with its 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

With max_threads = 1 the same setup gives 19.5-25.6 s before and 4.3-5.6 s after. All 45 distributed_plan stateless tests were run locally against the patched build; the only ones that did not pass are the s3 test 04824_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_stop already covers this — it is the test the bug was breaking, and it only passes reliably with the fix.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...


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

`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
@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b070170]

Summary: ✅


AI Review

Summary

This PR changes ReadFromDistributedPlanSource to park the initiator in the async queue while distributed-plan stages run, using WakeupFd notifications to wake promptly on Linux/macOS. The Linux/macOS path looks coherent, but the fix is still incomplete on other supported Unix builds and there is still no deterministic regression test for the one-slot initiator path that motivated the change.

PR Metadata
  • Changelog category should not be CI Fix or Improvement: this patch changes query-execution behavior in make_distributed_plan, not just CI plumbing.
  • Suggested Changelog category: Improvement
  • A real Changelog entry is required for Improvement, and the current ... is not sufficient.
  • Suggested Changelog entry: Improves \make_distributed_plan` queries by yielding the initiator's execution thread while distributed-plan stages are waiting, so single-slot queries keep making progress instead of stalling until `max_execution_time`.`
Findings

⚠️ Majors

  • [src/Processors/Sources/ReadFromDistributedPlanSource.cpp:50] The non-blocking wait is still compiled only for OS_LINUX/OS_DARWIN. On other supported Unix builds tryGenerate still calls execute(100) from work, so a single execution-thread initiator can still keep re-running the driver instead of the second result source in distributed_plan_execute_locally. Either add the same async wait path there too or explicitly reject that execution mode on platforms without async executor support.
Tests
  • ⚠️ [src/Server/DistributedQuery/tests/gtest_distributed_query.cpp:565] The only changed test just threads the new stage_wakeup argument through the helper and still drives the executor directly with execute(100), so it never exercises the new ReadFromDistributedPlanSource async path. 04891_distributed_plan_limit_early_stop remains non-deterministic because it only fails when server-level concurrency control squeezes the initiator down to one slot. Please add a focused reproducer that forces a single-slot initiator and proves the query still makes progress through the source path.
Final Verdict

Not 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

Metric Baseline Current Δ
Lines 87.30% 87.30% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 82/85 (96.47%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 23, 2026
}

if (distributed_query_executor->execute())
#if defined(OS_LINUX) || defined(OS_DARWIN)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b0701704a with master 4ad204f1c (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 66b113278; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 710.23 MiB 707.19 MiB -3.04 MiB (-0.43%)

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 units

16 translation units recompiled, 62 s compile time in total, 16 of them have a recent master baseline.

Job report

#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)

@davenger davenger Aug 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@davenger
davenger added this pull request to the merge queue Aug 26, 2026
Merged via the queue into master with commit e8a7701 Aug 26, 2026
353 of 354 checks passed
@davenger
davenger deleted the fix-distributed-plan-driver-starvation branch August 26, 2026 14:36
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants