Skip to content

Fix flaky test_refreshable_mv_attach_without_multi_read - #108889

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-refreshable-mv-no-multi-read-test-108441
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-refreshable-mv-no-multi-read-test-108441

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Related: #106737
Related: #100394

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Description

Fixes a flaky test_refreshable_mv_no_multi_read/test.py::test_refreshable_mv_attach_without_multi_read (added in #108441). It failed deterministically in the db disk integration shards with AssertionError: assert 'Disabled' in 'Scheduled\t\n', reported on #100394.

The test downgrades the embedded Keeper from multi_read=1 to multi_read=0 and restarts, expecting the coordinated refreshable MV to re-attach and stop gracefully (Disabled). The embedded Keeper restarts together with the server; on a loaded host it can still be applying the new feature_flags config (re-deriving the /keeper/feature_flags system znode) while it already accepts client connections. If the server's ZooKeeper client negotiates feature flags in that window, it caches the stale MULTI_READ=enabled for the whole session. RefreshTask reads MULTI_READ only once, at attach, so the view comes up healthy (coordinated) and reports Scheduled with an empty exception instead of Disabled. Fast hosts win this race; the slower db disk shards lose it consistently.

Fix is test-only: after each Keeper config swap + restart, wait until the server's live Keeper session (system.zookeeper_connection.enabled_feature_flags) actually reflects the wanted MULTI_READ state before relying on the view's state, retrying the restart if the session raced to the stale value. The graceful-stop production code path is unchanged and is still exercised (the no-multi-read attach still logs "The view is stopped").

Validated with CLICKHOUSE_USE_DATABASE_DISK=1 on a debug build: the updated test passes repeatedly, and a controlled reproduction of the old single-restart logic under the stale-flag condition reproduces the exact assert 'Disabled' in 'Scheduled\t\n' failure.

The test downgrades the embedded Keeper from multi_read=1 to multi_read=0 and
restarts, expecting the coordinated refreshable MV to re-attach and stop
gracefully (Disabled). The embedded Keeper restarts together with the server and
can still be applying the new feature_flags config (re-deriving the
/keeper/feature_flags system znode) while it already accepts client connections.
If the server's ZooKeeper client negotiates feature flags in that window it caches
a stale MULTI_READ=enabled for the whole session, and RefreshTask reads MULTI_READ
only once (at attach), so the view comes up healthy and reports Scheduled with an
empty exception instead of Disabled. Fast hosts win this race; the slower db disk
integration shards lose it deterministically (assert 'Disabled' in 'Scheduled').

Fix is test-only: after each Keeper config swap + restart, wait until the server's
live Keeper session (system.zookeeper_connection.enabled_feature_flags) reflects
the wanted MULTI_READ state before relying on the view's state, retrying the
restart if the session raced to the stale value. The graceful-stop production code
path is unchanged and is still exercised.

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

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. With CLICKHOUSE_USE_DATABASE_DISK=1, forcing the stale-flag condition (the server's Keeper session still reporting MULTI_READ at the no-multi-read attach) reproduces assert 'Disabled' in 'Scheduled\t\n' on demand, byte-identical to the CI signature.
b Root cause explained? The embedded Keeper restarts with the server; while it is still applying the new feature_flags config (re-deriving /keeper/feature_flags) it can already accept connections. If the server's ZooKeeper client negotiates flags in that window it caches stale MULTI_READ=enabled for the session. RefreshTask reads MULTI_READ only once, at attach, so the coordinated MV comes up healthy and reports Scheduled+empty exception instead of Disabled.
c Fix matches root cause? Yes. The fix waits until the server's live Keeper session (system.zookeeper_connection.enabled_feature_flags) actually reflects the wanted MULTI_READ state before relying on the view state, retrying the restart if the session raced to the stale value. This closes the exact race in (b). No assertion is weakened.
d Test intent preserved / new tests added? Preserved. The no-multi-read attach is still exercised end to end (logs still show "The view is stopped" / Disabled). The change only makes the Keeper downgrade deterministic before the assertions; all original assertions are unchanged.
e Both directions demonstrated? Yes. Old single-restart logic under the stale-flag condition FAILS with assert 'Disabled' in 'Scheduled\t\n'; the fixed test PASSES 6/6 under db disk, with logs confirming the graceful-stop path is still hit.
f Fix is general across code paths? N/A (test-only fix; no production code changed). The graceful-stop logic in RefreshTask is correct and unchanged.
g Fix generalizes across inputs? N/A (test-only fix, not a code bug). The barrier is symmetric: it is used for both the multi_read=1 and multi_read=0 restarts.
h Backward compatible? N/A (test-only fix; no settings, formats, or behavior changed).
i Invariants and contracts preserved? N/A (test-only fix). The helper only adds a wait/retry around the existing restart; it introduces no new server-side state or contracts.

Session id: cron:clickhouse-worker-slot-0:20260630-025300

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @al13n321 — could you review this? It de-flakes test_refreshable_mv_attach_without_multi_read (added in #108441), which failed deterministically in the db disk integration shards with assert 'Disabled' in 'Scheduled'. The test downgrades the embedded Keeper to no-MULTI_READ via a config swap + restart, but the server's ZooKeeper client can negotiate a stale MULTI_READ=enabled while the Keeper is still applying the new feature_flags config on restart; since RefreshTask reads the flag only at attach, the coordinated MV comes up healthy instead of stopping. The fix waits until system.zookeeper_connection confirms the session actually sees the wanted MULTI_READ state before asserting (test-only; the graceful-stop path is unchanged and still exercised).

@groeneai

Copy link
Copy Markdown
Collaborator Author

Superseding this with #108890, which fixes the same race in the production code instead of only in the test. Same root cause (the refresh task reads the Keeper MULTI_READ flag once at attach, and a view re-attached during a restart can read it before its Keeper connection settles on the downgraded Keeper). #108890 re-checks the flag in doScheduling and stops the view gracefully, so the underlying Unexpected exception in refresh scheduling abort/crash-loop is gone and the test passes without needing the Keeper-session barrier added here. Closing this in favor of #108890.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant