Search in sources :

Example 1 with QueryPlan

use of com.datastax.oss.driver.internal.core.util.collection.QueryPlan in project java-driver by datastax.

the class BasicLoadBalancingPolicy method newQueryPlan.

@NonNull
@Override
public Queue<Node> newQueryPlan(@Nullable Request request, @Nullable Session session) {
    // Take a snapshot since the set is concurrent:
    Object[] currentNodes = liveNodes.dc(localDc).toArray();
    Set<Node> allReplicas = getReplicas(request, session);
    // in currentNodes
    int replicaCount = 0;
    if (!allReplicas.isEmpty()) {
        // Move replicas to the beginning
        for (int i = 0; i < currentNodes.length; i++) {
            Node node = (Node) currentNodes[i];
            if (allReplicas.contains(node)) {
                ArrayUtils.bubbleUp(currentNodes, i, replicaCount);
                replicaCount += 1;
            }
        }
        if (replicaCount > 1) {
            shuffleHead(currentNodes, replicaCount);
        }
    }
    LOG.trace("[{}] Prioritizing {} local replicas", logPrefix, replicaCount);
    // Round-robin the remaining nodes
    ArrayUtils.rotate(currentNodes, replicaCount, currentNodes.length - replicaCount, roundRobinAmount.getAndUpdate(INCREMENT));
    QueryPlan plan = currentNodes.length == 0 ? QueryPlan.EMPTY : new SimpleQueryPlan(currentNodes);
    return maybeAddDcFailover(request, plan);
}
Also used : SimpleQueryPlan(com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan) Node(com.datastax.oss.driver.api.core.metadata.Node) CompositeQueryPlan(com.datastax.oss.driver.internal.core.util.collection.CompositeQueryPlan) SimpleQueryPlan(com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan) LazyQueryPlan(com.datastax.oss.driver.internal.core.util.collection.LazyQueryPlan) QueryPlan(com.datastax.oss.driver.internal.core.util.collection.QueryPlan) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with QueryPlan

use of com.datastax.oss.driver.internal.core.util.collection.QueryPlan in project java-driver by datastax.

the class BasicLoadBalancingPolicy method maybeAddDcFailover.

@NonNull
protected Queue<Node> maybeAddDcFailover(@Nullable Request request, @NonNull Queue<Node> local) {
    if (maxNodesPerRemoteDc <= 0 || localDc == null) {
        return local;
    }
    if (!allowDcFailoverForLocalCl && request instanceof Statement) {
        Statement<?> statement = (Statement<?>) request;
        ConsistencyLevel consistency = statement.getConsistencyLevel();
        if (consistency == null) {
            consistency = defaultConsistencyLevel;
        }
        if (consistency.isDcLocal()) {
            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);
}
Also used : ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) Node(com.datastax.oss.driver.api.core.metadata.Node) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) IntUnaryOperator(java.util.function.IntUnaryOperator) LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) ArrayUtils(com.datastax.oss.driver.internal.core.util.ArrayUtils) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) LoggerFactory(org.slf4j.LoggerFactory) ThreadSafe(net.jcip.annotations.ThreadSafe) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) CompositeQueryPlan(com.datastax.oss.driver.internal.core.util.collection.CompositeQueryPlan) ByteBuffer(java.nio.ByteBuffer) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) OptionalLocalDcHelper(com.datastax.oss.driver.internal.core.loadbalancing.helper.OptionalLocalDcHelper) NodeDistance(com.datastax.oss.driver.api.core.loadbalancing.NodeDistance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Map(java.util.Map) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) SimpleQueryPlan(com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan) Predicates(com.datastax.oss.driver.shaded.guava.common.base.Predicates) LazyQueryPlan(com.datastax.oss.driver.internal.core.util.collection.LazyQueryPlan) DcAgnosticNodeSet(com.datastax.oss.driver.internal.core.loadbalancing.nodeset.DcAgnosticNodeSet) Session(com.datastax.oss.driver.api.core.session.Session) NodeState(com.datastax.oss.driver.api.core.metadata.NodeState) Token(com.datastax.oss.driver.api.core.metadata.token.Token) DefaultNodeDistanceEvaluatorHelper(com.datastax.oss.driver.internal.core.loadbalancing.helper.DefaultNodeDistanceEvaluatorHelper) SingleDcNodeSet(com.datastax.oss.driver.internal.core.loadbalancing.nodeset.SingleDcNodeSet) Logger(org.slf4j.Logger) DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) QueryPlan(com.datastax.oss.driver.internal.core.util.collection.QueryPlan) Set(java.util.Set) UUID(java.util.UUID) Request(com.datastax.oss.driver.api.core.session.Request) NodeDistanceEvaluator(com.datastax.oss.driver.api.core.loadbalancing.NodeDistanceEvaluator) Objects(java.util.Objects) MultiDcNodeSet(com.datastax.oss.driver.internal.core.loadbalancing.nodeset.MultiDcNodeSet) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Optional(java.util.Optional) NodeSet(com.datastax.oss.driver.internal.core.loadbalancing.nodeset.NodeSet) Queue(java.util.Queue) Collections(java.util.Collections) Statement(com.datastax.oss.driver.api.core.cql.Statement) CompositeQueryPlan(com.datastax.oss.driver.internal.core.util.collection.CompositeQueryPlan) Statement(com.datastax.oss.driver.api.core.cql.Statement) LazyQueryPlan(com.datastax.oss.driver.internal.core.util.collection.LazyQueryPlan) CompositeQueryPlan(com.datastax.oss.driver.internal.core.util.collection.CompositeQueryPlan) SimpleQueryPlan(com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan) LazyQueryPlan(com.datastax.oss.driver.internal.core.util.collection.LazyQueryPlan) QueryPlan(com.datastax.oss.driver.internal.core.util.collection.QueryPlan) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 3 with QueryPlan

