From 92bdcae5a4beb9c2e6f94baaa812d2a58aa0a2a2 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Sun, 30 Oct 2022 03:51:28 +0200 Subject: [PATCH 1/3] Fix KeyError in async cluster --- redis/commands/cluster.py | 19 +++++++++++++++++++ tests/test_asyncio/test_cluster.py | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/redis/commands/cluster.py b/redis/commands/cluster.py index 8e4c8ef1d0..f0eaaf75c5 100644 --- a/redis/commands/cluster.py +++ b/redis/commands/cluster.py @@ -316,6 +316,25 @@ async def _split_command_across_slots(self, command: str, *keys: KeyT) -> int: # 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 f1bbe42267..cb8ed94b24 100644 --- a/tests/test_asyncio/test_cluster.py +++ b/tests/test_asyncio/test_cluster.py @@ -802,6 +802,14 @@ async def test_unlink(self, r: RedisCluster) -> None: await asyncio.sleep(0.1) assert await r.unlink(*d.keys()) == 0 + async def test_initialize_before_run_execute_multykey_command( + self, request: FixtureRequest + ) -> None: + 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() From 91764ddbb694e7dd59591b3eea5a19cf2761b36b Mon Sep 17 00:00:00 2001 From: dvora-h Date: Sun, 30 Oct 2022 03:54:16 +0200 Subject: [PATCH 2/3] link to issue --- tests/test_asyncio/test_cluster.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_asyncio/test_cluster.py b/tests/test_asyncio/test_cluster.py index cb8ed94b24..25b11ac050 100644 --- a/tests/test_asyncio/test_cluster.py +++ b/tests/test_asyncio/test_cluster.py @@ -805,6 +805,7 @@ async def test_unlink(self, r: RedisCluster) -> None: async def test_initialize_before_run_execute_multykey_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") From ea74e03e354cf11e357ac16275a8ad78c70b31d4 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Sun, 30 Oct 2022 03:58:02 +0200 Subject: [PATCH 3/3] typo --- tests/test_asyncio/test_cluster.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_asyncio/test_cluster.py b/tests/test_asyncio/test_cluster.py index 25b11ac050..27f11900c4 100644 --- a/tests/test_asyncio/test_cluster.py +++ b/tests/test_asyncio/test_cluster.py @@ -802,7 +802,7 @@ async def test_unlink(self, r: RedisCluster) -> None: await asyncio.sleep(0.1) assert await r.unlink(*d.keys()) == 0 - async def test_initialize_before_run_execute_multykey_command( + async def test_initialize_before_execute_multi_key_command( self, request: FixtureRequest ) -> None: # Test for issue https://github.com/redis/redis-py/issues/2437