11
11
List ,
12
12
Mapping ,
13
13
Optional ,
14
- Tuple ,
15
14
Type ,
16
15
TypeVar ,
17
16
Union ,
@@ -223,7 +222,7 @@ def __init__(
223
222
reinitialize_steps : int = 5 ,
224
223
cluster_error_retry_attempts : int = 3 ,
225
224
connection_error_retry_attempts : int = 3 ,
226
- max_connections : int = 2 ** 31 ,
225
+ max_connections : int = 2 ** 31 ,
227
226
# Client related kwargs
228
227
db : Union [str , int ] = 0 ,
229
228
path : Optional [str ] = None ,
@@ -517,44 +516,37 @@ def set_response_callback(self, command: str, callback: ResponseCallbackT) -> No
517
516
518
517
async def _determine_nodes (
519
518
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.
528
522
if not node_flag :
529
523
# get the nodes group for this command if it was predefined
530
524
node_flag = self .command_flags .get (command )
531
525
532
526
if node_flag in self .node_flags :
533
527
if node_flag == self .__class__ .DEFAULT_NODE :
534
528
# return the cluster's default node
535
- return [self .nodes_manager .default_node ], True
529
+ return [self .nodes_manager .default_node ]
536
530
if node_flag == self .__class__ .PRIMARIES :
537
531
# 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 )
539
533
if node_flag == self .__class__ .REPLICAS :
540
534
# 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 )
542
536
if node_flag == self .__class__ .ALL_NODES :
543
537
# return all nodes
544
- return list (self .nodes_manager .nodes_cache .values ()), False
538
+ return list (self .nodes_manager .nodes_cache .values ())
545
539
if node_flag == self .__class__ .RANDOM :
546
540
# 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 ()))]
550
542
551
543
# get the node that holds the key's slot
552
544
return [
553
545
self .nodes_manager .get_node_from_slot (
554
546
await self ._determine_slot (command , * args ),
555
547
self .read_from_replicas and command in READ_COMMANDS ,
556
548
)
557
- ], False
549
+ ]
558
550
559
551
async def _determine_slot (self , command : str , * args : Any ) -> int :
560
552
if self .command_flags .get (command ) == SLOT_ID :
@@ -671,13 +663,18 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
671
663
try :
672
664
if not target_nodes_specified :
673
665
# 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 (
675
667
* args , node_flag = passed_targets
676
668
)
677
669
if not target_nodes :
678
670
raise RedisClusterException (
679
671
f"No targets were found to execute { args } command on"
680
672
)
673
+ if (
674
+ len (target_nodes ) == 1
675
+ and target_nodes [0 ] == self .get_default_node ()
676
+ ):
677
+ is_default_node = True
681
678
682
679
if len (target_nodes ) == 1 :
683
680
# Return the processed result
@@ -896,7 +893,7 @@ def __init__(
896
893
port : Union [str , int ],
897
894
server_type : Optional [str ] = None ,
898
895
* ,
899
- max_connections : int = 2 ** 31 ,
896
+ max_connections : int = 2 ** 31 ,
900
897
connection_class : Type [Connection ] = Connection ,
901
898
** connection_kwargs : Any ,
902
899
) -> None :
@@ -1456,7 +1453,7 @@ async def _execute(
1456
1453
if passed_targets and not client ._is_node_flag (passed_targets ):
1457
1454
target_nodes = client ._parse_target_nodes (passed_targets )
1458
1455
else :
1459
- target_nodes , is_default_node = await client ._determine_nodes (
1456
+ target_nodes = await client ._determine_nodes (
1460
1457
* cmd .args , node_flag = passed_targets
1461
1458
)
1462
1459
if not target_nodes :
@@ -1465,8 +1462,9 @@ async def _execute(
1465
1462
)
1466
1463
if len (target_nodes ) > 1 :
1467
1464
raise RedisClusterException (f"Too many targets for command { cmd .args } " )
1468
-
1469
1465
node = target_nodes [0 ]
1466
+ if node == client .get_default_node ():
1467
+ is_default_node = True
1470
1468
if node .name not in nodes :
1471
1469
nodes [node .name ] = (node , [])
1472
1470
nodes [node .name ][1 ].append (cmd )
0 commit comments