Skip to content

Commit 5fc84f2

Browse files
committed
Use keyspace metadata in BasicLoadBalancingPolicy#getReplicas
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.
1 parent a568e59 commit 5fc84f2

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/loadbalancing/BasicLoadBalancingPolicy.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.datastax.oss.driver.api.core.metadata.Tablet;
3838
import com.datastax.oss.driver.api.core.metadata.TabletMap;
3939
import com.datastax.oss.driver.api.core.metadata.TokenMap;
40+
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
4041
import com.datastax.oss.driver.api.core.metadata.token.Partitioner;
4142
import com.datastax.oss.driver.api.core.metadata.token.Token;
4243
import com.datastax.oss.driver.api.core.session.Request;
@@ -328,7 +329,12 @@ protected Set<Node> getReplicas(@Nullable Request request, @Nullable Session ses
328329
return Collections.emptySet();
329330
}
330331

331-
if (table != null) {
332+
Optional<KeyspaceMetadata> ksMetadata =
333+
context.getMetadataManager().getMetadata().getKeyspace(keyspace);
334+
if (ksMetadata.isPresent() && ksMetadata.get().isUsingTablets()) {
335+
if (table == null) {
336+
return Collections.emptySet();
337+
}
332338
if (token == null) {
333339
if (partitioner != null) {
334340
token = partitioner.hash(key);
@@ -338,12 +344,10 @@ protected Set<Node> getReplicas(@Nullable Request request, @Nullable Session ses
338344
Tablet targetTablet =
339345
tabletMap.getTablet(keyspace, table, ((TokenLong64) token).getValue());
340346
if (targetTablet != null) {
341-
Set<Node> replicas = targetTablet.getReplicaNodes();
342-
if (!replicas.isEmpty()) {
343-
return replicas;
344-
}
347+
return targetTablet.getReplicaNodes();
345348
}
346349
}
350+
return Collections.emptySet();
347351
}
348352

349353
if (!maybeTokenMap.isPresent()) {

core/src/main/java/com/datastax/oss/driver/internal/core/metadata/schema/queries/CassandraSchemaQueries.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,20 @@ private void handleResult(
213213
AdminResult result,
214214
Throwable error,
215215
Function<Iterable<AdminRow>, CassandraSchemaRows.Builder> builderUpdater,
216-
boolean ignoreTargetDoesNotExistErrors) {
216+
boolean ignoreServerErrors) {
217217

218218
// If another query already failed, we've already propagated the failure so just ignore this one
219219
if (schemaRowsFuture.isCompletedExceptionally()) {
220220
return;
221221
}
222222

223-
if (ignoreTargetDoesNotExistErrors && error instanceof UnexpectedResponseException) {
223+
// Meant to allow through "(keyspace/table) does not exist" or "unconfigured" errors for
224+
// specific, optional queries
225+
if (ignoreServerErrors && error instanceof UnexpectedResponseException) {
224226
UnexpectedResponseException castedError = (UnexpectedResponseException) error;
225-
if (castedError.message.opcode == ProtocolConstants.ErrorCode.SERVER_ERROR
226-
&& castedError.getMessage().contains("does not exist")) {
227+
if (castedError.message.opcode == ProtocolConstants.ErrorCode.SERVER_ERROR) {
228+
LOG.debug("Silencing error: ", error);
229+
// Consider such query 'done', but ignore its result
227230
pendingQueries -= 1;
228231
if (pendingQueries == 0) {
229232
LOG.debug(
@@ -244,8 +247,7 @@ private void handleResult(
244247
.nextPage()
245248
.whenCompleteAsync(
246249
(nextResult, nextError) ->
247-
handleResult(
248-
nextResult, nextError, builderUpdater, ignoreTargetDoesNotExistErrors),
250+
handleResult(nextResult, nextError, builderUpdater, ignoreServerErrors),
249251
adminExecutor);
250252
} else {
251253
pendingQueries -= 1;

0 commit comments

Comments
 (0)