Skip to content

Fix flaky test 04546_constant_join_shrink_stored_blocks - #111921

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-04546-constant-join-shrink-flaky
Jul 26, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-04546-constant-join-shrink-flaky

Conversation

@groeneai

@groeneai groeneai commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fix flaky test 04546_constant_join_shrink_stored_blocks

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

Related: #110886

04546_constant_join_shrink_stored_blocks intermittently outputs 0 instead of 1 for its middle assertion shrunk_stored_blocks.

The assertion reads the ConstantJoin "Shrinking stored blocks" line from system.text_log, filtered by a 60-second recency window (event_time >= now() - 60). text_log.event_time is the message emission time, not the flush time, so the window races the duration of the preceding SYSTEM FLUSH LOGS. On a loaded parallel runner that flush takes tens of seconds, the emitted line ages out of the window, and count() returns 0.

Measured on four failing runs. In each the shrink line was emitted and only the time predicate excluded it, and the line's age at the assertion sits at or just past the 60-second boundary:

Run SYSTEM FLUSH LOGS line age at assertion
master 693a303c1017 65.8 s ~66 s
#110886 06b7c4b127f 59.7 s ~60.4 s
#111790 101.2 s ~101 s
#111691 19338ecf1813 60.9 s ~62 s

For the master and #110886 runs the server log holds the line for the exact failing query id (ConstantJoin: Shrinking stored blocks at 06:26:18.990 and 01:55:02.893), so the shrink did happen. system.text_log dropped nothing: the failing master run logs no Queue is full for system log and its flush offsets advance continuously.

The in-place reruns point at runner load rather than at randomized settings: in the #110886 job 19 databases ran this test, 18 of them flushed in 148-420 ms, and only the failing one took 59.7 s. The master job likewise ran the test once, at 65.8 s.

Since the test reached master on 2026-07-24 this signature has hit 10 runs in CIDB: master once plus 9 unrelated PRs, 9 of them on Stateless tests (amd_tsan, parallel) and one on Stateless tests (amd_msan, WasmEdge, parallel, 1/2). Those 10 runs have a median duration of 74 s, against a few seconds for the several thousand passing runs on the same checks.

Fix: widen the window to 600 s. The per-test timeout is 600 s (clickhouse-test --timeout default, and no CI stateless job overrides it), so a flush slow enough to push the line past a 600 s window trips that timeout first; the observed worst case leaves a 6x margin. The assertion is not weakened: count() > 0 and the reference are unchanged, and rows stay scoped exactly by query_id from system.query_log filtered on log_comment and current_database = currentDatabase(), with a fresh database per test run, so the time predicate only prunes the scan. If shrinking ever regressed, the line would be absent and the test would still fail. 600 s is the common outer recency bound among the stateless tests that time-bound a system.text_log read, and the 60 s bound here was the outlier; 00177_memory_bound_merging.sh uses now() - 600 on the same text_log-via-query_id pattern.

Reports:

Version info

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

The middle assertion reads the ConstantJoin "Shrinking stored blocks" log
line from system.text_log, filtered by a 60-second recency window
(event_time >= now() - 60). text_log.event_time is the message emission
time, not the flush time, so this window races the duration of the
preceding SYSTEM FLUSH LOGS. On a loaded parallel runner the flush can take
tens of seconds, and the emitted line then falls out of the window, so
count() returns 0 instead of 1.

Measured on four failing runs, all on the parallel sanitizer jobs: the flush
took 65.8s on master sha 693a303, 59.7s on PR ClickHouse#110886 sha 06b7c4b,
101.2s on PR ClickHouse#111790 and 60.9s on PR ClickHouse#111691 sha 19338ec. In each case
the line's age at the assertion was at or just past the 60-second boundary
(66s, 60.1s, 101s, 62s), and for the master and ClickHouse#110886 runs the server log
holds the shrink line for the exact failing query id, so the shrink did
happen and only the time predicate excluded it.

