Repository navigation
Cluster: don't refresh CLUSTER SLOTS on every -READONLY from a known replica - #662
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ 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 990f751. Configure here.
990f751 to
68f7dcd
Compare
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this. The fix is small and the reasoning makes sense to me. A One question. The PR description lists "slot primary that isn't connected" as one of the cases that now skip the refresh. Before this change, a Nitpicking: the commit message on The core fix looks solid. I'd mainly like the failover/disconnected-primary question answered before this merges. |
…replica With --cluster-mode and a non-primary --read-preference, a -READONLY reply that has no primary to retry on (keyless request, no --retry-on-error, --transaction, or the slot primary not connected) made memtier call set_cluster_slots() on the rejecting connection before dropping the request. In a steady topology that is one CLUSTER SLOTS per -READONLY reply: a keyless writing EVAL marked --command-is-read sent ~20000 CLUSTER SLOTS to the replica for 20000 requests, against the comment above the branch that says -READONLY is a per-request signal and not a topology event. A refresh can only help when the slot map is wrong about the rejecting node, i.e. it still calls it a primary (the demoted-primary case). On a connection the map already calls a replica, -READONLY is the expected answer to a write, so drop the request without refreshing. The new test drives scripted cluster peers and counts the CLUSTER SLOTS they receive: 198-199 for 200 requests before, 1 after. The existing stale-primary test now also asserts that the demoted-primary path still refreshes. Fixes #651 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both tests assumed a -READONLY from a connection the slot map already calls a replica schedules a CLUSTER SLOTS refresh. Since the previous commit it does not: in a stable topology the refresh cannot change the answer, and it cost one CLUSTER SLOTS per reply. - test_cluster_failed_keys.py: the "replica" topology is stable (every map says A is the primary, B its replica), so this is not the demoted-primary case. The test's point is that --failed-keys-file does not change -READONLY recovery; it now asserts B gets no refresh with and without --failed-keys-file (was: at least one per two rejected writes), and is renamed test_readonly_from_replica_drops_without_retry_on_error. - test_cluster_slots_refresh_overlap.py (#617): the -READONLY replies now come from the peer the map calls the primary, which still refreshes, so the test keeps covering a refresh requested while one is in flight. With the #617 fix reverted it still fails (hang, "cluster slot failed", 3 CLUSTER SLOTS instead of 2). Co-Authored-By: Claude Opus 5.5 <[email protected]>
68f7dcd to
f6d2bb6
Compare
|
Replying to the automated review above, point by point. 1. Failover where the slot's primary dies, another node is promoted, and all traffic for the slot goes to our replica. Short answer: with this PR, nothing refreshes the map in that case. A reviewer reproduced it on a 9-node cluster:
So the real gap is detecting a dead idle primary connection. That is not specific to 2. Docstring nit. Done in f6d2bb6. The The branch is also rebased onto master 66b58e7. Locally, |
|
Follow-up for the dead idle primary connection gap mentioned above: #689. |

Decision: a
-READONLYwith no primary to retry on now triggers a CLUSTER SLOTS refresh only when the slot map still calls the rejecting connection a primary (the demoted-primary case). On a connection the map already calls a replica, the request is dropped without a refresh. Rate-limiting was the alternative. On a known replica the refresh cannot fix the rejected request: that replica keeps answering writes with-READONLYwhatever the map says. Before this change, those per-reply refreshes did incidentally pick up unrelated staleness elsewhere in the map (for example other shards after a failover); with this PR that is left to the normal paths (MOVED, reconnect, a-READONLYfrom a node the map calls primary), which is how a pure-GET workload already behaves.Problem
With
--cluster-modeand--read-preferenceother thanprimary, a-READONLYreply that memtier can't retry on a primary caused a CLUSTER SLOTS refresh. That covers keyless requests, runs without--retry-on-error,--transaction, and a slot primary that isn't connected. In a steady topology this sends one CLUSTER SLOTS per-READONLY. With a local 3+3 cluster and a keyless writing EVAL marked--command-is-readon--read-preference secondary, the replica ran 19990 CLUSTER SLOTS for 19992-READONLY(20000 requests). With--pipeline 16it ran 1244, about one per batch.Root cause
cluster_client::handle_response(),-READONLYbranch: whenprimary_target == UINT_MAXit always calledm_connections[conn_id]->set_cluster_slots()beforefinalize_dropped_readonly(). The comment above the branch says this should not happen: "-READONLY is a per-request signal, not a topology event".Fix
Call
set_cluster_slots()on that path only whenm_connections[conn_id]->get_role() == role_primary. That keeps the refresh for the one case where the map is provably stale: a node the map calls primary answers-READONLY. The drop and its accounting (#592) are unchanged. The only other change is a comment.Test evidence
New
test_readonly_on_replica_does_not_refreshintests/test_cluster_readonly_drop.py. Scripted peers: B is the primary for every slot, A is its replica. A--command=SET k value --command-is-read --read-preference=secondaryrun of 200 requests without--retry-on-error(so every-READONLYtakes the no-retry-target path), and the test counts the CLUSTER SLOTS each peer receives (bound: <= 3).Related cluster suites
test_read_preference_failover.pyandtest_read_preference_modes.pypass locally against a 3+3 RLTest cluster (12 PASS, 0 FAIL).The existing
staletest (A calls itself the only primary and answers-READONLY) now also asserts that A gets more than one CLUSTER SLOTS, so the demoted-primary refresh stays covered.Real cluster (redis 8.6.0, 3 primaries + 3 replicas, the issue's EVAL repro,
-t 1 -c 2 --requests 10000), counts from the replica:-READONLYUpdated tests (68f7dcd). Two scripted-peer tests asserted the old refresh and failed in the OSS-CLUSTER and Single Endpoint CI cells:
test_cluster_failed_keys.py::test_readonly_from_replica_refreshes_without_retry_on_error: its "replica" topology is stable (every map says A primary, B replica), so it is the useless-refresh case. It now asserts B gets 0 refreshes with and without--failed-keys-file(the test is about--failed-keys-filenot changing recovery), renamed..._drops_without_retry_on_error.test_cluster_slots_refresh_overlap.py(Cluster --read-preference: a second CLUSTER SLOTS sent while one is in flight wedges the connection; run hangs #617): the-READONLYreplies now come from the peer the map calls the primary, which still refreshes, so it still covers a second refresh requested while one is in flight. It asserts 2 CLUSTER SLOTS (setup + one refresh). With the Cluster --read-preference: a second CLUSTER SLOTS sent while one is in flight wedges the connection; run hangs #617 fix reverted it still fails (hang, "cluster slot failed", 3 CLUSTER SLOTS).Locally,
test_cluster_failed_keys.py,test_cluster_slots_refresh_overlap.pyandtest_cluster_readonly_drop.pypass 5/5 runs (10/10 tests each run), on the branch rebased on origin/master 66b58e7.Fixes #651
Affected versions
2.5.0 through 2.5.2 (latest) and master.
--read-preferencedoes not exist before 2.5.0.-READONLYbranch withset_cluster_slots()on the no-primary path. The first tag that contains it is 2.5.0.Note on scope: since #592, keyed
--commandrequests carrym_keyunder--retry-on-error, so they do reach the retry path. They are not affected by this-READONLYbranch.🤖 Generated with Claude Code
Follow-up for the dead idle primary connection gap: #689.