@@ -1629,6 +1629,7 @@ def _refresh_node_list_and_token_map(self, connection, preloaded_results=None):
16291629 if partitioner and tokens :
16301630 token_map [host ] = tokens
16311631
1632+ should_rebuild_token_map = False
16321633 found_hosts = set ()
16331634 for row in peers_result :
16341635 addr = row .get ("rpc_address" )
@@ -1645,8 +1646,9 @@ def _refresh_node_list_and_token_map(self, connection, preloaded_results=None):
16451646 if host is None :
16461647 log .debug ("[control connection] Found new host to connect to: %s" , addr )
16471648 host = self ._cluster .add_host (addr , datacenter , rack , signal = True )
1649+ should_rebuild_token_map = True
16481650 else :
1649- self ._update_location_info (host , datacenter , rack )
1651+ should_rebuild_token_map |= self ._update_location_info (host , datacenter , rack )
16501652
16511653 tokens = row .get ("tokens" )
16521654 if partitioner and tokens :
@@ -1657,22 +1659,25 @@ def _refresh_node_list_and_token_map(self, connection, preloaded_results=None):
16571659 old_host .address not in found_hosts and \
16581660 old_host .address not in self ._cluster .contact_points :
16591661 log .debug ("[control connection] Found host that has been removed: %r" , old_host )
1662+ should_rebuild_token_map = True
16601663 self ._cluster .remove_host (old_host )
16611664
1662- if partitioner :
1663- log .debug ("[control connection] Fetched ring info, rebuilding metadata" )
1665+ log .debug ("[control connection] Finished fetching ring info" )
1666+ if partitioner and should_rebuild_token_map :
1667+ log .debug ("[control connection] Rebuilding token map due to topology changes" )
16641668 self ._cluster .metadata .rebuild_token_map (partitioner , token_map )
16651669
16661670 def _update_location_info (self , host , datacenter , rack ):
16671671 if host .datacenter == datacenter and host .rack == rack :
1668- return
1672+ return False
16691673
16701674 # If the dc/rack information changes, we need to update the load balancing policy.
16711675 # For that, we remove and re-add the node against the policy. Not the most elegant, and assumes
16721676 # that the policy will update correctly, but in practice this should work.
16731677 self ._cluster .load_balancing_policy .on_down (host )
16741678 host .set_location_info (datacenter , rack )
16751679 self ._cluster .load_balancing_policy .on_up (host )
1680+ return True
16761681
16771682 def _handle_topology_change (self , event ):
16781683 change_type = event ["change_type" ]
0 commit comments