Search in sources :

Example 1 with RequestNotRetriedEvent

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

the class RetryOrchestrator method maybeRetry.

/**
 * Retry or cancel the given request, depending on its state and the configured {@link RetryStrategy}.
 *
 * @param ctx the core context into which timer the request is submitted.
 * @param request the request in question.
 * @param reason the reason why the request is being retried.
 */
public static void maybeRetry(final CoreContext ctx, final Request<? extends Response> request, final RetryReason reason) {
    if (request.completed()) {
        return;
    }
    if (reason.alwaysRetry()) {
        retryWithDuration(ctx, request, controlledBackoff(request.context().retryAttempts()), reason);
        return;
    }
    request.retryStrategy().shouldRetry(request, reason).whenComplete((retryAction, throwable) -> {
        if (throwable != null) {
            ctx.environment().eventBus().publish(new RequestNotRetriedEvent(Event.Severity.INFO, request.getClass(), request.context(), reason, throwable));
        }
        Optional<Duration> duration = retryAction.duration();
        if (duration.isPresent()) {
            final Duration cappedDuration = capDuration(duration.get(), request);
            retryWithDuration(ctx, request, cappedDuration, reason);
        } else {
            // unmonitored request's severity is downgraded to debug to not spam the info-level logs
            Event.Severity severity = request instanceof UnmonitoredRequest ? Event.Severity.DEBUG : Event.Severity.INFO;
            ctx.environment().eventBus().publish(new RequestNotRetriedEvent(severity, request.getClass(), request.context(), reason, null));
            request.cancel(CancellationReason.noMoreRetries(reason), retryAction.exceptionTranslator());
        }
    });
}
Also used : UnmonitoredRequest(com.couchbase.client.core.msg.UnmonitoredRequest) RequestNotRetriedEvent(com.couchbase.client.core.cnc.events.request.RequestNotRetriedEvent) RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent) Event(com.couchbase.client.core.cnc.Event) RequestNotRetriedEvent(com.couchbase.client.core.cnc.events.request.RequestNotRetriedEvent) Duration(java.time.Duration)

Example 2 with RequestNotRetriedEvent

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

RequestNotRetriedEvent (com.couchbase.client.core.cnc.events.request.RequestNotRetriedEvent)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Event (com.couchbase.client.core.cnc.Event)1 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)1 RequestRetryScheduledEvent (com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent)1 Authenticator (com.couchbase.client.core.env.Authenticator)1 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)1 Request (com.couchbase.client.core.msg.Request)1 RequestContext (com.couchbase.client.core.msg.RequestContext)1 UnmonitoredRequest (com.couchbase.client.core.msg.UnmonitoredRequest)1 Duration (java.time.Duration)1 Test (org.junit.jupiter.api.Test)1