Skip to content

Send ASKING before resending a request after -ASK - #600

Merged
fcostaoliveira merged 4 commits into
masterfrom
fix/cluster-ask-asking
Oct 14, 2026
Merged

fcostaoliveira merged 4 commits into
masterfrom
fix/cluster-ask-asking

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In cluster mode with --retry-on-error, a request that gets -ASK during a slot migration never completes (#597). It is resent to the migrating node, without ASKING, and bounces until --max-retries runs out and ends as an error. With the default unlimited retries, a --requests run never finishes. In a local repro with half the keys of a slot migrated, about 53% of requests on master end as -ASK errors.

Root cause

The -ASK branch of cluster_client::handle_response() used the MOVED retry path, retry_after_redirect():

  • It routes by the slot map, which still points at the migrating node. It ignores the <host>:<port> in the -ASK reply.
  • shard_connection::replay_request() writes only the captured command bytes. Even if a resend reached the importing node, it would arrive without ASKING, and that node would answer -MOVED back.

Fix

  • Routing: new cluster_client::retry_after_ask(). It parses the target from the -ASK reply and finds the primary connection with that address and port. Three connection states are accepted:

    • connected;
    • still being established, e.g. a TLS handshake at startup;
    • disconnected but waiting on its reconnect timer, the same rule hand_off_replay_requests() uses.

    The retry waits in that connection's queue. Only when no such connection exists does it fall back to retry_after_redirect(), and that fallback is now logged at debug level. The retry goes to a primary whatever --read-preference says, because only the importing primary serves the key after ASKING.

  • Retries left over after reconnect: fill_pipeline() drains retries left in the queue once the connection is connected and set up, the same way it already handles replays. It only does this when no drain timer is pending, so --retry-backoff-ms is still respected.

  • Sending ASKING: replay_request() writes *1\r\n$6\r\nASKING\r\n immediately before the resent bytes whenever the request's m_asking flag is set.

    • Both writes go into the same output buffer, one right after the other, so ASKING applies to exactly that command at any --pipeline depth.
    • replay_request() does not clear m_asking. The flag stays set while the request is routed to the node the -ASK named, so a resend after a reconnect is preceded by ASKING again.
    • The flag is cleared when the request is routed by the slot map again: a MOVED retry through retry_after_redirect(), or a hand-off after a topology change.
  • Not counted in stats: the ASKING reply is a new internal rt_asking request type, handled like rt_discard. It is not a workload request, so it is not counted in ops, latency, bytes or routed ops, and its reply is only logged. It does not take a --rate-limiting token either, because the resend it prefixes takes its own.

  • Retry accounting: retry_after_redirect() and retry_after_ask() share it in the new enqueue_redirect_retry().

  • --transaction is unchanged. Redirects inside a MULTI block are dropped on purpose (Cluster --transaction: CLUSTER SLOTS sent inside an open MULTI wedges the main connection #569), and an ASKING sent inside an open MULTI would be queued, not applied. The ASK branch for --transaction never reaches the retry path.

Behaviour change: ASK/sec. It used to count every bounce of the same request, roughly max-retries + 1 per request on master. It now counts the redirects that actually happen, normally one per ASKed request.

Dependency met: with a password-protected cluster and --reconnect-on-error, requests resent after a reconnect used to go out before AUTH (#527). That is fixed by #613, which is now merged, and this branch is rebased onto it. The test passes --authenticate when the cluster has a password. test_ask_retry_waits_for_reconnect passes 2/2 with --oss_password.

Test evidence

New tests/test_cluster_ask_asking.py, which runs on oss-cluster only.

Setup:

  • Load memtier-1..200.
  • Put every slot of one primary that holds a key into MIGRATING/IMPORTING towards another primary.
  • MIGRATE only the even-numbered keys. The odd ones stay on the migrating node, which still serves them.

Memtier runs with --retry-on-error --max-retries=3 --failed-keys-file.

test_ask_retry_sends_asking covers built-in GET, built-in SET and --command="GET __key__" at --pipeline 1 and 16. Each run must:

  • complete all requests;
  • record ASK/sec > 0;
  • show cmdstat_asking calls > 0 on the importing node and 0 on the other two (all nodes are on 127.0.0.1, so this catches a wrong address match);
  • have no failed keys and no GET misses.

A --transaction MULTI/GET/SET/EXEC rotation must send no ASKING at all and produce no -MOVED failure.

test_ask_retry_waits_for_reconnect kills the importing node's client connections every 0.5 s during a --reconnect-on-error --reconnect-backoff-factor=1 --rate-limiting=500 --pipeline=8 --ratio=1:1 run. No request may end on a -ASK/-MOVED failure. Replays that run out of retries (connection-dropped) are allowed.

Local results (ports 20200–20219):

Binary plaintext TLS
this branch (606c168; re-run after rebasing onto master 18f8266) 6/6 pass (plus 8/8 of the reconnect test alone) 6/6 pass
previous head 5c97821 reconnect test fails 10/10 (19–326 -ASK failures per run) —
first head bd15206 — 8/10 (connection-setup race fixed in 5c97821)
origin/master fails in every case (575–782 -ASK failures per case) fails in every case

Repro from the review (3 primaries, no password, importing node's clients killed every 1 s, -n 3000 --pipeline 8 --rate-limiting 500 --ratio 1:1 --reconnect-on-error --reconnect-backoff-factor 1):

--max-retries 3 unlimited retries
previous head 5c97821 687 errors, all -ASK 15.9M retries
this branch 0 errors (2/2 runs), 1280 retries 0 errors, 1232 retries

Manual repro from #597 (--max-retries 5, 1000 requests): master ends with 535, 533 and 534 errors (GET, GET+SET, GET at pipeline 16); this branch ends with 0.

After rebasing onto master 18f8266 (#613, #615, #618, #599, #612), on one 3-shard cluster, test_cluster_ask_asking.py, test_cluster_redirect_stats.py and test_retry_on_error.py pass 26/26.

Before that rebase, these existing tests also passed:

  • standalone: test_cluster_redirect_stats.py, test_retry_on_error.py, test_reconnections.py (23/23);
  • oss-cluster: test_cluster_transaction.py, test_transaction_pipeline.py, test_retry_on_error.py, test_reconnections.py (38/38).

make format-check is clean.

Fixes #597

Affected versions

2.4.0 through 2.5.2 and master. The ASK retry came with 08cfc24 (#381, first tag 2.4.0). Confirmed by running on master, 2.5.2 and 2.4.0; 2.4.1–2.4.4 and 2.5.0–2.5.1 are derived from history.

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster redirect/retry behavior on the hot path for migrations and reconnects; incorrect ASK/ASKING pairing could still loop or mis-route, but scope is limited to --retry-on-error and is covered by new integration tests.

Overview
Fixes --retry-on-error in cluster mode when keys hit -ASK mid slot migration: retries no longer follow the MOVED path (slot map → migrating node without ASKING), which caused bounce loops or hung runs.

-ASK handling adds retry_after_ask(), which parses the importing node from the reply, queues the resend on that primary (including connecting/reconnect-pending connections), sets request::m_asking, and only falls back to retry_after_redirect() if no matching primary exists. MOVED retries clear m_asking so routing is slot-map based again; replay hand-offs do the same.

Wire behavior: replay_request() emits ASKING immediately before the captured command when m_asking is set; internal rt_asking replies are ignored for workload stats and do not consume --rate-limiting tokens. fill_pipeline() drains retries stuck on a connection that was still handshaking/reconnecting once setup completes.

Shared retry enqueue logic moves to enqueue_redirect_retry(). New oss-cluster tests in test_cluster_ask_asking.py cover GET/SET/arbitrary/transaction and reconnect while the importing node drops clients.

Reviewed by Cursor Bugbot for commit 2633c6d. 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 still needs to review this before merge.

Thanks for this, and for the detailed description. I went through the diff and the surrounding retry and reconnect code. The core fix looks right to me:

  • The ASK target is parsed from the reply, so it no longer comes from the slot map.
  • ASKING is written into the same output buffer just before the resent bytes.
  • m_asking is cleared whenever the slot map takes over routing again.

The new test also covers the bar from #364: it fails on master (575–782 -ASK failures per case) and passes here, and checking cmdstat_asking on every node is a nice touch. A few questions:

  • Drain timer vs. setup ladder. The new drain in fill_pipeline() waits for is_conn_setup_done(), but handle_retry_drain_event() (the timer path) still only checks conn_connected. retry_after_ask() now puts retries on connections that are still connecting or waiting to reconnect. With --retry-backoff-ms > 0, I think the timer could fire after the reconnect but before the AUTH/HELLO replies arrive. I believe that is still safe on the wire, because the setup commands are written first and Redis answers in order. Is that the reasoning, or should the timer path use the same check? The asymmetry is easy to trip over later.
  • Pipeline depth. The rt_asking entry goes through push_req(), so I guess it takes a pipeline slot and counts in pending responses. At --pipeline=16 during a migration, ASK resends would then use up to twice as many slots. That's probably fine for a transient state, but was it intended?
  • ASK/sec semantics. Good that you called this out. It isn't a schema change, and the old number was inflated by repeated bounces of the same request, so I don't see a less-breaking option worth taking. It may still deserve a line in the release notes for anyone comparing ASK/sec across versions.
  • Nitpicking: retry_after_ask() matches the -ASK host against get_address() as a plain string. If a cluster announces hostnames in CLUSTER SLOTS but memtier holds a resolved IP (or the reverse), the match fails and falls back to slot-map routing. You'd only see that in the debug log. Probably an edge case, but worth confirming both sides always use the same announced endpoint.

Since this changes cluster routing, it may be worth a second look from whoever owns the cluster client. Otherwise this looks good to me once the drain-timer question is answered.

@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.

Stale Bugbot comment from a previous run.

Comment thread shard_connection.cpp Resolved
Comment thread tests/test_cluster_ask_asking.py Resolved
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. I pushed 42b5262. It also fixes the red Test OSS-CLUSTER API: TCP TLS job, which turned out to be about your first point.

The CI failure (TLS). The address lookup was fine. Under TLS, the ASK reply and CLUSTER SLOTS both give 127.0.0.1:<tls-port>, and the connection is found. I added a temporary trace on the fallback to check. It showed the target as 127.0.0.1:20202 role=primary state=conn_in_progress. The connection to the importing node was still in its TLS handshake when the first -ASK came back. retry_after_ask() only accepted conn_connected, so it fell back to the slot map, which sends the request back to the migrating node. With no backoff, --max-retries=3 ran out before the handshake finished. That is why only a few requests failed (6, 3, 4, 64, 36, 64), always early in the run. Plaintext connects fast enough that it never showed up there. Locally the old head failed this way in 2 of 10 TLS runs.

Fix: accept a target that is still connecting (any state except conn_disconnected). Its retry queue can't drain before the connection is up, and the drain timer doesn't fire again. So fill_pipeline() now drains retries left in the queue once the connection is connected and set up, the same way it already handles replays. It only does this when no drain timer is pending, so --retry-backoff-ms is still respected. If the connect fails, disconnect() already moves the retry queue into the replay queue.

Your points:

  • Silent fallback: agreed. There is now a benchmark_debug_log("no live primary connection for %s; retrying by slot map"). The fallback now only happens when no primary connection exists for that host:port, or it is disconnected. A connection that is still being set up no longer counts, and that case was the actual CI failure.
  • ASKING lost on a reconnect replay: you're right, and Bugbot flagged the same thing. The rt_asking placeholder is dropped as a non-workload request on disconnect, and the request was replayed without ASKING. m_asking now stays set for the life of the request, so every resend is preceded by ASKING. That is harmless when the resend goes elsewhere: the owner serves the command as usual, and a node that neither owns nor imports the slot answers -MOVED with or without ASKING.
  • Read preference: intended. The comment above retry_after_ask() now says why: only the importing primary serves the key after ASKING, and the slot map would send the request back to the migrating node.
  • Duplicated stats tail: both retry_after_redirect() and retry_after_ask() now go through a new enqueue_redirect_retry(). I left the READONLY path as it was to keep the diff small.
  • ASK/sec skipped for the arbitrary case: there was no reason for it. --command="GET __key__" reports under Gets with ASK/sec > 0, so all cases now assert it. The test also asserts that MIGRATE moved more than 0 keys before memtier runs.

On Bugbot's "TLS cluster MIGRATE will fail": it doesn't. RLTest starts TLS cluster nodes with --tls-cluster yes, and Redis then uses TLS for the MIGRATE connection. The CI log shows 65 > 0 keys moved and ASK/sec > 0 in the TLS job. The failures there were the handshake race above, not a migration that never started.

Verification of the new head (local, ports 20200–20219):

plaintext TLS
test_cluster_ask_asking.py, 42b5262 5/5 pass 10/10 pass
same test, bd15206 (previous head) 8/8 pass 8/10 pass (2 fail with the CI signature)
same test, master e4d14e8 5/5 fail 3/3 fail

These also pass with 42b5262: test_cluster_redirect_stats.py and test_retry_on_error.py (12/12, standalone), and test_cluster_transaction.py, test_transaction_pipeline.py and test_retry_on_error.py (28/28, oss-cluster).

🤖 Generated with Claude Code

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Pushed 606c168. The branch is rebased onto master 6b6d6df. Each review point:

  1. Fallback while the importing node's connection waits on its reconnect timer (should-fix). Fixed. retry_after_ask() now also accepts a disconnected primary when is_reconnect_pending() is true, the same rule as hand_off_replay_requests(). The retry waits in that connection's queue, and the fill_pipeline() drain added in 5c97821 sends it once setup is done. If the connection gives up instead, it is in the same state hand_off_replay_requests() already accepts.
  2. Misrouting on a shared host (test). Each run now resets stats and asserts cmdstat_asking calls > 0 on the importing node and 0 on the migrating node and the third node. Under --transaction it asserts 0 everywhere. All four checks fail on master, where no ASKING is sent anywhere.
  3. _migrate_half migrated nearly everything (test). Right: with one key per slot, keys[::2] took the only key. It is now _migrate_even_keys(). It puts every key-bearing slot into MIGRATING/IMPORTING and moves only the even-numbered keys, so the odd ones stay on the migrating node, which keeps serving them. It asserts that both groups are non-empty. I avoided a hash-tag prefix because built-in key routing ignores {tag} in --key-prefix (it hashes the whole key), which would have added MOVED noise unrelated to this fix.
  4. PR body vs. m_asking. Corrected. replay_request() keeps the flag set, and the body now says so. I also applied the optional part: m_asking is cleared when the request is routed by the slot map again (retry_after_redirect(), i.e. a MOVED retry or the ASK fallback, and hand_off_replay_requests()).
  5. Nits:
    • push_req() no longer takes a --rate-limiting token for rt_asking, since the resend it prefixes takes its own.
    • The memtier timeout per run is 30 s, or 60 s for the reconnect case, which is rate-limited by design.
    • The PR body now notes the ASK/sec change: it counts redirects that actually happen, normally one per ASKed request, instead of one per bounce.

Verification of 606c168 (local, ports 20200–20219):

plaintext TLS
this branch 6/6 pass 6/6 pass
master fails every case fails every case

These also pass with 606c168:

  • standalone: test_cluster_redirect_stats.py, test_retry_on_error.py, test_reconnections.py (23/23);
  • oss-cluster: test_cluster_transaction.py, test_transaction_pipeline.py, test_retry_on_error.py, test_reconnections.py (38/38).

🤖 Generated with Claude Code

fcostaoliveira and others added 4 commits October 14, 2026 08:31
During a slot migration the migrating node answers -ASK for keys it no
longer holds, and the importing node serves such a key only to a command
sent right after ASKING. With --retry-on-error the -ASK branch used the
MOVED retry path: it resent to the slot-map owner, which is still the
migrating node, and without ASKING. The request bounced until
--max-retries ran out and ended as an error, or forever with the default
unlimited retries.

Resend to the primary connection named in the -ASK reply and write
ASKING immediately before the resent bytes, so it applies to that
command at any --pipeline depth. Its reply is consumed like DISCARD's
and is not a workload request. Without a connection to that node the
old slot-map routing is kept. --transaction still drops redirects inside
a MULTI block: ASKING there would be queued, not applied.

Fixes #597

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Over TLS the connection to the importing node can still be in its
handshake when the first -ASK arrives. retry_after_ask() only accepted
a connected target, so it fell back to the slot map, which sends the
request back to the migrating node; with no backoff the retries ran out
within the handshake. The TLS cluster CI job failed this way.

Accept a target that is still connecting. Its retry queue cannot drain
before the connection is up, and the drain timer does not fire again,
so fill_pipeline() now drains retries left behind once the connection is
connected and set up, as it already does for replays.

Keep m_asking set for the life of the request, so a resend after a
reconnect is still preceded by ASKING; ASKING changes nothing on a node
that is not importing the slot. Log the fallback to slot-map routing,
share the retry accounting with retry_after_redirect(), note why the
retry ignores --read-preference, and check ASK/sec for the arbitrary
command case in the test too.

Fixes #597

Co-Authored-By: Claude Opus 5.5 <[email protected]>
retry_after_ask() fell back to the slot map when the connection to the
importing node was down and waiting on its reconnect timer. The slot map
points at the migrating node, which answers -ASK again, so the request
bounced until --max-retries ran out (649-704 errors per run with
--max-retries 3 while that node's clients were killed every second), or
for millions of retries without a limit. Accept such a connection, as
hand_off_replay_requests() does: the retry waits in its queue, and
fill_pipeline() sends it once the connection is set up again.

Clear m_asking when the request is routed by the slot map again (a MOVED
retry or a hand-off), and don't charge ASKING a --rate-limiting token:
the resend it prefixes takes its own.

The test now kills the importing node's clients during a run, checks
with INFO commandstats that ASKING reaches the importing node only, and
moves only the even-numbered keys, so the migrating node keeps serving
the others.

Fixes #597

Co-Authored-By: Claude Opus 5.5 <[email protected]>
After the replay block was rewritten (#613), say directly which retries
the fill_pipeline() drain is for. Pass --authenticate when the test
cluster has a password, so the reconnect case also runs with AUTH.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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 2633c6d. Configure here.

Comment thread cluster_client.cpp
return false;
if (target == UINT_MAX) {
benchmark_debug_log("no live primary connection for %s; retrying by slot map\n", status ? status : "-ASK");
return retry_after_redirect(conn_id, req);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ASK retry misses reconnecting shards

Medium Severity

retry_after_ask only keeps a disconnected importing primary when is_reconnect_pending() is true. For a non-main shard, the reconnect timer clears that flag and then only requests CLUSTER SLOTS on the seed connection, so the importer stays conn_disconnected with no pending reconnect until that refresh dials it. New -ASK redirects in that window fall back to retry_after_redirect, which resends on the slot map without ASKING and can bounce until --max-retries is exhausted.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2633c6d. Configure here.

@fcostaoliveira
fcostaoliveira merged commit 94d1ad2 into master Oct 14, 2026
127 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 --retry-on-error: requests retried after -ASK are never sent with ASKING and bounce until --max-retries

1 participant