Skip to content

Commit 700a244

Browse files
authored
Fixing async cluster pipeline execution when client is created with cluster_error_retry_attempts=0 (#3545)
1 parent e4b6de1 commit 700a244

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

redis/asyncio/cluster.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -1516,29 +1516,28 @@ async def execute(
15161516
return []
15171517

15181518
try:
1519-
for _ in range(self._client.cluster_error_retry_attempts):
1520-
if self._client._initialize:
1521-
await self._client.initialize()
1522-
1519+
retry_attempts = self._client.cluster_error_retry_attempts
1520+
while True:
15231521
try:
1522+
if self._client._initialize:
1523+
await self._client.initialize()
15241524
return await self._execute(
15251525
self._client,
15261526
self._command_stack,
15271527
raise_on_error=raise_on_error,
15281528
allow_redirections=allow_redirections,
15291529
)
1530-
except BaseException as e:
1531-
if type(e) in self.__class__.ERRORS_ALLOW_RETRY:
1532-
# Try again with the new cluster setup.
1533-
exception = e
1530+
1531+
except self.__class__.ERRORS_ALLOW_RETRY as e:
1532+
if retry_attempts > 0:
1533+
# Try again with the new cluster setup. All other errors
1534+
# should be raised.
1535+
retry_attempts -= 1
15341536
await self._client.aclose()
15351537
await asyncio.sleep(0.25)
15361538
else:
15371539
# All other errors should be raised.
1538-
raise
1539-
1540-
# If it fails the configured number of times then raise an exception
1541-
raise exception
1540+
raise e
15421541
finally:
15431542
self._command_stack = []
15441543

tests/test_asyncio/test_cluster.py

+12-1
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)