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();
}
}
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();
}
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);
}
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));
}
}
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));
});
}
Aggregations