Search in sources :

Example 46 with CoreContext

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

the class ReplicaHelper method getAllReplicasRequests.

/**
 * Helper method to assemble a stream of requests to the active and all replicas
 *
 * @param core the core to execute the request
 * @param collectionIdentifier the collection containing the document
 * @param documentId the ID of the document
 * @param clientContext (nullable) client context info
 * @param retryStrategy the retry strategy to use
 * @param timeout the timeout until we need to stop the get all replicas
 * @param parent the "get all/any replicas" request span
 * @return a stream of requests.
 */
public static CompletableFuture<Stream<GetRequest>> getAllReplicasRequests(final Core core, final CollectionIdentifier collectionIdentifier, final String documentId, final Map<String, Object> clientContext, final RetryStrategy retryStrategy, final Duration timeout, final RequestSpan parent) {
    notNullOrEmpty(documentId, "Id");
    final CoreContext coreContext = core.context();
    final CoreEnvironment environment = coreContext.environment();
    final BucketConfig config = core.clusterConfig().bucketConfig(collectionIdentifier.bucket());
    if (config instanceof CouchbaseBucketConfig) {
        int numReplicas = ((CouchbaseBucketConfig) config).numberOfReplicas();
        List<GetRequest> requests = new ArrayList<>(numReplicas + 1);
        RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET, parent);
        GetRequest activeRequest = new GetRequest(documentId, timeout, coreContext, collectionIdentifier, retryStrategy, span);
        activeRequest.context().clientContext(clientContext);
        requests.add(activeRequest);
        for (short replica = 1; replica <= numReplicas; replica++) {
            RequestSpan replicaSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET_REPLICA, parent);
            ReplicaGetRequest replicaRequest = new ReplicaGetRequest(documentId, timeout, coreContext, collectionIdentifier, retryStrategy, replica, replicaSpan);
            replicaRequest.context().clientContext(clientContext);
            requests.add(replicaRequest);
        }
        return CompletableFuture.completedFuture(requests.stream());
    } else if (config == null) {
        // no bucket config found, it might be in-flight being opened so we need to reschedule the operation until
        // the timeout fires!
        final Duration retryDelay = Duration.ofMillis(100);
        final CompletableFuture<Stream<GetRequest>> future = new CompletableFuture<>();
        coreContext.environment().timer().schedule(() -> {
            getAllReplicasRequests(core, collectionIdentifier, documentId, clientContext, retryStrategy, timeout.minus(retryDelay), parent).whenComplete((getRequestStream, throwable) -> {
                if (throwable != null) {
                    future.completeExceptionally(throwable);
                } else {
                    future.complete(getRequestStream);
                }
            });
        }, retryDelay);
        return future;
    } else {
        final CompletableFuture<Stream<GetRequest>> future = new CompletableFuture<>();
        future.completeExceptionally(CommonExceptions.getFromReplicaNotCouchbaseBucket());
        return future;
    }
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) DefaultErrorUtil.keyValueStatusToException(com.couchbase.client.core.error.DefaultErrorUtil.keyValueStatusToException) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocumentUnretrievableException(com.couchbase.client.core.error.DocumentUnretrievableException) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) GetResponse(com.couchbase.client.core.msg.kv.GetResponse) IndividualReplicaGetFailedEvent(com.couchbase.client.core.cnc.events.request.IndividualReplicaGetFailedEvent) BucketConfig(com.couchbase.client.core.config.BucketConfig) Reactor(com.couchbase.client.core.Reactor) CommonExceptions(com.couchbase.client.core.error.CommonExceptions) Mono(reactor.core.publisher.Mono) CompletionException(java.util.concurrent.CompletionException) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest) ErrorContext(com.couchbase.client.core.error.context.ErrorContext) 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) Collections(java.util.Collections) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) Duration(java.time.Duration) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CompletableFuture(java.util.concurrent.CompletableFuture) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest)

Example 47 with CoreContext

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

the class BaseEndpointTest method beforeEach.

@BeforeEach
void beforeEach() {
    eventLoopGroup = new NioEventLoopGroup(1);
    eventBus = new SimpleEventBus(true, Collections.singletonList(EndpointStateChangedEvent.class));
    environment = CoreEnvironment.builder().eventBus(eventBus).build();
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, environment, authenticator);
    ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) NioEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.nio.NioEventLoopGroup) Core(com.couchbase.client.core.Core) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 48 with CoreContext

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

the class BaseEndpointTest method disconnectDuringRetry.

