Skip to content

Fix flaky 02703_max_local_read_bandwidth: restore throttler headroom - #113365

Merged
PedroTadim merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-02703-local-read-bandwidth-headroom
Aug 5, 2026
Merged

PedroTadim merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-02703-local-read-bandwidth-headroom

Conversation

@groeneai

@groeneai groeneai commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #113107
Related: #106680
Related: #103422

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

...

Description

02703_max_local_read_bandwidth intermittently fails on pread_threadpool with only its 4th column
wrong (1 1 1 0), most recently on this
report.

Root cause: the 4th assertion is a coupled oracle. Throttler::throttle increments the bytes
counter unconditionally (Throttler.cpp:63-64) but the sleep counter only inside if (block)
(:78-82), and block requires tokens_value < 0 (:70). The token bucket (:110-125) goes
negative only while bytes arrive faster than the cap, so once a loaded runner's read rate falls to
about the cap the throttler correctly does not sleep while bytes still record. The fixture had no
margin: at a 1e6 B/s cap over an 8 MB payload it owes exactly 7.0 s, exactly what column 1 also
demands, and column 4 had only 2.0x over its 3.5 s threshold.

The sleep is not lost or misattributed. In the reported failure QueryLocalReadThrottlerBytes passed
while its sibling sleep counter was 0, and both are charged through the same
CurrentThread::getProfileEvents a few lines apart, so a detached thread would have lost both. No
pool thread is involved either: for pread_threadpool the throttle call runs on the consuming
pipeline thread (AsynchronousReadBufferFromFileDescriptor.cpp:141-142), not in ThreadPoolReader,
which never throttles.

The change co-reduces the payload (1e6 -> 2e5 rows) and the cap (1e6 -> 160000 B/s), raising the
owed sleep to 9.0 s at comparable wall clock: 1.29x over the duration floor and 2.57x over the
unchanged 3.5 s sleep threshold. Byte thresholds track the payload at 8e6 -> 1.6e6,
still requiring the whole scan be throttled. All four assertions, both time thresholds and .reference are
unchanged. Same fix as #106680 for the sibling 04103_user_network_bandwidth_throttler.

Validation. Capping the arrival rate (max_execution_speed_bytes, max_threads=1) reproduces
the signature: unmodified prints 1 1 1 0 on all three arms, 1 1 1 1 without the injection;
modified stays green. 50/50 clean local runs, runtime 1.18x.

Version info

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

groeneai and others added 3 commits August 4, 2026 13:50
…headroom

The test intermittently failed on pread_threadpool with only its 4th column
wrong (1 1 1 0): QueryLocalReadThrottlerSleepMicroseconds came out 0 while the
query was demonstrably slow and all bytes had passed the throttler.

The 4th assertion is a coupled oracle. Throttler::throttle increments the bytes
counter unconditionally, but the sleep counter only inside `if (block)`, and
block requires tokens_value < 0. The token bucket only goes negative when bytes
arrive faster than the cap, so once a loaded runner's read rate falls to about
the cap the throttler correctly does not sleep and the assertion fails. With a
1 MiB/s cap over an 8 MB payload the fixture had only 1.115x of margin between
the cap and the arrival rate at which the assertion breaks (measured), which a
contended sanitizer runner erases. The nominal required sleep was also
8e6/1048576-1 = 6.63 s, already below the 7 s the first column demands, so that
column had been passing on unrelated overhead; the in-file comment claiming
"(8-1)/1=7 seconds" was wrong because '1M' is 1048576, not 1e6.

The sleep time is not lost or misattributed to a pool thread:
ThrottlerSleepMicroseconds is 0 as well in the reproduced failure, so no thread
slept at all. Both counters are charged through the same
CurrentThread::getProfileEvents() a few lines apart, and the pread_threadpool
throttle call runs on the consuming pipeline thread in
AsynchronousReadBufferFromFileDescriptor::nextImpl, not on a pool thread -
ThreadPoolReader never throttles.

