summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordvora-h <67596500+dvora-h@users.noreply.github.com>2022-10-30 09:14:00 +0200
committerGitHub <noreply@github.com>2022-10-30 09:14:00 +0200
commit9f3791fa781f979664edc0869eb2c02562abc4bb (patch)
treef2ae1001f8120e55136d6e4f5bdd6a222e44a514
parent51797765d6c9cf72776eaa031b0ebb1e6a5ed77a (diff)
downloadredis-py-9f3791fa781f979664edc0869eb2c02562abc4bb.tar.gz
Fix KeyError in async cluster - initialize before execute multi key commands (#2439)
* Fix KeyError in async cluster * link to issue * typo
-rw-r--r--redis/commands/cluster.py19
-rw-r--r--tests/test_asyncio/test_cluster.py9
2 files changed, 28 insertions, 0 deletions
diff --git a/redis/commands/cluster.py b/redis/commands/cluster.py
index 8e4c8ef..f0eaaf7 100644
--- a/redis/commands/cluster.py
+++ b/redis/commands/cluster.py
@@ -316,6 +316,25 @@ class AsyncClusterMultiKeyCommands(ClusterMultiKeyCommands):
# Sum up the reply from each command
return sum(await self._execute_pipeline_by_slot(command, slots_to_keys))
+ async def _execute_pipeline_by_slot(
+ self, command: str, slots_to_args: Mapping[int, Iterable[EncodableT]]
+ ) -> List[Any]:
+ if self._initialize:
+ await self.initialize()
+ read_from_replicas = self.read_from_replicas and command in READ_COMMANDS
+ pipe = self.pipeline()
+ [
+ pipe.execute_command(
+ command,
+ *slot_args,
+ target_nodes=[
+ self.nodes_manager.get_node_from_slot(slot, read_from_replicas)
+ ],
+ )
+ for slot, slot_args in slots_to_args.items()
+ ]
+ return await pipe.execute()
+
class ClusterManagementCommands(ManagementCommands):
"""
diff --git a/tests/test_asyncio/test_cluster.py b/tests/test_asyncio/test_cluster.py
index f1bbe42..27f1190 100644
--- a/tests/test_asyncio/test_cluster.py
+++ b/tests/test_asyncio/test_cluster.py
@@ -802,6 +802,15 @@ class TestClusterRedisCommands:
await asyncio.sleep(0.1)
assert await r.unlink(*d.keys()) == 0
+ async def test_initialize_before_execute_multi_key_command(
+ self, request: FixtureRequest
+ ) -> None:
+ # Test for issue https://github.com/redis/redis-py/issues/2437
+ url = request.config.getoption("--redis-url")
+ r = RedisCluster.from_url(url)
+ assert 0 == await r.exists("a", "b", "c")
+ await r.close()
+
@skip_if_redis_enterprise()
async def test_cluster_myid(self, r: RedisCluster) -> None:
node = r.get_random_node()