Skip to content

Route --read-preference=primary reads to a still-connecting primary (fixes cluster seed stall) - #612

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/cluster-first-shard-stall
Oct 14, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/cluster-first-shard-stall

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In cluster mode with the default --read-preference=primary, a run stalls with 0 ops and 100% CPU when the -p seed owns none of the keys being read (#610):

memtier_benchmark -p <shard that doesn't own memtier-4> --cluster-mode \
  --key-minimum=4 --key-maximum=4 --ratio 0:1

With the default -t 4 -c 50, memtier prints the 30 s "connection-stage failures" abort but still doesn't exit, because every worker is stuck at ~98% of a core. --ratio 1:1 stalls the same way. Seeding on the owning shard works.

This also blocks #609: once hash tags in --key-prefix are honoured, a tag whose slot isn't on the seed hits this stall instead of a MOVED.

Root cause

It is a busy loop inside one fill_pipeline() call, not a fill/schedule ping-pong (confirmed with gdb: fill_pipeline ← process_response of the CLUSTER SLOTS reply, looping on create_get_request → get_key_for_conn).

  • The seed's CLUSTER SLOTS reply creates the other shard connections (conn_in_progress) and then calls fill_pipeline() on the seed.
  • Under rp_primary, select_target_conn() returned UINT_MAX for a read unless the slot's primary passed is_ready_for_reads(). The owner is still connecting, so get_key_for_conn() returns not_available.
  • hold_pipeline() doesn't yield. The no-route gate skips rp_primary. The write-side gate needs no live primary at all, and the seed is live. The in-flight budget never fills.
  • So fill_pipeline() never returns to libevent, and the other connections never handle their connect events.

Writes don't stall: select_target_conn() already returns a write's slot primary while it is connecting. The write waits in that connection's key index pool, and the pool is only drained after the target's setup ladder is done.

Fix

Route rp_primary reads the same way as writes: select_target_conn() returns the slot's primary even while it is connecting. One line in cluster_client.cpp.

Behaviour change (release notes)

This is not limited to bootstrap. When a primary goes down mid-run under --read-preference=primary, reads for its slots used to be skipped silently. A read-only run kept going at full rate on the other shards (about 100k ops/sec in review), and the dead shard's keys were simply never read.

Now those reads wait in that primary's pool until it reconnects or a slot map refresh moves its slots. In the meantime the in-flight budget stalls every shard, at about 100 ops/sec. Writes already behaved this way. The new behaviour is arguably more correct, because it no longer quietly skews the keyspace, but users can see the difference, so it belongs in the release notes.

If a slot map refresh retires the connection, its queued reads are uncounted, exactly like queued writes (connect_shard_connection() and the retire path in handle_cluster_slots()).

Test

tests_oss_simple_flow.py:test_cluster_seed_owns_no_keys is cluster only. It looks up the owner of memtier-4, seeds on a different master, and runs four cases with a 20 s timeout:

  • --ratio 0:1, --ratio 1:1 and --command="GET __key__", each with -t 1 -c 1 --requests 200. These assert Totals.Count == 200.
  • --ratio 0:1 --test-time 3 with the default threads and clients. This asserts Count > 0. Without --requests, only the in-flight budget in hold_pipeline() bounds what the seed queues for the owner.

Every case also asserts MOVED/sec == 0.

Latest runs on 0f97f5f:

binary result
origin/master (f1b3087) 0/3 passes. All four cases time out in every run.
this branch 5/5 passes (~10 s per test run).
this branch with the gate disabled (if (false && in_flight >= in_flight_budget)) 0/3 passes. Only get_test_time fails, with a timeout. The --requests cases pass, which is why the case was added.

Earlier runs of the two-case version on 6018f0d:

Local 3-shard OSS cluster, Redis 8.6.0:

binary result
origin/master 0/3 passes. Both ratios hit the 20 s timeout in every run (~43 s per test run).
this branch 15/15 passes (~5 s per test run), and again after the rebase onto 1ef64ef.

Manual repro with default -t 4 -c 50 --test-time 5: master is still running at 60 s (killed), this branch finishes in 6.1 s with ~94k ops/sec.

Regression suites on this branch, all passing:

  • No replicas: tests_oss_simple_flow.py 28/31 in the full run, and the other 3 pass when run on their own (see below); test_mget_cluster.py 9/9, test_cluster_redirect_stats.py 4/4, test_cluster_transaction.py 17/17, test_reconnections.py 10/10, test_read_preference_modes.py 6/6.
  • --use-slaves: test_read_preference_modes.py 6/6, test_read_preference_failover.py 2/2, test_read_preference_mget.py 5/5, test_read_preference_transaction.py 4/4, test_cluster_failover_retry.py 1/1.

In the first full tests_oss_simple_flow.py run, 3 SET tests failed on key counts. I had accidentally started a second RLTest cluster on the same ports. Run on their own, those tests pass with both this branch and master.

Merge order

Merge this PR first, then #609 and #616. Once this lands, they can drop their seed pins.

Fixes #610

Affected versions

2.5.0 through 2.5.2 (latest) and master. Introduced by ee757e7, "feat: --read-preference" (#456). That change added the rp_primary readiness gate in select_target_conn(). First tag: 2.5.0.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

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

Thanks for the thorough write-up and the gdb trace, it made this easy to follow. The fix looks right to me: with this change rp_primary reads take the same path that cross-shard writes already take after the round-13 / round-25 changes. They get queued on the owner's pool, the drain side's is_conn_setup_done() holds them, and the in-flight budget in hold_pipeline() bounds the producer. Most of the safety argument is already in the existing comment block around the cross-shard push, so the one-line change seems justified.

A couple of questions, nothing blocking:

  • Just checking: this also changes steady-state behavior, not only bootstrap. When a primary drops mid-run, rp_primary reads used to get not_available (and only the write-side "no live primary" gate could yield). Now they queue on the disconnected primary and call set_cluster_slots() on the producer for every key, the same way writes already do. I think that's fine, since test_read_preference_failover.py and test_cluster_failover_retry.py pass under --use-slaves. Can you confirm that a primary whose pool fills up to KEY_INDEX_QUEUE_MAX_SIZE while it's down still gets through the retire/uncount path cleanly, and that reads don't just pile up behind it? This may be worth a second look from whoever owns cluster routing.
  • Is the comment above the first gate in hold_pipeline() (around line 1771, "The gate triggers for any non-primary read preference…") still accurate now that rp_primary reads can't return UINT_MAX except on an empty topology? It's probably fine as written, but it might be worth a short note so the next reader doesn't think rp_primary reads can still spin there.

Nice that the test runs --test-time with the default -t 4 -c 50 as well as the --requests cases. Without that case the in-flight budget wouldn't really be tested, and the MOVED/sec == 0 check is a good extra guard. It's also a real fails-then-passes regression test, in line with what PR#364 did.

Overall this looks good to me. I'd just like the steady-state question answered before merge.

fcostaoliveira and others added 2 commits October 14, 2026 04:32
In cluster mode the -p seed connects first, and its CLUSTER SLOTS reply
creates the other shard connections. If every read key belongs to another
shard, select_target_conn() returned UINT_MAX for it under rp_primary,
because that primary was still connecting. get_key_for_conn() then
returned not_available, and hold_pipeline() didn't yield: the no-route
gate skips rp_primary, and the write-side gate needs no live primary at
all. So the seed's fill_pipeline() looped forever without returning to
libevent. The other connections never finished connecting, and the run
sat at 0 ops and 100% CPU. --test-time and the connection-stage abort
couldn't stop the thread either.

Writes already go to the slot's primary while it connects: they wait in
its key index pool, which its fill_pipeline() only drains after the
setup ladder is done. Route rp_primary reads the same way. The other
read preferences keep their own bootstrap holds and are unchanged.

Fixes #610

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With --requests alone the run stops generating by itself, so the test
passed even with hold_pipeline()'s in-flight budget disabled. Add a
--test-time run with the default threads and clients: with the budget
gate disabled it times out. Also cover --command=GET __key__, which
stalled the same way, and check that no request was answered with
MOVED.

Update the comments that still said reads always need a live target:
rp_primary reads now go to the slot's primary like writes, and a slot
map refresh that retires a connection uncounts its queued requests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/cluster-first-shard-stall branch from 6018f0d to 0f97f5f Compare October 14, 2026 03:40
@fcostaoliveira

fcostaoliveira commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Point by point:

  1. Mid-run primary loss: intended. It's the same path writes already take. While the primary is down, rp_primary reads now queue on its pool instead of being skipped. Each queued read calls set_cluster_slots() on the producer. That sets the producer's m_cluster_slots to setup_none. Its fill_pipeline() then sends one CLUSTER SLOTS and generates nothing more until the reply, so there is one refresh per producer, not one per read. Writes already did the same.

    Retired connections: queued reads are uncounted the same way queued writes are. The pool holds (command_index, key_index) pairs with no read/write distinction. Both connect_shard_connection() (on reconnect) and the retire path in handle_cluster_slots() clear the pool and take n/2 off m_reqs_generated. So a --requests run still finishes.

    This is a user-visible change. A read-only run used to keep going at full rate and silently skip a dead shard's keys. Now it stalls at the in-flight budget until the shard is back. I've added a "Behaviour change" section to the PR body for the release notes.

  2. Stale TOCTOU comment: fixed in 0f97f5f. It now says that writes and rp_primary reads can target a primary that is connecting or disconnected. I also fixed two related comments: the one in select_target_conn() that said reads always use the full readiness predicate, and the outdated "line ~1352" reference.

  3. next() without a default: fixed. The test now builds the list of non-owner masters and skips if it's empty.

Also added in 0f97f5f:

  • a --test-time 3 case with the default threads and clients, which fails with the in-flight budget gate disabled;
  • a --command="GET __key__" case;
  • MOVED/sec == 0 checks.

Results: master 0/3 (all four cases time out), this branch 5/5, gate mutant 0/3.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit 1274448 into master Oct 14, 2026
127 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 15, 2026
The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 15, 2026
* Route cluster --command requests by the full key argument

A --command key argument can put text around __key__, for example
'{ask}-__key__' or 'user:__key__'. Redis hashes that whole argument (only
the tag when there is one), but get_key_for_conn() hashed the generated
key alone. Outside --transaction, most such requests went to the wrong
shard and came back -MOVED (110 of 200 for 'SET {ask}-__key__ v', 134 of
200 for 'SET test1:__key__ v').

The --transaction lookahead already rebuilt data_prefix + key +
data_suffix and hashed it with the hash-tag rule. Move that into
calc_arbitrary_key_hslot() and use it in get_key_for_conn() whenever
arbitrary commands are defined. Cluster mode allows only one key argument
per command, so the first key argument is the one that is sent.

Add an OSS cluster test that runs tagged prefix, tagged suffix and
untagged prefix key arguments, for SET and GET, and asserts that no
server replied -MOVED.

Fixes #614

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

* Run the --command key affix test from the default seed

The test seeded memtier at the slot owner to avoid the first-shard stall
(#610). #612 fixed that, so seed at the first primary as the other
cluster tests do. That also covers routing from a seed that owns none of
the keys.

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

* Cover more --command key shapes in the key affix test

Add cases from the review: two --command lines with different key
affixes in one run, keys that are not the first argument (OBJECT
ENCODING, EVAL with numkeys 1), and --read-preference=secondary with
--command-is-read. The last one needs replicas, so it also asserts that
reads reached them, and it skips without replicas. Run the file in the
"OSS-CLUSTER + replicas: read-preference (modes)" CI cell as well.

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

---------

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 --read-preference=primary: run stalls at 100% CPU when the seed shard owns none of the read keys

1 participant