Skip to content

Route cluster --command requests by the full key argument - #616

Merged
fcostaoliveira merged 3 commits into
masterfrom
fix/command-key-hash-tag-routing
Oct 15, 2026
Merged

fcostaoliveira merged 3 commits into
masterfrom
fix/command-key-hash-tag-routing

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

#609 merged (1f923e8), and #612 (fixes #610) is in master as well. This branch is based on master ade1d56 and contains only this PR's three commits: 1b78b55 (the fix), 1caedda (the test runs from the default seed) and 2647248 (more key shapes in the test, plus the CI cell).

Problem

Outside --transaction, cluster routing for --command hashed only the generated key (memtier-N), even when the key argument has text around __key__. Redis hashes the whole argument, or only its {tag} if there is one, so these requests went to the wrong shard and got -MOVED. See #614.

Measured on master e4d14e8 as errorstat_MOVED out of 200 requests: SET {ask}-__key__ v 110, GET {ask}-__key__ 92, SET __key__{ask} v 112, SET test1:__key__ v 134. The command shape from #333 (HSET/HGET test1:__key__) sees about 67% MOVED.

Root cause

cluster_client::get_key_for_conn() called calc_hslot_crc16_cluster(m_obj_gen->get_key(), ...). The --transaction lookahead in create_arbitrary_request() already rebuilt data_prefix + key + data_suffix and hashed that with the hash-tag rule. The non-transaction path never did.

Fix

Move that rebuild into cluster_client::calc_arbitrary_key_hslot(const arbitrary_command &). Use it in the --transaction lookahead (same behaviour as before) and in get_key_for_conn() whenever --command is in use. Cluster mode already rejects commands with more than one key argument, so the first key argument is the key that is sent. Default SET/GET and --multi-key-get routing are unchanged.

Behaviour change (release notes)

For --command keys with text around __key__ (for example user:__key__ or {tag}-__key__), MOVED/sec drops to 0 and Ops/sec goes up (+19% in review). Before this change, about 10.6% of the counted "ops" were -MOVED replies that never reached the slot owner. Results for such commands are therefore not comparable with 2.2.1–2.5.2.

Test results on master + #609 + this PR

tests/test_cluster_command_key_affixes.py now has 5 tests. Each asserts 0 errorstat_MOVED on every node and 0 MOVED/sec in the JSON row:

  • {tag}-__key__, __key__:{tag} (SET) and {tag}-__key__ (GET), with a tag owned by each shard. For SET it also asserts that keys land only on the owner.
  • SET user:__key__ and GET user:__key__, no tag.
  • Two --command lines with different text around the key in one run: SET user:__key__ + GET session:__key__.
  • Key not in the first argument: OBJECT ENCODING p:__key__ and EVAL return 1 p:__key__ (numkeys 1).
  • --read-preference=secondary with GET user:__key__ --command-is-read. It also asserts that GETs reached the replicas. Without replicas it is skipped. It now runs in the "OSS-CLUSTER + replicas: read-preference (modes)" CI cell, which was added to TEST in ci.yml.

