Skip to content

Fix flaky test_refreshable_mv_in_replicated_db - #107132

Merged
nikitamikhaylov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-refreshable-mv-in-replicated-db
Jun 11, 2026
Merged

nikitamikhaylov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-refreshable-mv-in-replicated-db

Conversation

@groeneai

@groeneai groeneai commented Jun 11, 2026 •

Copy link
Copy Markdown
Collaborator

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 chronic flakiness of test_refreshable_mv/test.py::test_refreshable_mv_in_replicated_db.

The test fails intermittently with:

Code: 36. DB::Exception: Refreshable view re.<name> (<uuid>) doesn't exist.
  InterpreterSystemQuery::getRefreshTasks()

right after a SYSTEM SYNC DATABASE REPLICA re, on SYSTEM WAIT VIEW / SYSTEM TEST VIEW.

Root cause: when a Replicated database replica applies the replicated CREATE MATERIALIZED VIEW log entry, the ZooKeeper log_ptr node is written as part of the metadata transaction that is committed inside database->createTable(), before IStorage::startup() runs. The refresh task is only registered into the RefreshSet later, in StorageMaterializedView::startup() -> RefreshTask::startup(). SYSTEM SYNC DATABASE REPLICA waits only for the log pointer, so it can return before the refresh task is registered. A following SYSTEM WAIT/TEST/REFRESH VIEW then calls getRefreshTasks(), finds nothing in the RefreshSet, and throws BAD_ARGUMENTS. The same file already documents this (system sync database replica does not guarantee a replicated object is fully initialized).

Fix is test-side only: after SYSTEM SYNC DATABASE REPLICA, wait until the view is registered (visible in system.view_refreshes) before issuing SYSTEM *VIEW commands. system.view_refreshes lists whatever is in the RefreshSet and never throws for a missing view, so it is a safe barrier. No assertions are weakened and no settings are pinned.

Reproduced deterministically by setting the existing debug server setting startup_mv_delay_ms high (it sleeps in StorageMaterializedView::startup() before registering the refresh task): the test fails reliably without this change and passes with it. Validated with the default config as well.

Version info

  • Merged into: 26.6.1.684

SYSTEM SYNC DATABASE REPLICA only waits for the DDL log pointer, which on a
Replicated database replica is committed (inside createTable) before the
table's startup() runs. A refreshable MV registers its refresh task into the
RefreshSet only in StorageMaterializedView::startup(), so SYNC can return
before the task is registered. A following SYSTEM WAIT/TEST VIEW then calls
getRefreshTasks(), finds nothing, and throws 'Refreshable view ... doesn't
exist' (BAD_ARGUMENTS).

Wait until the view is registered (visible in system.view_refreshes) after
each SYSTEM SYNC DATABASE REPLICA, before issuing SYSTEM *VIEW commands.

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. Setting the existing debug server setting startup_mv_delay_ms high (it sleeps in StorageMaterializedView::startup() before the refresh task is registered) makes SYSTEM WAIT/TEST VIEW after SYSTEM SYNC DATABASE REPLICA throw Refreshable view ... doesn't exist on every run.
b Root cause explained? Yes. On a Replicated DB replica, the ZK log_ptr node is written in the metadata transaction committed inside database->createTable(), before IStorage::startup(). A refreshable MV registers its refresh task into the RefreshSet only in StorageMaterializedView::startup() -> RefreshTask::startup(). SYSTEM SYNC DATABASE REPLICA waits only for the log pointer, so it can return before the task is registered. A following SYSTEM WAIT/TEST/REFRESH VIEW calls getRefreshTasks(), finds nothing in the RefreshSet, and throws BAD_ARGUMENTS.
c Fix matches root cause? Yes. After each SYSTEM SYNC DATABASE REPLICA, the test now waits until the view is registered (appears in system.view_refreshes) before issuing SYSTEM *VIEW commands, closing exactly the gap between log-pointer advance and refresh-task registration.
d Test intent preserved? Yes. No assertion is weakened, no setting is pinned, no no-random tags added. Only a synchronization barrier is inserted. It mirrors the pattern already documented in this same file (system sync database replica does not guarantee a replicated object is fully initialized).
e Demonstrated in both directions? Yes. With startup_mv_delay_ms=3000: FAILS without the change (getRefreshTasks() ... doesn't exist at system wait view re.a), PASSES with the change (1 passed in 122.90s). Also PASSES with the default config (1 passed in 142.94s).
f Fix is general, not a narrow patch? N/A for source (test-only change). The barrier is applied at all three post-sync SYSTEM *VIEW sites in this test, not just the single line CI happened to fail on.

Session: cron:clickhouse-worker-slot-23:20260611-014800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @al13n321 — could you review this? It fixes the chronic flaky test_refreshable_mv_in_replicated_db: SYSTEM SYNC DATABASE REPLICA only waits for the DDL log pointer (committed in createTable before startup()), so a refreshable MV's refresh task may not be registered in the RefreshSet yet when the test runs SYSTEM WAIT/TEST VIEW, giving "Refreshable view ... doesn't exist". The test now waits until the view appears in system.view_refreshes after each sync.

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

clickhouse-gh Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1d8880f]

Summary: ✅


AI Review

Summary

This PR makes test_refreshable_mv_in_replicated_db wait for a replicated refreshable materialized view to appear in system.view_refreshes after SYSTEM SYNC DATABASE REPLICA before issuing SYSTEM WAIT VIEW or SYSTEM TEST VIEW. That matches the traced registration path, where StorageMaterializedView::startup calls RefreshTask::startup, which registers the task through RefreshSet::emplace; I did not find any findings that need inline comments.

Final Verdict

Status: ✅ Approve

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

clickhouse-gh Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

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

Newly covered by added/modified tests: 270 line(s), 38 function(s) across 98 file(s) · Details

Top files
  • src/Storages/WindowView/StorageWindowView.cpp: 22 line(s), 1 function(s)
  • src/AggregateFunctions/TimeSeries/AggregateFunctionTimeseriesChanges.h: 14 line(s), 32 function(s)
  • src/Backups/BackupCoordinationStageSync.cpp: 10 line(s)
  • src/Storages/StorageReplicatedMergeTree.cpp: 9 line(s)
  • src/Functions/array/FunctionArrayMapped.h: 8 line(s)

Full report

@nikitamikhaylov nikitamikhaylov self-assigned this Jun 11, 2026
@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Jun 11, 2026
Merged via the queue into ClickHouse:master with commit 895c73c Jun 11, 2026
166 of 167 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 Jun 11, 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 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.

4 participants