Skip to content

QueryRunner follow-up: do not occupy threads eagerly - #114522

Merged
mstetsyuk merged 8 commits into
masterfrom
query-runner-demand-driven-workers
Aug 18, 2026
Merged

mstetsyuk merged 8 commits into
masterfrom
query-runner-demand-driven-workers

Conversation

@mstetsyuk

@mstetsyuk mstetsyuk commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

QueryRunner tables now start worker threads on demand and release them once idle, instead of occupying threads for the table's whole lifetime.


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

Version info

  • Merged into: 26.8.1.1606 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [43b3246]

Summary: ✅


AI Review

Summary

This PR switches QueryRunner from eagerly started worker threads plus a separate bounded queue to on-demand ThreadPool workers that retire when idle, and adds ThreadPool::finish() so shutdown can stop admitting new work. The direction is reasonable, but the current head still has one live shutdown-admission race and one dismissed silent-drop regression, so I do not think it is ready to merge yet.

Findings

❌ Blockers

  • [src/Storages/StorageQueryRunner.cpp:343-367] shutdown() still does not close the admission path synchronously. shutdown_called flips before pool.finish(), but submit() still goes straight into pool.trySchedule(...) and only looks at shutdown_called after scheduling fails. A producer already inside QueryRunnerSink::consume can therefore enqueue extra no-op jobs in that window, so DETACH TABLE / server shutdown is still coupled to the producer feeding rows instead of discarding the tail immediately. Suggested fix: reject new submissions before calling into the pool once shutdown_called is set, or fold the shutdown flag into the same critical section that closes the pool.

  • [src/Storages/StorageQueryRunner.cpp:343-351, src/Common/ThreadPool.cpp:438-448, src/Common/ThreadPool.cpp:462-477] [dismissed by author -- https://github.com/QueryRunner follow-up: do not occupy threads eagerly #114522#discussion_r3768585332] QueryRunner now treats every trySchedule() failure as backpressure and just logs/discards the row, but ThreadPool::scheduleImpl<bool> also returns false when worker creation fails or the thread-allocation fault injector fires. Before this PR those cases surfaced as exceptions during startup(); now the INSERT still succeeds while some queries are silently lost. Suggested fix: keep a throwing path for worker-start failures and reserve the drop-on-false behavior for real queue-saturation / finished-pool cases.

Tests
  • ⚠️ ThreadPool::finish() still has no focused regression coverage for the blocked-scheduler path. Nothing under src/Common/tests exercises a bounded pool where one thread is waiting inside scheduleOrThrow / trySchedule, another thread calls finish(), and the waiter returns promptly instead of hanging. A small gtest for that scenario would lock down the two shutdown fixes added in this PR (job_finished.notify_all() and poisoning remaining_pool_capacity).
Final Verdict

Changes requested.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.80% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.20% +0.00%

Changed lines: Changed C/C++ lines covered: 93/99 (93.94%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 12, 2026
@mstetsyuk
mstetsyuk requested a review from azat August 12, 2026 16:44
Comment thread src/Storages/StorageQueryRunner.cpp Resolved
while (queue.tryPop(job))
finishJob(job.batch, job.seq);

cluster_executors.cancelAll();

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.

Removing queue.finish() here means shutdown no longer closes the admission path. A QueryRunnerSink that is already inside consume can keep calling submit after shutdown_called flips, and submit will still enqueue some of those rows into the pool until it fills. They only become no-ops later in executeJob, so pool.wait() now waits for the producer to stop feeding rows instead of discarding the tail of the batch immediately.

With a large INSERT ... SELECT, DETACH TABLE / server shutdown can therefore stay blocked arbitrarily long on post-shutdown submissions. Please reject/drop new submissions synchronously once shutdown starts, equivalent to the old finished-queue behavior.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

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 is still present in the current code. submit still unconditionally enters pool.trySchedule(...) at src/Storages/StorageQueryRunner.cpp:345, and shutdown() only closes the pool at src/Storages/StorageQueryRunner.cpp:363-366. A producer already inside QueryRunnerSink::consume can therefore keep enqueueing rows in that window after shutdown_called flips, so the admission path is not rejected synchronously yet.

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 is still present in the current head. shutdown() only flips shutdown_called at src/Storages/StorageQueryRunner.cpp:363 and closes the pool at line 366, while submit() still enters pool.trySchedule(...) unconditionally at src/Storages/StorageQueryRunner.cpp:343-346 and checks shutdown_called only after scheduling fails. A producer already inside QueryRunnerSink::consume can still enqueue extra no-op jobs in that window, so post-shutdown submissions are not rejected synchronously yet.

@clickhouse-gh

clickhouse-gh Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 43b32465b with master 08fb6b41e (stripped binary size, per-symbol sizes and ThinLTO time; 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 701.29 MiB 698.26 MiB -3.03 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

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

Job report

@azat azat self-assigned this Aug 13, 2026

@azat azat left a comment

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.

Yes, much better!

Comment thread src/Common/ThreadPool.h Resolved
Comment thread src/Storages/StorageQueryRunner.cpp Resolved
Comment thread src/Storages/StorageQueryRunner.cpp Resolved
Comment thread src/Common/ThreadPool.h Resolved
Comment thread src/Common/ThreadPool.cpp Resolved
@groeneai groeneai mentioned this pull request Aug 14, 2026
1 task done
@mstetsyuk
mstetsyuk requested a review from azat August 14, 2026 10:02
Comment thread src/Common/ThreadPool.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

Absolutely everything failed in the CI. Please do not merge without fixing it.

@azat

azat commented Aug 15, 2026

Copy link
Copy Markdown
Member

Suddenly #109891 hits CI more often, I will take a look

@mstetsyuk

Copy link
Copy Markdown
Member Author

Segfaults are fixed in #114940 - I think we can wait until it's merged.

Comment thread src/Common/ThreadPool.cpp
}

template <typename Thread>
void ThreadPoolImpl<Thread>::finishNoLock(const std::lock_guard<std::mutex> &)

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 new ThreadPool::finish() path now carries a shutdown correctness contract, but there is still no focused regression test for it. The exact race here already needed two follow-up fixes in this PR (job_finished.notify_all() and poisoning remaining_pool_capacity), and nothing under src/Common/tests currently exercises a scheduler blocked on a full bounded pool while another thread calls finish().

Without that coverage it is easy to regress back to scheduleOrThrow hanging after finish() or to spawning a stray worker after shutdown. Please add a small gtest that fills a bounded pool, blocks a second scheduler in scheduleOrThrow or trySchedule, calls finish(), and asserts the waiter returns promptly with CANNOT_SCHEDULE_TASK or false and wait() drains without hanging.

@mstetsyuk
mstetsyuk added this pull request to the merge queue Aug 18, 2026
Merged via the queue into master with commit 2bb9dae Aug 18, 2026
181 checks passed
@mstetsyuk
mstetsyuk deleted the query-runner-demand-driven-workers branch August 18, 2026 08:32
@clickgapai

Copy link
Copy Markdown
Contributor

@mstetsyuk @azat — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

AST fuzzer: fuzzed QueryRunner tables with threads = 1024 exhaust the global thread pool (CANNOT_SCHEDULE_TASK storm, job killed as Received signal 15)

5 participants