Skip to content

Fix flaky test_keeper_session_loss_during_coordinated_refresh - #107413

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/flaky-refreshable-mv-keeper-loss-single-replica-isolation
Jun 14, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/flaky-refreshable-mv-keeper-loss-single-replica-isolation

Conversation

@groeneai

@groeneai groeneai commented Jun 13, 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

Stabilizes the flaky integration test
test_refreshable_mv_keeper_loss/test.py::test_keeper_session_loss_during_coordinated_refresh
(added in #104051). Test-only change; no product behavior changes.

Related: #104051

Flakiness: 8 failures across 8 unrelated PRs in 30 days, 0 on master (4036 master runs). The two observed modes:

  • assert 2 == 1 / "Expected 1 row APPENDed for the timeslot, got 2" (3 of 4 recent).
  • Code: 242 ... Table is in readonly mode during SYSTEM SYNC REPLICA (1 of 4).

Root cause (from CI server logs): the test restarted all three keeper nodes to expire the running replica's (node2) session so its running ephemeral disappears, then made node1 want to refresh the same timeslot. On a loaded runner keeper quorum reforms slowly; node2's session expired three times over ~30s while quorum reformed. node1's crash-detection grace period is a hard-coded 1.25x session_timeout (6.25s with session_timeout_ms=5000), so node1 declared node2 crashed, cleared the coordination lock, and started a duplicate refresh before node2 could reconnect and re-register. That duplicate-after-extended-outage is by design, so the assertion is stronger than what the product guarantees under a long/flapping outage. The readonly mode is node1's target table being briefly read-only right after a keeper session re-establish.

Fix (test-only):

  • Isolate only node2 from keeper with PartitionManager.drop_instance_zk_connections instead of restarting the whole cluster. Keeper stays healthy for node1, so its grace period ticks from a stable clock, and node2 reconnects promptly (~1s) after restore, well within grace. This is exactly the "brief keeper connection loss on the running replica" condition Refreshable MV: avoid duplicate refresh on brief keeper connection loss #104051 guards against. node1 still enters the grace-period wait while node2's ephemeral is missing, so the test still fails if the coordination fix is reverted (teeth preserved).
  • Use query_with_retry for SYSTEM SYNC REPLICA to tolerate a replica being briefly read-only after re-establishing its session.

Verification: 18/18 local runs pass with the change. Server logs confirm node1 takes the grace-period wait path ("refresh is running on replica '2', ... Waiting for 6250 ms ...") and node2 re-creates its ephemeral within grace, so exactly one refresh runs (no duplicate). The failing CI report this was found from: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96130&sha=566bdf29bbc637b8e193a4a89be1417799fc6dc6&name_0=PR&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%201%2F4%29

Version info

  • Merged into: 26.6.1.790

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? The original flake is rate-based (~0.03% in CI), but the failure mechanism is deterministically explained and confirmed from CI server logs (see b). Locally the fixed test path is exercised on demand: node1 reliably enters the grace-period wait while node2's ephemeral is gone, verified across 18/18 runs.
b Root cause explained? The test restarts all 3 keeper nodes to expire node2's session so its running ephemeral disappears. On a loaded runner, quorum reforms slowly (CI logs: node2's session expired 3x over ~30s). node1's crash-detection grace period is a hard-coded 1.25x session_timeout = 6.25s. node1's grace expires before node2 can reconnect and re-register, so node1 declares node2 crashed ("Replica '2' appears to have crashed... Clearing the lock"), clears refresh_running, and starts a by-design duplicate refresh -> 2 appended rows. Second mode: node1's target table briefly read-only during SYSTEM SYNC REPLICA after a session re-establish.
c Fix matches root cause? Yes. The flake is the unbounded/flapping keeper-quorum-reformation delay after a full restart. Isolating only node2 (keeper stays healthy for node1) makes node2 reconnect promptly (~1s, well within grace), removing the delay that caused node1 to wrongly take over. query_with_retry directly addresses the transient-readonly mode. No product change.
d Test intent preserved / new tests added? Preserved. node1 still enters the 1.25x session_timeout grace-period wait while node2's ephemeral is missing (confirmed in server logs: "refresh is running on replica '2', ... Waiting for 6250 ms"). This is exactly the coordination path #104051 added, so the test still fails if that fix is reverted. The "brief keeper connection loss on the running replica" condition is a purer match for the regression than a full cluster restart.
e Both directions demonstrated? Fixed test: 18/18 local pass; logs confirm exactly one refresh runs (node1: 0, node2: 1). Direction-without-fix is evidenced by the original CI failures themselves (node1 logs show the duplicate-refresh take-over and the assertion firing assert 2 == 1); the product fix (#104051) cannot be locally reverted here (submodule build constraints), but the test demonstrably still drives node1 through the grace-period decision the fix governs.
f Fix is general, not a narrow patch? N/A (test-only change, no code bug). The fix targets the test's fault-injection method, not a source guard.

Session id: cron:clickhouse-worker-slot-4:20260613-165700

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @al13n321 — could you review this? It de-flakes the coordinated-refresh keeper-loss test you added in #104051: instead of restarting the whole keeper cluster (whose slow/flapping quorum reformation on loaded runners makes node1's 6.25s crash-detection grace expire before node2 can re-register, so node1 starts a by-design duplicate), it isolates only node2 from keeper so node2 reconnects promptly within grace. node1 still takes the grace-period wait path, so the test still catches a regression of the #104051 coordination fix.

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

clickhouse-gh Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7c1b99d]

