@@ -643,7 +643,6 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
643643 command = args [0 ]
644644 target_nodes = []
645645 target_nodes_specified = False
646- is_default_node = False
647646 retry_attempts = self .cluster_error_retry_attempts
648647
649648 passed_targets = kwargs .pop ("target_nodes" , None )
@@ -657,7 +656,10 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
657656 for _ in range (execute_attempts ):
658657 if self ._initialize :
659658 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+ ):
661663 # Replace the default cluster node
662664 self .replace_default_node ()
663665 try :
@@ -670,11 +672,6 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
670672 raise RedisClusterException (
671673 f"No targets were found to execute { args } command on"
672674 )
673- if (
674- len (target_nodes ) == 1
675- and target_nodes [0 ] == self .get_default_node ()
676- ):
677- is_default_node = True
678675
679676 if len (target_nodes ) == 1 :
680677 # Return the processed result
@@ -1447,7 +1444,6 @@ async def _execute(
14471444 ]
14481445
14491446 nodes = {}
1450- is_default_node = False
14511447 for cmd in todo :
14521448 passed_targets = cmd .kwargs .pop ("target_nodes" , None )
14531449 if passed_targets and not client ._is_node_flag (passed_targets ):
@@ -1463,8 +1459,6 @@ async def _execute(
14631459 if len (target_nodes ) > 1 :
14641460 raise RedisClusterException (f"Too many targets for command { cmd .args } " )
14651461 node = target_nodes [0 ]
1466- if node == client .get_default_node ():
1467- is_default_node = True
14681462 if node .name not in nodes :
14691463 nodes [node .name ] = (node , [])
14701464 nodes [node .name ][1 ].append (cmd )
@@ -1500,8 +1494,18 @@ async def _execute(
15001494 result .args = (msg ,) + result .args [1 :]
15011495 raise result
15021496
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
15051509
15061510 return [cmd .result for cmd in stack ]
15071511
0 commit comments