Results:

  • With replicas (--use-slaves): 5/5 pass on this branch, 5/5 fail on master 4d97c8b.
  • Re-run on master ade1d56 (includes Honour hash tags in --key-prefix when routing cluster requests #609): make check 6/6, test_cluster_command_key_affixes.py 5/5 (the replica test skips without replicas).
  • Without replicas: 5/5 (the replica test skips). test_cluster_hash_tag_key_prefix 2/2, test_mget_cluster 9/9, test_cluster_transaction 17/17, make check 6/6.
  • Earlier, on c6006e7: test_arbitrary_command_misses 16/16, test_retry_on_error 8/8, test_cluster_redirect_stats 9/9, test_monitor_cluster 3/3.

Interaction with #609 (merged; resolution applied)

#609 makes calc_hslot_crc16_cluster() tag-aware and removes calc_hslot_crc16_with_hash_tag(). Rebasing onto #609 conflicted in the --transaction lookahead block of create_arbitrary_request(). The conflict was resolved like this:

  1. Keep this PR's unsigned int hslot = calc_arbitrary_key_hslot(look); and drop Honour hash tags in --key-prefix when routing cluster requests #609's inline data_prefix/data_suffix rebuild.
  2. In calc_arbitrary_key_hslot(), rename calc_hslot_crc16_with_hash_tag to calc_hslot_crc16_cluster (2 calls).

Affected versions

2.2.1 through 2.5.2 (latest) and master. The bug was introduced by #154, which first allowed text around __key__ (master fbe4a27; backported to 2.2.1 as 9a2028b). Confirmed by running on 2.2.1, 2.3.0, 2.5.2 and master; see #614.

Related: #333 has the same command shape.

Fixes #614

Rebased onto #606 (38fae5d): #606 now declares unsigned int hslot = 0; above the --transaction lookahead loop and later uses it for m_txn_wait_slot = (int) hslot;. The conflict was resolved with an assignment, hslot = calc_arbitrary_key_hslot(look);, not a new declaration, so the outer hslot is not shadowed and #606's wait-slot logic keeps seeing the computed slot. After the rebase: make check 6/6, test_cluster_transaction.py 18/18 and test_cluster_command_key_affixes.py 5/5 on a 3-shard cluster, and a -Wshadow build reports nothing for hslot.

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster slot selection for arbitrary --command workloads (behavioral fix with benchmark comparability impact); routing logic is covered by new integration tests but touches a hot path in cluster_client.

Overview
Fixes cluster routing for --command when the key argument wraps __key__ with literals or hash tags (e.g. user:__key__, {tag}-__key__). Previously slot selection hashed only the generated key segment, so requests often hit the wrong shard and returned -MOVED; Redis hashes the full argument (or the {tag} substring).

calc_arbitrary_key_hslot centralizes building prefix + key + suffix and calling calc_hslot_crc16_cluster. It is used in get_key_for_conn for arbitrary commands and replaces duplicated logic in --transaction lookahead. Built-in SET/GET and MGET paths are unchanged.

Adds tests/test_cluster_command_key_affixes.py (hash tags, prefixes, multi-command runs, non-first key args, --read-preference=secondary) and runs it in the OSS-CLUSTER + replicas CI matrix cell alongside read-preference mode tests.

Reviewed by Cursor Bugbot for commit f997b45. Bugbot is set up for automated code reviews on this repo. Configure here.

@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 this. I read through it and it looks right to me. Pulling the --transaction lookahead logic into calc_arbitrary_key_hslot() and reusing it in get_key_for_conn() is the smaller diff, and it leaves one place where the slot gets computed instead of two that could drift apart.

A few things I checked so nobody has to redo them:

One question, not blocking: affixed keys in cluster mode now build a std::string for every request, even though create_arbitrary_request() builds the same string again right after. Most prefixes are short enough that small-string optimization means no heap allocation, so I guess it doesn't matter in practice. But did you consider caching the computed slot or the full key between the two calls, in the spirit of the "prepare the prefix and suffix up front" feedback on #154? If it doesn't show up in a profile, I'm fine leaving it.

Whoever owns cluster routing may want to take a second look at how this interacts with the #606 wait-slot rebase. The assignment rather than a new declaration looks correct to me. Otherwise I think this is good to go.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Answers point by point:

1. Keys staged for another connection. This can't happen. A cross-connection hand-off stores the command index together with the key. In get_key_for_conn() the cross-shard branch pushes a pair, key_idx_pool->push(command_index); key_idx_pool->push(*key_index);. The target drains it in cluster_client::create_request(). That function pops the stored command_index first and then calls client::create_arbitrary_request(command_index, ...) with it, so the key goes out with the same command whose affixes were used to hash it. The "pop path" at the top of get_key_for_conn() hands back only the key index. By then, create_request() has already popped the command index that belongs with it, and assert(m_key_index_pools[conn_id]->size() == pool_size - 2) checks that the pair was used together. The single-entry pushes in create_arbitrary_request(), both the available_for_conn re-push and the transaction rotation key, stay on the same connection and are consumed right away by the same command. So SET user:__key__ + GET session:__key__ keeps every key with the command it was routed for. That's why test_cluster_command_key_affixes_per_command passes, and it doesn't depend on rotation order. Measured on this branch: MOVED goes from 17006 on master to 0, and ops/sec rises 19%.

2. Heap std::string per request. It only runs when the key argument actually has affixes. The no-affix case still hashes the bare key and allocates nothing. With affixes, one short allocation per request costs much less than what it removes: on master, these requests were routed to the wrong shard and paid a MOVED round trip plus a resend, which is the 19% above. The transaction lookahead already used this exact code, so this PR moves it rather than adding new hot-path work. A KEY_BUFFER_STACK_SIZE stack buffer, or hashing {tag} straight from the affixes, is a fair micro-optimization. Doing it right needs a fallback for keys longer than the buffer and for a { in the prefix that closes in the generated part, so I'd rather do that as a follow-up than grow this fix.

Nit: command_index >= size() fallback. The guard isn't only a bounds check. The same branch also serves the built-in SET/GET path, where arbitrary_commands isn't defined and command_index is SET_CMD_IDX/GET_CMD_IDX, and that path has to hash the bare key. When arbitrary commands are defined, the index always comes from that command list, so the bound can't fail. An assert there would need its own is_defined() split for no behaviour change, so I've kept the single condition.

Other checks done in review: all 5 mutants are killed, and ASan is clean under CLIENT KILL churn.

Merge-order note: #606 also touches this function. If it lands after this PR, it needs to assign hslot = ... in the new branch and not add a second unsigned int hslot declaration.

No code changes from this review. Head stays at 2647248.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira force-pushed the fix/command-key-hash-tag-routing branch from 2647248 to edcfe0e Compare October 14, 2026 11:46
fcostaoliveira added a commit that referenced this pull request Oct 14, 2026
The per-second consistency check allowed 1/count ms of slack for the
%lld truncation of "Accumulated Latency", but "Average Latency" is
also rounded by %.3f (up to 0.0005 ms). Both fields come from the same
whole-microsecond sum, so the gap is at most 0.999/count + 0.0005.
When the true sum is just under an integer ms and the avg rounds up
(6 ops, 41.999 ms -> avg 7.000, acc 41), the gap equals 1/count and
float error made the check fail (seen in TSAN CI on PR #616).

Allow 1/count + 0.0005 plus a tiny epsilon, and add a deterministic
regression case with two buckets: the CI values (exactly on 1/count,
needs the epsilon) and 7 ops / 7.999 ms -> avg 1.143, acc 7 (gap 0.143
> 1/7, needs the rounding term).

Fixes #635

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira and others added 3 commits October 15, 2026 07:22
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]>
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]>
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]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/command-key-hash-tag-routing branch from edcfe0e to f997b45 Compare October 15, 2026 06:22
fcostaoliveira added a commit that referenced this pull request Oct 15, 2026
The per-second consistency check allowed 1/count ms of slack for the
%lld truncation of "Accumulated Latency", but "Average Latency" is
also rounded by %.3f (up to 0.0005 ms). Both fields come from the same
whole-microsecond sum, so the gap is at most 0.999/count + 0.0005.
When the true sum is just under an integer ms and the avg rounds up
(6 ops, 41.999 ms -> avg 7.000, acc 41), the gap equals 1/count and
float error made the check fail (seen in TSAN CI on PR #616).

Allow 1/count + 0.0005 plus a tiny epsilon, and add a deterministic
regression case with two buckets: the CI values (exactly on 1/count,
needs the epsilon) and 7 ops / 7.999 ms -> avg 1.143, acc 7 (gap 0.143
> 1/7, needs the rounding term).

Fixes #635

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira merged commit fd7b23c into master Oct 15, 2026
166 of 168 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 15, 2026
The per-second consistency check allowed 1/count ms of slack for the
%lld truncation of "Accumulated Latency", but "Average Latency" is
also rounded by %.3f (up to 0.0005 ms). Both fields come from the same
whole-microsecond sum, so the gap is at most 0.999/count + 0.0005.
When the true sum is just under an integer ms and the avg rounds up
(6 ops, 41.999 ms -> avg 7.000, acc 41), the gap equals 1/count and
float error made the check fail (seen in TSAN CI on PR #616).

Allow 1/count + 0.0005 plus a tiny epsilon, and add a deterministic
regression case with two buckets: the CI values (exactly on 1/count,
needs the epsilon) and 7 ops / 7.999 ms -> avg 1.143, acc 7 (gap 0.143
> 1/7, needs the rounding term).

Fixes #635

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
The per-second consistency check allowed 1/count ms of slack for the
%lld truncation of "Accumulated Latency", but "Average Latency" is
also rounded by %.3f (up to 0.0005 ms). Both fields come from the same
whole-microsecond sum, so the gap is at most 0.999/count + 0.0005.
When the true sum is just under an integer ms and the avg rounds up
(6 ops, 41.999 ms -> avg 7.000, acc 41), the gap equals 1/count and
float error made the check fail (seen in TSAN CI on PR #616).

Allow 1/count + 0.0005 plus a tiny epsilon, and add a deterministic
regression case with two buckets: the CI values (exactly on 1/count,
needs the epsilon) and 7 ops / 7.999 ms -> avg 1.143, acc 7 (gap 0.143
> 1/7, needs the rounding term).

Fixes #635

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
The per-second consistency check allowed 1/count ms of slack for the
%lld truncation of "Accumulated Latency", but "Average Latency" is
also rounded by %.3f (up to 0.0005 ms). Both fields come from the same
whole-microsecond sum, so the gap is at most 0.999/count + 0.0005.
When the true sum is just under an integer ms and the avg rounds up
(6 ops, 41.999 ms -> avg 7.000, acc 41), the gap equals 1/count and
float error made the check fail (seen in TSAN CI on PR #616).

Allow 1/count + 0.0005 plus a tiny epsilon, and add a deterministic
regression case with two buckets: the CI values (exactly on 1/count,
needs the epsilon) and 7 ops / 7.999 ms -> avg 1.143, acc 7 (gap 0.143
> 1/7, needs the rounding term).

Fixes #635

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
The per-second consistency check allowed 1/count ms of slack for the
%lld truncation of "Accumulated Latency", but "Average Latency" is
also rounded by %.3f (up to 0.0005 ms). Both fields come from the same
whole-microsecond sum, so the gap is at most 0.999/count + 0.0005.
When the true sum is just under an integer ms and the avg rounds up
(6 ops, 41.999 ms -> avg 7.000, acc 41), the gap equals 1/count and
float error made the check fail (seen in TSAN CI on PR #616).

Allow 1/count + 0.0005 plus a tiny epsilon, and add a deterministic
regression case with two buckets: the CI values (exactly on 1/count,
needs the epsilon) and 7 ops / 7.999 ms -> avg 1.143, acc 7 (gap 0.143
> 1/7, needs the rounding term).

Fixes #635

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

1 participant