Repository navigation
Fix flaky test_zookeeper_fallback_session by waiting for zoo1 to catch up before asserting fallback - #114055
Conversation
…h up test_fallback_session failed as "assert 'zoo2' == 'zoo1'" at test.py:112 on 5 unrelated PRs in the last 30 days (7 on 7 PRs over 60 days), on arm_binary, amd_asan_ubsan, amd_tsan and amd_msan. There are no master failures; one further 60-day row is on the 26.5 release branch. The test asserted a convergence the client/server contract does not promise. Since c39cac2 a Keeper refuses a reconnecting client whose last_zxid_seen exceeds that host's own applied zxid, telling it to try another server (KeeperTCPHandler.cpp:352, :356). The client carries that watermark across session restarts by design (ZooKeeper.cpp:395-398, :1539), and Coordination::ZooKeeper::connect falls through to the next priority host on a handshake failure, so a refusal by zoo1 demotes the node to zoo2. In the final phase zoo1 is a Raft follower applying the tail of the log asynchronously - the fixture sets async_replication=1 - so a node whose reconnect lands inside that window is refused by zoo1 and ends up on zoo2. It then stays there: in_order has hasOptimalNode() == false, so may_benefit_from_reconnecting is false and the ZKReconnect task that would move it back to the optimal host is never armed (ZooKeeper.cpp:236-237). A longer retry budget therefore cannot help. Wait for zoo1 to apply everything the nodes have already seen before blocking zoo3 and asserting re-convergence, which closes the multi-second catch-up window these failures came from. The invariant has to hold on two consecutive samples, so a single stale snapshot is not enough to proceed. It is still not an absolute guarantee: the cluster keeps committing and every heartbeat response carries a zxid, so a client watermark can advance between the wait and the handshake. The wait removes the window that was actually observed. srvr's Zxid is the right oracle because it and the handler's check both read KeeperStorage::getZXID() (FourLetterCommand.cpp:430 -> KeeperStorage.cpp:1279; KeeperStateMachine.cpp:1917-1921). Note this fixture's Keepers listen on 2181, not the 9181 default of keeper_utils. Every existing assertion is unchanged, so the diff is purely additive. On timeout the wait reports zoo1's applied zxid and the clients' watermark instead of degrading into the uninformative original assertion. Verified by widening the race deterministically (freezing zoo1's inbound Raft traffic): without the wait the test fails with the original signature and zoo1 logs "Refusing session as the client has seen zxid 161 while our last processed zxid is 146"; with it the test passes and logs no refusals. A control arm that keeps the helper but does not await it fails again, so the pass comes from the wait itself. Sequential runs are green: 50 on the previous revision, 10 on this one, which adds the two-consecutive-samples requirement. The helper treats a missing system.zookeeper_connection row as "not known yet" and keeps polling rather than reading it as a low watermark: the table has no row while a session has no connected host, which is exactly the state a reconnect passes through. It also samples the clients after zk_node, because a client watermark advances on every heartbeat response and one sampled earlier could already be stale. Not addressed here: a production in_order deployment can be pinned to a suboptimal Keeper the same way, since ZKReconnect is only armed when hasOptimalNode() is true. That is a Keeper connection-management change and is left to the Keeper owners.
Internal second-model review - 7 findings agreed and fixed, 4 disagreed with evidence, 1 notedIndependent review of this change by a second model over six rounds, plus my own cold
Also checked and not changed: The residual is stated rather than hidden: the wait closes the observed multi-second |
Pre-PR validation gate (a)-(i)
Also: no |
|
cc @antonio2368, could you review this? It is a test-only de-flake: since |
|
Workflow [PR], commit [3942e36] Summary: ✅ AI ReviewSummaryThis PR de-flakes Findings
Final VerdictNeeds another fix before merge; the partial-read regression is fixed, but the new wait does not make the final |
CI finish ledger - d2830d3Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
No test-level failure on this head: all 32 executed checks concluded Session id: cron:our-pr-ci-monitor:20260809-230000 |
Two defects in the wait helper added by this PR, both of which can make the de-flake itself flaky. get_zk_applied_zxid hand-rolled the 4lw exchange and read the reply with a single recv(). A 4lw reply is not guaranteed to arrive in one segment, which keeper_utils.send_4lw_cmd already documents and handles by reading until the peer closes. A short read would leave the payload without its "Zxid:" line, the oracle would report None, and the wait would burn its whole timeout while zoo1 was in fact caught up. Rather than duplicating the read loop, reuse send_4lw_cmd: it only needed to accept a bare container name, since this fixture's Keepers are addressed by name and not by instance. All existing callers pass instances, for which getattr returns the same node.name as before, so their behaviour is unchanged. The success branch also required two consecutive satisfying samples. That requirement cannot add a guarantee: the clients are read after zk_node, so client_zxid is never older than applied, and a Keeper's committed zxid only grows (KeeperStorageImpl throws on any attempt to commit a smaller one), so a pair that satisfies applied >= client_zxid keeps satisfying it. What the requirement did add was a failure mode, because the counter resets whenever a sample does not satisfy the condition: satisfying samples interleaved with a freshly advanced client watermark never reach two in a row, and the helper times out after 60s. It then printed the last sample, so the report could state that zoo1 had applied N while the clients had seen N, i.e. contradict itself. It fired that way on this PR's own head in CI. Returning on the first satisfying pair removes both the false timeout and the ambiguous message. Verified against a live three-Keeper fixture: the srvr reply parses to an integer for all three Keepers over the bare-name path, the applied zxid is monotonic across an insert (99 -> 113), and the wait returns in 0.35s.
The test breaks the nodes' zoo3 session and asserts that `in_order` balancing puts them back on zoo1. A Keeper refuses a reconnecting client whose `last_zxid_seen` exceeds its own applied zxid (`KeeperTCPHandler::receiveHandshake`), and `hasOptimalNode` returns false for `IN_ORDER`, so `may_benefit_from_reconnecting` is false, the `ZKReconnect` task is never created and the demotion is permanent for the rest of the test. If zoo1 is behind at the instant the nodes reconnect, they land on zoo2 and stay there. ClickHouse#114055 sampled "zoo1 has caught up" before the cut. That cannot be made to work: while the nodes are still connected to zoo3 they keep advancing the very quantity zoo1 must overtake, because `Coordination::ZooKeeper::receiveEvent` stores every response header with `zxid > 0` into `last_zxid_seen` and does so before the `PING_XID` branch, so a plain heartbeat raises it too. Its own check is also unreliable on its own terms: it reads the Keeper first and the clients second, so any commit landing between the two reads fails the comparison. Measured on a healthy cluster with no interference, taking those two reads 250 times at the check's own cadence and adding a third read of the Keeper after the client read: 47 rounds (18.8%) would have failed, all 47 with `applied_before < client <= applied_after`, zero real lags, and the gap always exactly 1 - the `applied 214 / seen 215` shape reported in CI. Remove the movement instead of sampling it. The second partition now cuts the nodes off every Keeper and finalizes their sessions with `SYSTEM RECONNECT ZOOKEEPER`, which joins the receiving thread so the watermark can no longer advance; zoo3's own applied zxid is then read once as a fixed target, which bounds every zxid the nodes can have seen because they are asserted to be on zoo3 immediately above; and the wait is retargeted to that constant. The final assertion moves out of the block so all three Keepers are reachable when the nodes reconnect, which strengthens it: previously zoo3 was still excluded, so `in_order` only had to prefer zoo1 over zoo2. `get_max_client_zxid` is deleted. A disconnected client has no row in `system.zookeeper_connection`, which is exactly why the target now comes from the Keeper side, and the metric that does survive a session loss, `ZooKeeperClientLastZXIDSeen`, needs `SYSTEM RELOAD ASYNCHRONOUS METRICS`, measured at 9 to 16 seconds per node while ZooKeeper is unreachable. Validated by cutting zoo1's Raft link inside its own container after moving Raft leadership away, so zoo1 keeps its container, its DNS entry and its client listener but stops applying. Under that forcing the current test fails 4 of 4 runs with `assert 'zoo2' == 'zoo1'` and the verbatim refusal in zoo1's log, while the fixed test passes under identical parameters, with `ZooKeeperClientLastZXIDSeen` read twice a second apart after the finalize loop and unchanged on all three nodes and below the target. Weakening the cut back to zoo3 alone makes the fixed test fail again. 30 of 30 sequential unforced runs pass, 33 to 42 seconds each against 54 seconds before. Related: ClickHouse#66966 Related: ClickHouse#114055 Co-Authored-By: Claude Opus 5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
test_fallback_sessionfails asassert 'zoo2' == 'zoo1'attest.py:112: 5 hits on 5 unrelated PRsin 30 days, 7 on 7 over 60 days, on four build flavours. No master hits; one further 60-day row is
on the
26.5release branch.Root cause: the test asserts a convergence the contract does not promise. Since
c39cac20ff8Keeperrefuses a reconnecting client whose
last_zxid_seenexceeds that host's applied zxid ("The clientshould try another server",
KeeperTCPHandler.cpp:352), and the client keeps that watermark acrosssession restarts, so
connectfalls through to the next host. In the final phase zoo1 is a followerapplying the log asynchronously (
async_replication=1), so a node reconnecting in that window landson zoo2 and stays:
in_orderhashasOptimalNode() == false, so theZKReconnecttask that wouldmove it back is never armed. A longer retry budget cannot help.
Fix: before blocking zoo3 and asserting re-convergence, wait until zoo1 has applied everything the
nodes have seen, closing the multi-second catch-up window these failures came from. Not absolute: the
cluster keeps committing, so a watermark can still advance between wait and handshake. On timeout the
wait reports both zxids instead of the bare
'zoo2' == 'zoo1'.srvr'sZxidis the right oracle:it and the handler's check both read
KeeperStorage::getZXID().Validated by freezing zoo1's inbound Raft traffic. Without the wait it fails with the original
signature and zoo1 logs
Refusing session as the client has seen zxid 161 while our last processed zxid is 146; with it it passes, no refusals. A control arm keeping the helper but not awaiting itfails again. Sequential runs green: 50 on the previous revision, 10 on this one.
A production
in_orderdeployment can be pinned the same way; that Keeper change is for its owners.Related: #113575, where a separate fix PR was requested.
Version info
26.8.1.1278(included in26.8and later)