Search in sources :

Example 1 with RetryStrategy

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

the class RawManager method callManagement.

private static Mono<RawManagerResponse> callManagement(final Cluster cluster, final RawManagerRequest request, final RawManagerOptions options) {
    final ClusterEnvironment environment = cluster.environment();
    final RawManagerOptions.Built opts = options.build();
    JsonSerializer serializer = opts.serializer() != null ? opts.serializer() : environment.jsonSerializer();
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().managementTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    final GenericManagerRequest req = new GenericManagerRequest(timeout, cluster.core().context(), retryStrategy, () -> {
        FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, request.method(), request.uri());
        for (Map.Entry<String, Object> e : opts.httpHeaders().entrySet()) {
            httpRequest.headers().set(e.getKey(), e.getValue());
        }
        return httpRequest;
    }, request.method().equals(HttpMethod.GET), null);
    cluster.core().send(req);
    return Reactor.wrap(req, req.response(), true).map(res -> new RawManagerResponse(request.serviceType(), serializer, res.httpStatus(), res.content()));
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) Duration(java.time.Duration) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) GenericManagerRequest(com.couchbase.client.core.msg.manager.GenericManagerRequest) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Map(java.util.Map)

Example 2 with RetryStrategy

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

the class AsyncCollection method touchRequest.

/**
 * Helper method to create the touch request.
 *
 * @param id the id of the document to update.
 * @param expiry the new expiry for the document.
 * @param options the custom options.
 * @return the touch request.
 */
TouchRequest touchRequest(final String id, final Expiry expiry, final TouchOptions options) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(expiry, "Expiry", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(options, "TouchOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    TouchOptions.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_TOUCH, opts.parentSpan().orElse(null));
    long encodedExpiry = expiry.encode();
    TouchRequest request = new TouchRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, encodedExpiry, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : Duration(java.time.Duration) GetAndTouchOptions(com.couchbase.client.java.kv.GetAndTouchOptions) TouchOptions(com.couchbase.client.java.kv.TouchOptions) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) TouchRequest(com.couchbase.client.core.msg.kv.TouchRequest) GetAndTouchRequest(com.couchbase.client.core.msg.kv.GetAndTouchRequest) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 3 with RetryStrategy

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

the class AsyncCollection method upsertRequest.

/**
 * Helper method to generate the upsert request.
 *
 * @param id the document id to upsert.
 * @param content the document content to upsert.
 * @param opts custom options to customize the upsert behavior.
 * @return the upsert request.
 */
UpsertRequest upsertRequest(final String id, final Object content, final UpsertOptions.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_UPSERT, 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();
    final UpsertRequest request = new UpsertRequest(id, encoded.encoded(), expiry, opts.preserveExpiry(), encoded.flags(), timeout, coreContext, collectionIdentifier, retryStrategy, opts.durabilityLevel(), span);
    request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
    return request;
}
Also used : UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) 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 4 with RetryStrategy

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

the class AsyncCollection method fullGetRequest.

/**
 * Helper method to create a get request for a full doc fetch.
 *
 * @param id the document id which is used to uniquely identify it.
 * @param opts custom options to change the default behavior.
 * @return the get request.
 */
@Stability.Internal
GetRequest fullGetRequest(final String id, final GetOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET, opts.parentSpan().orElse(null));
    GetRequest request = new GetRequest(id, timeout, coreContext, collectionIdentifier, retryStrategy, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 5 with RetryStrategy

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

the class AsyncCollection method subdocGetRequest.

/**
 * Helper method to create a get request for a subdoc fetch.
 *
 * @param id the document id which is used to uniquely identify it.
 * @param opts custom options to change the default behavior.
 * @return the subdoc get request.
 */
@Stability.Internal
SubdocGetRequest subdocGetRequest(final String id, final GetOptions.Built opts) {
    try {
        notNullOrEmpty(id, "Id");
        if (opts.withExpiry()) {
            if (opts.projections().size() > 15) {
                throw InvalidArgumentException.fromMessage("Only a maximum of 16 fields can be " + "projected per request due to a server limitation (includes the expiration macro as one field).");
            }
        } else {
            if (opts.projections().size() > 16) {
                throw InvalidArgumentException.fromMessage("Only a maximum of 16 fields can be " + "projected per request due to a server limitation.");
            }
        }
    } catch (Exception cause) {
        throw new InvalidArgumentException("Argument validation failed", cause, ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    }
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    List<SubdocGetRequest.Command> commands = new ArrayList<>(16);
    if (!opts.projections().isEmpty()) {
        if (opts.projections().size() > 16) {
            throw new UnsupportedOperationException("Only a maximum of 16 fields can be " + "projected per request.");
        }
        List<String> projections = opts.projections();
        for (int i = 0; i < projections.size(); i++) {
            commands.add(new SubdocGetRequest.Command(SubdocCommandType.GET, projections.get(i), false, commands.size()));
        }
    } else {
        commands.add(new SubdocGetRequest.Command(SubdocCommandType.GET_DOC, "", false, commands.size()));
    }
    if (opts.withExpiry()) {
        // xattrs must go first
        commands.add(0, new SubdocGetRequest.Command(SubdocCommandType.GET, LookupInMacro.EXPIRY_TIME, true, commands.size()));
        // JSON compat flags.
        if (opts.projections().isEmpty()) {
            commands.add(1, new SubdocGetRequest.Command(SubdocCommandType.GET, LookupInMacro.FLAGS, true, commands.size()));
        }
    }
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_LOOKUP_IN, opts.parentSpan().orElse(null));
    SubdocGetRequest request = new SubdocGetRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, (byte) 0x00, commands, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : ArrayList(java.util.ArrayList) Duration(java.time.Duration) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

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