Skip to content

Commit 6f3fae7

Browse files
committed
Fixing async cluster pipeline execution when client is created with cluster_error_retry_attempts=0
1 parent b8ba391 commit 6f3fae7

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

redis/asyncio/cluster.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,29 +1509,28 @@ async def execute(
15091509
return []
15101510

15111511
try:
1512-
for _ in range(self._client.cluster_error_retry_attempts):
1513-
if self._client._initialize:
1514-
await self._client.initialize()
1515-
1512+
retry_attempts = self._client.cluster_error_retry_attempts
1513+
while True:
15161514
try:
1515+
if self._client._initialize:
1516+
await self._client.initialize()
15171517
return await self._execute(
15181518
self._client,
15191519
self._command_stack,
15201520
raise_on_error=raise_on_error,
15211521
allow_redirections=allow_redirections,
15221522
)
1523-
except BaseException as e:
1524-
if type(e) in self.__class__.ERRORS_ALLOW_RETRY:
1525-
# Try again with the new cluster setup.
1526-
exception = e
1523+
1524+
except self.__class__.ERRORS_ALLOW_RETRY as e:
1525+
if retry_attempts > 0:
1526+
# Try again with the new cluster setup. All other errors
1527+
# should be raised.
1528+
retry_attempts -= 1
15271529
await self._client.aclose()
15281530
await asyncio.sleep(0.25)
15291531
else:
15301532
# All other errors should be raised.
1531-
raise
1532-
1533-
# If it fails the configured number of times then raise an exception
1534-
raise exception
1533+
raise e
15351534
finally:
15361535
self._command_stack = []
15371536

tests/test_asyncio/test_cluster.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,17 @@ async def test_redis_cluster_pipeline(self, r: RedisCluster) -> None:
26732673
)
26742674
assert result == [True, b"1", 1, {b"F": b"V"}, True, True, b"2", b"3", 1, 1, 1]
26752675

2676+
async def test_cluster_pipeline_execution_zero_cluster_err_retries(
2677+
self, r: RedisCluster
2678+
) -> None:
2679+
"""
2680+
Test that we can run successfully cluster pipeline execute at least once when
2681+
cluster_error_retry_attempts is set to 0
2682+
"""
2683+
r.cluster_error_retry_attempts = 0
2684+
result = await r.pipeline().set("A", 1).get("A").delete("A").execute()
2685+
assert result == [True, b"1", 1]
2686+
26762687
async def test_multi_key_operation_with_a_single_slot(
26772688
self, r: RedisCluster
26782689
) -> None:
@@ -2733,7 +2744,7 @@ async def parse_response(
27332744
await pipe.get(key).execute()
27342745
assert (
27352746
node.parse_response.await_count
2736-
== 3 * r.cluster_error_retry_attempts - 2
2747+
== 3 * r.cluster_error_retry_attempts + 1
27372748
)
27382749

27392750
async def test_connection_error_not_raised(self, r: RedisCluster) -> None:

0 commit comments

Comments
 (0)