/**
 * Make sure that while we are retrying and a disconnect event comes along, we stop
 * retrying and end up in the disconnected state right away.
 */
@Test
void disconnectDuringRetry() {
    SimpleEventBus eventBus = new SimpleEventBus(true, Collections.singletonList(EndpointStateChangedEvent.class));
    CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).timeoutConfig(TimeoutConfig.connectTimeout(Duration.ofMillis(10))).build();
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, authenticator);
    ServiceContext ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
    try {
        final CompletableFuture<Channel> cf = new CompletableFuture<>();
        InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
        endpoint.connect();
        waitUntilCondition(() -> eventBus.publishedEvents().size() >= 3);
        endpoint.disconnect();
        waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
        waitUntilCondition(() -> eventBus.publishedEvents().size() >= 4);
        int warn = 0;
        int debug = 0;
        for (Event event : eventBus.publishedEvents()) {
            if (event.severity() == Event.Severity.WARN) {
                warn++;
                assertTrue(event instanceof EndpointConnectionFailedEvent);
            } else if (event.severity() == Event.Severity.DEBUG) {
                debug++;
                assertTrue(event instanceof EndpointConnectionAbortedEvent);
            } else {
                throw new RuntimeException("Unexpected Event: " + event);
            }
        }
        assertEquals(3, warn);
        assertEquals(1, debug);
    } finally {
        environment.shutdown();
    }
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) EndpointStateChangedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent) CompletableFuture(java.util.concurrent.CompletableFuture) EndpointConnectionAbortedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EndpointDisconnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) EndpointConnectionAbortedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) EndpointConnectionIgnoredEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent) Event(com.couchbase.client.core.cnc.Event) EndpointStateChangedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent) EndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 49 with CoreContext

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

the class RequestContextTest method customPayloadCanBeAttached.

@Test
void customPayloadCanBeAttached() {
    Request<?> request = mock(Request.class);
    Core core = mock(Core.class);
    RequestContext ctx = new RequestContext(new CoreContext(core, 1, null, mock(Authenticator.class)), request);
    assertNull(ctx.clientContext());
    Map<String, Object> payload = new HashMap<>();
    payload.put("foo", true);
    ctx.clientContext(payload);
    assertEquals(payload, ctx.clientContext());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) HashMap(java.util.HashMap) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 50 with CoreContext

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

the class RetryOrchestratorTest method cancelIfNoMoreRetriesAllowed.

@Test
@SuppressWarnings({ "unchecked" })
void cancelIfNoMoreRetriesAllowed() {
    RetryStrategy retryStrategy = mock(RetryStrategy.class);
    when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.noRetry()));
    Request<?> request = mock(Request.class);
    when(request.completed()).thenReturn(false);
    when(request.retryStrategy()).thenReturn(retryStrategy);
    RequestContext requestContext = mock(RequestContext.class);
    when(request.context()).thenReturn(requestContext);
    CoreEnvironment env = mock(CoreEnvironment.class);
    SimpleEventBus eventBus = new SimpleEventBus(true);
    when(env.eventBus()).thenReturn(eventBus);
    CoreContext context = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    RetryOrchestrator.maybeRetry(context, request, RetryReason.UNKNOWN);
    verify(request, times(1)).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN), Function.identity());
    assertEquals(1, eventBus.publishedEvents().size());
    RequestNotRetriedEvent retryEvent = (RequestNotRetriedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.INFO, retryEvent.severity());
    assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
    assertEquals(requestContext, retryEvent.context());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) RequestNotRetriedEvent(com.couchbase.client.core.cnc.events.request.RequestNotRetriedEvent) Request(com.couchbase.client.core.msg.Request) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) RequestContext(com.couchbase.client.core.msg.RequestContext) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Aggregations

CoreContext (com.couchbase.client.core.CoreContext)52 Core (com.couchbase.client.core.Core)46 Test (org.junit.jupiter.api.Test)25 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)22 Authenticator (com.couchbase.client.core.env.Authenticator)17 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)14 BeforeEach (org.junit.jupiter.api.BeforeEach)13 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 ArrayList (java.util.ArrayList)10 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)9 HostAndPort (com.couchbase.client.core.util.HostAndPort)9 List (java.util.List)8 TimeoutConfig (com.couchbase.client.core.env.TimeoutConfig)7 ServiceContext (com.couchbase.client.core.service.ServiceContext)7 Duration (java.time.Duration)7 Optional (java.util.Optional)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 SeedNode (com.couchbase.client.core.env.SeedNode)6 Event (com.couchbase.client.core.cnc.Event)5