1111 List ,
1212 Mapping ,
1313 Optional ,
14- Tuple ,
1514 Type ,
1615 TypeVar ,
1716 Union ,
@@ -223,7 +222,7 @@ def __init__(
223222 reinitialize_steps : int = 5 ,
224223 cluster_error_retry_attempts : int = 3 ,
225224 connection_error_retry_attempts : int = 3 ,
226- max_connections : int = 2 ** 31 ,
225+ max_connections : int = 2 ** 31 ,
227226 # Client related kwargs
228227 db : Union [str , int ] = 0 ,
229228 path : Optional [str ] = None ,
@@ -517,44 +516,37 @@ def set_response_callback(self, command: str, callback: ResponseCallbackT) -> No
517516
518517 async def _determine_nodes (
519518 self , command : str , * args : Any , node_flag : Optional [str ] = None
520- ) -> Tuple [List ["ClusterNode" ], bool ]:
521- """Determine which nodes should be executed the command on
522-
523- Returns:
524- tuple[list[Type[ClusterNode]], bool]:
525- A tuple containing a list of target nodes and a bool indicating
526- if the return node was chosen because it is the default node
527- """
519+ ) -> List ["ClusterNode" ]:
520+ # Determine which nodes should be executed the command on.
521+ # Returns a list of target nodes.
528522 if not node_flag :
529523 # get the nodes group for this command if it was predefined
530524 node_flag = self .command_flags .get (command )
531525
532526 if node_flag in self .node_flags :
533527 if node_flag == self .__class__ .DEFAULT_NODE :
534528 # return the cluster's default node
535- return [self .nodes_manager .default_node ], True
529+ return [self .nodes_manager .default_node ]
536530 if node_flag == self .__class__ .PRIMARIES :
537531 # return all primaries
538- return self .nodes_manager .get_nodes_by_server_type (PRIMARY ), False
532+ return self .nodes_manager .get_nodes_by_server_type (PRIMARY )
539533 if node_flag == self .__class__ .REPLICAS :
540534 # return all replicas
541- return self .nodes_manager .get_nodes_by_server_type (REPLICA ), False
535+ return self .nodes_manager .get_nodes_by_server_type (REPLICA )
542536 if node_flag == self .__class__ .ALL_NODES :
543537 # return all nodes
544- return list (self .nodes_manager .nodes_cache .values ()), False
538+ return list (self .nodes_manager .nodes_cache .values ())
545539 if node_flag == self .__class__ .RANDOM :
546540 # return a random node
547- return [
548- random .choice (list (self .nodes_manager .nodes_cache .values ()))
549- ], False
541+ return [random .choice (list (self .nodes_manager .nodes_cache .values ()))]
550542
551543 # get the node that holds the key's slot
552544 return [
553545 self .nodes_manager .get_node_from_slot (
554546 await self ._determine_slot (command , * args ),
555547 self .read_from_replicas and command in READ_COMMANDS ,
556548 )
557- ], False
549+ ]
558550
559551 async def _determine_slot (self , command : str , * args : Any ) -> int :
560552 if self .command_flags .get (command ) == SLOT_ID :
@@ -671,13 +663,18 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
671663 try :
672664 if not target_nodes_specified :
673665 # Determine the nodes to execute the command on
674- target_nodes , is_default_node = await self ._determine_nodes (
666+ target_nodes = await self ._determine_nodes (
675667 * args , node_flag = passed_targets
676668 )
677669 if not target_nodes :
678670 raise RedisClusterException (
679671 f"No targets were found to execute { args } command on"
680672 )
673+ if (
674+ len (target_nodes ) == 1
675+ and target_nodes [0 ] == self .get_default_node ()
676+ ):
677+ is_default_node = True
681678
682679 if len (target_nodes ) == 1 :
683680 # Return the processed result
@@ -896,7 +893,7 @@ def __init__(
896893 port : Union [str , int ],
897894 server_type : Optional [str ] = None ,
898895 * ,
899- max_connections : int = 2 ** 31 ,
896+ max_connections : int = 2 ** 31 ,
900897 connection_class : Type [Connection ] = Connection ,
901898 ** connection_kwargs : Any ,
902899 ) -> None :
@@ -1456,7 +1453,7 @@ async def _execute(
14561453 if passed_targets and not client ._is_node_flag (passed_targets ):
14571454 target_nodes = client ._parse_target_nodes (passed_targets )
14581455 else :
1459- target_nodes , is_default_node = await client ._determine_nodes (
1456+ target_nodes = await client ._determine_nodes (
14601457 * cmd .args , node_flag = passed_targets
14611458 )
14621459 if not target_nodes :
@@ -1465,8 +1462,9 @@ async def _execute(
14651462 )
14661463 if len (target_nodes ) > 1 :
14671464 raise RedisClusterException (f"Too many targets for command { cmd .args } " )
1468-
14691465 node = target_nodes [0 ]
1466+ if node == client .get_default_node ():
1467+ is_default_node = True
14701468 if node .name not in nodes :
14711469 nodes [node .name ] = (node , [])
14721470 nodes [node .name ][1 ].append (cmd )
0 commit comments