Skip to content

Raise the per-type task limit of a background schedule pool when its size is increased at runtime - #128899

Merged
nauu merged 7 commits into
ClickHouse:masterfrom
groeneai:fix-schedule-pool-per-type-limit-reload
Nov 8, 2026
Merged

nauu merged 7 commits into
ClickHouse:masterfrom
groeneai:fix-schedule-pool-per-type-limit-reload

Conversation

@groeneai

@groeneai groeneai commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

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

Fixed SYSTEM RELOAD CONFIG not taking full effect for an increased background_message_broker_schedule_pool_size or another background_*_schedule_pool_size setting: the streaming consumers of Kafka, RabbitMQ, NATS and FileLog tables kept running at most as many at once as the startup pool size allowed, until a restart. This was a regression in 25.9.

Description

Closes: #128878
Related: #84008

Since #84008 (25.9) a schedule pool runs at most max_parallel_tasks_per_type tasks of one callable type at once. That limit was computed once from the startup pool size (times background_schedule_pool_max_parallel_tasks_per_type_ratio for the main pool, the pool size for the others), and increaseThreadsCount, which SYSTEM RELOAD CONFIG calls for the five IncreaseOnly pool sizes, raised only the thread cap. All consumers of all Kafka tables share one closure type, so after a reload they stayed capped at the old size; a group already at the limit was not even counted as demand for new threads.

On 26.8.34.13 and 26.10.2.103, with background_schedule_pool_size 4 and the ratio 1, raising the size to 16 and running SYSTEM RELOAD CONFIG is reported by system.server_settings, but only 4 of 6 FileLog tables whose streaming tasks each hold a worker ingest until a restart; 25.3.14.14 ingests all 6 right after the reload.

The pool now keeps the ratio and derives the limit from its current size with the same formula as at startup, so startup limits do not change. On a resize the limit is only ever raised (a truncated product can come out lower while groups already run that many tasks); groups paused at the old limit become runnable again and parked workers are woken.

Validation: three new gtests in gtest_BackgroundSchedulePool.cpp. IncreaseThreadsCountRaisesSameTypeLimit fails on master (2 of 4 tasks run after increaseThreadsCount(4)); the other two use the new double parameter and do not compile on master. End to end (6 FileLog tables, pool 8 -> 16, ratio 0.5), all 6 ingest within 1 s of the reload with the fix, while master stays at 4. The integration test test_storage_kafka/test_message_broker_pool_resize.py (Kafka and Kafka2, pool 2 -> 8) runs 4 streaming tasks at once after the reload; master stays at 2.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1643-master (included in 26.11 and later)

