Skip to content

Commit

Permalink
Use keyspace metadata in BasicLoadBalancingPolicy#getReplicas
Browse files Browse the repository at this point in the history
Modifies the getReplicas method to do the tablet lookup if and only if the
keyspace metadata indicates that it's a tablets-based keyspace. Otherwise refer
to the token map.
Previous behavior was to try tablet map lookup first regardless of the
keyspace configuration.
  • Loading branch information
Bouncheck committed Jan 10, 2025
1 parent 01987fe commit 98ebc5f
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.datastax.oss.driver.api.core.metadata.Tablet;
import com.datastax.oss.driver.api.core.metadata.TabletMap;
import com.datastax.oss.driver.api.core.metadata.TokenMap;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import com.datastax.oss.driver.api.core.metadata.token.Partitioner;
import com.datastax.oss.driver.api.core.metadata.token.Token;
import com.datastax.oss.driver.api.core.session.Request;
Expand Down Expand Up @@ -328,7 +329,12 @@ protected Set<Node> getReplicas(@Nullable Request request, @Nullable Session ses
return Collections.emptySet();
}

if (table != null) {
Optional<KeyspaceMetadata> ksMetadata =
context.getMetadataManager().getMetadata().getKeyspace(keyspace);
if (ksMetadata.isPresent() && ksMetadata.get().isUsingTablets()) {
if (table == null) {
return Collections.emptySet();
}
if (token == null) {
if (partitioner != null) {
token = partitioner.hash(key);
Expand All @@ -338,12 +344,10 @@ protected Set<Node> getReplicas(@Nullable Request request, @Nullable Session ses
Tablet targetTablet =
tabletMap.getTablet(keyspace, table, ((TokenLong64) token).getValue());
if (targetTablet != null) {
Set<Node> replicas = targetTablet.getReplicaNodes();
if (!replicas.isEmpty()) {
return replicas;
}
return targetTablet.getReplicaNodes();
}
}
return Collections.emptySet();
}

if (!maybeTokenMap.isPresent()) {
Expand Down

0 comments on commit 98ebc5f

Please sign in to comment.