Search in sources :

Example 6 with CasMismatchException

use of com.couchbase.client.core.error.CasMismatchException in project couchbase-jvm-clients by couchbase.

the class SubdocMutateIntegrationTest method reviveDocumentWithCAS.

@IgnoreWhen(missesCapabilities = { Capabilities.SUBDOC_REVIVE_DOCUMENT })
@Test
void reviveDocumentWithCAS() {
    String docId = docId();
    JsonObject body = JsonObject.create().put("foo", "bar");
    MutateInResult mr = coll.mutateIn(docId, Collections.singletonList(upsert("txn", JsonObject.create().put("stgd", body).put("baz", "qux")).xattr().createPath()), MutateInOptions.mutateInOptions().createAsDeleted(true).accessDeleted(true).storeSemantics(StoreSemantics.INSERT));
    // Create a CAS mismatch
    coll.mutateIn(docId, Collections.singletonList(upsert("txn", JsonObject.create().put("stgd", body).put("baz", "qux")).xattr().createPath()), MutateInOptions.mutateInOptions().accessDeleted(true));
    try {
        coll.mutateIn(docId, Arrays.asList(new ReplaceBodyWithXattr("txn.stgd"), MutateInSpec.remove("txn").xattr()), MutateInOptions.mutateInOptions().accessDeleted(true).cas(mr.cas()).storeSemantics(StoreSemantics.REVIVE));
        fail();
    } catch (CasMismatchException ignored) {
    }
}
Also used : MutateInResult(com.couchbase.client.java.kv.MutateInResult) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) JsonObject(com.couchbase.client.java.json.JsonObject) ReplaceBodyWithXattr(com.couchbase.client.java.kv.ReplaceBodyWithXattr) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 7 with CasMismatchException

use of com.couchbase.client.core.error.CasMismatchException in project couchbase-jvm-clients by couchbase.

the class SubdocIntegrationTest method subdocCASWithDurability.

// JVMCBC-728
@Test
@IgnoreWhen(missesCapabilities = Capabilities.SYNC_REPLICATION, clusterTypes = ClusterType.CAVES)
void subdocCASWithDurability() {
    JsonObject initial = JsonObject.create().put("mutated", 0);
    String id = UUID.randomUUID().toString();
    collection.upsert(id, initial);
    GetResult gr = collection.get(id);
    int errorCount = 0;
    try {
        collection.mutateIn(id, Collections.singletonList(MutateInSpec.upsert("mutated", 1)), MutateInOptions.mutateInOptions().cas(gr.cas()).durability(DurabilityLevel.MAJORITY));
    } catch (CasMismatchException err) {
        errorCount += 1;
    }
    try {
        collection.mutateIn(id, Collections.singletonList(MutateInSpec.upsert("mutated", 2)), MutateInOptions.mutateInOptions().cas(gr.cas()).durability(DurabilityLevel.MAJORITY));
    } catch (CasMismatchException err) {
        errorCount += 1;
    }
    assertEquals(1, errorCount);
}
Also used : CasMismatchException(com.couchbase.client.core.error.CasMismatchException) JsonObject(com.couchbase.client.java.json.JsonObject) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 8 with CasMismatchException

use of com.couchbase.client.core.error.CasMismatchException 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 9 with CasMismatchException

use of com.couchbase.client.core.error.CasMismatchException 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 10 with CasMismatchException

use of com.couchbase.client.core.error.CasMismatchException 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

CasMismatchException (com.couchbase.client.core.error.CasMismatchException)12 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)7 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)6 PathNotFoundException (com.couchbase.client.core.error.subdoc.PathNotFoundException)5 LookupInResult (com.couchbase.client.java.kv.LookupInResult)5 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)5 Test (org.junit.jupiter.api.Test)5 JsonObject (com.couchbase.client.java.json.JsonObject)4 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)4 GetResult (com.couchbase.client.java.kv.GetResult)2 MutateInResult (com.couchbase.client.java.kv.MutateInResult)2 ReplaceBodyWithXattr (com.couchbase.client.java.kv.ReplaceBodyWithXattr)2 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)1 DmlFailureException (com.couchbase.client.core.error.DmlFailureException)1 DocumentExistsException (com.couchbase.client.core.error.DocumentExistsException)1 ErrorCodeAndMessage (com.couchbase.client.core.error.ErrorCodeAndMessage)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