Repository navigation
Limit the number of parallel tasks of the same type in BackgroundSchedulePool - #84008
Merged
Merged
Conversation
Contributor
|
Workflow [PR], commit [5f0bcd9] Summary: ❌
|
serxa
reviewed
Jul 22, 2025
| if (runnable_task_types.empty()) | ||
| continue; | ||
|
|
||
| task_type_to_run = runnable_task_types[thread_local_rng() % runnable_task_types.size()]; |
Member
There was a problem hiding this comment.
A round-robin over groups might be a bit more fair and faster because we are under lock here anyway
Member
Author
There was a problem hiding this comment.
I thought about simple round-robin initially, but I'm not sure how many different types of tasks we have, and it would need to iterate through all groups until it finds something that it can execute, so I implemented O(1) task selection. And I'm not sure how to impelement round-robin over runnable_task_types because we move elements there
tavplubix
force-pushed
the
less_unfair_background_schedule
branch
from
July 23, 2025 17:49
6234f1a to
5ed6b1a
Compare
BackgroundSchedulePool less unfairBackgroundSchedulePool
tavplubix
marked this pull request as ready for review
July 23, 2025 19:52
serxa
approved these changes
Jul 29, 2025
tavplubix
enabled auto-merge
August 22, 2025 13:57
alexey-milovidov
pushed a commit
to ainthero/ClickHouse
that referenced
this pull request
Nov 8, 2026
`BackgroundSchedulePool` lets at most `max_parallel_tasks_per_type` tasks of one callable type run at once (ClickHouse#84008). The limit was fixed at construction: 0 meant the startup pool size, and the main schedule pool got `startup_size * background_schedule_pool_max_parallel_tasks_per_type_ratio` from `Context`. `increaseThreadsCount`, which `SYSTEM RELOAD CONFIG` calls for the five resizable pools, raised the thread cap but never the per-type limit. Every consumer of every Kafka table runs the same closure, so they form one group: once that group hit the old limit it was dropped from `runnable_task_types`, the eager spawn counted no demand for it, and the extra threads were never used until a restart (ClickHouse#128878). The pool now stores the ratio (0 = no limit beyond the pool size) and derives the limit from the current size with the formula `Context` used. On a resize the limit becomes the larger of the current and the recomputed value: a truncated product maps to the pool size, so the recomputed value can be lower (ratio 0.4: size 2 -> 2, size 3 -> 1), and lowering it while a group already runs more tasks would break the `num_running < max_parallel_tasks_per_type` invariant. When the limit grows, groups paused at the old limit are put back into `runnable_task_types` and parked workers are woken, so idle workers and the existing eager spawn both see that demand. The startup limit of every pool is unchanged. Closes ClickHouse#128878 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Limit the number of tasks of each type in
BackgroundSchedulePool. Avoid situations when all slots are occupied by task of one type, while other tasks are starving. Also avoids deadlocks when tasks wait for each other. This is controlled bybackground_schedule_pool_max_parallel_tasks_per_type_ratioserver settingProbably fixes #81500
Documentation entry for user-facing changes