@@ -742,13 +742,22 @@ def on_add(self, host):
742742 if self .is_shutdown :
743743 return
744744
745- log .debug ("Adding or renewing pools for new host %s and notifying listeners" , host )
746- self ._prepare_all_queries (host )
747- log .debug ("Done preparing queries for new host %s" , host )
745+ log .debug ("Handling new host %r and notifying listeners" , host )
746+
747+ distance = self .load_balancing_policy .distance (host )
748+ if distance != HostDistance .IGNORED :
749+ self ._prepare_all_queries (host )
750+ log .debug ("Done preparing queries for new host %r" , host )
748751
749752 self .load_balancing_policy .on_add (host )
750753 self .control_connection .on_add (host )
751754
755+ if distance == HostDistance .IGNORED :
756+ log .debug ("Not adding connection pool for new host %r because the "
757+ "load balancing policy has marked it as IGNORED" , host )
758+ self ._finalize_add (host )
759+ return
760+
752761 futures_lock = Lock ()
753762 futures_results = []
754763 futures = set ()
@@ -1711,6 +1720,7 @@ def _refresh_node_list_and_token_map(self, connection, preloaded_results=None):
17111720 if partitioner and tokens :
17121721 token_map [host ] = tokens
17131722
1723+ should_rebuild_token_map = False
17141724 found_hosts = set ()
17151725 for row in peers_result :
17161726 addr = row .get ("rpc_address" )
@@ -1727,8 +1737,9 @@ def _refresh_node_list_and_token_map(self, connection, preloaded_results=None):
17271737 if host is None :
17281738 log .debug ("[control connection] Found new host to connect to: %s" , addr )
17291739 host = self ._cluster .add_host (addr , datacenter , rack , signal = True )
1740+ should_rebuild_token_map = True
17301741 else :
1731- self ._update_location_info (host , datacenter , rack )
1742+ should_rebuild_token_map |= self ._update_location_info (host , datacenter , rack )
17321743
17331744 tokens = row .get ("tokens" )
17341745 if partitioner and tokens :
@@ -1739,22 +1750,25 @@ def _refresh_node_list_and_token_map(self, connection, preloaded_results=None):
17391750 old_host .address not in found_hosts and \
17401751 old_host .address not in self ._cluster .contact_points :
17411752 log .debug ("[control connection] Found host that has been removed: %r" , old_host )
1753+ should_rebuild_token_map = True
17421754 self ._cluster .remove_host (old_host )
17431755
1744- if partitioner :
1745- log .debug ("[control connection] Fetched ring info, rebuilding metadata" )
1756+ log .debug ("[control connection] Finished fetching ring info" )
1757+ if partitioner and should_rebuild_token_map :
1758+ log .debug ("[control connection] Rebuilding token map due to topology changes" )
17461759 self ._cluster .metadata .rebuild_token_map (partitioner , token_map )
17471760
17481761 def _update_location_info (self , host , datacenter , rack ):
17491762 if host .datacenter == datacenter and host .rack == rack :
1750- return
1763+ return False
17511764
17521765 # If the dc/rack information changes, we need to update the load balancing policy.
17531766 # For that, we remove and re-add the node against the policy. Not the most elegant, and assumes
17541767 # that the policy will update correctly, but in practice this should work.
17551768 self ._cluster .load_balancing_policy .on_down (host )
17561769 host .set_location_info (datacenter , rack )
17571770 self ._cluster .load_balancing_policy .on_up (host )
1771+ return True
17581772
17591773 def _handle_topology_change (self , event ):
17601774 change_type = event ["change_type" ]
0 commit comments