Search in sources :

Example 1 with RetryExhaustedException

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

the class AsyncQueryIndexManager method watchIndexes.

/**
 * Watches/Polls indexes until they are online with custom options.
 * <p>
 * By default, this method will watch the indexes on the bucket. If the indexes should be watched on a collection,
 * both {@link WatchQueryIndexesOptions#scopeName(String)} and
 * {@link WatchQueryIndexesOptions#collectionName(String)} must be set.
 *
 * @param bucketName the name of the bucket where the indexes should be watched.
 * @param indexNames the names of the indexes to watch.
 * @param timeout the maximum amount of time the indexes should be watched.
 * @param options the custom options to apply.
 * @return a {@link CompletableFuture} completing when the operation is applied or failed with an error.
 * @throws CouchbaseException (async) if any other generic unhandled/unexpected errors.
 */
public CompletableFuture<Void> watchIndexes(final String bucketName, final Collection<String> indexNames, final Duration timeout, final WatchQueryIndexesOptions options) {
    notNullOrEmpty(bucketName, "BucketName");
    notNull(indexNames, "IndexNames");
    notNull(timeout, "Timeout");
    notNull(options, "Options");
    Set<String> indexNameSet = new HashSet<>(indexNames);
    WatchQueryIndexesOptions.Built builtOpts = options.build();
    RequestSpan parent = cluster.environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_MQ_WATCH_INDEXES, null);
    parent.attribute(TracingIdentifiers.ATTR_SYSTEM, TracingIdentifiers.ATTR_SYSTEM_COUCHBASE);
    return Mono.fromFuture(() -> failIfIndexesOffline(bucketName, indexNameSet, builtOpts.watchPrimary(), parent, builtOpts.scopeName(), builtOpts.collectionName())).retryWhen(Retry.onlyIf(ctx -> hasCause(ctx.exception(), IndexesNotReadyException.class)).exponentialBackoff(Duration.ofMillis(50), Duration.ofSeconds(1)).timeout(timeout).toReactorRetry()).onErrorMap(t -> t instanceof RetryExhaustedException ? toWatchTimeoutException(t, timeout) : t).toFuture().whenComplete((r, t) -> parent.end());
}
Also used : Arrays(java.util.Arrays) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CbThrowables.findCause(com.couchbase.client.core.util.CbThrowables.findCause) TimeoutException(java.util.concurrent.TimeoutException) QueryResult(com.couchbase.client.java.query.QueryResult) Collectors.toMap(java.util.stream.Collectors.toMap) Duration(java.time.Duration) Map(java.util.Map) CbThrowables.throwIfUnchecked(com.couchbase.client.core.util.CbThrowables.throwIfUnchecked) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) READ_ONLY(com.couchbase.client.java.manager.query.AsyncQueryIndexManager.QueryType.READ_ONLY) IndexExistsException(com.couchbase.client.core.error.IndexExistsException) Collectors.toSet(java.util.stream.Collectors.toSet) DropPrimaryQueryIndexOptions.dropPrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.DropPrimaryQueryIndexOptions.dropPrimaryQueryIndexOptions) CbThrowables.hasCause(com.couchbase.client.core.util.CbThrowables.hasCause) Predicate(java.util.function.Predicate) Mapper(com.couchbase.client.core.json.Mapper) Collection(java.util.Collection) Set(java.util.Set) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) QueryOptions(com.couchbase.client.java.query.QueryOptions) List(java.util.List) GetAllQueryIndexesOptions.getAllQueryIndexesOptions(com.couchbase.client.java.manager.query.GetAllQueryIndexesOptions.getAllQueryIndexesOptions) RedactableArgument.redactMeta(com.couchbase.client.core.logging.RedactableArgument.redactMeta) Optional(java.util.Optional) WRITE(com.couchbase.client.java.manager.query.AsyncQueryIndexManager.QueryType.WRITE) CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions) CreateQueryIndexOptions.createQueryIndexOptions(com.couchbase.client.java.manager.query.CreateQueryIndexOptions.createQueryIndexOptions) Retry(com.couchbase.client.core.retry.reactor.Retry) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) CompletableFuture(java.util.concurrent.CompletableFuture) WatchQueryIndexesOptions.watchQueryIndexesOptions(com.couchbase.client.java.manager.query.WatchQueryIndexesOptions.watchQueryIndexesOptions) QueryException(com.couchbase.client.core.error.QueryException) Function(java.util.function.Function) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CommonOptions(com.couchbase.client.java.CommonOptions) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) IndexFailureException(com.couchbase.client.core.error.IndexFailureException) IndexesNotReadyException(com.couchbase.client.core.error.IndexesNotReadyException) DropQueryIndexOptions.dropQueryIndexOptions(com.couchbase.client.java.manager.query.DropQueryIndexOptions.dropQueryIndexOptions) Reactor(com.couchbase.client.core.Reactor) IndexNotFoundException(com.couchbase.client.core.error.IndexNotFoundException) Mono(reactor.core.publisher.Mono) JsonArray(com.couchbase.client.java.json.JsonArray) Collectors.toList(java.util.stream.Collectors.toList) BuildQueryIndexOptions.buildDeferredQueryIndexesOptions(com.couchbase.client.java.manager.query.BuildQueryIndexOptions.buildDeferredQueryIndexesOptions) AsyncCluster(com.couchbase.client.java.AsyncCluster) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) HashSet(java.util.HashSet) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 2 with RetryExhaustedException

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

