Skip to content

Commit 88553c0

Browse files
committed
remove COMMAND GETKEYS call from ZUNION and ZUNIONSTORE (#4)
* make EVALSHA and EVALSHA_RO to avoid command_keys as well * make ZUNION, ZUNIONSTORE to avoid command_keys as well
1 parent bd2b3cc commit 88553c0

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

redis/cluster.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ def determine_slot(self, *args):
940940
# redis server to parse the keys. Besides, there is a bug in redis<7.0
941941
# where `self._get_command_keys()` fails anyway. So, we special case
942942
# EVAL/EVALSHA.
943-
if command in ("EVAL", "EVALSHA"):
943+
if command in ("EVAL", "EVALSHA", "EVAL_RO", "EVALSHA_RO"):
944944
# command syntax: EVAL "script body" num_keys ...
945945
if len(args) <= 2:
946946
raise RedisClusterException(f"Invalid args in command: {args}")
@@ -951,6 +951,14 @@ def determine_slot(self, *args):
951951
if len(eval_keys) == 0:
952952
return random.randrange(0, REDIS_CLUSTER_HASH_SLOTS)
953953
keys = eval_keys
954+
elif command == "ZUNIONSTORE":
955+
# https://github.com/redis/redis/pull/12380
956+
# command syntax: ZUNIONSTORE destination numkeys key [key ...]
957+
# [WEIGHTS weight [weight ...]] [AGGREGATE <SUM | MIN | MAX>]
958+
if len(args) <= 3:
959+
raise RedisClusterException(f"Invalid args in ZUNIONSTORE: {args}")
960+
num_actual_keys = args[2]
961+
keys = (args[1],) + args[3 : 3 + num_actual_keys]
954962
else:
955963
keys = self._get_command_keys(*args)
956964
if keys is None or len(keys) == 0:

tests/test_cluster.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,10 +1883,30 @@ def test_cluster_zunion(self, r):
18831883
]
18841884

18851885
def test_cluster_zunionstore_sum(self, r):
1886+
assert r.zunionstore("{foo}d", ["{foo}" + str(i) for i in range(0, 256)]) == 0
1887+
18861888
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
18871889
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
18881890
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1891+
1892+
result_key = "{foo}d"
1893+
failed_keys = ["{foo1}a", "{foo}b", "{foo}c"]
1894+
with pytest.raises(
1895+
RedisClusterException,
1896+
match="ZUNIONSTORE - all keys must map to the same key slot",
1897+
):
1898+
r.zunionstore(result_key, failed_keys)
1899+
1900+
result_key = "{foo1}d"
1901+
failed_keys = ["{foo}a", "{foo}b"]
1902+
with pytest.raises(
1903+
RedisClusterException,
1904+
match="ZUNIONSTORE - all keys must map to the same key slot",
1905+
):
1906+
r.zunionstore(result_key, failed_keys)
1907+
18891908
assert r.zunionstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"]) == 4
1909+
assert r.zunionstore("{foo}e", ["{foo}a"]) == 3
18901910
assert r.zrange("{foo}d", 0, -1, withscores=True) == [
18911911
(b"a2", 3),
18921912
(b"a4", 4),

0 commit comments

Comments
 (0)