Skip to content

Commit 0bdb547

Browse files
committed
Avoid rebuilding token metadata when topology has not changed
1 parent b5fbb20 commit 0bdb547

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Bug Fixes
1919
* Fix unintended rebuild of token replica map when keyspaces are
2020
discovered (on startup), added, or updated and TokenAwarePolicy is not
2121
in use.
22+
* Avoid rebuilding token metadata when cluster topology has not
23+
actually changed
2224

2325
1.1.2
2426
=====

cassandra/cluster.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)