Skip to content

Fix flaky test 02770_async_buffer_ignore - #107298

Merged
PedroTadim merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-02770-async-buffer-ignore
Sep 18, 2026
Merged

PedroTadim merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-02770-async-buffer-ignore

Conversation

@groeneai

@groeneai groeneai commented Jun 11, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #100391
Related: #106231

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

Fixes the flaky test 02770_async_buffer_ignore, and makes it assert the regression it was written for. Investigated at the request of alexey-milovidov on #100391, raised again on #106231.

The flake

The test asserted exact S3 read profile-event counts for a narrow-range read:

ProfileEvents['S3ReadRequestsCount'], ProfileEvents['ReadBufferFromS3Bytes'], ProfileEvents['ReadCompressedBytes']

against the reference 4 66446 66446. Those counts are not deterministic across CI jobs and the test cannot pin them away (it is already no-random-settings). Over 120 days CIDB shows 253 failures across 28 distinct observed triples, 7 of them on master, first seen 2026-03-26. The largest groups are 4 830 66446 (x174, filesystem cache hit), 6 67909 67909 (x22, parallel replicas), 4 7223260 66446 (x5, S3 read-ahead), 7 825111 569492 (x5), 4 17844 17844 (x5), and a long tail of 4 66446 679xx/4 66446 66533 where only ReadCompressedBytes drifts by tens to ~1700 bytes. The two most recent master hits are 4 66446 68063 (2026-07-26) and 4 66446 66533 (2026-07-27).

What the test guards, and why no bound on ReadCompressedBytes can guard it

The regression fixed together with this test was AsynchronousBoundedReadBuffer::seek taking the lazy-ignore branch while file_offset_of_buffer_end was still 0, so the buffer downloaded the whole prefix ahead of the first needed granule and discarded it.

I reproduced that by removing the file_offset_of_buffer_end && guard in AsynchronousBoundedReadBuffer::seek together with the !current_buf || short-circuit in ReadBufferFromRemoteFSGather::isSeekCheap (the second one is required, otherwise isSeekCheap() returns true on the first seek and the branch is unreachable). On this fixture the narrow read then transfers 853846 S3 bytes instead of 66446, of which 787400 are discarded.

ReadCompressedBytes does not move at all: it is incremented in CompressedReadBufferBase for blocks handed to the decompressor, while the ignored prefix is dropped lower down in ThreadPoolRemoteFSReader::execute before any block is assembled. On the buggy build ReadCompressedBytes is 66446, byte-for-byte equal to the healthy build. So no threshold on it can detect the over-read: not < 1000000, not < 100000, not even the exact 66446 the reference used to pin. The previous revision of this PR claimed the 1 MB bound would still catch an over-read; that claim was wrong and is retracted.

Measured, narrow read, healthy vs buggy:

build S3ReadRequestsCount ReadBufferFromS3Bytes ReadCompressedBytes AsynchronousReaderIgnoredBytes
healthy 4 66446 66446 0
guards removed 4 853846 66446 787400

The fix

Assert the counter that actually observes the defect. The query seeks each column once, from offset 0, so a correct lazy ignore never triggers and AsynchronousReaderIgnoredBytes is exactly 0:

ProfileEvents['RemoteFSSeeks'] > 0,
ProfileEvents['AsynchronousReaderIgnoredBytes'] = 0,
ProfileEvents['S3ReadRequestsCount'] < 100,
ProfileEvents['ReadCompressedBytes'] < 1000000

RemoteFSSeeks > 0 is an activation check. With remote_filesystem_read_method = 'read' there is no AsynchronousBoundedReadBuffer at all, so the ignored-bytes assertion would pass vacuously: on the buggy build, method read reports RemoteFSSeeks 0 and AsynchronousReaderIgnoredBytes 0 while threadpool reports 2 and 787400. tests/clickhouse-test does randomize remote_filesystem_read_method over read and threadpool, but the existing no-random-settings tag already suppresses that for this test, so the pin and the assertion are not guarding against the current randomizer: they guard against the default changing, the tag being dropped, or the test being edited onto the synchronous path, any of which would silently turn the ignored-bytes check into a no-op.

