Search in sources :

Example 6 with RequestContext

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

the class RetryOrchestratorTest method retryWithDelay.

@Test
@SuppressWarnings({ "unchecked" })
void retryWithDelay() {
    Timer timer = Timer.createAndStart(CoreEnvironment.DEFAULT_MAX_NUM_REQUESTS_IN_RETRY);
    RetryStrategy retryStrategy = mock(RetryStrategy.class);
    when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.withDuration(Duration.ofMillis(200))));
    Request<?> request = mock(Request.class);
    RequestContext requestContext = mock(RequestContext.class);
    when(request.completed()).thenReturn(false);
    when(request.context()).thenReturn(requestContext);
    when(request.retryStrategy()).thenReturn(retryStrategy);
    when(request.absoluteTimeout()).thenReturn(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(2500));
    Core core = mock(Core.class);
    CoreEnvironment env = mock(CoreEnvironment.class);
    SimpleEventBus eventBus = new SimpleEventBus(true);
    when(env.timer()).thenReturn(timer);
    when(env.eventBus()).thenReturn(eventBus);
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    long start = System.nanoTime();
    RetryOrchestrator.maybeRetry(ctx, request, RetryReason.UNKNOWN);
    verify(requestContext, times(1)).incrementRetryAttempts(Duration.ofMillis(200), RetryReason.UNKNOWN);
    verify(request, never()).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN));
    waitUntilCondition(() -> !Mockito.mockingDetails(core).getInvocations().isEmpty());
    long end = System.nanoTime();
    verify(core, times(1)).send(request, false);
    verify(core, never()).send(request);
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) >= 200);
    timer.stop();
    assertEquals(1, eventBus.publishedEvents().size());
    RequestRetryScheduledEvent retryEvent = (RequestRetryScheduledEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.DEBUG, retryEvent.severity());
    assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
    assertEquals(requestContext, retryEvent.context());
    assertEquals(RetryReason.UNKNOWN, retryEvent.retryReason());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Request(com.couchbase.client.core.msg.Request) Timer(com.couchbase.client.core.Timer) 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)

Example 7 with RequestContext

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

the class RetryOrchestratorTest method capsRetryDelay.

@Test
@SuppressWarnings({ "unchecked" })
void capsRetryDelay() {
    Timer timer = Timer.createAndStart(CoreEnvironment.DEFAULT_MAX_NUM_REQUESTS_IN_RETRY);
    RetryStrategy retryStrategy = mock(RetryStrategy.class);
    when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.withDuration(Duration.ofMillis(200))));
    Request<?> request = mock(Request.class);
    RequestContext requestContext = mock(RequestContext.class);
    when(request.completed()).thenReturn(false);
    when(request.context()).thenReturn(requestContext);
    when(request.retryStrategy()).thenReturn(retryStrategy);
    when(request.absoluteTimeout()).thenAnswer(invocationOnMock -> System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(10));
    Core core = mock(Core.class);
    CoreEnvironment env = mock(CoreEnvironment.class);
    SimpleEventBus eventBus = new SimpleEventBus(true);
    when(env.timer()).thenReturn(timer);
    when(env.eventBus()).thenReturn(eventBus);
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    long start = System.nanoTime();
    RetryOrchestrator.maybeRetry(ctx, request, RetryReason.UNKNOWN);
    verify(requestContext, never()).incrementRetryAttempts((Duration.ofMillis(200)), RetryReason.UNKNOWN);
    verify(request, never()).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN));
    waitUntilCondition(() -> !Mockito.mockingDetails(core).getInvocations().isEmpty());
    long end = System.nanoTime();
    verify(core, times(1)).send(request, false);
    verify(core, never()).send(request);
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) < 200);
    timer.stop();
    assertEquals(1, eventBus.publishedEvents().size());
    RequestRetryScheduledEvent retryEvent = (RequestRetryScheduledEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.DEBUG, retryEvent.severity());
    assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
    assertEquals(requestContext, retryEvent.context());
    assertEquals(RetryReason.UNKNOWN, retryEvent.retryReason());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Request(com.couchbase.client.core.msg.Request) Timer(com.couchbase.client.core.Timer) 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)

Example 8 with RequestContext

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

the class Core method initGlobalConfig.

/**
 * Instructs the client to, if possible, load and initialize the global config.
 *
 * <p>Since global configs are an "optional" feature depending on the cluster version, if an error happens
 * this method will not fail. Rather it will log the exception (with some logic dependent on the type of error)
 * and will allow the higher level components to move on where possible.</p>
 */
@Stability.Internal
public void initGlobalConfig() {
    long start = System.nanoTime();
    configurationProvider.loadAndRefreshGlobalConfig().subscribe(v -> {
    }, throwable -> {
        InitGlobalConfigFailedEvent.Reason reason = InitGlobalConfigFailedEvent.Reason.UNKNOWN;
        if (throwable instanceof UnsupportedConfigMechanismException) {
            reason = InitGlobalConfigFailedEvent.Reason.UNSUPPORTED;
        } else if (throwable instanceof GlobalConfigNotFoundException) {
            reason = InitGlobalConfigFailedEvent.Reason.NO_CONFIG_FOUND;
        } else if (throwable instanceof ConfigException) {
            if (throwable.getCause() instanceof RequestCanceledException) {
                RequestContext ctx = ((RequestCanceledException) throwable.getCause()).context().requestContext();
                if (ctx.request().cancellationReason() == CancellationReason.SHUTDOWN) {
                    reason = InitGlobalConfigFailedEvent.Reason.SHUTDOWN;
                }
            } else if (throwable.getMessage().contains("NO_ACCESS")) {
                reason = InitGlobalConfigFailedEvent.Reason.NO_ACCESS;
            }
        } else if (throwable instanceof AlreadyShutdownException) {
            reason = InitGlobalConfigFailedEvent.Reason.SHUTDOWN;
        }
        eventBus.publish(new InitGlobalConfigFailedEvent(reason.severity(), Duration.ofNanos(System.nanoTime() - start), context(), reason, throwable));
    });
}
Also used : RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) InitGlobalConfigFailedEvent(com.couchbase.client.core.cnc.events.core.InitGlobalConfigFailedEvent) GlobalConfigNotFoundException(com.couchbase.client.core.error.GlobalConfigNotFoundException) ConfigException(com.couchbase.client.core.error.ConfigException) RequestContext(com.couchbase.client.core.msg.RequestContext) AlreadyShutdownException(com.couchbase.client.core.error.AlreadyShutdownException) UnsupportedConfigMechanismException(com.couchbase.client.core.error.UnsupportedConfigMechanismException)

