From d838e54cdadafeeafef65aedbc2a7b74c2fecc85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20B=C4=85czkowski?= Date: Tue, 26 Nov 2024 21:16:56 +0100 Subject: [PATCH] Make TabletMap return empty replica set when stale replica is found Makes TabletMap return empty collection for some calls to `getReplicas()`. If the current replica list for a tablet turns out to contain a host that is not present in current Metadata then empty collection is returned in an effort to misroute the query. This query will cause the tablet information to be updated when the result with the up to date information is received. It is possible that the query will be randomly routed correctly, but this case is significantly less likely with each subsequent query, although this can depend on underlying load balancing policy. This covers the node removal/replacement case of #378. --- .../java/com/datastax/driver/core/TabletMap.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/driver-core/src/main/java/com/datastax/driver/core/TabletMap.java b/driver-core/src/main/java/com/datastax/driver/core/TabletMap.java index baecc5959b..95136a19d8 100644 --- a/driver-core/src/main/java/com/datastax/driver/core/TabletMap.java +++ b/driver-core/src/main/java/com/datastax/driver/core/TabletMap.java @@ -57,7 +57,9 @@ public Map getMapping() { } /** - * Finds hosts that have replicas for a given table and token combination + * Finds hosts that have replicas for a given table and token combination. Meant for use in query + * planning. Can return empty collection if internal replica list information is determined not up + * to date. * * @param keyspace the keyspace that table is in * @param table the table name @@ -93,8 +95,14 @@ public Set getReplicas(String keyspace, String table, long token) { HashSet uuidSet = new HashSet<>(); for (HostShardPair hostShardPair : row.replicas) { - if (cluster.metadata.getHost(hostShardPair.getHost()) != null) + if (cluster.metadata.getHost(hostShardPair.getHost()) == null) { + // We've encountered a stale host. Return an empty set to + // misroute the request. If misrouted then response will + // contain up to date tablet information that will be processed. + return Collections.emptySet(); + } else { uuidSet.add(hostShardPair.getHost()); + } } return uuidSet; } finally {