use of com.couchbase.client.core.msg.Request in project couchbase-jvm-clients by couchbase.
the class PooledService method connectReservedEndpoint.
/**
* Connect the reserved endpoint and dispatch the request into it if possible.
* <p>
* Note that there are two synchronized sections in this method, because the subscription callback works on
* a different thread.
*
* @param request the request that needs to bee dispatched.
*/
private synchronized <R extends Request<? extends Response>> void connectReservedEndpoint(final R request) {
if (!disconnected.get()) {
Endpoint endpoint = createEndpoint();
endpointStates.register(endpoint, endpoint);
endpoint.states().skipUntil(s -> s == EndpointState.CONNECTING).filter(s -> s == EndpointState.CONNECTED || s == EndpointState.DISCONNECTED).takeUntil(s -> s == EndpointState.CONNECTED || s == EndpointState.DISCONNECTED).publishOn(context().environment().scheduler()).subscribe(s -> {
synchronized (PooledService.this) {
reservedEndpoints.remove(endpoint);
if (disconnected.get()) {
endpoint.disconnect();
endpointStates.deregister(endpoint);
RetryOrchestrator.maybeRetry(serviceContext, request, RetryReason.ENDPOINT_NOT_AVAILABLE);
} else {
endpoints.add(endpoint);
if (s == EndpointState.CONNECTED) {
endpoint.send(request);
} else if (s == EndpointState.DISCONNECTED) {
RetryOrchestrator.maybeRetry(serviceContext, request, RetryReason.ENDPOINT_NOT_AVAILABLE);
}
}
}
});
endpoint.connect();
reservedEndpoints.add(endpoint);
}
}
use of com.couchbase.client.core.msg.Request in project couchbase-jvm-clients by couchbase.
the class KeyValueMessageHandler method handleNotMyVbucket.
/**
* Helper method to handle a "not my vbucket" response.
*
* @param request the request to retry.
* @param response the response to extract the config from, potentially.
*/
private void handleNotMyVbucket(final KeyValueRequest<Response> request, final ByteBuf response) {
request.indicateRejectedWithNotMyVbucket();
eventBus.publish(new NotMyVbucketReceivedEvent(ioContext, request.partition()));
final String origin = request.context().lastDispatchedTo() != null ? request.context().lastDispatchedTo().hostname() : null;
RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.KV_NOT_MY_VBUCKET);
body(response).map(b -> b.toString(UTF_8).trim()).filter(c -> c.startsWith("{")).ifPresent(c -> ioContext.core().configurationProvider().proposeBucketConfig(new ProposedBucketConfigContext(request.bucket(), c, origin)));
}
use of com.couchbase.client.core.msg.Request in project couchbase-jvm-clients by couchbase.
the class NodeTest method retriesIfGlobalServiceNotFound.
@Test
void retriesIfGlobalServiceNotFound() {
final Service s = mock(Service.class);
final AtomicReference<Request<?>> retried = new AtomicReference<>();
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;
}
@Override
protected <R extends Request<? extends Response>> void sendIntoRetry(R request) {
retried.set(request);
}
};
QueryRequest r = mock(QueryRequest.class);
when(r.serviceType()).thenReturn(ServiceType.QUERY);
node.send(r);
verify(s, never()).send(eq(r));
assertEquals(r, retried.get());
}
use of com.couchbase.client.core.msg.Request 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());
}
use of com.couchbase.client.core.msg.Request 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());
}
Aggregations