Search in sources :

Example 1 with RequestCanceledException

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

the class KeyValueMessageHandlerTest method attemptsRetryIfInstructedByErrorMap.

/**
 * If an unknown response code is returned and the consulted error map indicates a retry, it should be passed to
 * the retry orchestrator for correct handling.
 */
@Test
void attemptsRetryIfInstructedByErrorMap() {
    EmbeddedChannel channel = new EmbeddedChannel(new KeyValueMessageHandler(null, CTX, Optional.of(BUCKET)));
    ErrorMap errorMap = mock(ErrorMap.class);
    Map<Short, ErrorMap.ErrorCode> errors = new HashMap<>();
    ErrorMap.ErrorCode code = mock(ErrorMap.ErrorCode.class);
    errors.put((short) 0xFF, code);
    Set<ErrorMap.ErrorAttribute> attributes = new HashSet<>();
    attributes.add(ErrorMap.ErrorAttribute.RETRY_NOW);
    when(code.attributes()).thenReturn(attributes);
    when(errorMap.errors()).thenReturn(errors);
    channel.attr(ChannelAttributes.ERROR_MAP_KEY).set(errorMap);
    channel.pipeline().fireChannelActive();
    try {
        GetRequest request = new GetRequest("key", Duration.ofSeconds(1), CTX, CID, FailFastRetryStrategy.INSTANCE, null);
        channel.writeOutbound(request);
        ByteBuf getResponse = MemcacheProtocol.response(channel.alloc(), MemcacheProtocol.Opcode.GET, (byte) 0, (short) 0xFF, request.opaque(), 0, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
        channel.writeInbound(getResponse);
        ExecutionException exception = assertThrows(ExecutionException.class, () -> request.response().get());
        assertTrue(exception.getCause() instanceof RequestCanceledException);
        assertEquals("NO_MORE_RETRIES", request.cancellationReason().identifier());
        assertEquals(RetryReason.KV_ERROR_MAP_INDICATED, request.cancellationReason().innerReason());
        assertEquals(0, getResponse.refCnt());
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : HashMap(java.util.HashMap) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with RequestCanceledException

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

the class ReactorTest method completesWithErrorAfterSubscription.

@Test
void completesWithErrorAfterSubscription() {
    NoopRequest request = new NoopRequest(Duration.ZERO, mock(RequestContext.class), mock(RetryStrategy.class), mock(CollectionIdentifier.class));
    Mono<NoopResponse> mono = Reactor.wrap(request, request.response(), true);
    RequestCanceledException exception = mock(RequestCanceledException.class);
    StepVerifier verifier = StepVerifier.create(mono).expectError(RequestCanceledException.class);
    request.fail(exception);
    verifier.verify();
}
Also used : NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) NoopResponse(com.couchbase.client.core.msg.kv.NoopResponse) RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) RequestContext(com.couchbase.client.core.msg.RequestContext) StepVerifier(reactor.test.StepVerifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) Test(org.junit.jupiter.api.Test)

Example 3 with RequestCanceledException

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

the class CircuitBreakerIntegrationTest method shouldTriggerWithTimeouts.

@Test
void shouldTriggerWithTimeouts() {
    int threshold = collection.environment().ioConfig().kvCircuitBreakerConfig().volumeThreshold();
    int timeouts = 0;
    int cancellations = 0;
    for (int i = 0; i < threshold * 3; i++) {
        FailingGetRequestOnEncode request = new FailingGetRequestOnEncode("foo", Duration.ofMillis(1), collection.core().context(), new CollectionIdentifier(config().bucketname(), Optional.of(collection.scopeName()), Optional.of(collection.name())), FailFastRetryStrategy.INSTANCE);
        collection.core().send(request);
        try {
            request.response().get();
            fail();
        } catch (ExecutionException ex) {
            if (ex.getCause() instanceof TimeoutException) {
                timeouts++;
            } else if (ex.getCause() instanceof RequestCanceledException) {
                cancellations++;
                CancellationReason reason = ((RequestCanceledException) ex.getCause()).context().requestContext().request().cancellationReason();
                assertEquals(reason.innerReason(), RetryReason.ENDPOINT_CIRCUIT_OPEN);
            }
        } catch (Throwable t) {
            fail(t);
        }
    }
    assertTrue(timeouts > 0);
    assertTrue(cancellations > 0);
}
Also used : CancellationReason(com.couchbase.client.core.msg.CancellationReason) RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) ExecutionException(java.util.concurrent.ExecutionException) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) TimeoutException(com.couchbase.client.core.error.TimeoutException) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with RequestCanceledException

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

the class BaseRequest method cancel.

@Override
public void cancel(final CancellationReason reason, Function<Throwable, Throwable> exceptionTranslator) {
    if (STATE_UPDATER.compareAndSet(this, State.INCOMPLETE, State.CANCELLED)) {
        cancelTimeoutRegistration();
        cancellationReason = reason;
        final Throwable exception;
        final String msg = this.getClass().getSimpleName() + ", Reason: " + reason;
        final CancellationErrorContext ctx = new CancellationErrorContext(context());
        if (reason == CancellationReason.TIMEOUT) {
            exception = idempotent() ? new UnambiguousTimeoutException(msg, ctx) : new AmbiguousTimeoutException(msg, ctx);
        } else {
            exception = new RequestCanceledException(msg, reason, ctx);
        }
        response.completeExceptionally(exceptionTranslator.apply(exception));
    }
}
Also used : RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) AmbiguousTimeoutException(com.couchbase.client.core.error.AmbiguousTimeoutException) UnambiguousTimeoutException(com.couchbase.client.core.error.UnambiguousTimeoutException) CancellationErrorContext(com.couchbase.client.core.error.context.CancellationErrorContext)

Example 5 with RequestCanceledException

use of com.couchbase.client.core.error.RequestCanceledException 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)

Aggregations

RequestCanceledException (com.couchbase.client.core.error.RequestCanceledException)7 Test (org.junit.jupiter.api.Test)5 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)4 RequestContext (com.couchbase.client.core.msg.RequestContext)4 NoopRequest (com.couchbase.client.core.msg.kv.NoopRequest)3 NoopResponse (com.couchbase.client.core.msg.kv.NoopResponse)3 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)3 StepVerifier (reactor.test.StepVerifier)3 CancellationReason (com.couchbase.client.core.msg.CancellationReason)2 ExecutionException (java.util.concurrent.ExecutionException)2 InitGlobalConfigFailedEvent (com.couchbase.client.core.cnc.events.core.InitGlobalConfigFailedEvent)1 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)1 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)1 AlreadyShutdownException (com.couchbase.client.core.error.AlreadyShutdownException)1 AmbiguousTimeoutException (com.couchbase.client.core.error.AmbiguousTimeoutException)1 ConfigException (com.couchbase.client.core.error.ConfigException)1 GlobalConfigNotFoundException (com.couchbase.client.core.error.GlobalConfigNotFoundException)1 TimeoutException (com.couchbase.client.core.error.TimeoutException)1 UnambiguousTimeoutException (com.couchbase.client.core.error.UnambiguousTimeoutException)1 UnsupportedConfigMechanismException (com.couchbase.client.core.error.UnsupportedConfigMechanismException)1