Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions cassandra/client_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def handle_client_routes_change(self, connection: 'Connection', timeout: float,
return

routes = self._query_routes_for_change_event(connection, timeout, pairs)
self._routes.merge(routes, affected_host_ids=set(host_uuids))
self._routes.merge(routes, affected_host_ids={host_id for _, host_id in pairs})

def _query_all_routes_for_connections(self, connection: 'Connection', timeout: float,
connection_ids: Set[str]) -> List[_Route]:
Expand Down Expand Up @@ -322,27 +322,25 @@ def _query_all_routes_for_connections(self, connection: 'Connection', timeout: f
def _query_routes_for_change_event(self, connection: 'Connection', timeout: float,
route_pairs: List[Tuple[str, uuid.UUID]]) -> List[_Route]:
"""
Query specific routes affected by a CLIENT_ROUTES_CHANGE event.
Query current routes for hosts affected by a CLIENT_ROUTES_CHANGE event.

Takes a list of (connection_id, host_id) pairs that represent the exact
routes affected by an operation. This provides precise updates without
fetching unrelated routes.

If the pairs list is empty or None, falls back to a complete refresh
of all routes for safety.
The in-memory route store keeps a single preferred route per host. When
any configured connection_id changes for a host, fetch all configured
connection_ids for that host so the existing preferred route can be
retained if it is still present.

:param connection: Connection to execute query on
:param timeout: Query timeout in seconds
:param route_pairs: List of (connection_id, host_id) tuples
:param route_pairs: List of affected (connection_id, host_id) tuples
:return: List of _Route
"""
unique_pairs = list(dict.fromkeys(route_pairs))

conn_ids = list(dict.fromkeys(cid for cid, _ in unique_pairs))
conn_ids = sorted(self._connection_ids)
host_ids = list(dict.fromkeys(hid for _, hid in unique_pairs))

log.debug("[client routes] Querying route pairs from CLIENT_ROUTES_CHANGE "
"(first 5 of %d): %s", len(unique_pairs), unique_pairs[:5])
log.debug("[client routes] Querying routes from CLIENT_ROUTES_CHANGE "
"for host_ids (first 5 of %d): %s", len(host_ids), host_ids[:5])

conn_ph = ', '.join('?' for _ in conn_ids)
host_ph = ', '.join('?' for _ in host_ids)
Expand Down
Loading
Loading