Search in sources :

Example 11 with RequestSpan

use of com.couchbase.client.core.cnc.RequestSpan in project couchbase-jvm-clients by couchbase.

the class AsyncBinaryCollection method appendRequest.

AppendRequest appendRequest(final String id, final byte[] content, final AppendOptions.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());
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_APPEND, opts.parentSpan().orElse(null));
    AppendRequest request = new AppendRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, content, opts.cas(), opts.durabilityLevel(), span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : Duration(java.time.Duration) AppendRequest(com.couchbase.client.core.msg.kv.AppendRequest) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 12 with RequestSpan

use of com.couchbase.client.core.cnc.RequestSpan in project couchbase-jvm-clients by couchbase.

the class AsyncBucket method viewRequest.

ViewRequest viewRequest(final String designDoc, final String viewName, final ViewOptions.Built opts) {
    notNullOrEmpty(designDoc, "DesignDoc", () -> new ReducedViewErrorContext(designDoc, viewName, name));
    notNullOrEmpty(viewName, "ViewName", () -> new ReducedViewErrorContext(designDoc, viewName, name));
    String query = opts.query();
    Optional<byte[]> keysJson = Optional.ofNullable(opts.keys()).map(s -> s.getBytes(StandardCharsets.UTF_8));
    boolean development = opts.development();
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().viewTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    final RequestSpan span = environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_VIEWS, opts.parentSpan().orElse(null));
    ViewRequest request = new ViewRequest(timeout, core.context(), retryStrategy, authenticator, name, designDoc, viewName, query, keysJson, development, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : ReducedViewErrorContext(com.couchbase.client.core.error.context.ReducedViewErrorContext) Duration(java.time.Duration) ViewRequest(com.couchbase.client.core.msg.view.ViewRequest) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 13 with RequestSpan

use of com.couchbase.client.core.cnc.RequestSpan 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 14 with RequestSpan

use of com.couchbase.client.core.cnc.RequestSpan 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 15 with RequestSpan

use of com.couchbase.client.core.cnc.RequestSpan 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)

Aggregations

RequestSpan (com.couchbase.client.core.cnc.RequestSpan)42 Duration (java.time.Duration)30 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)28 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 Stability (com.couchbase.client.core.annotation.Stability)10 Validators.notNullOrEmpty (com.couchbase.client.core.util.Validators.notNullOrEmpty)10 List (java.util.List)10 Core (com.couchbase.client.core.Core)9 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 BucketConfig (com.couchbase.client.core.config.BucketConfig)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)8 Objects.requireNonNull (java.util.Objects.requireNonNull)8 Mono (reactor.core.publisher.Mono)8 Reactor (com.couchbase.client.core.Reactor)7 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)7 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)7 Function (java.util.function.Function)7 CoreContext (com.couchbase.client.core.CoreContext)6