groeneai and others added 4 commits November 3, 2026 05:22
`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]>
A group can already be in `runnable_task_types` when `increaseThreadsCount`
raises the limit: a worker that has just finished one of its tasks re-adds it
with `notify_one` and is not yet idle. The eager spawn then counts every parked
worker as a taker, so with the old conditional `notify_all` some of them stayed
asleep and the group ran below the new limit. Notify on every raise.

`IncreaseThreadsCountNeverLowersPerTypeLimit` now schedules a second wave after
the first one has finished and requires it to run two tasks at once, so a
lowered limit fails the test instead of being seen only by `chassert`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ype limit test

The task body counts a task as finished before the pool decrements the
group's running count after the function returns, so the second wave
could be scheduled while the first one was still accounted. A lowered
limit could then pass in a release build or abort on the chassert in a
debug build. Waiting until getTasks() is empty (running_tasks.erase and
the running-count decrement share one tasks_mutex section) makes the
assertion the only way a lowered limit can fail the test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten two comments in increaseThreadsCount to bare invariants.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 runs, 4 findings; the last run on the final head found none) plus my own review of the same tree.

# Sev Finding Verdict Evidence / action
1 ❌ A raise of the per-type limit can leave parked workers asleep when the group was already runnable (increaseThreadsCount notified only after re-adding a paused group) AGREE, fixed @ (final head) notify_all on every raise of the limit
2 ⚠️ IncreaseThreadsCountNeverLowersPerTypeLimit saw a lowered limit only through chassert AGREE, fixed @ (final head) a second wave scheduled after the resize must run 2 tasks at once; it is scheduled only once getTasks() is empty, because the pool retires a task after its function returns
3 ⚠️ The 1 s window in IncreaseThreadsCountKeepsPerTypeRatio is a sleep DISAGREE it is the window of a negative assertion and cannot fail correct code (the limit is 4, so no more than 4 can run at any moment); same pattern as ScheduleAfterHugeDelayDoesNotFire in this file; no API exposes scheduler quiescence
4 💡 No test catches a dropped notify_all noted, not blocking idle workers are not observable without a test hook; checked locally by a scratch variant that lets them park first (20/20 red without notify_all, 20/20 green with it)

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261103-080300

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. unit_tests_dbms --gtest_filter='BackgroundSchedulePool.IncreaseThreadsCountRaisesSameTypeLimit' fails on master every time (2 of 4 tasks run after increaseThreadsCount(4)). End to end, 6 FileLog tables stay at 4 ingested after SYSTEM RELOAD CONFIG raises the pool size, on master and on 26.8.34.13 / 26.10.2.103.
b Root cause explained? The per-type limit was computed once from the startup size, and increaseThreadsCount raised only the thread cap. A group at the limit is out of runnable_task_types, so neither the eager spawn nor idle workers ever ran more of its tasks.
c Fix matches root cause? Yes. The pool keeps the ratio, derives the limit from its current size with the startup formula, and on a raise re-adds the paused groups and wakes parked workers.
d Test intent preserved / new tests added? Three new gtests: the reported shape (ratio 0), the ratio kept after a resize, and a lower recomputed limit being ignored. Existing tests unchanged.
e Both directions demonstrated? Yes. The first gtest fails on master and passes with the fix; e2e reload 4 of 6 on master, 6 of 6 with the fix; 25.3 (before #84008) also ingests 6 of 6.
f Fix is general across code paths? Yes. All five resizable pools reach it through increaseThreadsCount; Server.cpp is unchanged. The iceberg pool is never resized.
g Fix generalizes across inputs (params/datatypes/wrappers)? Ratio 0, a fractional ratio, and the truncation edge where the recomputed limit is lower are covered by tests. An equal or smaller size keeps the existing behaviour. No data types involved.
h Backward compatible? (maintainer-approved exception only) Yes. Startup limits are unchanged (same formula and inputs). No setting, default or format change.
i Invariants and contracts preserved? num_running < max_parallel_tasks_per_type for runnable groups holds because the limit only grows. Capacity is reserved before any mutation. The limit is now TSA_GUARDED_BY(tasks_mutex).

Session id: cron:clickhouse-impl-slot-4:20261103-050900

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

cc @tavplubix @antaljanosbenjamin, could you review this and add the can be tested label so CI runs? The per-type task limit from #84008 was computed once from the startup pool size, so SYSTEM RELOAD CONFIG raised only the thread cap; the pool now keeps the ratio, raises the limit with the size, re-adds groups paused at the old limit and wakes parked workers.

@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1ff5c9a]

Summary: ✅


AI Review

Summary

The PR fixes runtime increases of background schedule pools by raising their per-type concurrency limits, restoring paused task groups, and waking parked workers. No actionable findings remain in the current diff or prior discussion.

Missing context / blind spots
  • ⚠️ Current-head sanitizer and integration jobs were pending during review; no local tests were run. Prior-revision CI at 6a30f01440bd passed all 14 BackgroundSchedulePool tests, including the three new resize cases (report), and both new Kafka integration cases (report). Completed current-head CI would close this remaining validation gap.
Final Verdict

✅ Approve based on code review and existing regression evidence, subject to required current-head CI.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 3, 2026
@nauu nauu added the can be tested Allows running workflows for external contributors label Nov 3, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-background-ops Background pools/scheduling for merges, cleanup, maintenance. label Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1ff5c9ab4 with master 7b5f2bf32 (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

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

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

Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -91.5 s (-6%) in total before that adjustment.

Job report

@nauu

nauu commented Nov 5, 2026

Copy link
Copy Markdown
Member

@groeneai can you add the integration test for the kafka engine scenarios

…hedule_pool_size

The server starts with a message broker pool of 2, four Kafka tables with
materialized views over empty topics keep their streaming tasks busy, and
the test raises the pool to 8 with SYSTEM RELOAD CONFIG. All four tasks
must then run at once. It covers both the old Kafka engine and Kafka2.

On master both cases stay at 2 running tasks after the reload
(BackgroundMessageBrokerSchedulePoolSize already reports 8); with the fix
both reach 4.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

Added in 78fd44f: tests/integration/test_storage_kafka/test_message_broker_pool_resize.py. The server starts with background_message_broker_schedule_pool_size = 2, four Kafka tables with materialized views keep their streaming tasks busy, then the test raises the size to 8 with SYSTEM RELOAD CONFIG and expects BackgroundMessageBrokerSchedulePoolTask to reach 4. It runs for both the old Kafka engine and Kafka2.

Locally it passed 10/10 with the fix. With a master binary both cases fail at 2 running tasks, while BackgroundMessageBrokerSchedulePoolSize already reports 8.

@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

The previous run on 78fd44f was cancelled by a CI-wide cancellation before most jobs ran.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nauu nauu self-assigned this Nov 5, 2026
@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 6a30f01

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
La Casa Del Dolor (amd_debug) / Logical error: Part size exceeded max_upload_part_size (STID 2991-407b) not from this PR (it changes only the per-type task limit of BackgroundSchedulePool): the random server config gave the S3 disk s3_strict_upload_part_size 87368190 with s3_max_upload_part_size 16777216, which is accepted, so the first multipart part of a Log table write (29304992 bytes) exceeded the maximum #129578 (mine, open)
Upgrade check (amd_release) / Error message in clickhouse-server.log (see upgrade_error_messages.txt) not from this PR: every line is ILLEGAL_DIVISION intDiv(100, x) While executing TTL from a background merge, the intentionally failing case of 05073_column_ttl_recompute_materialized; seen on 59 PRs in 14 days #127731 (mine, open)
Mergeable Check, PR praktika rollups of the rows above #129578 and #127731 (same as above)

Session id: cron:our-pr-ci-monitor:20261105-190008

@nauu
nauu added this pull request to the merge queue Nov 8, 2026
Merged via the queue into ClickHouse:master with commit 4eada96 Nov 8, 2026
187 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 8, 2026
@nauu

nauu commented Nov 11, 2026

Copy link
Copy Markdown
Member

@groeneai since this issue is a bug , it needs to be backported to the 26.4 and all later major versions.

@groeneai

Copy link
Copy Markdown
Collaborator Author

I can't add labels on this repo, so the backport label has to come from a maintainer. v26.4-must-backport is enough: the backport robot treats a version label as the oldest release to reach and also covers every newer active release branch, which today are 26.8, 26.9 and 26.10 (26.4 to 26.7 are end-of-life). All three contain #84008 and not this fix.

26.3 (LTS, still supported) also contains #84008; v26.3-must-backport instead would include it.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 11, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 11, 2026
Backport #128899 to 26.8: Raise the per-type task limit of a background schedule pool when its size is increased at runtime
clickhouse-gh Bot added a commit that referenced this pull request Nov 11, 2026
Backport #128899 to 26.10: Raise the per-type task limit of a background schedule pool when its size is increased at runtime
clickhouse-gh Bot added a commit that referenced this pull request Nov 11, 2026
Backport #128899 to 26.9: Raise the per-type task limit of a background schedule pool when its size is increased at runtime
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…nd schedule pool when its size is increased at runtime
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…nd schedule pool when its size is increased at runtime
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…und schedule pool when its size is increased at runtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-background-ops Background pools/scheduling for merges, cleanup, maintenance. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hot-increasing background_message_broker_schedule_pool_size leaves Kafka task concurrency capped at the initial pool size

6 participants