Skip to content

Commit

Permalink
JAVA-2987 BasicLoadBalancingPolicy remote compute nodes check all liv…
Browse files Browse the repository at this point in the history
…eNodes (#1576)

* BasicLoadBalancingPolicy remote compute nodes -> don't presume local dc nodes to be up and among the liveNodes that where found (can happen if the local dc wasn't up when the application started)

* BasicLoadBalancingPolicy: follow suggestion from @absurdfarce for remote computeNodes to make the code cleaner and more efficient

* BasicLoadBalancingPolicy: fix formatting
  • Loading branch information
Schnikonos authored Feb 14, 2022
1 parent 85efee4 commit 73f9141
Showing 1 changed file with 10 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.datastax.oss.driver.internal.core.util.collection.LazyQueryPlan;
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 edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -322,30 +323,19 @@ protected Queue<Node> maybeAddDcFailover(@Nullable Request request, @NonNull Que

@Override
protected Object[] computeNodes() {
Object[] dcs = liveNodes.dcs().toArray();
if (dcs.length <= 1) {
return EMPTY_NODES;
}
Object[] remoteNodes = new Object[(dcs.length - 1) * maxNodesPerRemoteDc];
int remoteNodesLength = 0;
for (Object dc : dcs) {
if (!dc.equals(localDc)) {
Object[] remoteNodesInDc = liveNodes.dc((String) dc).toArray();
for (int i = 0; i < maxNodesPerRemoteDc && i < remoteNodesInDc.length; i++) {
remoteNodes[remoteNodesLength++] = remoteNodesInDc[i];
}
}
}
Set<String> dcs = liveNodes.dcs();
Object[] remoteNodes =
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);
if (remoteNodes.length == remoteNodesLength) {
return remoteNodes;
}
Object[] trimmedRemoteNodes = new Object[remoteNodesLength];
System.arraycopy(remoteNodes, 0, trimmedRemoteNodes, 0, remoteNodesLength);
return trimmedRemoteNodes;
return remoteNodes;
}
};

Expand Down

0 comments on commit 73f9141

Please sign in to comment.