Skip to content

JAVA-3142: Ability to optionally specify remote dcs for deterministic failovers when remote dcs are used in query plan #1896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,13 @@ public enum DefaultDriverOption implements DriverOption {
* <p>Value-type: {@link java.time.Duration}
*/
SSL_KEYSTORE_RELOAD_INTERVAL("advanced.ssl-engine-factory.keystore-reload-interval"),
;
/**
* Ordered preference list of remote dcs optionally supplied for automatic failover.
*
* <p>Value type: {@link java.util.List List}&#60;{@link String}&#62;
*/
LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS(
"advanced.load-balancing-policy.dc-failover.preferred-remote-dcs");

private final String path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ protected static void fillWithDriverDefaults(OptionsMap map) {
map.put(TypedDriverOption.LOAD_BALANCING_DC_FAILOVER_MAX_NODES_PER_REMOTE_DC, 0);
map.put(TypedDriverOption.LOAD_BALANCING_DC_FAILOVER_ALLOW_FOR_LOCAL_CONSISTENCY_LEVELS, false);
map.put(TypedDriverOption.METRICS_GENERATE_AGGREGABLE_HISTOGRAMS, true);
map.put(
TypedDriverOption.LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS, ImmutableList.of(""));
Comment on lines +384 to +385
Copy link
Contributor

@jahstreet jahstreet Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nitinitt . Thanks for this contribution, very useful.
While testing it on my side, I've stumbled upon this default (we use OptionsMap for configuring the driver, but I see the same default is in the reference.conf). With this default, we never go to buildRemoteQueryPlanAll and fall into buildRemoteQueryPlanPreferred, which obviously cannot find nodes of the DC "".
Was this set intentionally to default to a List with empty string for some reason?

Copy link
Contributor Author

@nitinitt nitinitt Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jahstreet
Is the code empty check: https://github.com/apache/cassandra-java-driver/pull/1896/files#diff-bbdb6181132c9a28e06a98058d5b3db08d3398ffb4233f242f8da44b26a9249bR333-R336
if (preferredRemoteDcs.isEmpty()) { return new CompositeQueryPlan(local, buildRemoteQueryPlanAll()); }
not getting invoked?
Apologies for late reply, the intention was that the isEmpty() check prevents this condition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually doesn't, cause there is an empty string in the array.

}

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,16 @@ public String toString() {
DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_ALLOW_FOR_LOCAL_CONSISTENCY_LEVELS,
GenericType.BOOLEAN);

/**
* Ordered preference list of remote dcs optionally supplied for automatic failover and included
* in query plan. This feature is enabled only when max-nodes-per-remote-dc is greater than 0.
*/
public static final TypedDriverOption<List<String>>
LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS =
new TypedDriverOption<>(
DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS,
GenericType.listOf(String.class));

