Search in sources :

Example 11 with RetryStrategy

use of com.couchbase.client.core.retry.RetryStrategy in project couchbase-jvm-clients by couchbase.

the class AsyncCollection method existsRequest.

/**
 * Helper method to create the exists request from its options.
 *
 * @param id the document ID
 * @param options custom options to change the default behavior
 * @return the observe request used for exists.
 */
GetMetaRequest existsRequest(final String id, final ExistsOptions options) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(options, "ExistsOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    ExistsOptions.Built opts = options.build();
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_EXISTS, opts.parentSpan().orElse(null));
    GetMetaRequest request = new GetMetaRequest(id, timeout, coreContext, collectionIdentifier, retryStrategy, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : GetMetaRequest(com.couchbase.client.core.msg.kv.GetMetaRequest) Duration(java.time.Duration) ExistsOptions(com.couchbase.client.java.kv.ExistsOptions) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 12 with RetryStrategy

use of com.couchbase.client.core.retry.RetryStrategy in project couchbase-jvm-clients by couchbase.

the class AsyncCollection method insertRequest.

/**
 * Helper method to generate the insert request.
 *
 * @param id the document id to insert.
 * @param content the document content to insert.
 * @param opts custom options to customize the insert behavior.
 * @return the insert request.
 */
InsertRequest insertRequest(final String id, final Object content, final InsertOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(content, "Content", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    Duration timeout = decideKvTimeout(opts, environment.timeoutConfig());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_INSERT, opts.parentSpan().orElse(null));
    final RequestSpan encodeSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ENCODING, span);
    long start = System.nanoTime();
    Transcoder.EncodedValue encoded;
    try {
        encoded = transcoder.encode(content);
    } finally {
        encodeSpan.end();
    }
    long end = System.nanoTime();
    long expiry = opts.expiry().encode();
    InsertRequest request = new InsertRequest(id, encoded.encoded(), expiry, encoded.flags(), timeout, coreContext, collectionIdentifier, retryStrategy, opts.durabilityLevel(), span);
    request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
    return request;
}
Also used : InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) Duration(java.time.Duration) Transcoder(com.couchbase.client.java.codec.Transcoder) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 13 with RetryStrategy

use of com.couchbase.client.core.retry.RetryStrategy in project couchbase-jvm-clients by couchbase.

the class ReplicaHelper method getAnyReplicaAsync.

/**
 * @param clientContext (nullable)
 * @param parentSpan (nullable)
 * @param responseMapper converts the GetReplicaResponse to the client's native result type
 */