Summary: ✅


AI Review

Summary

This PR stabilizes test_refreshable_mv_keeper_loss/test.py::test_keeper_session_loss_during_coordinated_refresh by isolating only node2 from Keeper, putting node1 on the fake clock before the partition, restoring node2 as soon as node1 observes the missing running ephemeral znode, and retrying SYSTEM SYNC REPLICA. I did not find a current correctness issue worth an inline comment; the previous bot thread was addressed by the latest commit and has been resolved.

Missing context / blind spots
  • ⚠️ The direct Praktika report URL for commit 7c1b99d1 currently returned 0 test rows, and I did not run the integration test locally because this checkout has no build* directory or test binary. A successful PR integration run for test_refreshable_mv_keeper_loss would close this gap.
Final Verdict

Status: ✅ Approve

No review blockers. The revised ordering removes the prior race, and the PR discussion provides focused local validation for this test-only CI flake fix; CI should provide the final runtime signal.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 13, 2026
The test puts replica B (node2) on a fake clock so it starts a coordinated
refresh, makes node2 lose its keeper session so its 'running' ephemeral
expires, then checks replica A (node1) does not start a duplicate refresh for
the same timeslot (the guarantee added in ClickHouse#104051).

It was flaky (8 PR failures / 30d, 0 on master across 4036 master runs). Two
issues, both in the test's timing:

1. node1's crash-detection grace deadline is computed from currentTime(),
   which returns the frozen fake clock once SET FAKE TIME is applied. node1
   enters the missing-'running'-ephemeral grace path purely from watching
   node2's znodes (the check runs before the stop_requested check, so STOP
   VIEW does not gate it). The old test dropped node2's keeper connection and
   only THEN set node1's fake clock. On a loaded runner node2's ephemeral
   could expire and node1 could record running_znode_missing_since on the REAL
   clock before that SET FAKE TIME; the subsequent jump to a fake time far in
   the future then put the grace deadline in the past, so node1 immediately
   declared node2 crashed, cleared the lock and ran a duplicate refresh.

2. node2's reconnect was timed with fixed sleeps relative to the partition,
   not to when node1 actually started its grace period, leaving a load-
   dependent margin before node1's ~6.25s deadline.

Fix the test (the behavior under a genuinely long outage is by design):

- Set node1's fake clock and START its view BEFORE partitioning node2, so
  node1 always records grace-start on the frozen fake clock and its deadline
  never arrives while node2 is briefly disconnected.
- Cut only node2 off from keeper (PartitionManager) instead of restarting the
  whole keeper cluster, so keeper stays healthy for node1 and node2 reconnects
  promptly.