ReadCompressedBytes < 1000000 is kept as a granule-selection bound (a whole-column read is ~4 MB compressed), with a comment that no longer overstates what it covers. ReadBufferFromS3Bytes stays dropped: it is the unstable raw-transfer count (830 on a cache hit, 7.2 MB under read-ahead).

Verification

Local server over MinIO s3_disk (same disk definition as tests/config/config.d/storage_conf.xml), Build IDs recorded for each direction:

  • healthy build 66c6aded: 1 1 1 1, 10/10, and stable 15/15 on a fresh database.
  • guards removed, build 0c2a53f6: 1 0 1 1, 10/10. Only the new column reacts; the two surviving bounds print 1 1 on the buggy build, which is exactly the coverage gap this change closes.
  • activation mutation: dropping the read-method pin on the buggy build gives RemoteFSSeeks 0 / ignored 0 for method read (bug invisible, caught by the activation assertion) versus RemoteFSSeeks 2 / ignored 787400 for threadpool.
  • AsynchronousReaderIgnoredBytes is 0 across the regimes that made the old assertion flaky: repeated reads, prefetch off, max_threads 1 and 16, remote_read_min_bytes_for_seek = 0, parallel replicas, larger prefetch steps, larger read buffer.

This flake is unrelated to #100391: the query has no ORDER BY, so the read-in-order path that PR changes is not exercised.


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

Version info

  • Merged into: 26.10.1.75 (included in 26.10 and later)

The test asserted exact S3 read profile-event counts (S3ReadRequestsCount,
ReadBufferFromS3Bytes, ReadCompressedBytes) for a narrow-range read. Those
counts are non-deterministic across CI jobs: filesystem cache hits drop
ReadBufferFromS3Bytes to ~830, parallel replicas raise the request count,
and S3 read-ahead inflates the byte counts. Over 90 days this produced 212
failures across 12 unrelated PRs.

Assert bounds instead: a narrow read of 2001 of 1000000 rows must not pull
the whole part (~4 MB compressed per column), so S3ReadRequestsCount stays
small and ReadCompressedBytes stays far below the full-column size. This
still catches the over-read regression the test guards against while
tolerating cache, read-ahead, and parallel-replica variance. Mirrors the
approach in 03164_s3_settings_for_queries_and_merges.

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. Local MinIO s3_disk: a cold narrow read is deterministically 4 66446 66446, but a warm re-read of the same table is deterministically off-reference (3 66031 66031, 2 65616 65616) because the filesystem cache serves part of the data. On-demand reproducible.
b Root cause explained? The test asserts exact S3-level profile-event counts (S3ReadRequestsCount, ReadBufferFromS3Bytes, ReadCompressedBytes) for a narrow-range read. Those counts vary with filesystem-cache warmth (ReadBufferFromS3Bytes -> ~830), S3 read-ahead (-> up to 7.2 MB), and parallel replicas (request count -> 6). The test is already no-random-settings, so these cannot be pinned at the query level. CIDB: 212 fails / 12 unrelated PRs / 90 days, 1 on master.
c Fix matches root cause? Yes. Replaces the three unstable exact-count columns with two bounded invariants (S3ReadRequestsCount < 100, ReadCompressedBytes < 1000000) that are stable under cache/read-ahead/parallel-replica variance. Drops ReadBufferFromS3Bytes (the chameleon raw-transfer count). Mirrors the sibling test 03164_s3_settings_for_queries_and_merges (same author), which asserts ratios for the same reason.
d Test intent preserved / new tests added? Preserved. The test verifies a narrow read (2001 of 1000000 rows) does not pull the whole part. The full column is ~4 MB compressed; the bound ReadCompressedBytes < 1000000 still catches an over-read regression. Verified a full-column scan still trips the bound (1 0).
e Both directions demonstrated? Yes. Narrow read passes 15/15 with fresh DBs; warm re-reads that broke the old assertion pass the new one; the full-column-scan regression is caught (1 0).
f Fix is general, not a narrow patch? N/A (flaky-test assertion fix, not a code bug).

Session id: cron:clickhouse-worker-slot-2:20260611-231200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @kssenii — could you review this? It is a test-only fix for the flaky 02770_async_buffer_ignore: the test asserted exact S3 read profile-event counts that vary with filesystem-cache warmth, S3 read-ahead, and parallel replicas (212 fails / 12 unrelated PRs / 90d). It now asserts bounds (S3ReadRequestsCount < 100, ReadCompressedBytes < 1000000), keeping the over-read guard while tolerating the variance, mirroring your 03164_s3_settings_for_queries_and_merges.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f1e0782]

Summary: ✅


AI Review

Summary

This PR rewrites 02770_async_buffer_ignore to stop pinning exact S3 counters and instead force the async remote-read path with remote_filesystem_read_method='threadpool', require RemoteFSSeeks > 0, and assert AsynchronousReaderIgnoredBytes = 0 as the actual oracle for the prefix over-read. I did not find a remaining correctness or coverage issue in the current revision: the earlier ReadCompressedBytes concern is addressed, and the added activation check keeps the ignored-bytes assertion from passing on the synchronous path.

Missing context / blind spots
  • ⚠️ Full CI for commit f1e07820d45af0671ed040c59063cf6212b6f8ca is still in progress as of 2026-09-17; only Fast test and Style check were complete during this review. I also could not re-run the shell test locally because this checkout does not contain a ready build/test environment.
Final Verdict

✅ No new review findings. I would let CI finish, but I do not have an inline blocker to post on this PR.

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

clickhouse-gh Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.40% 92.40% +0.00%
Branches 77.30% 77.30% +0.00%

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

Newly covered by added/modified tests: 279 line(s), 45 function(s) across 113 file(s) · Details

Top files
  • src/Storages/MaterializedView/RefreshTask.cpp: 15 line(s)
  • src/AggregateFunctions/TimeSeries/AggregateFunctionTimeseriesChanges.h: 14 line(s), 32 function(s)
  • src/IO/S3/copyS3File.cpp: 10 line(s)
  • src/Functions/FunctionsNumericIndexedVector.h: 9 line(s), 4 function(s)
  • src/IO/AzureBlobStorage/PocoHTTPClient.cpp: 9 line(s)

Full report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI summary — HEAD 6ce2bae

CI has fully finished (Finish Workflow: success). The fix target 02770_async_buffer_ignore is green everywhere, including the flaky check at 50 runs across all four sanitizers (amd_debug / amd_asan_ubsan / amd_tsan / amd_msan) and on every s3/ParallelReplicas/coverage variant.

Two unrelated failures remain, neither caused by this test-only .reference change:

This PR only changes 02770_async_buffer_ignore.{sh,reference}. Ready for review.

The bounded rewrite of this test stopped the chronic result-differs flake but
left no assertion able to observe the defect the test exists for.

That defect was AsynchronousBoundedReadBuffer::seek taking the lazy-ignore branch
while file_offset_of_buffer_end was still 0, so the buffer downloaded the whole
prefix ahead of the first needed granule and discarded it. Reproduced by removing
the file_offset_of_buffer_end && guard together with the !current_buf ||
short-circuit in ReadBufferFromRemoteFSGather::isSeekCheap - the second is
required, otherwise isSeekCheap() is true on the first seek and the branch is
unreachable. The narrow read then transfers 853846 S3 bytes instead of 66446, of
which 787400 are discarded.

ReadCompressedBytes cannot see any of that. It is incremented in
CompressedReadBufferBase for blocks handed to the decompressor, while the ignored
prefix is dropped in ThreadPoolRemoteFSReader::execute before a block is
assembled. On the buggy build it reads 66446, byte-for-byte equal to the healthy
build, so no threshold on it can fail on the over-read: not < 1000000, not
< 100000, not the exact 66446 the reference used to pin.

Assert AsynchronousReaderIgnoredBytes = 0 instead: this query seeks each column
once from offset 0, so a correct lazy ignore never triggers. Add
RemoteFSSeeks > 0 and pin remote_filesystem_read_method so the check cannot pass
vacuously on the synchronous path, which has no async buffer at all (that path
reports RemoteFSSeeks 0 and ignored 0 even on the buggy build). no-random-settings
already stops the runner selecting it, so this guards against the default
changing, the tag being dropped, or the test being edited onto that path.
ReadCompressedBytes < 1000000 is kept as a granule-selection bound with a comment
that no longer overstates it; ReadBufferFromS3Bytes stays dropped as the unstable
raw-transfer count.

