Search in sources :

Example 1 with SimpleQueryPlan

use of com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan 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 SimpleQueryPlan

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

the class CqlRequestHandler method onThrottleReady.

@Override
public void onThrottleReady(boolean wasDelayed) {
    DriverExecutionProfile executionProfile = Conversions.resolveExecutionProfile(initialStatement, context);
    if (wasDelayed && // avoid call to nanoTime() if metric is disabled:
    sessionMetricUpdater.isEnabled(DefaultSessionMetric.THROTTLING_DELAY, executionProfile.getName())) {
        sessionMetricUpdater.updateTimer(DefaultSessionMetric.THROTTLING_DELAY, executionProfile.getName(), System.nanoTime() - startTimeNanos, TimeUnit.NANOSECONDS);
    }
    Queue<Node> queryPlan = this.initialStatement.getNode() != null ? new SimpleQueryPlan(this.initialStatement.getNode()) : context.getLoadBalancingPolicyWrapper().newQueryPlan(initialStatement, executionProfile.getName(), session);
    sendRequest(initialStatement, null, queryPlan, 0, 0, true);
}
Also used : SimpleQueryPlan(com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Node(com.datastax.oss.driver.api.core.metadata.Node)

Example 3 with SimpleQueryPlan

use of com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan 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)

Example 4 with SimpleQueryPlan

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

the class GraphRequestHandler method onThrottleReady.

@Override
public void onThrottleReady(boolean wasDelayed) {
    DriverExecutionProfile executionProfile = Conversions.resolveExecutionProfile(initialStatement, context);
    if (wasDelayed && // avoid call to nanoTime() if metric is disabled:
    sessionMetricUpdater.isEnabled(DefaultSessionMetric.THROTTLING_DELAY, executionProfile.getName())) {
        sessionMetricUpdater.updateTimer(DefaultSessionMetric.THROTTLING_DELAY, executionProfile.getName(), System.nanoTime() - startTimeNanos, TimeUnit.NANOSECONDS);
    }
    Queue<Node> queryPlan = initialStatement.getNode() != null ? new SimpleQueryPlan(initialStatement.getNode()) : context.getLoadBalancingPolicyWrapper().newQueryPlan(initialStatement, executionProfile.getName(), session);
    sendRequest(initialStatement, null, queryPlan, 0, 0, true);
}
Also used : SimpleQueryPlan(com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Node(com.datastax.oss.driver.api.core.metadata.Node)

Aggregations

Node (com.datastax.oss.driver.api.core.metadata.Node)4 SimpleQueryPlan (com.datastax.oss.driver.internal.core.util.collection.SimpleQueryPlan)4 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)2 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)2 QueryPlan (com.datastax.oss.driver.internal.core.util.collection.QueryPlan)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 GraphNode (com.datastax.dse.driver.api.core.graph.GraphNode)1 CompositeQueryPlan (com.datastax.oss.driver.internal.core.util.collection.CompositeQueryPlan)1 LazyQueryPlan (com.datastax.oss.driver.internal.core.util.collection.LazyQueryPlan)1 BitSet (java.util.BitSet)1