Skip to content

Wake peers when a replica finishes a CLUSTER SLOTS refresh - #605

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/read-pref-replica-warming-wake
Oct 14, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/read-pref-replica-warming-wake

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With --cluster-mode --read-preference secondary (also secondaryPreferred / nearest, without --read-preference-fallback=primary), connections held while a replica wasn't ready for reads could be left held for good. A --requests run then stopped short with "client(s) stopped before reaching their stop condition" (exit 1), hung until the connection-stage supervisor aborted it (exit 2), or hung outright. That happened in two cases:

  1. A replica finishes a CLUSTER SLOTS refresh (Cluster --read-preference: connections held during a replica's CLUSTER SLOTS refresh are never woken; --requests run stops short #602). With a write script marked --command-is-read (replicas answer -READONLY, and each rejection triggers a refresh), master stalls 10/10 runs. A SET marked --command-is-read (replicas answer -MOVED) stalls 2/10.
  2. A replica disconnects or fails to reconnect instead of finishing (found in review). A replica whose connection drops mid-run and whose reconnects are refused hangs the run 5/5 on master, and also on the first version of this PR.

Root cause

cluster_client::hold_pipeline() holds every connection while any replica is connected (or connecting) but not is_ready_for_reads(). A replica with a refresh in flight or a reconnect in progress is in that state. Peers were woken only when a replica finished its READONLY handshake. Neither a completed refresh nor a disconnect woke them. A held connection with nothing left to read is never called again, and the requests already queued in its m_key_index_pools are never sent.

Fix

  • shard_connection::wake_peers() calls schedule_fill() on every other connected connection. It's the loop both READONLY reply arms already had, moved into a helper. The fill is deferred to the event loop.
  • Refresh: when a replica connection commits a CLUSTER SLOTS reply, it wakes its peers. The role is read before handle_cluster_slots(), because that call can promote this connection to a primary (Cursor Bugbot).
  • Disconnect: disconnect() wakes peers for replica connections, after the existing handle_connection_disconnect() notification. That also covers the "leaving connection dead and continuing" path in attempt_reconnect(), which goes through disconnect(). Waking during teardown is harmless: disconnect() cancels a pending deferred fill, and the destructor frees it.

Tests

In tests/test_read_preference_modes.py (-t 2 -c 4 --pipeline 4, cluster with replicas):

  • test_read_preference_replica_refresh_wakes_peers: the EVAL write script with --command-is-read, plus --command=SET __key__ y, so primary connections have queued work too. --read-preference=secondary, -n 2000. Asserts exit 0 and Totals Count == 16000.
  • test_read_preference_replica_disconnect_wakes_peers: the cluster advertises one replica at a local proxy (cluster-announce-port). The proxy drops the replica's connection after 200 client writes and then refuses connections. Built-in --ratio=1:1, --reconnect-on-error, -n 2000. Asserts that the proxy dropped the connection, exit 0, and Totals Count == 16000. Skipped under TLS.
  • Both tests first wait until every node lists every replica in CLUSTER SLOTS. Right after RLTest starts the cluster, nodes can disagree for about a second. memtier then sees a topology change on every refresh and retires and reconnects connections throughout the run, which also wakes held ones and hid the bug in about 1 run in 3. The wait also covers the case where a replica's bootstrap CLUSTER SLOTS is answered empty (tracked separately).
  • Each run is capped at 60 s, so a regression fails instead of hanging the suite.

Local RLTest (--env oss-cluster --shards-count 3 --use-slaves), on top of origin/master 013cb85:

Binary refresh test disconnect test
this branch (7029fcd; 0d07017 only changes comments and test layout, 1/1 pass) 5/5 pass 5/5 pass
origin/master 013cb85 5/5 FAIL 5/5 FAIL
M1: no wake after refresh 3/3 FAIL 3/3 pass
M3: wake_peers() loop starts at i = 1 3/3 FAIL 3/3 pass
M6: wake_peers() wakes replica peers only 3/3 FAIL 3/3 pass
MD: no wake in disconnect() 3/3 pass 3/3 FAIL

The previous PR head (refresh wake only), on the older base f548ae7: refresh test 4/5 pass, disconnect test 5/5 FAIL. The whole test_read_preference_modes.py passes 10/10 with this branch.

An independent reviewer run of the disconnect test's scenario: master hung 3/3 at 93% (14904 of 16000 ops); this branch finished 3/3 with Count 16000.

Manual, local 3+3 cluster: the EVAL/SET stall above went from 10/10 and 2/10 on master to 0/10. With a proxy that drops a replica on its CLUSTER SLOTS and then refuses connections (--reconnect-on-error), master and the previous head hang 3/3 and this branch completes 3/3.

Related

Fixes #602

Affected versions

The hold/wake gap dates from ee757e7 (#456, first tag 2.5.0), which added the warming gate and the READONLY-only wake. So 2.5.0–2.5.2 and master. Up to 2.5.2 it was masked, because the stalled worker was silently restarted: 2.5.2 exits 0 but writes a garbage negative Total duration in 3/3 runs. 7a6394e (#546, master only, found by bisect) made it fail visibly. Confirmed by running on master and 2.5.2; 2.5.0–2.5.1 derived from history.

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster read-preference pipeline gating and wake timing on replica topology/disconnect paths; incorrect wakes could affect throughput or ordering, but scope is limited to secondary read modes and deferred fills.

Overview
Fixes hangs under --read-preference secondary modes when hold_pipeline() blocks all connections while a replica is not ready for reads (#602).

Adds shard_connection::wake_peers() to schedule_fill() on every other connected peer (same logic previously duplicated on READONLY success/failure). Peers are now woken when a replica finishes a committed CLUSTER SLOTS refresh (role captured before handle_cluster_slots() can promote it) and when a replica disconnects (after routing is notified). READONLY paths call the helper instead of inline loops.

Tests: two cluster RLTests in test_read_preference_modes.py—replica slot-map refresh via EVAL-as-read + SET, and a dropping TCP proxy on a replica with --reconnect-on-error—both assert the run completes with full request counts; workloads wait for consistent CLUSTER SLOTS and use short timeouts to catch regressions.

Reviewed by Cursor Bugbot for commit 0d07017. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this, the root-cause write-up and the mutation table make it easy to follow. Moving the two READONLY loops into wake_peers() is a nice cleanup too.

I went through the teardown paths, since disconnect() now has a side effect on peers. In cluster_client::disconnect() and the retire loop in handle_cluster_slots(), a replica that disconnects early in the loop schedules a fill on peers that the same loop then disconnects or deletes. As far as I can tell that's covered: disconnect() cancels the pending deferred fill and the destructor frees the timer, as the description says. ~shard_connection() doesn't call disconnect(), so ~client() can't walk peers that were already freed. Reading the role before handle_cluster_slots() also looks right.

A couple of small things, neither blocking:

  • _wait_for_replicas_in_cluster_slots() sums len(entry) - 3 over every slot range and expects it to equal the number of replicas. I think that only holds while each shard owns a single contiguous range, which is RLTest's default layout. If a node ever has more than one range, the wait would time out instead of passing. Would it be more robust to collect the distinct replica node IDs/ports from the entries?
  • Nitpicking: the title still only mentions the refresh case, but the PR now also wakes peers on a replica disconnect. It might be worth updating the title so the squash commit reflects both.

The fix itself looks solid to me. Since this is cluster routing / hold_pipeline territory, a second look from whoever owns that path would still be good.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b3c2521. Configure here.

Comment thread shard_connection.cpp Resolved
Under --read-preference=secondary (with GETs in the ratio),
secondaryPreferred or nearest, hold_pipeline() holds every connection
while any replica is connected but not ready for reads. A replica with a
CLUSTER SLOTS refresh in flight is in that state. Peers were woken when a
replica finished its READONLY handshake, but not when it finished a
refresh. A held connection with nothing left to read then stayed idle
with requests queued in its key index pool, and a --requests run stopped
short ("client(s) stopped before reaching their stop condition").

Wake peers when a replica connection commits a CLUSTER SLOTS refresh, and
move the wake loop the READONLY paths already used into wake_peers().

Fixes #602

Co-Authored-By: Claude Opus 5.5 <[email protected]>
hold_pipeline() holds every connection while a replica is connected (or
reconnecting) but not ready for reads. The previous commit wakes them
when a replica finishes a CLUSTER SLOTS refresh. A replica connection
that drops or fails to reconnect instead leaves that state too, and
nothing woke them then either, so a run whose replica went away during a
refresh or a reconnect attempt hung. disconnect() now wakes peers for
replica connections, which also covers the "leaving connection dead and
continuing" path.

handle_cluster_slots() can also promote this connection to a primary, so
the refresh handler now checks the role it had before the reply.

Tests: the refresh test now mixes in a SET so that primary connections
have queued work too, waits until every node lists every replica in
CLUSTER SLOTS before it starts, and checks Totals Count. A new test
advertises one replica at a proxy that drops it mid-run and refuses
reconnects, and checks that the run completes with the requested count.

Fixes #602

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/read-pref-replica-warming-wake branch from 7029fcd to 0d07017 Compare October 14, 2026 07:21
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Point by point, against head 0d07017:

  1. Does the disconnect test fail without the disconnect() wake? Yes. The mutant table in the description now covers both tests. test_read_preference_replica_disconnect_wakes_peers fails 5/5 on master 013cb85 and 3/3 with only the disconnect() wake removed (refresh wake kept). It also failed 5/5 on the first commit alone (refresh wake only, older base). It passes 5/5 with the branch, and the refresh-only mutants (M1/M3/M6) pass it. So it catches the disconnect case specifically. It uses built-in SET/GET for that reason: with the EVAL workload, other replicas' refresh wakes rescued the held connections, and it caught the missing wake only 3/5.

  2. Teardown / retirement. Confirmed. wake_peers() only arms each connected peer's zero-delay deferred-fill timer (schedule_fill()), so no fill runs until control returns to the event loop. In cluster_client::connect(), the disconnect loop and the deletes run back to back without returning to the loop. Each later peer's own disconnect() cancels the timer, and the destructor frees it. On retirement inside handle_cluster_slots(), a fill armed on a connection that is then disconnected is cancelled the same way. schedule_fill() ignores peers that aren't connected, and a double wake coalesces because a pending timer isn't re-armed.

  3. Throughput. Not measurably, but this box is shared and noisy. -t 2 -c 4 --pipeline 4, 5 runs each:

    • EVAL -READONLY storm (one refresh per rejection, --read-preference-fallback primary, so master doesn't stall): master 126k–270k ops/sec, branch 135k–216k.
    • Control with built-in GET/SET, where the new code never runs after bootstrap: master 282k–383k, branch 269k–340k.

    The spreads overlap, and the control varies as much as the storm case does. Per refresh or disconnect it's one evtimer_pending() check per peer, plus at most one deferred fill per peer and loop iteration. Since Record the key of --command requests in --failed-keys-file #599, -READONLY with --retry-on-error or --failed-keys-file reroutes by key and doesn't refresh at all, so the per-rejection storm is limited to runs that set neither.

  4. after=200. The test waits until every node lists every replica before it starts, so the proxied replica gets its share of reads from the start. A run of 16,000 requests sends far more than 200 writes to it, and the drop happened in every run so far. If routing to that replica ever got that sparse, the "never dropped" assertion would fail loudly rather than pass silently, which is the behaviour I want. I left a constant rather than tying it to requests.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit f1cb946 into master Oct 14, 2026
145 of 146 checks passed
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.

Cluster --read-preference: connections held during a replica's CLUSTER SLOTS refresh are never woken; --requests run stops short

1 participant