private static Iterable<TypedDriverOption<?>> introspectBuiltInValues() {
try {
ImmutableList.Builder<TypedDriverOption<?>> result = ImmutableList.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@
import com.datastax.oss.driver.internal.core.util.collection.QueryPlan;
import com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan;
import com.datastax.oss.driver.shaded.guava.common.base.Predicates;
import com.datastax.oss.driver.shaded.guava.common.collect.Lists;
import com.datastax.oss.driver.shaded.guava.common.collect.Sets;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -117,6 +121,7 @@ public class BasicLoadBalancingPolicy implements LoadBalancingPolicy {
private volatile NodeDistanceEvaluator nodeDistanceEvaluator;
private volatile String localDc;
private volatile NodeSet liveNodes;
private final LinkedHashSet<String> preferredRemoteDcs;

public BasicLoadBalancingPolicy(@NonNull DriverContext context, @NonNull String profileName) {
this.context = (InternalDriverContext) context;
Expand All @@ -131,6 +136,11 @@ public BasicLoadBalancingPolicy(@NonNull DriverContext context, @NonNull String
this.context
.getConsistencyLevelRegistry()
.nameToLevel(profile.getString(DefaultDriverOption.REQUEST_CONSISTENCY));

preferredRemoteDcs =
new LinkedHashSet<>(
profile.getStringList(
DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS));
}

/**
Expand Down Expand Up @@ -320,27 +330,59 @@ protected Queue<Node> maybeAddDcFailover(@Nullable Request request, @NonNull Que
return local;
}
}
QueryPlan remote =
new LazyQueryPlan() {

@Override
protected Object[] computeNodes() {
Object[] remoteNodes =
liveNodes.dcs().stream()
.filter(Predicates.not(Predicates.equalTo(localDc)))
.flatMap(dc -> liveNodes.dc(dc).stream().limit(maxNodesPerRemoteDc))
.toArray();

int remoteNodesLength = remoteNodes.length;
if (remoteNodesLength == 0) {
return EMPTY_NODES;
}
shuffleHead(remoteNodes, remoteNodesLength);
return remoteNodes;
}
};

return new CompositeQueryPlan(local, remote);
if (preferredRemoteDcs.isEmpty()) {
return new CompositeQueryPlan(local, buildRemoteQueryPlanAll());
}
return new CompositeQueryPlan(local, buildRemoteQueryPlanPreferred());
}

private QueryPlan buildRemoteQueryPlanAll() {

return new LazyQueryPlan() {
@Override
protected Object[] computeNodes() {

Object[] remoteNodes =
liveNodes.dcs().stream()
.filter(Predicates.not(Predicates.equalTo(localDc)))
.flatMap(dc -> liveNodes.dc(dc).stream().limit(maxNodesPerRemoteDc))
.toArray();
if (remoteNodes.length == 0) {
return EMPTY_NODES;
}
shuffleHead(remoteNodes, remoteNodes.length);
return remoteNodes;
}
};
}

private QueryPlan buildRemoteQueryPlanPreferred() {

Set<String> dcs = liveNodes.dcs();
List<String> orderedDcs = Lists.newArrayListWithCapacity(dcs.size());
orderedDcs.addAll(preferredRemoteDcs);
orderedDcs.addAll(Sets.difference(dcs, preferredRemoteDcs));

QueryPlan[] queryPlans =
orderedDcs.stream()
.filter(Predicates.not(Predicates.equalTo(localDc)))
.map(
(dc) -> {
return new LazyQueryPlan() {
@Override
protected Object[] computeNodes() {
Object[] rv = liveNodes.dc(dc).stream().limit(maxNodesPerRemoteDc).toArray();
if (rv.length == 0) {
return EMPTY_NODES;
}
shuffleHead(rv, rv.length);
return rv;
}
};
})
.toArray(QueryPlan[]::new);

return new CompositeQueryPlan(queryPlans);
}

/** Exposed as a protected method so that it can be accessed by tests */
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ datastax-java-driver {
# Modifiable at runtime: no
# Overridable in a profile: yes
allow-for-local-consistency-levels = false
# Ordered preference list of remote dc's (in order) optionally supplied for automatic failover. While building a query plan, the driver uses the DC's supplied in order together with max-nodes-per-remote-dc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: should be a space between this and allow-for-local-consistency-levels in order to make it clear we're talking about two distinct configs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another nit: might be worth mentioning that users aren't required to specify all DCs when listing preferences via this config.

# Required: no
# Modifiable at runtime: no
# Overridable in a profile: no
preferred-remote-dcs = [""]
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.oss.driver.internal.core.loadbalancing;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.internal.core.metadata.DefaultNode;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet;
import java.util.Map;
import java.util.UUID;
import org.junit.Test;
import org.mockito.Mock;

public class BasicLoadBalancingPolicyPreferredRemoteDcsTest
extends BasicLoadBalancingPolicyDcFailoverTest {
@Mock protected DefaultNode node10;
@Mock protected DefaultNode node11;
@Mock protected DefaultNode node12;
@Mock protected DefaultNode node13;
@Mock protected DefaultNode node14;

@Override
@Test
public void should_prioritize_single_replica() {
when(request.getRoutingKeyspace()).thenReturn(KEYSPACE);
when(request.getRoutingKey()).thenReturn(ROUTING_KEY);
when(tokenMap.getReplicas(KEYSPACE, ROUTING_KEY)).thenReturn(ImmutableSet.of(node3));

// node3 always first, round-robin on the rest
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node3, node1, node2, node4, node5, node9, node10, node6, node7, node12, node13);
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node3, node2, node4, node5, node1, node9, node10, node6, node7, node12, node13);
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node3, node4, node5, node1, node2, node9, node10, node6, node7, node12, node13);
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node3, node5, node1, node2, node4, node9, node10, node6, node7, node12, node13);

// Should not shuffle replicas since there is only one
verify(policy, never()).shuffleHead(any(), eq(1));
// But should shuffle remote nodes
verify(policy, times(12)).shuffleHead(any(), eq(2));
}

@Override
@Test
public void should_prioritize_and_shuffle_replicas() {
when(request.getRoutingKeyspace()).thenReturn(KEYSPACE);
when(request.getRoutingKey()).thenReturn(ROUTING_KEY);
when(tokenMap.getReplicas(KEYSPACE, ROUTING_KEY))
.thenReturn(ImmutableSet.of(node1, node2, node3, node6, node9));

// node 6 and 9 being in a remote DC, they don't get a boost for being a replica
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node1, node2, node3, node4, node5, node9, node10, node6, node7, node12, node13);
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node1, node2, node3, node5, node4, node9, node10, node6, node7, node12, node13);

// should shuffle replicas
verify(policy, times(2)).shuffleHead(any(), eq(3));
// should shuffle remote nodes
verify(policy, times(6)).shuffleHead(any(), eq(2));
// No power of two choices with only two replicas
verify(session, never()).getPools();
}

