Skip to content

Commit d6ef268

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 05bf98c commit d6ef268

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
@@ -1882,10 +1882,30 @@ def test_cluster_zunion(self, r):
18821882
]
18831883

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

0 commit comments

Comments
 (0)