Skip to content

Fix KeyError in async cluster - initialize before execute multi key commands #2439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions redis/commands/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_asyncio/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,15 @@ 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_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()
Expand Down