Search in sources :

Example 16 with RetryStrategy

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

the class OptionsBuilder method buildQueryOptions.

static QueryOptions buildQueryOptions(Query query, QueryOptions options, QueryScanConsistency scanConsistency) {
    options = options != null ? options : QueryOptions.queryOptions();
    if (query.getParameters() != null) {
        if (query.getParameters() instanceof JsonArray) {
            options.parameters((JsonArray) query.getParameters());
        } else {
            options.parameters((JsonObject) query.getParameters());
        }
    }
    Meta meta = query.getMeta() != null ? query.getMeta() : new Meta();
    QueryOptions.Built optsBuilt = options.build();
    JsonObject optsJson = getQueryOpts(optsBuilt);
    QueryScanConsistency metaQueryScanConsistency = meta.get(SCAN_CONSISTENCY) != null ? ((ScanConsistency) meta.get(SCAN_CONSISTENCY)).query() : null;
    QueryScanConsistency qsc = fromFirst(QueryScanConsistency.NOT_BOUNDED, getScanConsistency(optsJson), scanConsistency, metaQueryScanConsistency);
    Duration timeout = fromFirst(Duration.ofSeconds(0), getTimeout(optsBuilt), meta.get(TIMEOUT));
    RetryStrategy retryStrategy = fromFirst(null, getRetryStrategy(optsBuilt), meta.get(RETRY_STRATEGY));
    if (qsc != null) {
        options.scanConsistency(qsc);
    }
    if (timeout != null) {
        options.timeout(timeout);
    }
    if (retryStrategy != null) {
        options.retryStrategy(retryStrategy);
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("query options: {}", getQueryOpts(options.build()));
    }
    return options;
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonObject(com.couchbase.client.java.json.JsonObject) QueryScanConsistency(com.couchbase.client.java.query.QueryScanConsistency) Duration(java.time.Duration) QueryOptions(com.couchbase.client.java.query.QueryOptions) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy)

Example 17 with RetryStrategy

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

the class AsyncCollection method lookupInRequest.

/**
 * Helper method to create the underlying lookup subdoc request.
 *
 * @param id the outer document ID.
 * @param specs the spec which specifies the type of lookups to perform.
 * @param opts custom options to modify the lookup options.
 * @return the subdoc lookup request.
 */
SubdocGetRequest lookupInRequest(final String id, final List<LookupInSpec> specs, final LookupInOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNullOrEmpty(specs, "LookupInSpecs", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    ArrayList<SubdocGetRequest.Command> commands = new ArrayList<>(specs.size());
    for (int i = 0; i < specs.size(); i++) {
        LookupInSpec spec = specs.get(i);
        commands.add(spec.export(i));
    }
    // xattrs come first
    commands.sort(Comparator.comparing(v -> !v.xattr()));
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    byte flags = 0;
    if (opts.accessDeleted()) {
        flags |= SubdocMutateRequest.SUBDOC_DOC_FLAG_ACCESS_DELETED;
    }
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_LOOKUP_IN, opts.parentSpan().orElse(null));
    SubdocGetRequest request = new SubdocGetRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, flags, commands, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : MutateInAccessor(com.couchbase.client.java.kv.MutateInAccessor) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) InsertOptions(com.couchbase.client.java.kv.InsertOptions) MutationResult(com.couchbase.client.java.kv.MutationResult) DEFAULT_REPLACE_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_REPLACE_OPTIONS) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) PersistTo(com.couchbase.client.java.kv.PersistTo) ReplaceAccessor(com.couchbase.client.java.kv.ReplaceAccessor) TouchAccessor(com.couchbase.client.java.kv.TouchAccessor) DEFAULT_GET_AND_LOCK_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_AND_LOCK_OPTIONS) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) GetAndTouchOptions(com.couchbase.client.java.kv.GetAndTouchOptions) LookupInMacro(com.couchbase.client.java.kv.LookupInMacro) BucketConfig(com.couchbase.client.core.config.BucketConfig) GetAllReplicasOptions(com.couchbase.client.java.kv.GetAllReplicasOptions) TouchRequest(com.couchbase.client.core.msg.kv.TouchRequest) RemoveOptions(com.couchbase.client.java.kv.RemoveOptions) ExistsOptions(com.couchbase.client.java.kv.ExistsOptions) InsertAccessor(com.couchbase.client.java.kv.InsertAccessor) DEFAULT_INSERT_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_INSERT_OPTIONS) GetAndTouchRequest(com.couchbase.client.core.msg.kv.GetAndTouchRequest) GetReplicaResult(com.couchbase.client.java.kv.GetReplicaResult) ExistsResult(com.couchbase.client.java.kv.ExistsResult) UnlockAccessor(com.couchbase.client.java.kv.UnlockAccessor) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) ReplicaHelper(com.couchbase.client.core.service.kv.ReplicaHelper) Expiry(com.couchbase.client.java.kv.Expiry) List(java.util.List) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest) GetAndLockRequest(com.couchbase.client.core.msg.kv.GetAndLockRequest) LookupInOptions(com.couchbase.client.java.kv.LookupInOptions) SubdocMutateRequest(com.couchbase.client.core.msg.kv.SubdocMutateRequest) GetAccessor(com.couchbase.client.java.kv.GetAccessor) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) Optional(java.util.Optional) ReplaceRequest(com.couchbase.client.core.msg.kv.ReplaceRequest) DEFAULT_GET_ALL_REPLICAS_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_ALL_REPLICAS_OPTIONS) DEFAULT_MUTATE_IN_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_MUTATE_IN_OPTIONS) GetOptions(com.couchbase.client.java.kv.GetOptions) UpsertAccessor(com.couchbase.client.java.kv.UpsertAccessor) RemoveAccessor(com.couchbase.client.java.kv.RemoveAccessor) GetResult(com.couchbase.client.java.kv.GetResult) ExistsAccessor(com.couchbase.client.java.kv.ExistsAccessor) TouchOptions(com.couchbase.client.java.kv.TouchOptions) UnlockOptions(com.couchbase.client.java.kv.UnlockOptions) DEFAULT_GET_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_OPTIONS) RemoveRequest(com.couchbase.client.core.msg.kv.RemoveRequest) StoreSemantics(com.couchbase.client.java.kv.StoreSemantics) CompletableFuture(java.util.concurrent.CompletableFuture) CommonDurabilityOptions(com.couchbase.client.java.kv.CommonDurabilityOptions) GetMetaRequest(com.couchbase.client.core.msg.kv.GetMetaRequest) ArrayList(java.util.ArrayList) DEFAULT_TOUCH_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_TOUCH_OPTIONS) GetAnyReplicaOptions(com.couchbase.client.java.kv.GetAnyReplicaOptions) ReplaceOptions(com.couchbase.client.java.kv.ReplaceOptions) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) GetAndLockOptions(com.couchbase.client.java.kv.GetAndLockOptions) DEFAULT_REMOVE_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_REMOVE_OPTIONS) MutateInSpec(com.couchbase.client.java.kv.MutateInSpec) Stability(com.couchbase.client.core.annotation.Stability) DEFAULT_EXISTS_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_EXISTS_OPTIONS) DEFAULT_GET_ANY_REPLICA_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_ANY_REPLICA_OPTIONS) UpsertOptions(com.couchbase.client.java.kv.UpsertOptions) LookupInResult(com.couchbase.client.java.kv.LookupInResult) MutateInOptions(com.couchbase.client.java.kv.MutateInOptions) MutateInResult(com.couchbase.client.java.kv.MutateInResult) DEFAULT_UPSERT_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_UPSERT_OPTIONS) UnlockRequest(com.couchbase.client.core.msg.kv.UnlockRequest) LookupInAccessor(com.couchbase.client.java.kv.LookupInAccessor) DEFAULT_GET_AND_TOUCH_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_AND_TOUCH_OPTIONS) UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) LookupInSpec(com.couchbase.client.java.kv.LookupInSpec) DEFAULT_LOOKUP_IN_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_LOOKUP_IN_OPTIONS) Transcoder(com.couchbase.client.java.codec.Transcoder) DurabilityLevel(com.couchbase.client.core.msg.kv.DurabilityLevel) 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) Comparator(java.util.Comparator) DEFAULT_UNLOCK_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_UNLOCK_OPTIONS) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) SubdocCommandType(com.couchbase.client.core.msg.kv.SubdocCommandType) BucketConfigUtil(com.couchbase.client.core.util.BucketConfigUtil) ArrayList(java.util.ArrayList) LookupInSpec(com.couchbase.client.java.kv.LookupInSpec) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