the class CouchbaseArrayList method set.

@Override
public E set(int index, E element) {
    // fail fast on negative values, as they are interpreted as "starting from the back of the array" otherwise
    if (index < 0) {
        throw new IndexOutOfBoundsException("Index: " + index);
    }
    String idx = "[" + index + "]";
    for (int i = 0; i < arrayListOptions.casMismatchRetries(); i++) {
        try {
            LookupInResult current = collection.lookupIn(id, Collections.singletonList(LookupInSpec.get(idx)), lookupInOptions);
            long returnCas = current.cas();
            // this loop ensures we return exactly what we replaced
            final E result = current.contentAs(0, entityTypeClass);
            collection.mutateIn(id, Collections.singletonList(MutateInSpec.replace(idx, element)), arrayListOptions.mutateInOptions().cas(returnCas));
            return result;
        } catch (DocumentNotFoundException e) {
            createEmptyList();
        } catch (CasMismatchException ex) {
        // will need to retry get-and-set
        } catch (PathNotFoundException ex) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }
    throw new CouchbaseException("CouchbaseArrayList set failed", new RetryExhaustedException("Couldn't perform set in less than " + arrayListOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : LookupInResult(com.couchbase.client.java.kv.LookupInResult) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) PathNotFoundException(com.couchbase.client.core.error.subdoc.PathNotFoundException)

Example 3 with RetryExhaustedException

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

the class CouchbaseArrayList method remove.

@Override
public E remove(int index) {
    // fail fast on negative values, as they are interpreted as "starting from the back of the array" otherwise
    if (index < 0) {
        throw new IndexOutOfBoundsException("Index: " + index);
    }
    String idx = "[" + index + "]";
    for (int i = 0; i < arrayListOptions.casMismatchRetries(); i++) {
        try {
            // this loop will allow us to _know_ what element we really did remove.
            LookupInResult current = collection.lookupIn(id, Collections.singletonList(LookupInSpec.get(idx)), lookupInOptions);
            long returnCas = current.cas();
            E result = current.contentAs(0, entityTypeClass);
            collection.mutateIn(id, Collections.singletonList(MutateInSpec.remove(idx)), arrayListOptions.mutateInOptions().cas(returnCas));
            return result;
        } catch (DocumentNotFoundException e) {
            // ArrayList will throw if underlying list was cleared before a remove.
            throw new IndexOutOfBoundsException("Index:" + index);
        } catch (CasMismatchException ex) {
        // will have to retry get-and-remove
        } catch (PathNotFoundException e) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }
    throw new CouchbaseException("CouchbaseArrayList remove failed", new RetryExhaustedException("Couldn't perform remove in less than " + arrayListOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : LookupInResult(com.couchbase.client.java.kv.LookupInResult) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) PathNotFoundException(com.couchbase.client.core.error.subdoc.PathNotFoundException)

Example 4 with RetryExhaustedException

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

the class CouchbaseArraySet method remove.

@Override
public boolean remove(Object t) {
    enforcePrimitive(t);
    for (int i = 0; i < arraySetOptions.casMismatchRetries(); i++) {
        try {
            GetResult result = collection.get(id);
            JsonArray current = result.contentAsArray();
            long cas = result.cas();
            int index = 0;
            boolean found = false;
            for (Object next : current) {
                if (safeEquals(next, t)) {
                    found = true;
                    break;
                }
                index++;
            }
            String path = "[" + index + "]";
            if (!found) {
                return false;
            } else {
                collection.mutateIn(id, Collections.singletonList(MutateInSpec.remove(path)), arraySetOptions.mutateInOptions().cas(cas));
                return true;
            }
        } catch (CasMismatchException e) {
        // retry
        } catch (DocumentNotFoundException ex) {
            return false;
        }
    }
    throw new CouchbaseException("CouchbaseArraySet remove failed", new RetryExhaustedException("Couldn't perform remove in less than " + arraySetOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) GetResult(com.couchbase.client.java.kv.GetResult) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 5 with RetryExhaustedException

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

the class CouchbaseMap method remove.

@Override
public E remove(Object key) {
    String idx = checkKey(key);
    for (int i = 0; i < mapOptions.casMismatchRetries(); i++) {
        try {
            LookupInResult current = collection.lookupIn(id, Collections.singletonList(LookupInSpec.get(idx)), lookupInOptions);
            long returnCas = current.cas();
            E result = current.contentAs(0, entityTypeClass);
            collection.mutateIn(id, Collections.singletonList(MutateInSpec.remove(idx)), mapOptions.mutateInOptions().cas(returnCas));
            return result;
        } catch (DocumentNotFoundException | PathNotFoundException e) {
            return null;
        } catch (CasMismatchException ex) {
        // will have to retry get-and-remove
        }
    }
    throw new CouchbaseException("CouchbaseMap remove failed", new RetryExhaustedException("Couldn't perform remove in less than " + mapOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : LookupInResult(com.couchbase.client.java.kv.LookupInResult) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) PathNotFoundException(com.couchbase.client.core.error.subdoc.PathNotFoundException)

Aggregations

CouchbaseException (com.couchbase.client.core.error.CouchbaseException)7 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)7 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)6 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)6 PathNotFoundException (com.couchbase.client.core.error.subdoc.PathNotFoundException)5 LookupInResult (com.couchbase.client.java.kv.LookupInResult)5 JsonArray (com.couchbase.client.java.json.JsonArray)2 Reactor (com.couchbase.client.core.Reactor)1 Stability (com.couchbase.client.core.annotation.Stability)1 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)1 IndexExistsException (com.couchbase.client.core.error.IndexExistsException)1 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)1 IndexNotFoundException (com.couchbase.client.core.error.IndexNotFoundException)1 IndexesNotReadyException (com.couchbase.client.core.error.IndexesNotReadyException)1 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)1 QueryException (com.couchbase.client.core.error.QueryException)1 Mapper (com.couchbase.client.core.json.Mapper)1 RedactableArgument.redactMeta (com.couchbase.client.core.logging.RedactableArgument.redactMeta)1 Retry (com.couchbase.client.core.retry.reactor.Retry)1