Repository navigation
Honour hash tags in --key-prefix when routing cluster requests - #609
Conversation
244ec72 to
0f75064
Compare
|
🤖 Automated first-pass review. A human maintainer still needs to review this before merge. Thanks for this. Merging the two functions so every path follows one rule is the right fix. The unit test checks the function against real A few comments:
The core fix looks solid to me. I'd just like the literal-affix gap written down somewhere before this merges. |
47c8786 to
dfa5d56
Compare
Redis Cluster hashes only the part inside the first non-empty {...} of a
key. memtier routed SET/GET and --command __key__ requests (and
--read-preference reads, which take the same path) with
calc_hslot_crc16_cluster(), which hashed the whole key. With
--key-prefix '{ask}-' every key lives in slot 11420, but about half
of requests were sent to other shards and got -MOVED.
The other routing paths (--transaction, the MGET slot cache,
--monitor-input, redirect retries and replay) already used
calc_hslot_crc16_with_hash_tag(). Move that body into
calc_hslot_crc16_cluster() and drop the duplicate, so every path uses the
same rule. Keys without a valid tag hash to the same slot as before.
Add a unit test that checks slots against CLUSTER KEYSLOT values,
including the {} / unclosed { / first-tag-only cases, and an OSS cluster
test that runs a tagged --key-prefix against each shard and asserts the
servers sent no -MOVED.
Fixes #608
Co-Authored-By: Claude Opus 5.5 <[email protected]>
dfa5d56 to
34e2bc6
Compare
|
Thanks for the review. Point by point, as of head 34e2bc6:
Performance: the added work per request is one 🤖 Generated with Claude Code |
Problem
In
--cluster-mode, memtier ignored a hash tag inside--key-prefixwhen it picked the shard. With--key-prefix '{ask}-', Redis puts every key in slot 11420, but memtier hashed the whole key ({ask}-memtier-N). About half the requests went to other shards and came back-MOVED 11420 .... This affected the default SET/GET workload,--commandrequests that use__key__, and--read-preferencereads. See #608.Root cause
cluster_client::get_key_for_conn()routed withcalc_hslot_crc16_cluster(), which runs CRC16 over the whole key. Every other routing path (--transaction, the MGET slot cache,--monitor-input, redirect retries, replay) already usedcalc_hslot_crc16_with_hash_tag(), which follows the Redis hash tag rule.Fix
Move the body of
calc_hslot_crc16_with_hash_tag()intocalc_hslot_crc16_cluster()and remove the duplicate, so every path uses one function and one rule. Keys without a valid tag (no{,{},{without}) still hash the whole key, so their slots are unchanged. The function is declared incluster_client.hso a unit test can call it.Behaviour change (release notes)
With a hash tag in
--key-prefix, cluster runs now send every request to the slot owner. In review, Ops/sec rose 2.9x (18.5k to 54k on 3 shards), and MOVED/sec dropped to 0. Results for tagged prefixes are therefore not comparable with 1.2.11–2.5.2. Without--retry-on-error, those versions also silently lost the writes that got -MOVED: 444 of 1000 keys were never stored.Tests
On master f1cb946:
tests/unit/cluster_hslot_test.cpp(make check): compares 15 keys withCLUSTER KEYSLOTvalues from Redis 8.6. These cover{ask}-1,{user1000}.following/.followers,{}-x,{ask,foo{}{bar},foo{{bar}}zap,foo{bar}{zap}and}{a}. With the old whole-key function, 8 of 15 are wrong (for example,{ask}-1gives 11774 instead of 11420). With the fix, all pass.make checkpasses 6/6.tests/test_cluster_hash_tag_key_prefix.py(oss-cluster, 3 shards, default seed): for a tag owned by each shard, it runs--key-prefix={tag}-with--ratio=1:1, then with--command=SET __key__ __data__. It asserts 0errorstat_MOVEDon every primary, that keys land on the owner, and 0 MOVED/sec in the JSON. It passes 2/2 on this branch and fails on master. For two of the three tags, the seed shard owns none of the keys, which also exercises Route --read-preference=primary reads to a still-connecting primary (fixes cluster seed stall) #612's fix for Cluster --read-preference=primary: run stalls at 100% CPU when the seed shard owns none of the read keys #610.test_mget_cluster,test_cluster_transaction,test_monitor_cluster,test_cluster_redirect_stats,test_retry_on_error,tests_oss_simple_flow, andtest_read_preference_modes(with replicas).Affected versions
1.2.11 through 2.5.2 (latest) and master. The bug was introduced with cluster mode in fdd22d8 (#42). It was confirmed by running on 2.4.0, 2.5.2 and master, and derived from history for the other versions (see #608).
Fixes #608
🤖 Generated with Claude Code
Note
Medium Risk
Changes cluster hash-slot computation used on all routing paths; keys without a valid tag keep prior slots, but mistagged edge cases could still mis-route until caught by tests.
Overview
Cluster routing now follows Redis hash tags for every code path, fixing wrong-shard traffic and
-MOVEDwhen keys include a tag in--key-prefix(e.g.--key-prefix '{ask}-').calc_hslot_crc16_clusteris updated to use the former hash-tag-aware logic (first non-empty{tag}wins; otherwise hash the full key),calc_hslot_crc16_with_hash_tagis removed, and all routing sites (default SET/GET, MGET cache,--transaction, monitor input, redirect/replay) call the single helper. The function is declared incluster_client.hfor unit testing.Tests:
cluster_hslot_test(make check) checks 15 keys against RedisCLUSTER KEYSLOT;test_cluster_hash_tag_key_prefix.pyruns oss-cluster with--key-prefix={tag}-for SET/GET and--commandworkloads and asserts zeroMOVEDon primaries and in JSON stats.Reviewed by Cursor Bugbot for commit 34e2bc6. Bugbot is set up for automated code reviews on this repo. Configure here.