Search in sources :

Example 1 with RequestRetryScheduledEvent

use of com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent in project couchbase-jvm-clients by couchbase.

the class LoggingEventConsumerTest method attachesClientContextIfEnabled.

@Test
void attachesClientContextIfEnabled() {
    LoggingEventConsumer loggingEventConsumer = LoggingEventConsumer.create(LoggerConfig.enableDiagnosticContext(true).customLogger(logger).build());
    RequestContext context = mock(RequestContext.class);
    Map<String, Object> userContext = new HashMap<>();
    userContext.put("hello", "world");
    when(context.clientContext()).thenReturn(userContext);
    RequestRetryScheduledEvent retryEvent = new RequestRetryScheduledEvent(Duration.ofSeconds(1), context, GetRequest.class, RetryReason.UNKNOWN);
    loggingEventConsumer.accept(retryEvent);
    verify(logger, times(1)).attachContext(userContext);
}
Also used : RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent) HashMap(java.util.HashMap) RequestContext(com.couchbase.client.core.msg.RequestContext) Test(org.junit.jupiter.api.Test)

Example 2 with RequestRetryScheduledEvent

use of com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent 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 3 with RequestRetryScheduledEvent

use of com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent 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 4 with RequestRetryScheduledEvent

use of com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent in project couchbase-jvm-clients by couchbase.

the class LoggingEventConsumerTest method doesNotAttachClientContextByDefault.

@Test
void doesNotAttachClientContextByDefault() {
    RequestContext context = mock(RequestContext.class);
    Map<String, Object> userContext = new HashMap<>();
    userContext.put("hello", "world");
    when(context.clientContext()).thenReturn(userContext);
    RequestRetryScheduledEvent retryEvent = new RequestRetryScheduledEvent(Duration.ofSeconds(1), context, GetRequest.class, RetryReason.UNKNOWN);
    loggingEventConsumer.accept(retryEvent);
    verify(logger, never()).attachContext(userContext);
}
Also used : RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent) HashMap(java.util.HashMap) RequestContext(com.couchbase.client.core.msg.RequestContext) Test(org.junit.jupiter.api.Test)

Example 5 with RequestRetryScheduledEvent

use of com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent in project couchbase-jvm-clients by couchbase.

the class RetryOrchestrator method retryWithDuration.

/**
 * Helper method to perform the actual retry with the given duration.
 *
 * @param ctx the core context into which timer the request is submitted.
 * @param request the request in question.
 * @param duration the duration when to retry.
 * @param reason the reason why the request is being retried.
 */
private static void retryWithDuration(final CoreContext ctx, final Request<? extends Response> request, final Duration duration, final RetryReason reason) {
    ctx.environment().eventBus().publish(new RequestRetryScheduledEvent(duration, request.context(), request.getClass(), reason));
    request.context().incrementRetryAttempts(duration, reason);
    ctx.environment().timer().scheduleForRetry(ctx.core(), request, duration);
}
Also used : RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent)

Aggregations

RequestRetryScheduledEvent (com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent)5 RequestContext (com.couchbase.client.core.msg.RequestContext)4 Test (org.junit.jupiter.api.Test)4 Core (com.couchbase.client.core.Core)2 CoreContext (com.couchbase.client.core.CoreContext)2 Timer (com.couchbase.client.core.Timer)2 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)2 Authenticator (com.couchbase.client.core.env.Authenticator)2 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)2 Request (com.couchbase.client.core.msg.Request)2 HashMap (java.util.HashMap)2