Widen the window to 600s. This is safe because the per-test timeout is 600s,
so a flush slow enough to push the emitted line past a 600s window would trip
the per-test timeout first. The assertion is unchanged and never weakened:
rows are scoped exactly by query_id from system.query_log filtered by
log_comment and current_database = currentDatabase(), and CI assigns every
test run a unique database, so the time predicate only prunes the scan. This
mirrors 00177_memory_bound_merging.sh, which uses now() - 600 on the same
text_log-via-query_id pattern.

Reports:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=693a303c1017477fc39aaa7706af0ec078e13fbc&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110886&sha=06b7c4b127f65dbc45d7c26d867a4801c0742209&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review

Before opening this PR I ran an independent review of the change, across two rounds: a reviewer
session that re-derives the root cause from the CI artifacts without the author's notes, plus a second
model as a cold gate. Findings and how I resolved them:

Finding Verdict
❌ The draft description and commit message attributed a 101.2 s SYSTEM FLUSH LOGS to #110886. The artifacts for 06b7c4b127f show 59.7 s (shrink line at 01:55:02.893, assertion at 01:56:03, so the line was ~60.4 s old); 101.2 s belongs to #111790. Agreed. Corrected in the description and the commit message, with per-run figures for all four failing runs, each re-derived from the raw query_log and server logs.
❌ The description quoted "a median of 74 s against 4.7 s for the 4305 passing runs". The 74 s median and the 10-run count reproduce exactly, but the passing-run pair does not: that count grows by 100-190 rows per hour, so the figure was already stale when written and a reviewer re-running the query gets a different number. Agreed. Replaced with evidence that does not decay: in the #110886 job 19 databases ran this test, 18 flushed in 148-420 ms, and only the failing one took 59.7 s. The stable figures (74 s median over 10 runs) are kept.
⚠️ The claim that every other stateless test time-bounding a system.text_log read uses exactly now() - 600 is too broad: 02882_replicated_fetch_checksums_doesnt_match.sql ANDs an effective 120 s bound, and 02813_starting_in_text_log.sql uses a dynamic window around uptime(). Agreed, my own enumeration had missed the INTERVAL-form windows. Reworded to "600 s is the common outer recency bound, and the 60 s bound here was the outlier".
⚠️ The commit message carried no CI report URL, and the description used a bare #110886 reference rather than the full URL. Agreed. Both report URLs added in full canonical form; the description now links the full PR URL.
💡 The in-test comment restated the query below it. Agreed. Tightened to the one non-obvious invariant: the window must exceed any flush delay possible within the per-test timeout, because event_time is the emission time.
💡 Widening the window to 600 s could, in a local run with a fixed --database, match a prior run of the same test within 10 minutes. Not applicable in CI: every test run gets a fresh test_<random8> database and the query_id subquery filters on current_database = currentDatabase(). Noted, not changed; this matches the existing idiom in 00177_memory_bound_merging.sh.
💡 The two review corrections were folded into the single commit instead of being appended as follow-up commits. Kept as one commit. The branch was never pushed, so no reviewer, CI run or reader ever saw the earlier revision, and its content is fully contained in this one. Preserving it would have published the wrong 101.2 s figure for #110886 into history and then corrected it afterwards.

Verification behind the change: the root cause was confirmed on four independent failing runs (line
ages ~60.4 s / 62 s / 66 s / 101 s against a 60 s window), the shrink line was found in the server log
for the exact failing query id in the two runs where full logs were available, system.text_log
dropped nothing in those runs, and a local deterministic repro returns 0 with the 60 s window and 1
with the 600 s window on the same data. The assertion and the reference are unchanged, and the same
message logged by HashJoin has no other test asserting it.

Session id: cron:clickhouse-review-slot-11:20260725-213300

@groeneai

