4242
4343from collections import defaultdict , OrderedDict
4444import random
45- from typing import Dict , Iterable , Optional , Tuple , Type , Union
45+ from typing import Dict , Iterable , List , Optional , Tuple , Type , Union
4646
4747from .api .binary import get_binary_type , put_binary_type
4848from .api .cache_config import cache_get_names
@@ -80,10 +80,10 @@ class Client:
8080 _compact_footer : bool = None
8181 _connection_args : Dict = None
8282 _current_node : int = None
83- _nodes : Union [ Iterable [ Connection ], Dict [ 'UUID' , Connection ] ] = None
83+ _nodes : List [ Connection ] = None
8484
85- affinity_version : Tuple = None
86- protocol_version = None
85+ affinity_version : Optional [ Tuple ] = None
86+ protocol_version : Optional [ Tuple ] = None
8787
8888 def __init__ (
8989 self , compact_footer : bool = None , affinity_aware : bool = False ,
@@ -106,6 +106,7 @@ def __init__(
106106 """
107107 self ._compact_footer = compact_footer
108108 self ._connection_args = kwargs
109+ self ._nodes = []
109110 self ._current_node = 0
110111 self ._affinity_aware = affinity_aware
111112 self .affinity_version = (0 , 0 )
@@ -126,43 +127,6 @@ def get_protocol_version(self) -> Optional[Tuple]:
126127 def affinity_aware (self ):
127128 return self ._affinity_aware
128129
129- @select_version
130- def _add_node (
131- self , host : str = None , port : int = None ,
132- conn : Connection = None , node_uuid : 'UUID' = None ,
133- ) -> 'UUID' :
134- """
135- Opens a connection to GridGain server and adds it to the nodes'
136- collection (connection pool).
137- """
138- if self ._nodes is None :
139- self ._nodes = {}
140-
141- if conn is None :
142- conn = Connection (self , ** self ._connection_args )
143- hs_response = conn .connect (host , port )
144- node_uuid = hs_response ['node_uuid' ]
145-
146- if node_uuid :
147- self ._nodes [node_uuid ] = conn
148- return node_uuid
149-
150- def _add_node_130 (
151- self , host : str = None , port : int = None , conn : Connection = None ,
152- * args , ** kwargs ,
153- ):
154- if self ._nodes is None :
155- self ._nodes = []
156-
157- if conn is None :
158- conn = Connection (self , ** self ._connection_args )
159- conn .host = host
160- conn .port = port
161-
162- self ._nodes .append (conn )
163-
164- _add_node_120 = _add_node_130
165-
166130 def connect (self , * args ):
167131 """
168132 Connect to GridGain cluster node(s).
@@ -185,36 +149,51 @@ def connect(self, *args):
185149 else :
186150 raise ConnectionError ('Connection parameters are not valid.' )
187151
188- nodes = iter (nodes )
152+ # the following code is quite twisted, because the protocol version
153+ # is initially unknown
154+
189155 # TODO: open first node in foregroung, others − in background
156+ for i , node in enumerate (nodes ):
157+ host , port = node
158+ conn = Connection (self , ** self ._connection_args )
159+ conn .host = host
160+ conn .port = port
190161
191- first_node = Connection (self , ** self ._connection_args )
162+ try :
163+ if (
164+ self .protocol_version is None
165+ or self .protocol_version >= (1 , 4 , 0 )
166+ ):
167+ # open connection before adding to the pool
168+ conn .connect (host , port )
169+
170+ # now we have the protocol version
171+ if self .protocol_version < (1 , 4 , 0 ):
172+ # do not try to open more nodes
173+ self ._current_node = i
174+ else :
175+ # take a chance to schedule the reconnection
176+ # for all the failed connections, that was probed
177+ # before this
178+ for failed_node in self ._nodes [:i ]:
179+ failed_node .reconnect ()
192180
193- # now we know protocol version
194- self ._add_node (
195- conn = first_node ,
196- node_uuid = first_node .connect (* next (nodes )).get ('node_uuid' , None ),
197- )
181+ except connection_errors :
182+ conn ._fail ()
183+ if (
184+ self .protocol_version
185+ and self .protocol_version >= (1 , 4 , 0 )
186+ ):
187+ # schedule the reconnection
188+ conn .reconnect ()
198189
199- for host , port in nodes :
200- self ._add_node (host , port )
190+ self ._nodes .append (conn )
201191
202- @select_version
203192 def close (self ):
204- """
205- Close all connections to the server and clean the connection pool.
206- """
207- for conn in self ._nodes .values ():
208- conn .close ()
209- self ._nodes .clear ()
210-
211- def close_130 (self ):
212193 for conn in self ._nodes :
213194 conn .close ()
214195 self ._nodes .clear ()
215196
216- close_120 = close_130
217-
218197 @property
219198 @select_version
220199 def random_node (self ) -> Connection :
@@ -227,7 +206,7 @@ def random_node(self) -> Connection:
227206 """
228207 try :
229208 return random .choice (
230- list (n for n in self ._nodes . values () if n .alive )
209+ list (n for n in self ._nodes if n .alive )
231210 )
232211 except IndexError :
233212 # cannot choose from an empty sequence
0 commit comments