use of com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandlerTest method disconnectsEndpointOnRedialTimeout.
/**
* When a http streaming connection is outstanding, the handler needs to notify the endpoint that it disconnects
* itself in an orderly manner.
*/
@Test
void disconnectsEndpointOnRedialTimeout() throws Exception {
CoreEnvironment env = CoreEnvironment.builder().ioConfig(IoConfig.configIdleRedialTimeout(Duration.ofSeconds(2))).build();
try {
CoreContext ctx = new CoreContext(mock(Core.class), 1, env, PasswordAuthenticator.create(USER, PASS));
BaseEndpoint endpoint = mock(BaseEndpoint.class);
EndpointContext endpointContext = mock(EndpointContext.class);
when(endpointContext.environment()).thenReturn(env);
when(endpoint.context()).thenReturn(endpointContext);
EmbeddedChannel channel = new EmbeddedChannel(new ManagerMessageHandler(endpoint, ctx));
BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(Duration.ofSeconds(1), ctx, BestEffortRetryStrategy.INSTANCE, "bucket", ctx.authenticator());
channel.write(request);
HttpRequest outboundHeader = channel.readOutbound();
assertEquals(HttpMethod.GET, outboundHeader.method());
assertEquals("/pools/default/bs/bucket", outboundHeader.uri());
assertEquals(HttpVersion.HTTP_1_1, outboundHeader.protocolVersion());
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
HttpContent httpContent = new DefaultHttpContent(Unpooled.copiedBuffer("{}\n\n\n\n", StandardCharsets.UTF_8));
channel.writeInbound(httpResponse, httpContent);
BucketConfigStreamingResponse response = request.response().get();
assertEquals("{}", response.configs().blockFirst());
waitUntilCondition(() -> {
channel.runPendingTasks();
MockingDetails mockingDetails = Mockito.mockingDetails(endpoint);
return mockingDetails.getInvocations().stream().anyMatch(i -> i.getMethod().getName().equals("disconnect"));
});
channel.finish();
} finally {
env.shutdown();
}
}
use of com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest in project couchbase-jvm-clients by couchbase.
the class ClusterManagerBucketRefresherTest method shouldKeepTryingIfRequestFailsCompletely.
@Test
void shouldKeepTryingIfRequestFailsCompletely() {
final AtomicInteger streamingRequestAttempts = new AtomicInteger();
doAnswer(i -> {
streamingRequestAttempts.incrementAndGet();
BucketConfigStreamingRequest request = i.getArgument(0);
request.fail(new RuntimeException());
return null;
}).when(core).send(any(BucketConfigStreamingRequest.class));
refresher.register("bucketName").block();
waitUntilCondition(() -> streamingRequestAttempts.get() >= 2);
}
use of com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest in project couchbase-jvm-clients by couchbase.
the class ClusterManagerBucketRefresherTest method shouldKeepTryingIfHttpResponseHasNonSuccessStatus.
/**
* If a connection cannot be established, the client should keep trying until it finds one.
*/
@Test
void shouldKeepTryingIfHttpResponseHasNonSuccessStatus() {
final AtomicInteger streamingRequestAttempts = new AtomicInteger();
doAnswer(i -> {
streamingRequestAttempts.incrementAndGet();
BucketConfigStreamingRequest request = i.getArgument(0);
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
BucketConfigStreamingResponse response = request.decode(httpResponse, null);
request.succeed(response);
return null;
}).when(core).send(any(BucketConfigStreamingRequest.class));
refresher.register("bucketName").block();
waitUntilCondition(() -> streamingRequestAttempts.get() >= 2);
}
use of com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest in project couchbase-jvm-clients by couchbase.
the class ClusterManagerBucketRefresher method registerStream.
/**
* Registers the given bucket name with the http stream.
*
* <p>Note that this method deliberately subscribes "out of band" and not being flatMapped into the
* {@link #register(String)} return value. The idea is that the flux config subscription keeps on going
* forever until specifically unsubscribed through either {@link #deregister(String)} or {@link #shutdown()}.</p>
*
* @param ctx the core context to use.
* @param name the name of the bucket.
* @return once registered, returns the disposable so it can be later used to deregister.
*/
private Disposable registerStream(final CoreContext ctx, final String name) {
return Mono.defer(() -> {
BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(ctx.environment().timeoutConfig().managementTimeout(), ctx, BestEffortRetryStrategy.INSTANCE, name, ctx.authenticator());
core.send(request);
return Reactor.wrap(request, request.response(), true);
}).flux().flatMap(res -> {
if (res.status().success()) {
return res.configs().map(config -> new ProposedBucketConfigContext(name, config, res.address()));
} else {
eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.MANAGER, BucketConfigRefreshFailedEvent.Reason.INDIVIDUAL_REQUEST_FAILED, Optional.of(res)));
// and retry the whole thing
return Flux.error(new ConfigException());
}
}).doOnError(e -> eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.MANAGER, BucketConfigRefreshFailedEvent.Reason.STREAM_FAILED, Optional.of(e)))).doOnComplete(() -> {
eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.MANAGER, BucketConfigRefreshFailedEvent.Reason.STREAM_CLOSED, Optional.empty()));
// handled in the retryWhen below.
throw new ConfigException();
}).retryWhen(Retry.any().exponentialBackoff(Duration.ofMillis(32), Duration.ofMillis(4096)).toReactorRetry()).subscribe(provider::proposeBucketConfig);
}
use of com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandlerTest method closesStreamIfChannelClosed.
/**
* When a config stream is being processed and the underlying channel closes, the stream should also be closed
* so the upper level can re-establish a new one.
*/
@Test
void closesStreamIfChannelClosed() throws Exception {
CoreContext ctx = new CoreContext(mock(Core.class), 1, ENV, PasswordAuthenticator.create(USER, PASS));
BaseEndpoint endpoint = mock(BaseEndpoint.class);
EndpointContext endpointContext = mock(EndpointContext.class);
when(endpointContext.environment()).thenReturn(ENV);
when(endpoint.context()).thenReturn(endpointContext);
EmbeddedChannel channel = new EmbeddedChannel(new ManagerMessageHandler(endpoint, ctx));
BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(Duration.ofSeconds(1), ctx, BestEffortRetryStrategy.INSTANCE, "bucket", ctx.authenticator());
channel.write(request);
HttpRequest outboundHeader = channel.readOutbound();
assertEquals(HttpMethod.GET, outboundHeader.method());
assertEquals("/pools/default/bs/bucket", outboundHeader.uri());
assertEquals(HttpVersion.HTTP_1_1, outboundHeader.protocolVersion());
CompletableFuture<BucketConfigStreamingResponse> response = request.response();
assertFalse(response.isDone());
HttpResponse inboundResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
channel.writeInbound(inboundResponse);
BucketConfigStreamingResponse completedResponse = request.response().get();
final AtomicBoolean terminated = new AtomicBoolean(false);
Thread listener = new Thread(() -> completedResponse.configs().subscribe((v) -> {
}, (e) -> {
}, () -> terminated.set(true)));
listener.setDaemon(true);
listener.start();
assertFalse(terminated.get());
channel.close().awaitUninterruptibly();
waitUntilCondition(terminated::get);
channel.finish();
}
Aggregations