Search in sources :

Example 1 with NodeDistance

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);
}
Also used : NodeDistance(com.datastax.oss.driver.api.core.loadbalancing.NodeDistance)

Example 2 with NodeDistance

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);
        }
    }
}
Also used : SingleDcNodeSet(com.datastax.oss.driver.internal.core.loadbalancing.nodeset.SingleDcNodeSet) NodeDistance(com.datastax.oss.driver.api.core.loadbalancing.NodeDistance) MultiDcNodeSet(com.datastax.oss.driver.internal.core.loadbalancing.nodeset.MultiDcNodeSet) Node(com.datastax.oss.driver.api.core.metadata.Node) DcAgnosticNodeSet(com.datastax.oss.driver.internal.core.loadbalancing.nodeset.DcAgnosticNodeSet)

Example 3 with NodeDistance

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;
}
Also used : NodeDistance(com.datastax.oss.driver.api.core.loadbalancing.NodeDistance) Node(com.datastax.oss.driver.api.core.metadata.Node) HashSet(java.util.HashSet) VisibleForTesting(com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting)

Example 4 with NodeDistance

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);
    }
}
Also used : DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) NodeDistance(com.datastax.oss.driver.api.core.loadbalancing.NodeDistance) LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) Node(com.datastax.oss.driver.api.core.metadata.Node) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) BeforeClass(org.junit.BeforeClass)

Aggregations

NodeDistance (com.datastax.oss.driver.api.core.loadbalancing.NodeDistance)4 Node (com.datastax.oss.driver.api.core.metadata.Node)3 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 DriverContext (com.datastax.oss.driver.api.core.context.DriverContext)1 LoadBalancingPolicy (com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy)1 DcAgnosticNodeSet (com.datastax.oss.driver.internal.core.loadbalancing.nodeset.DcAgnosticNodeSet)1 MultiDcNodeSet (com.datastax.oss.driver.internal.core.loadbalancing.nodeset.MultiDcNodeSet)1 SingleDcNodeSet (com.datastax.oss.driver.internal.core.loadbalancing.nodeset.SingleDcNodeSet)1 VisibleForTesting (com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting)1 HashSet (java.util.HashSet)1 BeforeClass (org.junit.BeforeClass)1