Example 9 with RequestContext

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

the class KeyValueLocatorTest method pickCurrentIfNoFFMapAndNmvbSeen.

@Test
@SuppressWarnings("unchecked")
void pickCurrentIfNoFFMapAndNmvbSeen() {
    Locator locator = new KeyValueLocator();
    // Setup 2 nodes
    NodeInfo nodeInfo1 = new NodeInfo("http://foo:1234", "192.168.56.101:8091", Collections.EMPTY_MAP, null);
    NodeInfo nodeInfo2 = new NodeInfo("http://foo:1234", "192.168.56.102:8091", Collections.EMPTY_MAP, null);
    Node node1Mock = mock(Node.class);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    Node node2Mock = mock(Node.class);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock));
    // Configure Cluster and Bucket config
    ClusterConfig configMock = mock(ClusterConfig.class);
    CouchbaseBucketConfig bucketMock = mock(CouchbaseBucketConfig.class);
    when(configMock.bucketConfig("bucket")).thenReturn(bucketMock);
    when(bucketMock.nodes()).thenReturn(Arrays.asList(nodeInfo1, nodeInfo2));
    when(bucketMock.numberOfPartitions()).thenReturn(1024);
    when(bucketMock.nodeAtIndex(0)).thenReturn(nodeInfo1);
    when(bucketMock.nodeAtIndex(1)).thenReturn(nodeInfo2);
    when(bucketMock.hasFastForwardMap()).thenReturn(false);
    // Fake a vbucket move in ffwd map from node 0 to node 1
    when(bucketMock.nodeIndexForActive(656, false)).thenReturn((short) 0);
    when(bucketMock.nodeIndexForActive(656, true)).thenReturn((short) 1);
    // Create Request
    GetRequest getRequest = mock(GetRequest.class);
    when(getRequest.bucket()).thenReturn("bucket");
    when(getRequest.key()).thenReturn("key".getBytes(UTF_8));
    RequestContext requestCtx = mock(RequestContext.class);
    when(getRequest.context()).thenReturn(requestCtx);
    when(getRequest.rejectedWithNotMyVbucket()).thenReturn(9);
    locator.dispatch(getRequest, nodes, configMock, null);
    verify(node1Mock, times(1)).send(getRequest);
    verify(node2Mock, never()).send(getRequest);
    when(getRequest.rejectedWithNotMyVbucket()).thenReturn(1);
    locator.dispatch(getRequest, nodes, configMock, null);
    verify(node1Mock, times(2)).send(getRequest);
    verify(node2Mock, never()).send(getRequest);
}
Also used : CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) NodeInfo(com.couchbase.client.core.config.NodeInfo) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ArrayList(java.util.ArrayList) RequestContext(com.couchbase.client.core.msg.RequestContext) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Example 10 with RequestContext

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

the class NodeTest method sendsToFoundLocalService.

@Test
@SuppressWarnings({ "unchecked" })
void sendsToFoundLocalService() {
    final Service s = mock(Service.class);
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            when(s.type()).thenReturn(serviceType);
            return s;
        }
    };
    node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
    KeyValueRequest r = mock(KeyValueRequest.class);
    when(r.serviceType()).thenReturn(ServiceType.KV);
    when(r.bucket()).thenReturn("bucket");
    when(r.context()).thenReturn(new RequestContext(CTX, r));
    node.send(r);
    verify(s, times(1)).send(eq(r));
}
Also used : KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Optional(java.util.Optional) ServiceType(com.couchbase.client.core.service.ServiceType) Service(com.couchbase.client.core.service.Service) RequestContext(com.couchbase.client.core.msg.RequestContext) Test(org.junit.jupiter.api.Test)

Aggregations

RequestContext (com.couchbase.client.core.msg.RequestContext)13 Test (org.junit.jupiter.api.Test)12 Core (com.couchbase.client.core.Core)4 CoreContext (com.couchbase.client.core.CoreContext)4 RequestRetryScheduledEvent (com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent)4 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)4 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)4 NodeInfo (com.couchbase.client.core.config.NodeInfo)4 Authenticator (com.couchbase.client.core.env.Authenticator)4 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)4 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)4 ArrayList (java.util.ArrayList)4 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)3 Request (com.couchbase.client.core.msg.Request)3 Timer (com.couchbase.client.core.Timer)2 Service (com.couchbase.client.core.service.Service)2 ServiceType (com.couchbase.client.core.service.ServiceType)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 InitGlobalConfigFailedEvent (com.couchbase.client.core.cnc.events.core.InitGlobalConfigFailedEvent)1