Search in sources :

Example 1 with MutationToken

use of com.couchbase.client.core.msg.kv.MutationToken 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));
}
Also used : Reactor(com.couchbase.client.core.Reactor) Repeat(com.couchbase.client.core.retry.reactor.Repeat) ReplicaNotConfiguredException(com.couchbase.client.core.error.ReplicaNotConfiguredException) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) Mono(reactor.core.publisher.Mono) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) MutationToken(com.couchbase.client.core.msg.kv.MutationToken) Flux(reactor.core.publisher.Flux) List(java.util.List) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) ObserveViaSeqnoRequest(com.couchbase.client.core.msg.kv.ObserveViaSeqnoRequest) Duration(java.time.Duration) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) BucketConfig(com.couchbase.client.core.config.BucketConfig) ObserveViaSeqnoRequest(com.couchbase.client.core.msg.kv.ObserveViaSeqnoRequest) MutationToken(com.couchbase.client.core.msg.kv.MutationToken) ArrayList(java.util.ArrayList) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 2 with MutationToken

use of com.couchbase.client.core.msg.kv.MutationToken in project couchbase-elasticsearch-connector by couchbase.

the class BasicReplicationTest method createDeleteReject.

@Test
public void createDeleteReject() throws Throwable {
    try (TestCouchbaseClient cb = new TestCouchbaseClient(commonConfig)) {
        final Bucket bucket = cb.createTempBucket(couchbase);
        final Collection collection = bucket.defaultCollection();
        final ImmutableConnectorConfig config = withBucketName(commonConfig, bucket.name());
        try (TestEsClient es = new TestEsClient(config);
            AsyncTask connector = AsyncTask.run(() -> ElasticsearchConnector.run(config))) {
            assertIndexInferredFromDocumentId(bucket, es);
            // Create two documents in the same vbucket to make sure we're not conflating seqno and revision number.
            // This first one has a seqno and revision number that are the same... not useful for the test.
            final String firstKeyInVbucket = forceKeyToPartition("createdFirst", 0, 1024).get();
            upsertWithRetry(bucket, JsonDocument.create(firstKeyInVbucket, JsonObject.create()));
            // Here's the document we're going to test! Its seqno should be different than document revision.
            final String blueKey = forceKeyToPartition("color:blue", 0, 1024).get();
            final MutationResult upsertResult = upsertWithRetry(bucket, JsonDocument.create(blueKey, JsonObject.create().put("hex", "0000ff")));
            final JsonNode content = es.waitForDocument(CATCH_ALL_INDEX, blueKey);
            System.out.println(content);
            final long expectedDocumentRevision = 1;
            final JsonNode meta = content.path("meta");
            assertEquals(blueKey, meta.path("id").textValue());
            assertEquals(expectedDocumentRevision, meta.path("revSeqno").longValue());
            assertTrue(meta.path("lockTime").isIntegralNumber());
            assertEquals(0, meta.path("lockTime").longValue());
            assertTrue(meta.path("expiration").isIntegralNumber());
            assertEquals(0, meta.path("expiration").longValue());
            assertThat(meta.path("rev").textValue()).startsWith(expectedDocumentRevision + "-");
            assertTrue(meta.path("flags").isIntegralNumber());
            assertEquals(upsertResult.cas(), meta.path("cas").longValue());
            MutationToken mutationToken = upsertResult.mutationToken().orElseThrow(() -> new AssertionError("expected mutation token"));
            assertEquals(mutationToken.sequenceNumber(), meta.path("seqno").longValue());
            assertEquals(mutationToken.partitionID(), meta.path("vbucket").longValue());
            assertEquals(mutationToken.partitionUUID(), meta.path("vbuuid").longValue());
            assertEquals("0000ff", content.path("doc").path("hex").textValue());
            // Make sure deletions are propagated to elasticsearch
            collection.remove(blueKey);
            es.waitForDeletion(CATCH_ALL_INDEX, blueKey);
            // Create an incompatible document (different type for "hex" field, Object instead of String)
            final String redKey = "color:red";
            upsertWithRetry(bucket, JsonDocument.create(redKey, JsonObject.create().put("hex", JsonObject.create().put("red", "ff").put("green", "00").put("blue", "00"))));
            assertDocumentRejected(es, CATCH_ALL_INDEX, redKey, "mapper_parsing_exception");
            // Elasticsearch doesn't support BigInteger fields. This error surfaces when creating the index request,
            // before the request is sent to Elasticsearch. Make sure we trapped the error and converted it to a rejection.
            final String bigIntKey = "veryLargeNumber";
            upsertWithRetry(bucket, JsonDocument.create(bigIntKey, JsonObject.create().put("number", new BigInteger("17626319910530664276"))));
            assertDocumentRejected(es, CATCH_ALL_INDEX, bigIntKey, "mapper_parsing_exception");
        }
    }
}
Also used : ImmutableConnectorConfig(com.couchbase.connector.config.es.ImmutableConnectorConfig) AsyncTask(com.couchbase.connector.cluster.consul.AsyncTask) JsonNode(com.fasterxml.jackson.databind.JsonNode) Bucket(com.couchbase.client.java.Bucket) MutationToken(com.couchbase.client.core.msg.kv.MutationToken) Collection(com.couchbase.client.java.Collection) BigInteger(java.math.BigInteger) MutationResult(com.couchbase.client.java.kv.MutationResult) Test(org.junit.Test)

Aggregations

MutationToken (com.couchbase.client.core.msg.kv.MutationToken)2 Reactor (com.couchbase.client.core.Reactor)1 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)1 BucketConfig (com.couchbase.client.core.config.BucketConfig)1 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)1 FeatureNotAvailableException (com.couchbase.client.core.error.FeatureNotAvailableException)1 ReplicaNotConfiguredException (com.couchbase.client.core.error.ReplicaNotConfiguredException)1 ObserveViaSeqnoRequest (com.couchbase.client.core.msg.kv.ObserveViaSeqnoRequest)1 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)1 Repeat (com.couchbase.client.core.retry.reactor.Repeat)1 Bucket (com.couchbase.client.java.Bucket)1 Collection (com.couchbase.client.java.Collection)1 MutationResult (com.couchbase.client.java.kv.MutationResult)1 AsyncTask (com.couchbase.connector.cluster.consul.AsyncTask)1 ImmutableConnectorConfig (com.couchbase.connector.config.es.ImmutableConnectorConfig)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BigInteger (java.math.BigInteger)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1