Verified both directions with Build IDs recorded: healthy build prints 1 1 1 1
10/10 and 15/15 on a fresh database; with the two guards removed it prints
1 0 1 1 10/10, so only the new column reacts. AsynchronousReaderIgnoredBytes is 0
across the regimes that destabilized the old assertion - repeat reads, prefetch
off, method read, max_threads 1 and 16, remote_read_min_bytes_for_seek 0,
parallel replicas, larger prefetch steps and read buffer.
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 2cf39d3

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
Stateless tests (amd_tsan, s3 storage, parallel, 1/3) / Lost s3 keys Harness allowlist gap, not a product bug. The stateless log scanner in ci/jobs/scripts/clickhouse_proc.py flags a benign CacheMetadata: Error during background download ... Code: 499 ... The specified key does not exist line, which the stress harness already allowlists in tests/docker_scripts/stress_tests.lib. The filesystem-cache background-download worker prefetches a partially-downloaded segment whose S3 object was concurrently removed; the 499 is caught locally, the segment is marked download-failed and the data is re-read directly. Nothing is lost. 30-day breadth: 2 unrelated PRs plus a master hit on the same check. #112157 (external, open)
Stateless tests (amd_tsan, s3 storage, parallel, 1/3) / S3_ERROR No such key thrown (in clickhouse-server.log or clickhouse-server.err.log) Same log line as the row above: the two scanner checks run byte-identical greps and always fire together. One event. #112157 (external, open)
CH Inc sync - CH Inc sync (private, not actionable)

The job is otherwise clean: no test failed, only these two log-scanner pseudo-tests.

Session id: cron:our-pr-ci-monitor:20260728-090000

@groeneai groeneai added the groeneai-origin-unknown PR origin: unclassified (legacy task) label Aug 19, 2026
@abashkeev abashkeev added the comp-testing Test content only (no production code). label Sep 4, 2026
@PedroTadim PedroTadim self-assigned this Sep 17, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@groeneai is this pr still relevant?

@groeneai

Copy link
Copy Markdown
Collaborator Author

Yes, still relevant, and it applies to master unchanged (the two test files only, no conflicts). The two source predicates the measurement in the description rests on are byte-identical on master today (file_offset_of_buffer_end && in AsynchronousBoundedReadBuffer::seek, !current_buf || in ReadBufferFromRemoteFSGather::isSeekCheap), so the coverage argument still holds: the pinned reference cannot fail on the prefix over-read this test was written for, and the AsynchronousReaderIgnoredBytes column added here does.

The flakiness pressure is lower than it was in June, and I would rather say so than oversell it: on master this test has 12991 OK rows and 0 failures in the last 30 days, and the last master failure was 2026-07-26.

It still costs other people's PRs red jobs. Last 30 days, 13 failures across 3 PRs:

  • 9 rows are one unrelated PR, all printing 4 66446 67276 in nine different job flavours, with the runner's own diagnosis "Failure is reproducible" and "also fails without randomized settings". A deterministic +830 bytes on ReadCompressedBytes from a marks-reading change reddens this test everywhere; the bounds in this PR accept it.
  • 1 row is a transient extra S3 request (5 66446 66446, 62/62 reruns passed), also inside the bounds.
  • 3 rows are a different mode, empty output with no query_log row, which this PR does not address.

The branch is 28801 commits behind and untouched since 2026-07-28. I will leave it as it is unless you want a fresh CI run, in which case I will merge master.

@PedroTadim
PedroTadim added this pull request to the merge queue Sep 18, 2026
Merged via the queue into ClickHouse:master with commit 9622a4a Sep 18, 2026
178 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 18, 2026
@clickhouse-gh clickhouse-gh Bot added comp-io IO library: ReadBuffer / WriteBuffer and their implementations over files and memory, read and wr... and removed comp-testing Test content only (no production code). labels Nov 22, 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 comp-io IO library: ReadBuffer / WriteBuffer and their implementations over files and memory, read and wr... groeneai-origin-unknown PR origin: unclassified (legacy task) 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.

5 participants