Search in sources :

Example 1 with Request

use of com.datastax.oss.driver.api.core.session.Request in project java-driver by datastax.

the class ExecutionInfo method getSafePagingState.

/**
 * The paging state of the query, in a safe wrapper that checks if it's reused on the right
 * statement.
 *
 * <p>This represents the next page to be fetched if this query has multiple page of results. It
 * can be saved and reused later on the same statement.
 *
 * @return the paging state, or {@code null} if there is no next page.
 */
@Nullable
default PagingState getSafePagingState() {
    // Default implementation for backward compatibility, but we override it in the concrete class,
    // because it knows the attachment point.
    ByteBuffer rawPagingState = getPagingState();
    if (rawPagingState == null) {
        return null;
    } else {
        Request request = getRequest();
        if (!(request instanceof Statement)) {
            throw new IllegalStateException("Only statements should have a paging state");
        }
        Statement<?> statement = (Statement<?>) request;
        return new DefaultPagingState(rawPagingState, statement, AttachmentPoint.NONE);
    }
}
Also used : DefaultPagingState(com.datastax.oss.driver.internal.core.cql.DefaultPagingState) Request(com.datastax.oss.driver.api.core.session.Request) ByteBuffer(java.nio.ByteBuffer) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 2 with Request

use of com.datastax.oss.driver.api.core.session.Request 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

Request (com.datastax.oss.driver.api.core.session.Request)2 Nullable (edu.umd.cs.findbugs.annotations.Nullable)2 ByteBuffer (java.nio.ByteBuffer)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 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 Session (com.datastax.oss.driver.api.core.session.Session)1 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)1 DefaultPagingState (com.datastax.oss.driver.internal.core.cql.DefaultPagingState)1 DefaultNodeDistanceEvaluatorHelper (com.datastax.oss.driver.internal.core.loadbalancing.helper.DefaultNodeDistanceEvaluatorHelper)1