public static <R> CompletableFuture<R> getAnyReplicaAsync(final Core core, final CollectionIdentifier collectionIdentifier, final String documentId, final Duration timeout, final RetryStrategy retryStrategy, final Map<String, Object> clientContext, final RequestSpan parentSpan, final Function<GetReplicaResponse, R> responseMapper) {
    RequestSpan getAnySpan = core.context().environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_GET_ANY_REPLICA, parentSpan);
    CompletableFuture<List<CompletableFuture<R>>> listOfFutures = getAllReplicasAsync(core, collectionIdentifier, documentId, timeout, retryStrategy, clientContext, getAnySpan, responseMapper);
    // Aggregating the futures here will discard the individual errors, which we don't need
    CompletableFuture<R> anyReplicaFuture = new CompletableFuture<>();
    listOfFutures.whenComplete((futures, throwable) -> {
        if (throwable != null) {
            anyReplicaFuture.completeExceptionally(throwable);
        }
        final AtomicBoolean successCompleted = new AtomicBoolean(false);
        final AtomicInteger totalCompleted = new AtomicInteger(0);
        final List<ErrorContext> nestedContexts = Collections.synchronizedList(new ArrayList<>());
        futures.forEach(individual -> individual.whenComplete((result, error) -> {
            int completed = totalCompleted.incrementAndGet();
            if (error != null) {
                if (error instanceof CompletionException && error.getCause() instanceof CouchbaseException) {
                    nestedContexts.add(((CouchbaseException) error.getCause()).context());
                }
            }
            if (result != null && successCompleted.compareAndSet(false, true)) {
                anyReplicaFuture.complete(result);
            }
            if (!successCompleted.get() && completed == futures.size()) {
                anyReplicaFuture.completeExceptionally(new DocumentUnretrievableException(new AggregateErrorContext(nestedContexts)));
            }
        }));
    });
    return anyReplicaFuture.whenComplete((getReplicaResult, throwable) -> getAnySpan.end());
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) DefaultErrorUtil.keyValueStatusToException(com.couchbase.client.core.error.DefaultErrorUtil.keyValueStatusToException) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocumentUnretrievableException(com.couchbase.client.core.error.DocumentUnretrievableException) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) GetResponse(com.couchbase.client.core.msg.kv.GetResponse) IndividualReplicaGetFailedEvent(com.couchbase.client.core.cnc.events.request.IndividualReplicaGetFailedEvent) BucketConfig(com.couchbase.client.core.config.BucketConfig) Reactor(com.couchbase.client.core.Reactor) CommonExceptions(com.couchbase.client.core.error.CommonExceptions) Mono(reactor.core.publisher.Mono) CompletionException(java.util.concurrent.CompletionException) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest) ErrorContext(com.couchbase.client.core.error.context.ErrorContext) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) ReducedKeyValueErrorContext(com.couchbase.client.core.error.context.ReducedKeyValueErrorContext) Collections(java.util.Collections) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) DocumentUnretrievableException(com.couchbase.client.core.error.DocumentUnretrievableException) ErrorContext(com.couchbase.client.core.error.context.ErrorContext) ReducedKeyValueErrorContext(com.couchbase.client.core.error.context.ReducedKeyValueErrorContext) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) CompletionException(java.util.concurrent.CompletionException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 14 with RetryStrategy

use of com.couchbase.client.core.retry.RetryStrategy in project couchbase-jvm-clients by couchbase.

the class Observe method viaMutationToken.

private static Flux<ObserveItem> viaMutationToken(final int bucketReplicas, final ObserveContext ctx, final RequestSpan parent) {
    if (!ctx.mutationToken().isPresent()) {
        throw new IllegalStateException("MutationToken is not present, this is a bug!");
    }
    Duration timeout = ctx.timeout();
    RetryStrategy retryStrategy = ctx.retryStrategy();
    MutationToken mutationToken = ctx.mutationToken().get();
    String id = ctx.key();
    List<ObserveViaSeqnoRequest> requests = new ArrayList<>();
    if (ctx.persistTo() != ObservePersistTo.NONE) {
        final RequestSpan span = ctx.environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_OBSERVE, parent);
        requests.add(new ObserveViaSeqnoRequest(timeout, ctx, ctx.collectionIdentifier(), retryStrategy, 0, true, mutationToken.partitionUUID(), id, span));
    }
    if (ctx.persistTo().touchesReplica() || ctx.replicateTo().touchesReplica()) {
        for (short i = 1; i <= bucketReplicas; i++) {
            final RequestSpan span = ctx.environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_OBSERVE, parent);
            requests.add(new ObserveViaSeqnoRequest(timeout, ctx, ctx.collectionIdentifier(), retryStrategy, i, false, mutationToken.partitionUUID(), id, span));
        }
    }
    return Flux.fromIterable(requests).flatMap(request -> {
        ctx.core().send(request);
        return Reactor.wrap(request, request.response(), true).onErrorResume(t -> Mono.empty()).doFinally(signalType -> request.context().logicallyComplete());
    }).map(response -> ObserveItem.fromMutationToken(mutationToken, response));
}
Also used : Reactor(com.couchbase.client.core.Reactor) Repeat(com.couchbase.client.core.retry.reactor.Repeat) ReplicaNotConfiguredException(com.couchbase.client.core.error.ReplicaNotConfiguredException) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) Mono(reactor.core.publisher.Mono) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) MutationToken(com.couchbase.client.core.msg.kv.MutationToken) Flux(reactor.core.publisher.Flux) List(java.util.List) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) ObserveViaSeqnoRequest(com.couchbase.client.core.msg.kv.ObserveViaSeqnoRequest) Duration(java.time.Duration) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) BucketConfig(com.couchbase.client.core.config.BucketConfig) ObserveViaSeqnoRequest(com.couchbase.client.core.msg.kv.ObserveViaSeqnoRequest) MutationToken(com.couchbase.client.core.msg.kv.MutationToken) ArrayList(java.util.ArrayList) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 15 with RetryStrategy

use of com.couchbase.client.core.retry.RetryStrategy in project spring-data-couchbase by spring-projects.

the class OptionsBuilder method buildRemoveOptions.

public static RemoveOptions buildRemoveOptions(RemoveOptions options, PersistTo persistTo, ReplicateTo replicateTo, DurabilityLevel durabilityLevel, Long cas) {
    options = options != null ? options : RemoveOptions.removeOptions();
    if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
        options.durability(persistTo, replicateTo);
    } else if (durabilityLevel != DurabilityLevel.NONE) {
        options.durability(durabilityLevel);
    }
    RemoveOptions.Built optsBuilt = options.build();
    Duration timeout = fromFirst(Duration.ofSeconds(0), optsBuilt.timeout());
    RetryStrategy retryStrategy = fromFirst(null, optsBuilt.retryStrategy());
    if (timeout != null) {
        options.timeout(timeout);
    }
    if (retryStrategy != null) {
        options.retryStrategy(retryStrategy);
    }
    if (cas != null) {
        options.cas(cas);
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("remove options: {}" + toString(options));
    }
    return options;
}
Also used : Duration(java.time.Duration) RemoveOptions(com.couchbase.client.java.kv.RemoveOptions) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy)

Aggregations

RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)31 Duration (java.time.Duration)31 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)28 ArrayList (java.util.ArrayList)8 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)7 BucketConfig (com.couchbase.client.core.config.BucketConfig)7 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)7 List (java.util.List)7 Core (com.couchbase.client.core.Core)6 CoreContext (com.couchbase.client.core.CoreContext)6 Stability (com.couchbase.client.core.annotation.Stability)6 ReducedKeyValueErrorContext (com.couchbase.client.core.error.context.ReducedKeyValueErrorContext)6 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)6 Validators.notNullOrEmpty (com.couchbase.client.core.util.Validators.notNullOrEmpty)6 JsonObject (com.couchbase.client.java.json.JsonObject)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Reactor (com.couchbase.client.core.Reactor)5 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)5 Map (java.util.Map)5 Flux (reactor.core.publisher.Flux)5