Co-reduce the payload (1e6 -> 2e5 rows) and the cap ('1M' -> 160000 B/s)
together, which raises the required sleep to 9 s at comparable wall clock, and
rescale the two byte thresholds by the same factor so they keep asserting that
the whole payload passed through the throttler. All four assertions, both time
thresholds and the reference file are unchanged. This is the same fix that was
merged for the sibling 04103_user_network_bandwidth_throttler (ClickHouse#103422).

Reproduced deterministically by capping the arrival rate with
max_execution_speed_bytes and max_threads=1: the unmodified test prints
1 1 1 0 on all three arms, and 1 1 1 1 with the injection absent. The measured
failure boundary moves from 1.115x of the cap to at most 1.006x, and the test
now also passes at arrival rates below its own cap. 50/50 clean local runs;
runtime 23.9 s -> 28.3 s.

No source change: the throttler behaved correctly at every arrival rate
measured, so there is no product defect here.
Corrected narrative for the fix in a2a3a41, which restores the
throttler rate headroom in `02703_max_local_read_bandwidth`. The previous
commit message misread the size suffix `M` as binary and drew three wrong
conclusions from it; this commit states the arithmetic correctly and adds the
same figure to the test's leading comment. The executable content of the test
is byte-identical to a2a3a41.

The test intermittently failed on `pread_threadpool` with only its 4th column
wrong (`1 1 1 0`): `QueryLocalReadThrottlerSleepMicroseconds` came out 0 while
the query was demonstrably slow and all bytes had passed the throttler.

The 4th assertion is a coupled oracle. `Throttler::throttle` increments the
bytes counter unconditionally (`Throttler.cpp:63-64`), but the sleep counter
only inside `if (block)` (`:78-82`), and `block` requires `tokens_value < 0`
(`:70`). The token bucket (`:110-125`) only goes negative while bytes arrive
faster than the cap, so once a loaded runner's read rate falls to about the cap
the throttler correctly does not sleep and the assertion fails.

The fixture had no margin to lose. `M` is the decimal suffix in
`readTextWithSizeSuffix` (the binary form needs a trailing `i`, as
`01039_test_setting_parse` pins), so the old cap was 1 MB/s = 1e6 B/s and the
8 MB payload owed exactly (8e6 - 1e6) / 1e6 = 7.0 s. That is exactly what the
`query_duration_ms >= 7e3` column also demands, so column 1 was passing on
unrelated overhead, and column 4 had only 2.0x over its 3.5 s threshold. The
pre-existing comment `(8-1)/1=7 seconds` was right; the fixture's problem was
the absent margin, not the arithmetic.

The sleep time is not lost or misattributed to a pool thread:
`ThrottlerSleepMicroseconds` is 0 as well in the reproduced failure, so no
thread slept at all. Both counters are charged through the same
`CurrentThread::getProfileEvents` a few lines apart, and the
`pread_threadpool` throttle call runs on the consuming pipeline thread in
`AsynchronousReadBufferFromFileDescriptor::nextImpl`, not on a pool thread -
`ThreadPoolReader` never throttles.

The change co-reduces the payload (1e6 -> 2e5 rows) and the cap
(1e6 -> 160000 B/s), which raises the owed sleep from 7.0 s to 9.0 s at
comparable wall clock: 1.29x over the duration floor, 2.57x over the sleep
threshold, and the read rate must degrade 6.25x further before the token
bucket stops going negative. The two byte thresholds drop 8e6 -> 1.5e6, which
still requires 93.75% of the 1600982 B payload to pass through the throttler.
All four assertions, both time thresholds and the reference file are unchanged.
This is the same fix that was merged for the sibling
`04103_user_network_bandwidth_throttler` (ClickHouse#103422).

Reproduced deterministically by capping the arrival rate with
`max_execution_speed_bytes` and `max_threads=1`: the unmodified test prints
`1 1 1 0` on all three read methods, and `1 1 1 1` with the injection absent.
The modified test stays green under the same injection, and also at arrival
rates below its own cap. 50/50 clean local runs; runtime 23.9 s -> 28.3 s.

No source change: the throttler behaved correctly at every arrival rate
measured, so there is no product defect here.

Co-Authored-By: Claude Fable 5 <[email protected]>
Corrected narrative for the fix in a2a3a41, which restores the
throttler rate headroom in `02703_max_local_read_bandwidth`, plus the
reference to the pull request that surfaced the flake appended to the test's
leading comment. The executable content of the test is byte-identical to
a2a3a41.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113107&sha=8f0f86485d192241fd2ed5bc0f5cb0b7630d244c&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29
Related: ClickHouse#113107

The test intermittently failed on `pread_threadpool` with only its 4th column
wrong (`1 1 1 0`): the query was demonstrably slow and all bytes had passed
the throttler, but `QueryLocalReadThrottlerSleepMicroseconds` came out 0.

The 4th assertion is a coupled oracle. `Throttler::throttle` increments the
bytes counter unconditionally (`Throttler.cpp:63-64`), but the sleep counter
only inside `if (block)` (`:78-82`), and `block` requires `tokens_value < 0`
(`:70`). The token bucket (`:110-125`) only goes negative while bytes arrive
faster than the cap, so once a loaded runner's read rate falls to about the cap
the throttler correctly does not sleep and the assertion fails.

The fixture had no margin to lose. `M` is the decimal suffix in
`readTextWithSizeSuffix` (the binary form needs a trailing `i`, as
`01039_test_setting_parse` pins), so the old cap was 1 MB/s = 1e6 B/s and the
8 MB payload owed exactly (8e6 - 1e6) / 1e6 = 7.0 s. That is exactly what the
`query_duration_ms >= 7e3` column also demands, so column 1 was passing on
unrelated overhead, and column 4 had only 2.0x over its 3.5 s threshold. The
pre-existing comment `(8-1)/1=7 seconds` was right; the fixture's problem was
the absent margin, not the arithmetic.

The sleep time is not lost or misattributed to a pool thread. In the reported
failure `QueryLocalReadThrottlerBytes` exceeded 8e6 while its sibling sleep
counter was 0, and both are charged through the same
`CurrentThread::getProfileEvents` a few lines apart in `Throttler::throttle`,
so a thread detached from the query would have lost both; the bytes recorded,
therefore the thread was attached and the sleep branch simply never ran.
Structurally there is also no pool thread to lose it on: for `pread_threadpool`
the throttle call runs on the consuming pipeline thread in
`AsynchronousReadBufferFromFileDescriptor::nextImpl`, and `ThreadPoolReader`
never throttles.

The change co-reduces the payload (1e6 -> 2e5 rows) and the cap
(1e6 -> 160000 B/s), which raises the owed sleep from 7.0 s to 9.0 s at
comparable wall clock: 1.29x over the duration floor, 2.57x over the sleep
threshold, and the read rate must degrade 6.25x further before the token
bucket stops going negative. The two byte thresholds drop 8e6 -> 1.5e6, which
still requires 93.75% of the nominal payload to pass through the throttler.
All four assertions, both time thresholds and the reference file are unchanged.
This is the same fix that was merged for the sibling
`04103_user_network_bandwidth_throttler` in ClickHouse#106680 (issue ClickHouse#103422).

Reproduced deterministically by capping the arrival rate with
`max_execution_speed_bytes` and `max_threads=1`: the unmodified test prints
`1 1 1 0` on all three read methods, and `1 1 1 1` with the injection absent.
The modified test stays green under the same injection, and also at arrival
rates below its own cap. 50/50 clean local runs; runtime 23.9 s -> 28.3 s.

No source change: the throttler behaved correctly at every arrival rate
measured, so there is no product defect here.

Co-Authored-By: Claude Fable 5 <[email protected]>
@groeneai

groeneai commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author
Internal review before publication (2 blockers, 4 major, 1 nit)

Before opening this PR I ran three rounds of independent cold review of the resulting diff, each
paired with a second-model gate review. Every blocker and major was fixed before anything was
pushed. The change itself was never altered by the review: all findings were in the prose, on a PR
whose entire argument is arithmetic.

# Severity Finding Verdict
1 blocker An early draft read the size suffix M as binary, and concluded from that both that the pre-existing comment (8-1)/1=7 seconds was wrong and that the owed sleep had been 6.63 s. Both inverted: readTextWithSizeSuffix handles case 'M' as finish(1000000, 20) and takes the binary shift only on a following i. So the old cap was 1e6 B/s, the owed sleep was exactly 7.0 s, and the existing comment was right. AGREE, fixed
2 blocker The refutation of the sleep-attribution hypothesis was sold on ThrottlerSleepMicroseconds also being 0 in the local reproduction. That counter cannot carry the claim: it is one shared event (ProfileEvents.cpp:284) and the reproduction's own arrival-rate injection is its second writer (ExecutionSpeedLimits.cpp:42, reached at :107-108), with :56-61 reading the same cell back and subtracting it under /// Do not count sleeps in throttlers. Replaced with the unconfounded argument from CI's own numbers, which is also stronger. AGREE, fixed
3 major The draft's headline ("~19x more headroom") was a quotient of two excesses over 1.0x with an upper-bound denominator, computed from the same inverted cap. Replaced with figures the token bucket gives directly: cap down 6.25x, owed sleep 7.0 s to 9.0 s, margin over the unchanged threshold 2.0x to 2.57x. AGREE, fixed
4 major The draft said the byte thresholds were "rescaled by the same factor" as the payload. They are not (payload 5.00x, thresholds 5.33x), so the assertion now requires most of the payload rather than all of it. Stated accurately. AGREE, fixed
5 major The commit message was missing the CI report URL the repository asks for, and cited the prior art by its issue number rather than by the merged PR. Both added. AGREE, fixed
6 major The gate review asked for a deterministic arrival-rate injection inside the test itself. Declined: the only available knob writes the very counter that ExecutionSpeedLimits.cpp:57 reads back and subtracts, and that Throttler.cpp:79 also writes, so the test would assert its own confound. Neither the merged prior art nor the sibling write test injects. The reproduction stays offline, where it has a disagreeing negative control. DISAGREE
7 nit Two accepted trade-offs, recorded rather than fixed. The intermediate commit still carries the superseded sentence from finding 2 (amend and rebase are not used on this branch, and a fourth prose-only commit on a 9-line test change is not worth the churn). And the byte thresholds now assert 93.7% of the payload rather than 99.95%; the old 0.046% margin was itself a flake surface, and the new shape matches #106680. AGREE, accepted

The load-bearing correction is finding 1, and it strengthens the case: at 1e6 B/s the old fixture
owed exactly 7.0 s, which is exactly what its own query_duration_ms >= 7e3 column demands, so the
sleep assertion had no margin at all. 01039_test_setting_parse pins the suffix behaviour from
outside ('12M' yields 12000000, '31Mi' yields 32505856).

Derived purely from the fixture constants and the token bucket, the sleep assertion used to break
once the natural read rate fell below 2.284 MB/s, and now breaks below 0.291 MB/s. A runner may be
7.9x slower than before it flakes again.

Session id: cron:clickhouse-review-slot-49:20260804-171931

@groeneai

groeneai commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Check Verdict
a Deterministic repro? YES - max_execution_speed_bytes=1100000 max_threads=1 timeout_before_checking_execution_speed=0 makes the unmodified test print 1 1 1 0 on all three arms every run; the same test with the injection absent prints 1 1 1 1 (negative control).
b Root cause explained? YES - the sleep counter lives inside if (block) (Throttler.cpp:78-82) and block requires tokens_value < 0 (:70), which requires arrival rate above the cap. At arrival approximately equal to the cap the throttler correctly does not sleep while the bytes counter still records (:63-64), producing 1 1 1 0.
c Fix matches root cause? YES - it restores the arrival-rate-over-cap margin the assertion depends on (cap down 6.25x, owed sleep 7.0 s -> 9.0 s, margin over the unchanged 3.5 s threshold 2.0x -> 2.57x), instead of masking the symptom. The two previous attempts at this flake relaxed the threshold (fdfb1eda55b0c2e, 263be33297a2ada); a third relaxation was rejected.
d Test intent preserved / new tests added? YES - all four assertions kept, both time thresholds (>= 7e3, > 7e6*0.5) unchanged, .reference unchanged. The two byte thresholds drop 8e6 -> 1.5e6, keeping the same strictly-above-payload shape (payload 1600982 B vs threshold 1.5e6, as 8003703 B vs 8e6 before) and still requiring almost all of the payload through the throttler. The changed test is itself the regression test.
e Demonstrated in BOTH directions? YES - the old test FAILS 1 1 1 0 at an injected arrival of 1.100x its cap, while the new test PASSES 1 1 1 1 at 1.050x of its own cap and still passes down to 0.906x of it, so the fixed arm is held to the stricter relative severity. Plus 50/50 clean runs with no injection.
f Fix general across CODE paths? YES - carrier set closed: grep -rln ThrottlerSleep tests/ returns 3 files. All three read-method arms of this test re-verified (not only pread_threadpool); 02703_max_local_write_bandwidth.sh deliberately has no sleep assertion, so it cannot carry the defect; 04103_user_network_bandwidth_throttler.sh was already fixed by this same mechanism and is the prior art. 02844_max_backup_bandwidth_s3.sh asserts only query_duration_ms and is immune.
g Fix generalizes across INPUTS? YES - all three read methods verified green naturally and under injected arrival rates from 1.006x to 6.875x of the cap, and at rates below the cap. No type-wrapper or parameter matrix applies to a shell fixture.
h Backward compatible? YES - test-only. No setting default, no counter semantics, no serialization format; SettingsChangesHistory.cpp not involved.
i Invariants and contracts preserved? YES - the test's contract (a throttled read is slow, passes all bytes through the throttler, and demonstrably sleeps) is unchanged and now rests on a well-founded premise. No source contract touched.

Measured invariants: owed sleep 7.0 s -> 9.0 s; cap-to-natural-rate ratio <=0.0001x; sleep margin over the threshold 2.0x -> 2.57x; runtime 23.9 s -> 28.3 s (1.18x).

No source change: the throttler behaved correctly at every arrival rate measured, so there is no product defect here.

Session id: cron:clickhouse-review-slot-49:20260804-171931

@groeneai

groeneai commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author

cc @serxa, could you review this? The test's sleep assertion is coupled to the arrival rate: Throttler::throttle records bytes unconditionally but only records sleep inside if (block), so when a loaded runner's read rate falls to about the cap the throttler correctly does not sleep and the 4th column goes to 0. The old fixture owed exactly 7.0 s at a 1e6 B/s cap, the same value its duration column demands, so it had no margin; this co-reduces the payload and the cap to get back to 9.0 s.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Aug 4, 2026
@PedroTadim PedroTadim self-assigned this Aug 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [07c76df]

Summary: ✅


AI Review

Summary

This PR lowers the payload and max_local_read_bandwidth in 02703_max_local_read_bandwidth.sh so the test still accumulates enough throttler sleep on loaded runners while preserving the existing duration and byte-count assertions. I did not find a remaining correctness or coverage regression in the current patch; the earlier concern about weakened byte thresholds is fixed in the current code. The relevant CI shards are still pending, so the last proof is the live stateless runs.

Final Verdict

✅ No new review findings.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 4, 2026
…bandwidth

The headroom fix lowered both byte checks from `> 8e6` to `> 1.5e6` along with
the payload, but 2e5 `UInt64` rows are 1.6e6 raw bytes, so `> 1.5e6` left
100600 measured bytes of slack. That is a real loss of contract: the two byte
columns exist to prove the whole scan went through the local-read throttler,
and a regression letting about 100 KB bypass it would still have printed
`1 1 1 1`.

Measured on the fixture (all three read methods, warm and cold mark cache):
`ReadBufferFromFileDescriptorReadBytes` and `QueryLocalReadThrottlerBytes` are
both 1600600 warm and 1600777 cold, deterministic over 9 runs. `CODEC(NONE)`
makes the compressed file the raw payload plus a 25-byte header per compressed
block, so the counter is always strictly above 1.6e6. Raising both checks back
to `> 1.6e6` restores the same relative margin the pre-fix fixture had against
`> 8e6` (0.0375% versus 0.0381%).

Verified the tightened threshold is not vacuous by shrinking the payload: at
187500 rows (the 100 KB bypass the reviewer described) the counter is 1500746,
which passes `> 1.5e6` and fails `> 1.6e6`; even 199900 rows, 800 bytes short,
is caught. The sleep oracle this PR fixes is unaffected: the test passes three
times with no injection and twice under a rate injection at 1.05x of its own
cap, which is the arm that reproduces a loaded runner.

CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113107&sha=8f0f86485d192241fd2ed5bc0f5cb0b7630d244c&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29
Related: ClickHouse#113107
@groeneai

groeneai commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 07c76df

CI fully finished on this head: 174 of 174 check-runs completed (51 success, 123 skipped,
0 failure), 0 queued or in progress, Config Workflow and Finish Workflow both green,
past the 20-minute CIDB ingestion buffer.

Check / test Reason Owner / fixing PR
(none failed) - -

Nothing failed on this head, and CIDB holds zero FAIL/ERROR rows for this commit. Every
check that runs for a change of this scope passed, including Fast test, Style check,
Code Review, all 10 Stress test variants, every Stateless tests variant, and all four
flaky check runs of the modified test. The high skip count (123) is praktika's scope gating,
not dropped work: this PR touches one file,
tests/queries/0_stateless/02703_max_local_read_bandwidth.sh, so the build matrix and the
integration, unit, bugfix-validation, performance and docs jobs are all correctly not applicable.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113365&sha=07c76dfd25f332afc5436bd5ce30a01bc31e441c&name_0=PR

Session id: cron:our-pr-ci-monitor:20260805-023000

@PedroTadim
PedroTadim added this pull request to the merge queue Aug 5, 2026
Merged via the queue into ClickHouse:master with commit 234eedc Aug 5, 2026
180 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 Aug 5, 2026
@groeneai groeneai added the groeneai-origin-request PR origin: a maintainer pinged or directed groeneai label Aug 19, 2026
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 groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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