Skip to content

Limit the number of parallel tasks of the same type in BackgroundSchedulePool - #84008

Merged
tavplubix merged 2 commits into
masterfrom
less_unfair_background_schedule
Aug 25, 2025
Merged

tavplubix merged 2 commits into
masterfrom
less_unfair_background_schedule

Conversation

@tavplubix

@tavplubix tavplubix commented Jul 18, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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 by background_schedule_pool_max_parallel_tasks_per_type_ratio server setting

Probably fixes #81500

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Jul 18, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5f0bcd9]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) failure
Server died FAIL
Hung check failed, possible deadlock found (see hung_check.log) FAIL
Killed by signal (in clickhouse-server.log) FAIL
Fatal message in clickhouse-server.log (see fatal_messages.txt) FAIL
Killed by signal (output files) FAIL
Found signal in gdb.log FAIL

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 18, 2025
@serxa serxa self-assigned this Jul 22, 2025
if (runnable_task_types.empty())
continue;

task_type_to_run = runnable_task_types[thread_local_rng() % runnable_task_types.size()];

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.

A round-robin over groups might be a bit more fair and faster because we are under lock here anyway

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.

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
tavplubix force-pushed the less_unfair_background_schedule branch from 6234f1a to 5ed6b1a Compare July 23, 2025 17:49
@tavplubix tavplubix changed the title Try to make BackgroundSchedulePool less unfair Limit the number of parallel tasks of the same type in BackgroundSchedulePool Jul 23, 2025
@tavplubix
tavplubix marked this pull request as ready for review July 23, 2025 19:52
@tavplubix
tavplubix enabled auto-merge August 22, 2025 13:57
@tavplubix
tavplubix added this pull request to the merge queue Aug 25, 2025
Merged via the queue into master with commit 86b9c82 Aug 25, 2025
@tavplubix
tavplubix deleted the less_unfair_background_schedule branch August 25, 2025 16:19
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 25, 2025
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

If I specify a low size of the global thread pool in the server configuration, it hangs

3 participants