Skip to content

Fix rare false failure of RANDOMIZE FOR check in 02932_refreshable_materialized_views_2 - #133657

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-02932-randomize-two-draws
Nov 23, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-02932-randomize-two-draws

Conversation

@groeneai

@groeneai groeneai commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #118637

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

Stateless tests (amd_asan_ubsan, db disk, distributed plan, targeted) on #118637 printed <29: randomize> 1 0: next_refresh_time of rmv_g (RANDOMIZE FOR 4 DAY 1 HOUR) was exactly the unrandomized timeslot.

The spread is applied: every finite next refresh time goes through RefreshSchedule::addRandomSpread. But system.view_refreshes.next_refresh_time is a DateTime, so a drawn offset in [0, 1 s) prints as the timeslot. With this spread that is 5733 of the 2e9 + 1 possible draws, 2.87e-6 per run. CIDB has 3 such failures since 2024-07 (tsan, arm asan, amd asan) in about 1.04M runs, against about 3.5 expected.

The change: step 29 creates a second identical view rmv_gg. Every view must be inside the window, and at least one must be off the timeslot. The window check now allows half the spread on either side, as RANDOMIZE FOR is centred on the timeslot; it allowed the full spread. A missing spread still leaves both views at the timeslot, so the check still fails; a false failure now needs both draws in [0, 1 s) (about 8e-12 per run). The output and the .reference are unchanged.

Validation on a local debug build: the old test with RANDOMIZE FOR removed from rmv_g prints <29: randomize> 1 0; the new test prints 1 1 with it removed from rmv_g only, 1 0 with it removed from both views, and 1 0 against a binary whose addRandomSpread returns when unchanged. 50/50 randomized runs pass. A binary that adds the full spread passes the old window check and fails the new one; the extreme draws -1e9 and 1e9 pass it.


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

Version info

  • Merged into: 26.12.1.336-master (included in 26.12 and later)

groeneai and others added 2 commits November 23, 2026 02:33
…terialized_views_2

Step 29 checked a single RANDOMIZE FOR draw with `next_refresh_time != expected`.
Every finite next refresh time goes through `RefreshSchedule::addRandomSpread`, but
`system.view_refreshes` reports `next_refresh_time` as a `DateTime`, so a drawn offset
in [0, 1 s) prints as the unrandomized timeslot. With RANDOMIZE FOR 4 DAY 1 HOUR that
is `r` in [-5, 5727] out of [-1e9, 1e9], i.e. 2.87e-6 per run; CIDB has 3 such
failures since 2024-07 against ~3.5 expected.

Create a second identical view `rmv_gg` and require every view to be inside the
window and at least one of them off the timeslot. A missing spread still puts both
views exactly at the timeslot, so the check still fails for that regression, while a
chance false failure now needs two sub-second offsets (~8e-12 per run). The output
lines and the reference are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eshable_materialized_views_2

`RANDOMIZE FOR` is centred on the timeslot: `RefreshSchedule::addRandomSpread`
adds `spread * r / 2'000'000` ms with `r` in [-1e9, 1e9], so the offset is at
most half the spread on either side. The step 29 window check allowed the full
spread, so an offset of up to twice the documented window passed. The extreme
draws give exactly +-174600 s, so the tighter bound never fails by chance.

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

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

Pre-publication review by an independent model (engine: codex; 2 rounds: 2 findings, then 0 on the final head) plus my own review (1 nit).

# Sev Finding Verdict Evidence / action
1 ⚠️ The <29: randomize> window check allowed the full RANDOMIZE FOR spread on each side, twice the documented window AGREE, fixed @ 8f9f81b Bound is now 3600*(24*4+1)/2; RANDOMIZE FOR is centred on the timeslot (RefreshSchedule::addRandomSpread, offset = spread * r / 2e6, r in [-1e9, 1e9]); the extreme draws land exactly on the bound
2 ⚠️ The test does not verify that the two views draw independently DISAGREE Each RefreshTask draws from thread_local_rng in its constructor and on every timeslot change (RefreshTask.cpp drawRandomness); a shared or fixed draw also passes the single-view check on master, so no detection is lost
3 💡 The comment said "an offset under a second"; only [0, 1 s) is invisible AGREE, fixed @ 8f9f81b Comment reworded

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

Session id: cron:clickhouse-review-slot-8:20261123-033600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. An offset in [0, 1 s) prints exactly like no offset (DateTime has 1 s resolution), so the old test with RANDOMIZE FOR removed from rmv_g reproduces +<29: randomize> 1 0 on every run.
b Root cause explained? Yes. Every finite next refresh time goes through RefreshSchedule::addRandomSpread; a draw with offset in [0, 1 s) (5733 of 2e9 + 1 draws, 2.87e-6) is truncated to the timeslot by system.view_refreshes, and the single-view next_refresh_time != expected check reads 0. CIDB: 3 such failures since 2024-07 in about 1.04M runs, about 3.5 expected.
c Fix matches root cause? Yes. The defect is a single-sample assertion; the step now samples two independent draws. No bound widened, no tag, no setting pinned.
d Test intent preserved / new tests added? Yes. With RANDOMIZE FOR removed from both views, and against a binary whose addRandomSpread returns when unchanged, the new test still prints <29: randomize> 1 0; against a binary that adds the full spread it prints 0 1 (the window check is half the spread on either side, as RANDOMIZE FOR is centred). Test-only change, so no new test file.
e Both directions demonstrated? Yes. Same emulated unlucky draw on rmv_g: old test 1 0 (FAIL), new test 1 1. Unmodified new test: 50/50 randomized runs OK.
f Fix is general across code paths? Yes. Checked every single-draw RANDOMIZE FOR assertion in stateless and integration tests: the bound column and the stable check cannot fail by chance, 04757_refreshable_mv_randomize_for_overflow fails only on r = 0 (5e-10), and the replicated integration tests already retry or tolerate a coincidence.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A: test-only change, no product logic.
h Backward compatible? (maintainer-approved exception only) N/A: no product, setting or format change.
i Invariants and contracts preserved? Yes. Both views are dropped at the start of step 30, so later steps see the same state; the wait loop makes both next_refresh_time non-NULL before min/max; the stable check stays on rmv_g; output lines and .reference are unchanged.

Session id: cron:clickhouse-impl-slot-7:20261123-022600

@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8f9f81b]

Summary: ✅


AI Review

Summary

PR #133657 reduces rare false failures in 02932_refreshable_materialized_views_2 by sampling two randomized views, requiring both refresh times to remain within the documented window and at least one to differ from the unrandomized timeslot. The change also tightens the window to half the spread. No actionable findings at 8f9f81b481743dde343a6896890cf1213d91df30.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors comp-refreshable-materialized-view Refreshable materialized views: scheduled/periodic refresh, refresh state machine, refresh orches... labels Nov 23, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 23, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 23, 2026
Merged via the queue into ClickHouse:master with commit c9994aa Nov 23, 2026
388 of 389 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 23, 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-refreshable-materialized-view Refreshable materialized views: scheduled/periodic refresh, refresh state machine, refresh orches... 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