Skip to content

Fix flaky test 04103_user_network_bandwidth_throttler - #106680

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-04103-network-throttler-flaky
Jun 7, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-04103-network-throttler-flaky

Conversation

@groeneai

@groeneai groeneai commented Jun 7, 2026 •

Copy link
Copy Markdown
Collaborator

The test asserts ProfileEvents['UserThrottlerSleepMicroseconds'] is greater than half the throttled query duration. The throttler only enters its sleep path when the natural read rate exceeds the configured limit; when the natural rate drops close to the limit, the token bucket never goes negative and the assertion flaps.

PR #103586 added no-random-settings to remove S3-prefetch settings as one source of slow natural rate. Post-merge CIDB shows 4 sightings on Stateless tests (amd_tsan, parallel, 2/2) (PRs #106222, #102039, #49966, #106184), all with the read 1 1 0 shape (duration ok, bytes ok, sleep below threshold). All four already carry the no-random-settings tag, so the residual flakiness is contention-driven, not random-settings driven.

Drop the throttle limit and dataset 5x (1 MB/s -> 200 KB/s, 1e6 -> 2e5 rows). Wall-clock stays at ~8s. The 200 KB/s limit is well below the worst observed natural S3 read rate on contended TSAN parallel runners (~0.9 MB/s), giving a 4.5x safety margin. Local 10/10 iterations on an S3 disk produced sleep_us between 16.8s and 17.3s, comfortably above the 3.5s threshold.

Closes: #103422
Related: #103586
Related: #106184

CI report (post-merge sighting on PR #106184): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106184&sha=31a83e8fe57d67ba3f402133ba620f7b99a63ebd&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%2C%202%2F2%29

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

Not required.

Version info

  • Merged into: 26.6.1.481

The test asserts that ProfileEvents['UserThrottlerSleepMicroseconds'] is
greater than half of the target query duration. The throttler only sleeps
when the natural read rate exceeds the configured throttle limit; when the
natural rate drops close to the limit the token bucket never goes negative
and the throttler skips its sleep path (see `Throttler::throttle` in
`src/Common/Throttler.cpp`).

PR ClickHouse#103586 added `no-random-settings` to remove S3-prefetch settings as one
source of slow natural rate, but contention on the `Stateless tests
(amd_tsan, parallel, 2/2)` runner still occasionally drives the natural S3
read rate close to or below the previous 1 MB/s limit. Post-merge CIDB:
`pull_request_number != 0 AND check_start_time > 2026-06-05` shows 4
sightings (PRs ClickHouse#106222, ClickHouse#102039, ClickHouse#49966, ClickHouse#106184) all on amd_tsan parallel
2/2 with the `read 1 1 0` shape (duration ok, bytes ok, sleep below
threshold). All four PRs already include the `no-random-settings` tag, so
the remaining flakiness is contention-driven, not random-settings driven.

Drop the throttle limit and dataset 5x so the throttle is well below any
plausible natural rate (200 KB/s vs ~0.9 MB/s observed worst case = 4.5x
safety margin). Test wall-clock stays at ~8s. Local 10/10 runs against an
S3 disk produced sleep_us between 16.8s and 17.3s, comfortably above the
3.5s threshold.

Closes: ClickHouse#103422
Related: ClickHouse#103586
Related: ClickHouse#106184

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

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Partial. Failure shape read 1 1 0 reproduces on heavily-loaded Stateless tests (amd_tsan, parallel, 2/2) runners, where natural S3 read rate drops to ~0.89 MB/s (CIDB: 4 sightings post PR #103586 merge, all on the same shard, all with the read 1 1 0 shape). Locally the failure is contention-driven and not directly reproducible without artificial S3 throttling, but the mechanism is verified end-to-end against Throttler::throttle and ProfileEvents.
b Root cause explained? Yes. Throttler::throttle only enters its sleep path when tokens_value < 0, which requires natural rate to exceed max_speed. When the natural rate hovers near or below the throttle limit, the token bucket never goes negative and UserThrottlerSleepMicroseconds stays low, so the third assertion (sleep_us > duration*0.5) fails even though duration and bytes assertions pass. PR #103586 fixed one source of slow natural rate (random S3 prefetch settings); contention on the TSAN parallel runner is a second, independent source.
c Fix matches root cause? Yes. The fix lowers the throttle limit and dataset 5x (1 MB/s -> 200 KB/s, 1e6 -> 2e5 rows). This restores the invariant natural_rate >> max_speed that the throttler's sleep logic requires, with a 4.5x safety margin against the worst observed CI natural rate (0.89 MB/s). Wall-clock is preserved at ~8s.
d Test intent preserved / new tests added? Yes. The test still verifies (1) per-user throttling produces a duration floor, (2) UserThrottlerBytes accumulates the transferred bytes, and (3) UserThrottlerSleepMicroseconds reflects the throttler actively sleeping. Bytes threshold scaled proportionally (8e6 -> 1.6e6) so the assertion remains meaningful for the new dataset size.
e Both directions demonstrated? Partial. Without the fix, CIDB shows 4 deterministic-shape failures (read 1 1 0) on the affected check; with the fix, local 10/10 iterations against an S3 disk produced sleep_us between 16.8s and 17.3s and effective rate at 202 KB/s (right at the 200 KB/s limit, throttler firing reliably). Full CI signal will come from the PR build.
f Fix is general, not a narrow patch? N/A (flaky-test setting fix, not a code bug). The fix targets the test's brittleness against the natural-rate-vs-throttle invariant, not a defect in Throttler itself. The throttler's behavior (skip sleep when natural rate is below the limit) is correct; the test was relying on natural rate being reliably above the limit, which is not guaranteed under contention.

Session id: cron:clickhouse-ci-task-worker:20260607-150400

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

cc @serxa - could you review this? Test-only fix: lowers the throttle limit and dataset 5x so natural S3 read rate is reliably above the throttle limit on contended TSAN parallel runners. Follow-up to #103586 which fixed only the random-settings source of slow natural rate.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b92612e]

Summary: ✅


AI Review

Summary

This PR narrows the flaky 04103_user_network_bandwidth_throttler test by scaling the S3 dataset and max_network_bandwidth_for_user limit down together, preserving the throttling duration while making the configured cap sit well below the contended CI S3 read rate described in the PR. I checked the changed test, the throttler wiring for user-level remote read/write throttling, prior PR discussion, and the current Praktika report; I did not find a review finding that warrants an inline comment.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.60% +0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 863 line(s), 110 function(s) across 145 file(s) · Details

Top files
  • src/Databases/DataLake/GlueCatalog.cpp: 116 line(s), 12 function(s)
  • src/Storages/ObjectStorage/DataLakes/Iceberg/ComplexTypeSchemaProcessorFunctions.cpp: 88 line(s), 8 function(s)
  • src/Storages/ObjectStorage/DataLakes/Iceberg/MetadataGenerator.cpp: 52 line(s), 2 function(s)
  • src/Storages/StorageReplicatedMergeTree.cpp: 49 line(s)
  • src/Databases/DataLake/Common.cpp: 43 line(s), 3 function(s)

Full report

@alexey-milovidov alexey-milovidov self-assigned this Jun 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 7, 2026
Merged via the queue into ClickHouse:master with commit b9a4d62 Jun 7, 2026
166 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 8, 2026
pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Aug 5, 2026
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]>
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 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.

Flaky test: 04103_user_network_bandwidth_throttler

4 participants