use of com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse 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.BucketConfigStreamingResponse in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpResponse) {
currentResponse = ((HttpResponse) msg);
if (isStreamingConfigRequest()) {
streamingResponse = (BucketConfigStreamingResponse) currentRequest.decode(currentResponse, null);
currentRequest.succeed(streamingResponse);
ctx.pipeline().addFirst(new IdleStateHandler(coreContext.environment().ioConfig().configIdleRedialTimeout().toMillis(), 0, 0, TimeUnit.MILLISECONDS));
}
} else if (msg instanceof HttpContent) {
currentContent.writeBytes(((HttpContent) msg).content());
if (isStreamingConfigRequest()) {
// there might be more than one config in the full batch, so keep iterating until all are pushed
while (true) {
String encodedConfig = currentContent.toString(StandardCharsets.UTF_8);
int separatorIndex = encodedConfig.indexOf("\n\n\n\n");
// if -1 is returned it means that no full config has been located yet, need to wait for more chunks
if (separatorIndex >= 0) {
String content = encodedConfig.substring(0, separatorIndex);
streamingResponse.pushConfig(content.trim());
currentContent.clear();
currentContent.writeBytes(encodedConfig.substring(separatorIndex + 4).getBytes(StandardCharsets.UTF_8));
} else {
break;
}
}
}
if (msg instanceof LastHttpContent) {
if (isStreamingConfigRequest()) {
streamingResponse.completeStream();
streamingResponse = null;
ctx.pipeline().remove(IdleStateHandler.class);
} else {
byte[] copy = new byte[currentContent.readableBytes()];
currentContent.readBytes(copy);
Response response = currentRequest.decode(currentResponse, copy);
currentRequest.succeed(response);
}
currentRequest = null;
if (endpoint != null) {
endpoint.markRequestCompletion();
}
}
} else {
ioContext.environment().eventBus().publish(new UnsupportedResponseTypeReceivedEvent(ioContext, msg));
}
ReferenceCountUtil.release(msg);
}
use of com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse 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.BucketConfigStreamingResponse 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();
}
use of com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandlerTest method returnsNewConfigsWhenChunked.
/**
* Configs can come in all shapes and sizes chunked, but only after the 4 newlines are pushed by the cluster
* the config should be propagated into the flux.
*/
@Test
void returnsNewConfigsWhenChunked() 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 List<String> configsPushed = Collections.synchronizedList(new ArrayList<>());
final AtomicBoolean terminated = new AtomicBoolean(false);
Thread listener = new Thread(() -> completedResponse.configs().subscribe(configsPushed::add, (e) -> {
}, () -> terminated.set(true)));
listener.setDaemon(true);
listener.start();
ByteBuf fullContent = Unpooled.copiedBuffer(readResource("terse_stream_two_configs.json", ManagerMessageHandlerTest.class), StandardCharsets.UTF_8);
while (fullContent.readableBytes() > 0) {
int len = new Random().nextInt(fullContent.readableBytes() + 1);
if (len == 0) {
continue;
}
channel.writeInbound(new DefaultHttpContent(fullContent.readBytes(len)));
}
waitUntilCondition(() -> configsPushed.size() >= 1);
for (String config : configsPushed) {
assertTrue(config.startsWith("{"));
assertTrue(config.endsWith("}"));
}
assertFalse(terminated.get());
channel.writeInbound(new DefaultLastHttpContent());
waitUntilCondition(terminated::get);
ReferenceCountUtil.release(fullContent);
channel.close().awaitUninterruptibly();
}
Aggregations