Skip to content

Hold a --transaction rotation while its key's shard reconnects - #606

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/txn-repin-wrong-shard
Oct 14, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/txn-repin-wrong-shard

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With --cluster-mode --transaction, while a shard connection is reconnecting (for example after CLIENT KILL TYPE normal), every rotation whose key belongs to that shard is pinned to another shard. The keyed command gets -MOVED, and EXEC returns -EXECABORT. This happens for every such rotation for about 1s per kill. With --command-key-pattern=S each aborted rotation also leaves a hole in the key range (#603).

Root cause

The lookahead in cluster_client::create_arbitrary_request() pins a rotation to the owner of its first keyed command. When that owner was conn_disconnected, the fallback pinned the rotation to the current connection (target_conn = conn_id), which belongs to a different shard. The fallback was there so that no rotation pins to a dead connection and stalls every other connection in hold_pipeline().

Fix

Wait for the owner instead of falling back:

  • The lookahead keeps the staged key, records its slot in m_txn_wait_slot, and returns without pinning.
  • hold_pipeline() parks every connection while that slot's primary connection is disconnected.
  • The owner resumes the rotation from its own fill_pipeline() once it has reconnected. A slot map refresh that moves the slot to a live connection releases the hold too, because the owner is looked up again on each check.
  • The fallback to the current connection stays for rotations with no keyed command or no slot map yet.
  • The wait is cleared when a rotation wraps and on a full disconnect(), like the other pin state.
  • handle_cluster_slots() (the slot map refresh) wakes every live connection while a rotation waits (m_txn_wait_slot != -1), so a refresh that moves the slot to a live connection always gets some connection to re-run hold_pipeline() and resume the rotation.

test_transaction_connection_kills_do_not_hang (#569) used to require received MOVED mid-rotation in stderr. That MOVED only came from this misroute, so kills no longer produce it (and on master it was already flaky, since it depended on a kill landing mid-rotation). The test is now deterministic about the #569 path:

  • every key is {tag}__key__ with a hash tag owned by the main node, so the main connection drives every rotation and sits inside a MULTI whenever a kill of another shard makes it refresh the slot map;
  • it asserts the refresh was requested, no hang, rc 0, no cluster slot failed, and that the server saw 0 DISCARD and 0 errorstat_ERR. Sent inside the MULTI, the refresh would DISCARD it and the rotation's EXEC would fail with EXEC without MULTI.

test_transaction_waits_for_reconnecting_key_owner also uses --command-key-pattern=S on the SET and asserts the holes in the sequential key range are at most kills * pipeline (only transactions in flight on a killed connection may be lost).

Test evidence

New test test_transaction_waits_for_reconnecting_key_owner: MULTI/SET/EXEC with one non-main shard's clients killed every 0.5s for 8s. It asserts that the server's errorstat_MOVED and errorstat_EXECABORT are 0.

runs result
origin/master e4d14e8 (reproduced again on ade1d56 by a reviewer: 6.6k–8.6k MOVED/EXECABORT per kill-loop run) 8 8/8 fail: 853–8430 MOVED and the same number of EXECABORT
this PR 13 13/13 pass

Other checks on this branch:

Manual repro from #603 (local 3-primary cluster, -t 1 -c 1 --pipeline 1, --command-key-pattern=S on the SET, 5 kills over 12s), with server stats summed over the primaries:

MOVED EXECABORT missing keys in the S range
master (3 runs) 919 / 1320 / 1823 same 921 / 1322 / 1825
this PR (2 runs) 0 / 0 0 / 0 2 / 2

The 2 keys still missing per run come from a separate bug, #604 (PR #607): a MULTI lost on a killed connection is continued on another socket and ends in EXEC without MULTI. Together with that PR, the same runs had no error replies at all, and server SET calls equaled EXEC calls.

Throughput: the rotations that were misrouted never committed, so waiting for the owner costs no committed work. I ran 4 interleaved master/PR pairs (the same repro, -t 1 -c 1, 5 kills in 12s). Committed transactions/sec (memtier's Execs Hits/sec) were 2209 / 1748 / 2641 / 4427 on master and 1144 / 2876 / 3408 / 3707 with this PR. The host was shared and under load (loadavg about 20), so the spread is mostly noise, and the medians (about 2.4k vs 3.1k) show no regression. Without kills, the new code is never reached.

A permanently stopped shard (SHUTDOWN NOSAVE 2s into an 8s run, no failover) does not hang the run. It ends at --test-time with rc 0, as master does.

Affected versions

2.4.0 through 2.5.2 (latest) and master. The fallback was introduced by f7f8983 (#390), which is first in 2.4.0.

Fixes #603

Found during the review of #583.

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster transaction pinning and global pipeline hold behavior during reconnects; incorrect wait/clear logic could stall or mis-route MULTI/EXEC, though coverage is added for kill/reconnect scenarios.

Overview
Cluster --transaction mode no longer misroutes rotations when the key owner’s shard is reconnecting. Instead of pinning the rotation to the current connection (which caused -MOVED / EXECABORT and sequential key gaps under --command-key-pattern=S), the client now records the staged key’s slot in m_txn_wait_slot, skips pinning, and parks all connections in hold_pipeline() until that slot’s primary is connected again—or a slot-map refresh moves the slot to a live connection (with an extra schedule_fill wake on topology commit).

The disconnected-owner fallback to conn_id remains only when there is no keyed command or the slot map is not ready yet. Wait state is cleared on disconnect, rotation wrap, and alongside other pin state.

Tests: test_transaction_connection_kills_do_not_hang is retargeted to keys on the main shard (hash tag) and asserts slot-map refresh mid-run instead of MOVED; helpers and _run_with_shard_kills gain extra_args / per-command -- options. New test_transaction_waits_for_reconnecting_key_owner checks zero MOVED/EXECABORT and bounded sequential key holes during shard kills.

Reviewed by Cursor Bugbot for commit 4317558. 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. I went through the cluster_client changes and the test rework, and I have a couple of comments below.

The fix looks right to me. A rotation pinned to a non-owner can only end in MOVED/EXECABORT, so waiting for the owner costs no committed work. Clearing m_txn_wait_slot in disconnect() and on rotation wrap matches how the other pin state is handled. I also like that the owner is looked up again on each hold_pipeline() check, so a slot map refresh can release the wait without a separate code path. The test evidence is good too: the new test fails 8/8 on master, and the #569 mutant check shows the rewritten kill test still catches the original wedge, not just this bug.

  1. I wonder about a shard that stays down for a long time with no failover, using a random key pattern rather than S. Before this change, rotations whose keys belonged to the live shards kept committing, and only the dead shard's rotations failed. Now the first rotation that picks a key on the dead shard parks every connection until that shard comes back or the slot moves. You checked that the run still exits at --test-time with rc 0, which is the important part. But in that scenario the committed throughput goes from roughly (N-1)/N to ~0 for the rest of the run. I think that's the right trade for correctness (an EXECABORT isn't useful work either), but did you consider at least logging something when the wait lasts more than a reconnect cycle or two? Otherwise a user just sees the benchmark go quiet. This is a cluster-routing behavior change, so it may be worth a second look from whoever owns that path.

  2. Just checking: the wait only holds while the owner is conn_disconnected. Once it moves to in-progress/connected but hasn't finished its setup ladder, the hold releases and the lookahead pins to the owner. I assume the existing pin path already copes with a pin that isn't ready yet (that was also true of the old code), so I don't think this is a regression. Do you confirm?

Nitpicking: _reset_discard_calls() now also resets the errorstats that the new ERR/MOVED/EXECABORT assertions depend on. A name like _reset_server_stats() would make that dependency easier to see.

The core change looks solid to me. I'd mainly like to see the long-outage behavior in (1) acknowledged before this merges.

(would file this as a comment, not a formal block)

When the shard that owns a rotation's key was disconnected, the lookahead
fell back to pinning the rotation on the current connection, which belongs
to another shard. MULTI is accepted there, the keyed command is rejected
with -MOVED and EXEC returns -EXECABORT. This repeats for every such
rotation until the shard has reconnected, about 1s per CLIENT KILL. A
3-shard run that killed one shard's clients 5 times hit 1.4k-2.2k
MOVED/EXECABORT pairs, and the topology never changed.

Wait for the owner instead. Keep the staged key and record its slot.
hold_pipeline() then parks every connection while that slot's primary
connection is disconnected. The owner resumes the rotation from its own
fill_pipeline() once it reconnects. A slot map refresh that moves the
slot to a live connection releases the hold too: update_slot_map() wakes
every live connection while a rotation waits, so one of them re-runs
hold_pipeline() against the new owner. The fallback to the current
connection stays for rotations without a keyed command or without a
slot map.

The misrouted rotations never committed, so holding costs no committed
throughput: 19.1k-21.6k committed transactions per run, against
18.8k-21.4k before.

Kills no longer produce a MOVED, so the #569 kill test could no longer
rely on "received MOVED mid-rotation", which made it flaky. It now puts
every key on the main node, so the main connection is inside a MULTI
whenever a kill makes it refresh the slot map, and asserts that no
DISCARD and no ERR reply were needed. With the #569 hold removed it
fails (3 DISCARD, 3 "EXEC without MULTI").

Fixes #603

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/txn-repin-wrong-shard branch from b9d8c2a to 4317558 Compare October 14, 2026 10:06
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Pushed 4317558, rebased on master ade1d56. Answers in order:

  1. Wakeup when a refresh moves the slot. You were right that only multi_key_get > 0 || retired_any woke the connections. It probably worked in practice, because the connection that handles the CLUSTER SLOTS reply runs fill_pipeline() afterwards. I didn't want to depend on that, so I took your cheap option: update_slot_map() now also wakes every live connection while m_txn_wait_slot != -1. Whichever connection runs first re-checks hold_pipeline(), clears the wait because the new owner is live, and pins the rotation to that owner. There's a comment at the wakeup.

  2. A dead shard in a -n run. The hold lasts as long as the owner's connection keeps retrying, so it follows the reconnect policy:

    • With --max-reconnect-attempts=N, attempt_reconnect() gives up after N tries with "Maximum reconnection attempts (N) exceeded ..., ending this thread." and calls event_base_loopbreak(). The run fails rather than hanging.
    • With the default (0, unlimited), it waits as long as the reconnect loop does. That's what unlimited reconnect asks for. On master, a -n run in that state reached its count only through rotations that all ended in EXECABORT. They never committed, so it reported progress that didn't happen.
    • Without --reconnect-on-error, the connection error ends the thread as before.

    I checked this by reading the code, not with a run. I'd rather not add a separate timeout here. If you think unlimited plus -n needs one, it belongs in the reconnect policy, not in the transaction pin.

  3. Does the Cluster --transaction: CLUSTER SLOTS sent inside an open MULTI wedges the main connection #569 test still exercise the interleaving? It does now, and deterministically. The test puts every key on the main node ({tag}__key__, with a tag owned by the main node), so the main connection drives every rotation and is inside a MULTI almost all the time. Each kill of another shard makes it refresh the slot map. The test now also asserts that the server saw 0 DISCARD and 0 errorstat_ERR. I made setup_held_for_transaction() return false (the Cluster --transaction: CLUSTER SLOTS sent inside an open MULTI wedges the main connection #569 mutant), and the test failed with 3 DISCARD and 3 -ERR EXEC without MULTI. With the hold in place, test_cluster_transaction.py passes 18/18. This also replaces the old received MOVED mid-rotation check, which was flaky on other PRs.

  4. Unit test in cluster_transaction_test.cpp. Not added in this PR. The cluster test pins the behaviour from the server side (MOVED/EXECABORT = 0, and the holes in the sequential key range are bounded by kills * pipeline). I'm happy to add the unit test as a follow-up if a maintainer wants it.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit 38fae5d 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 --transaction: rotations for a reconnecting shard are pinned to the wrong shard (MOVED/EXECABORT storm)

1 participant