@@ -643,7 +643,6 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
643
643
command = args [0 ]
644
644
target_nodes = []
645
645
target_nodes_specified = False
646
- is_default_node = False
647
646
retry_attempts = self .cluster_error_retry_attempts
648
647
649
648
passed_targets = kwargs .pop ("target_nodes" , None )
@@ -657,7 +656,10 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
657
656
for _ in range (execute_attempts ):
658
657
if self ._initialize :
659
658
await self .initialize ()
660
- if is_default_node :
659
+ if (
660
+ len (target_nodes ) == 1
661
+ and target_nodes [0 ] == self .get_default_node ()
662
+ ):
661
663
# Replace the default cluster node
662
664
self .replace_default_node ()
663
665
try :
@@ -670,11 +672,6 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
670
672
raise RedisClusterException (
671
673
f"No targets were found to execute { args } command on"
672
674
)
673
- if (
674
- len (target_nodes ) == 1
675
- and target_nodes [0 ] == self .get_default_node ()
676
- ):
677
- is_default_node = True
678
675
679
676
if len (target_nodes ) == 1 :
680
677
# Return the processed result
@@ -1447,7 +1444,6 @@ async def _execute(
1447
1444
]
1448
1445
1449
1446
nodes = {}
1450
- is_default_node = False
1451
1447
for cmd in todo :
1452
1448
passed_targets = cmd .kwargs .pop ("target_nodes" , None )
1453
1449
if passed_targets and not client ._is_node_flag (passed_targets ):
@@ -1463,8 +1459,6 @@ async def _execute(
1463
1459
if len (target_nodes ) > 1 :
1464
1460
raise RedisClusterException (f"Too many targets for command { cmd .args } " )
1465
1461
node = target_nodes [0 ]
1466
- if node == client .get_default_node ():
1467
- is_default_node = True
1468
1462
if node .name not in nodes :
1469
1463
nodes [node .name ] = (node , [])
1470
1464
nodes [node .name ][1 ].append (cmd )
@@ -1500,8 +1494,18 @@ async def _execute(
1500
1494
result .args = (msg ,) + result .args [1 :]
1501
1495
raise result
1502
1496
1503
- if is_default_node :
1504
- client .replace_default_node ()
1497
+ default_node = nodes .get (client .get_default_node ().name )
1498
+ if default_node is not None :
1499
+ # This pipeline execution used the default node, check if we need
1500
+ # to replace it.
1501
+ # Note: when the error is raised we'll reset the default node in the
1502
+ # caller function.
1503
+ for cmd in default_node [1 ]:
1504
+ # Check if it has a command that failed with a relevant
1505
+ # exception
1506
+ if type (cmd .result ) in self .__class__ .ERRORS_ALLOW_RETRY :
1507
+ client .replace_default_node ()
1508
+ break
1505
1509
1506
1510
return [cmd .result for cmd in stack ]
1507
1511
0 commit comments