Repository navigation
Route cluster --command requests by the full key argument - #616
Conversation
|
🤖 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 A few things I checked so nobody has to redo them:
One question, not blocking: affixed keys in cluster mode now build a 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. |
23e6b05 to
8cbf1a8
Compare
8cbf1a8 to
6e896d9
Compare
bd09b54 to
2647248
Compare
|
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 2. Heap Nit: Other checks done in review: all 5 mutants are killed, and ASan is clean under Merge-order note: #606 also touches this function. If it lands after this PR, it needs to assign No code changes from this review. Head stays at 2647248. 🤖 Generated with Claude Code |
2647248 to
edcfe0e
Compare
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]>
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]>
edcfe0e to
f997b45
Compare
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]>
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]>
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]>
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]>
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]>
Problem
Outside
--transaction, cluster routing for--commandhashed 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_MOVEDout of 200 requests:SET {ask}-__key__ v110,GET {ask}-__key__92,SET __key__{ask} v112,SET test1:__key__ v134. The command shape from #333 (HSET/HGET test1:__key__) sees about 67% MOVED.Root cause
cluster_client::get_key_for_conn()calledcalc_hslot_crc16_cluster(m_obj_gen->get_key(), ...). The--transactionlookahead increate_arbitrary_request()already rebuiltdata_prefix + key + data_suffixand 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--transactionlookahead (same behaviour as before) and inget_key_for_conn()whenever--commandis 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-getrouting are unchanged.Behaviour change (release notes)
For
--commandkeys with text around__key__(for exampleuser:__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.pynow has 5 tests. Each asserts 0errorstat_MOVEDon 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__andGET user:__key__, no tag.--commandlines with different text around the key in one run:SET user:__key__+GET session:__key__.OBJECT ENCODING p:__key__andEVAL return 1 p:__key__(numkeys 1).--read-preference=secondarywithGET 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 toTESTinci.yml.Results:
--use-slaves): 5/5 pass on this branch, 5/5 fail on master 4d97c8b.make check6/6,test_cluster_command_key_affixes.py5/5 (the replica test skips without replicas).test_cluster_hash_tag_key_prefix2/2,test_mget_cluster9/9,test_cluster_transaction17/17,make check6/6.test_arbitrary_command_misses16/16,test_retry_on_error8/8,test_cluster_redirect_stats9/9,test_monitor_cluster3/3.Interaction with #609 (merged; resolution applied)
#609 makes
calc_hslot_crc16_cluster()tag-aware and removescalc_hslot_crc16_with_hash_tag(). Rebasing onto #609 conflicted in the--transactionlookahead block ofcreate_arbitrary_request(). The conflict was resolved like this:unsigned int hslot = calc_arbitrary_key_hslot(look);and drop Honour hash tags in --key-prefix when routing cluster requests #609's inlinedata_prefix/data_suffixrebuild.calc_arbitrary_key_hslot(), renamecalc_hslot_crc16_with_hash_tagtocalc_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--transactionlookahead loop and later uses it form_txn_wait_slot = (int) hslot;. The conflict was resolved with an assignment,hslot = calc_arbitrary_key_hslot(look);, not a new declaration, so the outerhslotis not shadowed and #606's wait-slot logic keeps seeing the computed slot. After the rebase:make check6/6,test_cluster_transaction.py18/18 andtest_cluster_command_key_affixes.py5/5 on a 3-shard cluster, and a-Wshadowbuild reports nothing forhslot.🤖 Generated with Claude Code
Note
Medium Risk
Changes cluster slot selection for arbitrary
--commandworkloads (behavioral fix with benchmark comparability impact); routing logic is covered by new integration tests but touches a hot path incluster_client.Overview
Fixes cluster routing for
--commandwhen 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_hslotcentralizes buildingprefix + key + suffixand callingcalc_hslot_crc16_cluster. It is used inget_key_for_connfor arbitrary commands and replaces duplicated logic in--transactionlookahead. 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.