Skip to content

Send CLUSTER SLOTS after MOVED even when the pipeline is full (#590) - #591

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/moved-retry-livelock
Oct 14, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/moved-retry-livelock

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In cluster mode with --retry-on-error, a connection whose node has given up its slots can loop on -MOVED forever. CLUSTER SLOTS is never sent on that connection, so every retry goes back to the node that just answered MOVED. With the default --max-retries=-1:

  • a --requests run never ends;
  • a --test-time run whose MOVED arrives after the deadline never ends either.

This was found during the build review of #584: about 2% of TLS runs of its slot-move test hung, and node errorstat_MOVED reached 29M. Details and gdb state are in #590.

Root cause

  1. handle_moved() calls set_cluster_slots() on the connection that got the MOVED.
  2. fill_pipeline() only sent setup commands from inside while (!finished() && m_pipeline->size() < m_config->pipeline).
  3. retry_after_redirect() routes the retry by the stale slot map, back to the same connection. replay_request() then resends it beyond --pipeline, up to the cluster in-flight budget of --pipeline × connections.
  4. The refresh was therefore never sent in two cases:
    • Full pipeline: the connection holds --pipeline or more requests. Each MOVED pops one request and the replay pushes it back, so the depth never drops.
    • After the deadline: --test-time is up (finished()), but the retries in flight are still being answered with MOVED.
  5. Later MOVED replies return early from handle_moved() because a refresh is already "pending".

Fix

fill_pipeline() now checks for pending setup commands before it checks pipeline depth. Once the run is finished, the loop still runs for setup commands while responses are pending, and it stops before any new request is created:

while (!finished() || (!is_conn_setup_done() && get_pending_resp() > 0)) {
    if (!is_conn_setup_done() && !(/* #569 MULTI deferral */)) { send_conn_setup_commands(now); return; }
    if (finished() || m_pipeline->size() >= m_config->pipeline) break;
    ...

A requested CLUSTER SLOTS goes out behind the requests already in flight. Once its reply arrives, the retries route to the new owner.

retry_after_redirect() still ignores the MOVED address, and replay_request() still ignores the --pipeline cap. Neither is needed to break the loop, so this PR leaves both alone.

--command workloads also need #599. Without a recorded m_key, retry_after_redirect() cannot hash an arbitrary request to its new owner, so even after the refresh those retries go back to the same shard until --max-retries.

Tests

All tests are in tests/test_cluster_redirect_stats.py and use scripted peers on ephemeral ports, with no real cluster. _SlotMoveCluster has two nodes:

  1. The leaving node answers whole batches with -TRYAGAIN until its connection is deeper than --pipeline.
  2. Between batches it switches to -MOVED, one reply at a time, waiting for each resend.
  3. Only CLUSTER SLOTS shows the slots on the other node.
Test What it checks
test_moved_refreshes_slots_on_overfull_connection --requests=2000. Run twice: MOVED one at a time, and a burst variant where the node answers CLUSTER SLOTS plus everything queued behind it in one write (needs #584, now in master). Checks exit 0, Totals Count == 2000, 0 < MOVED < 1000, and the depth > --pipeline premise.
test_moved_after_test_time_refreshes_slots --test-time=1. MOVED starts no sooner than 1.5 s, after the deadline (asserted). Checks exit 0 and 0 < MOVED < 1000.
test_pipeline_depth_stays_at_cap A batching standalone peer checks that a connection holds exactly --pipeline requests (1 and 4), so an off-by-one in the moved depth check cannot pass.

Results (local runs under RLTest):

Binary overfull (both variants) after --test-time depth cap
origin/master a388a86 0/3: hangs, 580k–624k MOVED per variant 0/3: hangs, 605k–693k MOVED pass
previous PR head f7c3038 3/3 0/6: hangs to the 30 s timeout, 470k–790k MOVED pass
> off-by-one mutant n/a n/a fail (2 == 1, 5 == 4)
this PR (56abf93) 20/20 20/20, 8–9 MOVED 10/10

With the reviewer's own peer_tt2.py (--test-time=1, nb=25 and nb=40), f7c3038 times out with 282k and 307k MOVED, and 56abf93 exits 0 with 9 and 8 MOVED. v2.5.2 and v2.4.0 hang on the overfull scenario too (checked with the first version of the test).

Locally on a 3-shard OSS cluster with 56abf93, these test files all pass: test_cluster_redirect_stats.py, test_cluster_transaction.py, test_retry_on_error.py, test_reconnections.py, tests_oss_simple_flow.py and test_mget_cluster.py. CI covers the full matrix.

Fixes #590

Affected versions

2.4.0 through 2.5.2 (latest) and master.

🤖 Generated with Claude Code


Note

Medium Risk
Changes core cluster request pumping and setup ordering in fill_pipeline(); behavior during MOVED retries and run shutdown is sensitive, though guarded by new integration tests and existing MULTI deferral logic is preserved.

Overview
Fixes an infinite -MOVED loop in cluster mode with --retry-on-error: when a connection’s pipeline stays at or above --pipeline (retries replay beyond the cap) or the run has ended (--test-time) but retries are still in flight, CLUSTER SLOTS was never sent because fill_pipeline() only ran inside the “not finished and pipeline not full” guard.

fill_pipeline() now keeps looping after finished() while setup is pending and responses are outstanding, and sends connection setup (including slot refresh) before enforcing pipeline depth—so CLUSTER SLOTS can go out behind in-flight work without issuing new workload requests once the run is done.

Adds scripted two-node slot-move tests in test_cluster_redirect_stats.py (overfull pipeline, post---test-time MOVED, burst vs trickle replies) plus test_pipeline_depth_stays_at_cap to lock normal pipeline sizing.

Reviewed by Cursor Bugbot for commit a0cae16. 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, and for the detailed root-cause write-up in #590. I went through the fill_pipeline() change and the new peers. The fix looks right to me. A few small questions:

  • I think the termination logic holds up. Once the run is finished, the loop only re-enters while setup is incomplete and get_pending_resp() > 0. With nothing pending, it falls through to the "done" tail as before, so disconnect_all() is still reachable. If the Cluster --transaction: CLUSTER SLOTS sent inside an open MULTI wedges the main connection #569 MULTI deferral skips the send, the new finished() || size() >= pipeline check breaks out instead of spinning. Do you confirm that's the intended reasoning? A one-line note in the comment saying the get_pending_resp() > 0 clause is what keeps the finished-and-idle exit reachable might help the next person who touches this loop.

  • After the deadline, what happens if the refreshed slot map names a node memtier has no connection to yet? In both new tests the new owner is already connected. A brand-new connection would get its retries through adopt_replay_request() and the replay drain on connect, so pending > 0 and the setup ladder should still run. I may be missing a path, though. If you already checked this, fine. If not, a variant where the owner isn't in the initial topology could be worth adding, or a second look from whoever owns the cluster routing code.

  • Nitpicking: the common path now calls m_conns_manager->finished() twice per created request: once in the loop header and once in the new break. fill_pipeline() runs on every response, and the comment a few lines up shows a ~4% regression from a single extra call here was enough to matter. client::finished() is cheap in the normal case, but it calls gettimeofday() in staircase mode. I wonder if it's worth reading it once at the top of each iteration.

The _SlotMoveCluster / _BatchPeer tests meet the regression-test bar well: the fix fails on master and on the earlier PR head and passes on this head. The > off-by-one mutant check is a nice touch. Also good that the PR stays out of retry_after_redirect() / replay_request() and leaves the --command side to #599. The core fix looks solid to me; the questions above are clarifications, not blockers.

(I'd file this as a comment, not a formal block.)

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Point by point:

1. Is setup_sent safe on a full pipeline? Yes. send_conn_setup_commands() only acts on states that are still setup_none. While the CLUSTER SLOTS reply is in flight the call is a no-op, so it never sends a second refresh. The early return also skips the idle check, which can't fire while m_pending_resp > 0 anyway.

2. --test-time: you were right, and it's fixed in 56abf93.

  • When --test-time is up, finished() is true but the retries in flight keep getting MOVED. The loop body never ran, so the refresh was never sent and the run looped forever, just like the full-pipeline case.

  • The loop now also runs after the deadline while a setup command is pending and responses are outstanding. It stops before creating any new request:

    while (!finished() || (!is_conn_setup_done() && get_pending_resp() > 0)) {
        if (!is_conn_setup_done() && !(/* #569 MULTI deferral */)) { send_conn_setup_commands(now); return; }
        if (finished() || m_pipeline->size() >= m_config->pipeline) break;
  • New test test_moved_after_test_time_refreshes_slots: --test-time=1, and the leaving node starts answering MOVED no sooner than 1.5 s. The test asserts that MOVED really started after the deadline.

    Binary Result
    master a388a86 0/3: hangs, 605k–693k MOVED
    previous head f7c3038 0/6: hangs, 470k–790k MOVED
    56abf93 20/20 pass, 8–9 MOVED

3. Timing windows.

  • The 0.05 s batch window only decides how fast the connection gets deeper than --pipeline. The test asserts that premise (max depth > --pipeline), so a slow runner fails loudly instead of passing without reproducing the bug.
  • The 0.1 s window is only a fallback for when memtier sends no resend.
  • The switch to MOVED now happens only between -TRYAGAIN batches, so memtier always has the whole last batch before the first MOVED.

Also added, from the build review:

🤖 Generated with Claude Code

fcostaoliveira and others added 2 commits October 14, 2026 03:08
With --retry-on-error, a node that gives up its slots answers MOVED.
handle_moved() then asks for a CLUSTER SLOTS refresh on that connection,
but fill_pipeline() only sends setup commands from inside its
"pipeline below --pipeline" loop. Retries are resent by replay_request()
beyond --pipeline, and retry_after_redirect() routes them by the stale
slot map back to the same node. Once that connection holds --pipeline
or more requests, every MOVED pops one request and the replay pushes it
back, so the depth never drops, the refresh is never sent and later
MOVED replies are ignored by handle_moved(). With the default
--max-retries=-1 the run loops on MOVED forever.

Check for pending setup commands before the pipeline-depth check, so
the refresh goes out behind the requests already in flight. Nothing
changes for a pipeline below --pipeline, and the deferral for a
pinned --transaction rotation inside an open MULTI is kept.

The test drives a scripted two-node peer: one node answers batches
with -TRYAGAIN until its connection is deeper than --pipeline, then
answers MOVED one reply at a time. Before this change the run never
finished; now it completes.

Fixes #590

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fill_pipeline() only ran its loop while the run was not finished. With
--test-time, a MOVED that arrives after the deadline still asks for a
CLUSTER SLOTS refresh, but the loop never ran, so the refresh was never
sent and the retries in flight kept bouncing on MOVED forever, just as
with a full pipeline. Once finished, keep running the loop for setup
commands while responses are pending, and stop it before any new
request is created.

Tests:
- test_moved_after_test_time_refreshes_slots: MOVED starts after
  --test-time=1 is up. It hung on the previous commit.
- test_pipeline_depth_stays_at_cap: a batching peer checks that each
  connection holds exactly --pipeline requests, which catches a
  "size() > pipeline" off-by-one in the moved depth check.
- The overfull-connection test also runs a burst variant, where the
  node answers CLUSTER SLOTS and everything queued behind it in one
  write (needs #584). It now checks Totals Count == --requests and
  bounds the MOVED count.

Refs #590

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 --retry-on-error: MOVED retries can livelock forever when the connection's pipeline is full (CLUSTER SLOTS never sent)

1 participant