Search in sources :

Example 1 with LazyQueryPlan

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

Aggregations

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 Node (com.datastax.oss.driver.api.core.metadata.Node)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 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)1 DefaultNodeDistanceEvaluatorHelper (com.datastax.oss.driver.internal.core.loadbalancing.helper.DefaultNodeDistanceEvaluatorHelper)1 OptionalLocalDcHelper (com.datastax.oss.driver.internal.core.loadbalancing.helper.OptionalLocalDcHelper)1 DcAgnosticNodeSet (com.datastax.oss.driver.internal.core.loadbalancing.nodeset.DcAgnosticNodeSet)1 MultiDcNodeSet (com.datastax.oss.driver.internal.core.loadbalancing.nodeset.MultiDcNodeSet)1