groeneai commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Run the CROSS JOIN with log_comment='04546_constant_join_shrink', SYSTEM FLUSH LOGS, then wait 65s (stand-in for the slow flush observed in CI) before the assertion: the 60s-window query returns 0 (reproduces the CI diff -1 +0), the 600s-window query returns 1.
b Root cause explained? Yes. The assertion filters system.text_log by event_time >= now() - 60. text_log.event_time is the message emission time, not the flush time, so the window races the duration of the preceding SYSTEM FLUSH LOGS. On a loaded parallel runner the flush takes 59.7-101.2s (verified on 4 failing runs), so the emitted "Shrinking stored blocks" line falls out of the 60s window and count() returns 0.
c Fix matches root cause? Yes. Widen the window to 600s so it exceeds any flush possible within the test's own 600s timeout. Not a band-aid: the racy invariant becomes structurally true instead of a 60s guess about runner load. The assertion itself is untouched.
d Test intent preserved / new tests added? Yes. The assertion is unchanged and the .reference is byte-identical (3 lines). It still catches "the shrink did not happen": the local repro confirms the line-presence count is exactly what is asserted. Test-only change, no new regression test needed.
e Both directions demonstrated? Yes. Same deterministic repro: 0 without the fix (60s window), 1 with the fix (600s window); the emitted-line count is 1 regardless, proving only the window excluded it.
f Fix is general across code paths? Yes. Of the 49 stateless tests reading system.text_log, 27 time-bound the read and 04546 carried the only 60s bound; 00177_memory_bound_merging.sh already uses now() - 600 on the same text_log-via-query_id pattern (the precedent). HashJoin's identical "Shrinking stored blocks" message has no text_log assertion anywhere in tests/, so there is no sibling test to fix. The part_log tests with tighter windows (01532, 03780, 03781, 03803) are a genuinely separate family and out of scope for this single-test directive.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test timing-window fix, not a code bug). Assertion semantics are unchanged (count() > 0 over query_ids scoped exactly by log_comment + current_database).
h Backward compatible? (maintainer-approved exception only) N/A (test-only; no settings, formats, or production code touched).
i Invariants and contracts preserved? Yes. The fix establishes the invariant "a log-table recency filter in a test assertion must exceed any flush delay possible within the test's own timeout". No false-pass risk: rows are scoped by query_id from query_log filtered by log_comment AND current_database = currentDatabase(), and CI uses a unique database per test run, so foreign/older runs cannot satisfy the subquery.

Session id: cron:clickhouse-impl-slot-7:20260725-184800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @antaljanosbenjamin — could you review this? It widens the system.text_log recency window in your 04546_constant_join_shrink_stored_blocks from 60 s to 600 s: event_time is the emission time, so the old window raced the preceding SYSTEM FLUSH LOGS, which took 59.7-101.2 s on the four failing runs and aged the "Shrinking stored blocks" line out. The assertion and the reference are unchanged.

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

clickhouse-gh Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c1096ff]

Summary: ✅


AI Review

Summary

This PR widens the system.text_log recency window in 04546_constant_join_shrink_stored_blocks and, in the current head, also narrows the system.query_log lookup to the newest matching query_id, which fixes the slow-SYSTEM FLUSH LOGS flake without reopening the stale-row false pass for local --database reruns. I did not find remaining correctness, coverage, or PR-metadata issues in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 26, 2026
The assertion matched every historical query_id carrying
log_comment = '04546_constant_join_shrink' in the current database. With
clickhouse-test --database the database is reused across runs, so a rerun
within the recency window could be satisfied by an earlier run's
"Shrinking stored blocks" row even when the current run shrank nothing.
Widening the window from 60 s to 600 s made that window ten times larger.

Taking only the newest matching query_id keeps the wider window doing what
it is for (absorbing a slow SYSTEM FLUSH LOGS) without broadening the match
set. CI is unaffected either way: clickhouse-test derives a fresh
test_<random8> database per run and no stateless job passes --database.

Verified against a local server, one fixed database, three consecutive
queries with the same log_comment:
  run 1 shrinks, run 2 does not (compression on keeps the stored blocks
  below the threshold) -> old subquery yields 1 (stale pass), new subquery
  yields 0; run 3 shrinks again -> new subquery yields 1, so a genuine
  regression still fails and a real shrink still passes.
  clickhouse-test --database, 3 consecutive runs: OK, OK, OK.
  clickhouse-test default mode: OK; 20 runs with randomized settings: 20/20 OK.
@alexey-milovidov alexey-milovidov self-assigned this Jul 26, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 26, 2026
Merged via the queue into ClickHouse:master with commit e60f543 Jul 26, 2026
178 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 26, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-join-shrink-flaky

Fix flaky test 04546_constant_join_shrink_stored_blocks
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.

3 participants