Example 18 with RetryStrategy

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

the class AsyncCollection method replaceRequest.

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

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

the class AsyncCollection method unlockRequest.

/**
 * Helper method to create the unlock request.
 *
 * @param id the id of the document.
 * @param cas the CAS value which is needed to unlock it.
 * @param options the options to customize.
 * @return the unlock request.
 */
UnlockRequest unlockRequest(final String id, final long cas, final UnlockOptions options) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(options, "UnlockOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    if (cas == 0) {
        throw new InvalidArgumentException("CAS cannot be 0", null, ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    }
    UnlockOptions.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_UNLOCK, opts.parentSpan().orElse(null));
    UnlockRequest request = new UnlockRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, cas, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) UnlockOptions(com.couchbase.client.java.kv.UnlockOptions) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) UnlockRequest(com.couchbase.client.core.msg.kv.UnlockRequest) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 20 with RetryStrategy

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

the class AsyncScope method analyticsRequest.

/**
 * Helper method to craft an analytics request.
 *
 * @param statement the statement to use.
 * @param opts the built analytics options.
 * @return the created analytics request.
 */
AnalyticsRequest analyticsRequest(final String statement, final AnalyticsOptions.Built opts) {
    notNullOrEmpty(statement, "Statement", () -> new ReducedAnalyticsErrorContext(statement));
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().analyticsTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    JsonObject query = JsonObject.create();
    query.put("statement", statement);
    query.put("timeout", encodeDurationToMs(timeout));
    query.put("query_context", AnalyticsRequest.queryContext(bucketName, scopeName));
    opts.injectParams(query);
    final byte[] queryBytes = query.toString().getBytes(StandardCharsets.UTF_8);
    final String clientContextId = query.getString("client_context_id");
    final RequestSpan span = environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ANALYTICS, opts.parentSpan().orElse(null));
    AnalyticsRequest request = new AnalyticsRequest(timeout, core.context(), retryStrategy, core.context().authenticator(), queryBytes, opts.priority(), opts.readonly(), clientContextId, statement, span, bucketName, scopeName);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : ReducedAnalyticsErrorContext(com.couchbase.client.core.error.context.ReducedAnalyticsErrorContext) AnalyticsRequest(com.couchbase.client.core.msg.analytics.AnalyticsRequest) JsonObject(com.couchbase.client.java.json.JsonObject) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

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