- Chain node2's restore to node1's "no corresponding ephemeral znode" log line
  instead of a fixed sleep, so node2 reconnects ~1s after node1 starts the
  grace period regardless of runner load.
- Use query_with_retry for SYSTEM SYNC REPLICA to tolerate a replica being
  briefly read-only right after re-establishing its keeper session.

node1 still enters the grace-period wait while node2's ephemeral is missing,
so the test still fails if the ClickHouse#104051 coordination fix is reverted. Verified
locally: 14/14 runs pass with the change; a forcing variant that deliberately
uses the old ordering deterministically reproduces node1 clearing the lock and
refreshing, confirming the root cause.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the fix/flaky-refreshable-mv-keeper-loss-single-replica-isolation branch from e33d7f8 to 7c1b99d Compare June 13, 2026 20:50
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (updated for revised timing fix)

# Question Answer
a Deterministic repro? Yes. A forcing variant using the old ordering (drop node2 -> let node1 record grace-start on the real clock -> then SET FAKE TIME) fails every run: cleared=True, node1_refreshed=True (node1 clears the lock and refreshes).
b Root cause explained? node1's grace deadline uses currentTime(), which returns the frozen fake clock once SET FAKE TIME is applied. node1 enters the missing-ephemeral grace branch in doScheduling from watching node2's znodes (before the stop_requested check). The old test dropped node2 first and set node1's fake clock second; on a loaded runner node1 could record running_znode_missing_since on the real clock, then the fake-time jump put the deadline in the past -> node1 declared node2 crashed and ran a duplicate.
c Fix matches root cause? Yes. Set node1's fake clock + START its view BEFORE partitioning node2, so grace-start is always recorded on the frozen fake clock and the deadline never arrives during a brief outage. Restore is chained to node1's "no corresponding ephemeral znode" log line, removing the load-dependent sleep margin.
d Test intent preserved / new tests added? Preserved. node1 still enters the grace-period wait while node2's ephemeral is missing, so the test still fails if the #104051 coordination fix is reverted (teeth confirmed: a forcing variant reproduces node1 clearing the lock). No assertion weakened.
e Both directions demonstrated? Yes. Fixed test: 14/14 local runs pass (1 + 3 + 10). Forcing variant with the old ordering: fails deterministically (bug reproduced).
f Fix is general, not a narrow patch? N/A (test-only timing fix, no product code change). The product behavior under a genuinely long outage is by design.

Session id: cron:clickhouse-worker-slot-2:20260613-195900

@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.80% 84.70% -0.10%
Functions 92.40% 92.30% -0.10%
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: 494 line(s), 23 function(s) across 138 file(s) · Details

Top files
  • src/Storages/RabbitMQ/RabbitMQSource.cpp: 50 line(s), 2 function(s)
  • src/Backups/BackupEntriesCollector.cpp: 29 line(s), 2 function(s)
  • src/Storages/RabbitMQ/StorageRabbitMQ.cpp: 16 line(s), 2 function(s)
  • src/Common/ZooKeeper/ZooKeeper.cpp: 15 line(s)
  • src/Interpreters/DeadLetterQueue.cpp: 13 line(s), 1 function(s)

Full report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI fully finished on HEAD 7c1b99d1 (revised single-replica-isolation + frozen-fake-clock timing fix). All test jobs pass; CIDB shows zero FAIL/ERROR for this commit.

  • Finish Workflow: success; Mergeable Check: pass; Style/Fast/Stateless/Integration/Unit/Coverage all green.
  • No PR-caused failures, no flaky/infra failures to track.
  • Only CH Inc sync remains pending (private fork-mirror sync, never completes for fork PRs; not a test).

Genuinely waiting on @al13n321 review (the bot change request on the test timing was addressed in 7c1b99d1 + replied in-thread).

Session id: cron:our-pr-ci-monitor:20260613-233000

@alexey-milovidov alexey-milovidov self-assigned this Jun 14, 2026
@alexey-milovidov
alexey-milovidov merged commit f2fc07e into ClickHouse:master Jun 14, 2026
165 of 166 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 Jun 14, 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.

3 participants