@@ -1683,14 +1683,7 @@ def protocol_downgrade(self, host_endpoint, previous_version):
16831683 "http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version" , self .protocol_version , new_version , host_endpoint )
16841684 self .protocol_version = new_version
16851685
1686- def _add_resolved_hosts (self ):
1687- for endpoint in self .endpoints_resolved :
1688- host , new = self .add_host (endpoint , signal = False )
1689- if new :
1690- host .set_up ()
1691- for listener in self .listeners :
1692- listener .on_add (host )
1693-
1686+ def _populate_hosts (self ):
16941687 self .profile_manager .populate (
16951688 weakref .proxy (self ), self .metadata .all_hosts ())
16961689 self .load_balancing_policy .populate (
@@ -1717,17 +1710,10 @@ def connect(self, keyspace=None, wait_for_all_pools=False):
17171710 self .contact_points , self .protocol_version )
17181711 self .connection_class .initialize_reactor ()
17191712 _register_cluster_shutdown (self )
1720-
1721- self ._add_resolved_hosts ()
17221713
17231714 try :
17241715 self .control_connection .connect ()
1725-
1726- # we set all contact points up for connecting, but we won't infer state after this
1727- for endpoint in self .endpoints_resolved :
1728- h = self .metadata .get_host (endpoint )
1729- if h and self .profile_manager .distance (h ) == HostDistance .IGNORED :
1730- h .is_up = None
1716+ self ._populate_hosts ()
17311717
17321718 log .debug ("Control connection created" )
17331719 except Exception :
@@ -3534,18 +3520,20 @@ def _set_new_connection(self, conn):
35343520 if old :
35353521 log .debug ("[control connection] Closing old connection %r, replacing with %r" , old , conn )
35363522 old .close ()
3537-
3538- def _connect_host_in_lbp (self ):
3523+
3524+ def _connect_host (self ):
35393525 errors = {}
3526+
35403527 lbp = (
35413528 self ._cluster .load_balancing_policy
35423529 if self ._cluster ._config_mode == _ConfigMode .LEGACY else
35433530 self ._cluster ._default_load_balancing_policy
35443531 )
35453532
3533+ # use endpoints from the default LBP if it is already initialized
35463534 for host in lbp .make_query_plan ():
35473535 try :
3548- return (self ._try_connect (host ), None )
3536+ return (self ._try_connect (host . endpoint ), None )
35493537 except ConnectionException as exc :
35503538 errors [str (host .endpoint )] = exc
35513539 log .warning ("[control connection] Error connecting to %s:" , host , exc_info = True )
@@ -3555,7 +3543,22 @@ def _connect_host_in_lbp(self):
35553543 log .warning ("[control connection] Error connecting to %s:" , host , exc_info = True )
35563544 if self ._is_shutdown :
35573545 raise DriverException ("[control connection] Reconnection in progress during shutdown" )
3558-
3546+
3547+ # if lbp not initialized use contact points provided to the cluster
3548+ if len (errors ) == 0 :
3549+ for endpoint in self ._cluster .endpoints_resolved :
3550+ try :
3551+ return (self ._try_connect (endpoint ), None )
3552+ except ConnectionException as exc :
3553+ errors [str (endpoint )] = exc
3554+ log .warning ("[control connection] Error connecting to %s:" , endpoint , exc_info = True )
3555+ self ._cluster .signal_connection_failure (endpoint , exc , is_host_addition = False )
3556+ except Exception as exc :
3557+ errors [str (endpoint )] = exc
3558+ log .warning ("[control connection] Error connecting to %s:" , endpoint , exc_info = True )
3559+ if self ._is_shutdown :
3560+ raise DriverException ("[control connection] Reconnection in progress during shutdown" )
3561+
35593562 return (None , errors )
35603563
35613564 def _reconnect_internal (self ):
@@ -3567,43 +3570,43 @@ def _reconnect_internal(self):
35673570 to the exception that was raised when an attempt was made to open
35683571 a connection to that host.
35693572 """
3570- (conn , _ ) = self ._connect_host_in_lbp ()
3573+ (conn , _ ) = self ._connect_host ()
35713574 if conn is not None :
35723575 return conn
35733576
35743577 # Try to re-resolve hostnames as a fallback when all hosts are unreachable
35753578 self ._cluster ._resolve_hostnames ()
35763579
3577- self ._cluster ._add_resolved_hosts ()
3580+ self ._cluster ._populate_hosts ()
35783581
3579- (conn , errors ) = self ._connect_host_in_lbp ()
3582+ (conn , errors ) = self ._connect_host ()
35803583 if conn is not None :
35813584 return conn
3582-
3585+
35833586 raise NoHostAvailable ("Unable to connect to any servers" , errors )
35843587
3585- def _try_connect (self , host ):
3588+ def _try_connect (self , endpoint ):
35863589 """
35873590 Creates a new Connection, registers for pushed events, and refreshes
35883591 node/token and schema metadata.
35893592 """
3590- log .debug ("[control connection] Opening new connection to %s" , host )
3593+ log .debug ("[control connection] Opening new connection to %s" , endpoint )
35913594
35923595 while True :
35933596 try :
3594- connection = self ._cluster .connection_factory (host . endpoint , is_control_connection = True )
3597+ connection = self ._cluster .connection_factory (endpoint , is_control_connection = True )
35953598 if self ._is_shutdown :
35963599 connection .close ()
35973600 raise DriverException ("Reconnecting during shutdown" )
35983601 break
35993602 except ProtocolVersionUnsupported as e :
3600- self ._cluster .protocol_downgrade (host . endpoint , e .startup_version )
3603+ self ._cluster .protocol_downgrade (endpoint , e .startup_version )
36013604 except ProtocolException as e :
36023605 # protocol v5 is out of beta in C* >=4.0-beta5 and is now the default driver
36033606 # protocol version. If the protocol version was not explicitly specified,
36043607 # and that the server raises a beta protocol error, we should downgrade.
36053608 if not self ._cluster ._protocol_version_explicit and e .is_beta_protocol_error :
3606- self ._cluster .protocol_downgrade (host . endpoint , self ._cluster .protocol_version )
3609+ self ._cluster .protocol_downgrade (endpoint , self ._cluster .protocol_version )
36073610 else :
36083611 raise
36093612
@@ -3879,6 +3882,9 @@ def _refresh_node_list_and_token_map(self, connection, preloaded_results=None,
38793882
38803883 self ._cluster .metadata .update_host (host , old_endpoint = connection .endpoint )
38813884 connection .original_endpoint = connection .endpoint = host .endpoint
3885+ else :
3886+ log .info ("Consider local host new found host" )
3887+ peers_result .append (local_row )
38823888 # Check metadata.partitioner to see if we haven't built anything yet. If
38833889 # every node in the cluster was in the contact points, we won't discover
38843890 # any new nodes, so we need this additional check. (See PYTHON-90)
@@ -4177,8 +4183,8 @@ def _get_peers_query(self, peers_query_type, connection=None):
41774183 query_template = (self ._SELECT_SCHEMA_PEERS_TEMPLATE
41784184 if peers_query_type == self .PeersQueryType .PEERS_SCHEMA
41794185 else self ._SELECT_PEERS_NO_TOKENS_TEMPLATE )
4180- host_release_version = self ._cluster .metadata .get_host (connection .original_endpoint ).release_version
4181- host_dse_version = self ._cluster .metadata .get_host (connection .original_endpoint ).dse_version
4186+ host_release_version = None if self . _cluster . metadata . get_host ( connection . original_endpoint ) == None else self ._cluster .metadata .get_host (connection .original_endpoint ).release_version
4187+ host_dse_version = None if self . _cluster . metadata . get_host ( connection . original_endpoint ) == None else self ._cluster .metadata .get_host (connection .original_endpoint ).dse_version
41824188 uses_native_address_query = (
41834189 host_dse_version and Version (host_dse_version ) >= self ._MINIMUM_NATIVE_ADDRESS_DSE_VERSION )
41844190
0 commit comments