use of com.datastax.oss.driver.api.core.loadbalancing.NodeDistance in project java-driver by datastax.
the class BasicLoadBalancingPolicy method onAdd.
@Override
public void onAdd(@NonNull Node node) {
NodeDistance distance = computeNodeDistance(node);
// Setting to a non-ignored distance triggers the session to open a pool, which will in turn
// set the node UP when the first channel gets opened, then #onUp will be called, and the
// node will be eventually added to the live set.
distanceReporter.setDistance(node, distance);
LOG.debug("[{}] {} was added, setting distance to {}", logPrefix, node, distance);
}
use of com.datastax.oss.driver.api.core.loadbalancing.NodeDistance in project java-driver by datastax.
the class BasicLoadBalancingPolicy method init.
@Override
public void init(@NonNull Map<UUID, Node> nodes, @NonNull DistanceReporter distanceReporter) {
this.distanceReporter = distanceReporter;
localDc = discoverLocalDc(nodes).orElse(null);
nodeDistanceEvaluator = createNodeDistanceEvaluator(localDc, nodes);
liveNodes = localDc == null ? new DcAgnosticNodeSet() : maxNodesPerRemoteDc <= 0 ? new SingleDcNodeSet(localDc) : new MultiDcNodeSet();
for (Node node : nodes.values()) {
NodeDistance distance = computeNodeDistance(node);
distanceReporter.setDistance(node, distance);
if (distance != NodeDistance.IGNORED && node.getState() != NodeState.DOWN) {
// This includes state == UNKNOWN. If the node turns out to be unreachable, this will be
// detected when we try to open a pool to it, it will get marked down and this will be
// signaled back to this policy, which will then remove it from the live set.
liveNodes.add(node);
}
}
}
use of com.datastax.oss.driver.api.core.loadbalancing.NodeDistance in project java-driver by datastax.
the class DataCentersFinder method getDataCenters.
@VisibleForTesting
Set<String> getDataCenters(Collection<Node> nodes, DriverExecutionProfile executionProfile) {
int remoteConnectionsLength = executionProfile.getInt(CONNECTION_POOL_REMOTE_SIZE);
Set<String> dataCenters = new HashSet<>();
for (Node n : nodes) {
NodeDistance distance = n.getDistance();
if (distance.equals(NodeDistance.LOCAL) || (distance.equals(NodeDistance.REMOTE) && remoteConnectionsLength > 0)) {
dataCenters.add(n.getDatacenter());
}
}
return dataCenters;
}
use of com.datastax.oss.driver.api.core.loadbalancing.NodeDistance in project java-driver by datastax.
the class PerProfileLoadBalancingPolicyIT method setup.
@BeforeClass
public static void setup() {
// sanity checks
DriverContext context = SESSION_RULE.session().getContext();
DriverConfig config = context.getConfig();
assertThat(config.getProfiles()).containsKeys("profile1", "profile2");
assertThat(context.getLoadBalancingPolicies()).hasSize(3).containsKeys(DriverExecutionProfile.DEFAULT_NAME, "profile1", "profile2");
LoadBalancingPolicy defaultPolicy = context.getLoadBalancingPolicy(DriverExecutionProfile.DEFAULT_NAME);
LoadBalancingPolicy policy1 = context.getLoadBalancingPolicy("profile1");
LoadBalancingPolicy policy2 = context.getLoadBalancingPolicy("profile2");
assertThat(defaultPolicy).isSameAs(policy2).isNotSameAs(policy1);
for (Node node : SESSION_RULE.session().getMetadata().getNodes().values()) {
// if node is in dc2 it should be ignored, otherwise (dc1, dc3) it should be local.
NodeDistance expectedDistance = Objects.equals(node.getDatacenter(), "dc2") ? NodeDistance.IGNORED : NodeDistance.LOCAL;
assertThat(node.getDistance()).isEqualTo(expectedDistance);
}
}
Aggregations