@Override
protected void assertRoundRobinQueryPlans() {
for (int i = 0; i < 3; i++) {
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node1, node2, node3, node4, node5, node9, node10, node6, node7, node12, node13);
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node2, node3, node4, node5, node1, node9, node10, node6, node7, node12, node13);
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node3, node4, node5, node1, node2, node9, node10, node6, node7, node12, node13);
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node4, node5, node1, node2, node3, node9, node10, node6, node7, node12, node13);
assertThat(policy.newQueryPlan(request, session))
.containsExactly(
node5, node1, node2, node3, node4, node9, node10, node6, node7, node12, node13);
}

verify(policy, atLeast(15)).shuffleHead(any(), eq(2));
}

@Override
protected BasicLoadBalancingPolicy createAndInitPolicy() {
when(node4.getDatacenter()).thenReturn("dc1");
when(node5.getDatacenter()).thenReturn("dc1");
when(node6.getDatacenter()).thenReturn("dc2");
when(node7.getDatacenter()).thenReturn("dc2");
when(node8.getDatacenter()).thenReturn("dc2");
when(node9.getDatacenter()).thenReturn("dc3");
when(node10.getDatacenter()).thenReturn("dc3");
when(node11.getDatacenter()).thenReturn("dc3");
when(node12.getDatacenter()).thenReturn("dc4");
when(node13.getDatacenter()).thenReturn("dc4");
when(node14.getDatacenter()).thenReturn("dc4");

// Accept 2 nodes per remote DC
when(defaultProfile.getInt(
DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_MAX_NODES_PER_REMOTE_DC))
.thenReturn(2);
when(defaultProfile.getBoolean(
DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_ALLOW_FOR_LOCAL_CONSISTENCY_LEVELS))
.thenReturn(false);

when(defaultProfile.getStringList(
DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS))
.thenReturn(ImmutableList.of("dc3", "dc2"));

// Use a subclass to disable shuffling, we just spy to make sure that the shuffling method was
// called (makes tests easier)
BasicLoadBalancingPolicy policy =
spy(
new BasicLoadBalancingPolicy(context, DriverExecutionProfile.DEFAULT_NAME) {
@Override
protected void shuffleHead(Object[] currentNodes, int headLength) {
// nothing (keep in same order)
}
});
Map<UUID, Node> nodes =
ImmutableMap.<UUID, Node>builder()
.put(UUID.randomUUID(), node1)
.put(UUID.randomUUID(), node2)
.put(UUID.randomUUID(), node3)
.put(UUID.randomUUID(), node4)
.put(UUID.randomUUID(), node5)
.put(UUID.randomUUID(), node6)
.put(UUID.randomUUID(), node7)
.put(UUID.randomUUID(), node8)
.put(UUID.randomUUID(), node9)
.put(UUID.randomUUID(), node10)
.put(UUID.randomUUID(), node11)
.put(UUID.randomUUID(), node12)
.put(UUID.randomUUID(), node13)
.put(UUID.randomUUID(), node14)
.build();
policy.init(nodes, distanceReporter);
assertThat(policy.getLiveNodes().dc("dc1")).containsExactly(node1, node2, node3, node4, node5);
assertThat(policy.getLiveNodes().dc("dc2")).containsExactly(node6, node7); // only 2 allowed
assertThat(policy.getLiveNodes().dc("dc3")).containsExactly(node9, node10); // only 2 allowed
assertThat(policy.getLiveNodes().dc("dc4")).containsExactly(node12, node13); // only 2 allowed
return policy;
}
}