use of com.datastax.oss.driver.internal.core.util.collection.QueryPlan in project java-driver by datastax.

the class DefaultLoadBalancingPolicy method newQueryPlan.

@NonNull
@Override
public Queue<Node> newQueryPlan(@Nullable Request request, @Nullable Session session) {
    if (!avoidSlowReplicas) {
        return super.newQueryPlan(request, session);
    }
    // Take a snapshot since the set is concurrent:
    Object[] currentNodes = getLiveNodes().dc(getLocalDatacenter()).toArray();
    Set<Node> allReplicas = getReplicas(request, session);
    // in currentNodes
    int replicaCount = 0;
    if (!allReplicas.isEmpty()) {
        // Move replicas to the beginning of the plan
        for (int i = 0; i < currentNodes.length; i++) {
            Node node = (Node) currentNodes[i];
            if (allReplicas.contains(node)) {
                ArrayUtils.bubbleUp(currentNodes, i, replicaCount);
                replicaCount++;
            }
        }
        if (replicaCount > 1) {
            shuffleHead(currentNodes, replicaCount);
            if (replicaCount > 2) {
                assert session != null;
                // Test replicas health
                Node newestUpReplica = null;
                // bit mask storing indices of unhealthy replicas
                BitSet unhealthyReplicas = null;
                long mostRecentUpTimeNanos = -1;
                long now = nanoTime();
                for (int i = 0; i < replicaCount; i++) {
                    Node node = (Node) currentNodes[i];
                    assert node != null;
                    Long upTimeNanos = upTimes.get(node);
                    if (upTimeNanos != null && now - upTimeNanos - NEWLY_UP_INTERVAL_NANOS < 0 && upTimeNanos - mostRecentUpTimeNanos > 0) {
                        newestUpReplica = node;
                        mostRecentUpTimeNanos = upTimeNanos;
                    }
                    if (newestUpReplica == null && isUnhealthy(node, session, now)) {
                        if (unhealthyReplicas == null) {
                            unhealthyReplicas = new BitSet(replicaCount);
                        }
                        unhealthyReplicas.set(i);
                    }
                }
                // When:
                // - there isn't any newly UP replica and
                // - there is one or more unhealthy replicas and
                // - there is a majority of healthy replicas
                int unhealthyReplicasCount = unhealthyReplicas == null ? 0 : unhealthyReplicas.cardinality();
                if (newestUpReplica == null && unhealthyReplicasCount > 0 && unhealthyReplicasCount < (replicaCount / 2.0)) {
                    // Reorder the unhealthy replicas to the back of the list
                    // Start from the back of the replicas, then move backwards;
                    // stop once all unhealthy replicas are moved to the back.
                    int counter = 0;
                    for (int i = replicaCount - 1; i >= 0 && counter < unhealthyReplicasCount; i--) {
                        if (unhealthyReplicas.get(i)) {
                            ArrayUtils.bubbleDown(currentNodes, i, replicaCount - 1 - counter);
                            counter++;
                        }
                    }
                } else // - dice roll 1d4 != 1
                if ((newestUpReplica == currentNodes[0] || newestUpReplica == currentNodes[1]) && diceRoll1d4() != 1) {
                    // Send it to the back of the replicas
                    ArrayUtils.bubbleDown(currentNodes, newestUpReplica == currentNodes[0] ? 0 : 1, replicaCount - 1);
                }
                // in-flight requests
                if (getInFlight((Node) currentNodes[0], session) > getInFlight((Node) currentNodes[1], session)) {
                    ArrayUtils.swap(currentNodes, 0, 1);
                }
            }
        }
    }
    LOG.trace("[{}] Prioritizing {} local replicas", logPrefix, replicaCount);
    // Round-robin the remaining nodes
    ArrayUtils.rotate(currentNodes, replicaCount, currentNodes.length - replicaCount, roundRobinAmount.getAndUpdate(INCREMENT));
    QueryPlan plan = currentNodes.length == 0 ? QueryPlan.EMPTY : new SimpleQueryPlan(currentNodes);
    return maybeAddDcFailover(request, plan);
}
Also used : SimpleQueryPlan(com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan) Node(com.datastax.oss.driver.api.core.metadata.Node) BitSet(java.util.BitSet) SimpleQueryPlan(com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan) QueryPlan(com.datastax.oss.driver.internal.core.util.collection.QueryPlan) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

Node (com.datastax.oss.driver.api.core.metadata.Node)3 QueryPlan (com.datastax.oss.driver.internal.core.util.collection.QueryPlan)3 SimpleQueryPlan (com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 CompositeQueryPlan (com.datastax.oss.driver.internal.core.util.collection.CompositeQueryPlan)2 LazyQueryPlan (com.datastax.oss.driver.internal.core.util.collection.LazyQueryPlan)2 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)1 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 DriverContext (com.datastax.oss.driver.api.core.context.DriverContext)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 LoadBalancingPolicy (com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy)1 NodeDistance (com.datastax.oss.driver.api.core.loadbalancing.NodeDistance)1 NodeDistanceEvaluator (com.datastax.oss.driver.api.core.loadbalancing.NodeDistanceEvaluator)1 NodeState (com.datastax.oss.driver.api.core.metadata.NodeState)1 TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)1 Token (com.datastax.oss.driver.api.core.metadata.token.Token)1 Request (com.datastax.oss.driver.api.core.session.Request)1 Session (com.datastax.oss.driver.api.core.session.Session)1