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;
}
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;
}
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());
}
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));
}
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;
}
Aggregations