Search in sources :

Example 1 with HttpContent

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent 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();
    }
}
Also used : HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) CoreContext(com.couchbase.client.core.CoreContext) BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) MockingDetails(org.mockito.MockingDetails) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 2 with HttpContent

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent in project couchbase-jvm-clients by couchbase.

the class QueryMessageHandlerTest method reportsCompletionOnlyOnceStreamingEnds.

/**
 * To avoid accidentally pipelining requests, we need to make sure that the handler only reports completion
 * once the streaming is fully completed, and not just the response completable future initially completed.
 *
 * <p>We send a request, make sure it is encoded correctly and then stream back the chunks, making sure the
 * final completion is only signaled once the last chunk arrived.</p>
 */
@Test
void reportsCompletionOnlyOnceStreamingEnds() {
    BaseEndpoint endpoint = mock(BaseEndpoint.class);
    EmbeddedChannel channel = new EmbeddedChannel(new QueryMessageHandler(endpoint, ENDPOINT_CTX));
    byte[] query = "doesn'tmatter".getBytes(CharsetUtil.UTF_8);
    QueryRequest request = new QueryRequest(ENV.timeoutConfig().queryTimeout(), CORE_CTX, ENV.retryStrategy(), CORE_CTX.authenticator(), "statement", query, false, null, null, null, null, null);
    channel.writeAndFlush(request);
    FullHttpRequest encodedRequest = channel.readOutbound();
    assertEquals("doesn'tmatter", encodedRequest.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.release(encodedRequest);
    verify(endpoint, never()).markRequestCompletion();
    assertFalse(request.response().isDone());
    ByteBuf fullResponse = Unpooled.copiedBuffer(readResource("success_response.json", QueryMessageHandlerTest.class), CharsetUtil.UTF_8);
    // send the header back first
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    channel.writeInbound(response);
    assertFalse(request.response().isDone());
    // send first chunk, should complete the future but not mark the endpoint completion
    HttpContent firstChunk = new DefaultHttpContent(fullResponse.readBytes(500));
    channel.writeInbound(firstChunk);
    assertTrue(request.response().isDone());
    verify(endpoint, never()).markRequestCompletion();
    // send the second chunk, no change
    HttpContent secondChunk = new DefaultHttpContent(fullResponse.readBytes(500));
    channel.writeInbound(secondChunk);
    verify(endpoint, never()).markRequestCompletion();
    // send the last chunk, mark for completion finally
    LastHttpContent lastChunk = new DefaultLastHttpContent(fullResponse.readBytes(fullResponse.readableBytes()));
    channel.writeInbound(lastChunk);
    verify(endpoint, times(1)).markRequestCompletion();
    channel.finishAndReleaseAll();
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) LastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) LastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) Test(org.junit.jupiter.api.Test)

Example 3 with HttpContent

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent 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);
}
Also used : BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) Response(com.couchbase.client.core.msg.Response) IdleStateHandler(com.couchbase.client.core.deps.io.netty.handler.timeout.IdleStateHandler) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) UnsupportedResponseTypeReceivedEvent(com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent) LastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent) LastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent)

Example 4 with HttpContent

use of com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent in project couchbase-jvm-clients by couchbase.

the class SearchMock method loadSearchTestCase.

/**
 * Given JSON in the form expected, e.g. those from https://github.com/chvck/sdk-testcases which contains the
 * returned JSON from the search service in a field "data", returns the completed SearchResult that the API
 * would return.
 */
public static SearchResult loadSearchTestCase(InputStream json) throws ExecutionException, InterruptedException, IOException {
    // The idea is to fake packets that have come from the search service.
    // Start by preparing the packets.
    JsonObject jo = JsonObject.fromJson(toByteArray(json));
    JsonObject data = jo.getObject("data");
    byte[] b = data.toString().getBytes(StandardCharsets.UTF_8);
    ByteBuf bytes = Unpooled.wrappedBuffer(b);
    HttpContent content = new DefaultLastHttpContent(bytes);
    HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    // Fake some core stuff
    Core mockedCore = mock(Core.class);
    CoreEnvironment env = CoreEnvironment.create();
    CoreContext ctx = new CoreContext(mockedCore, 0, env, PasswordAuthenticator.create("Administrator", "password"));
    // Our ChunkedSearchMessageHandler needs to be initialised by pretending we've sent an outbound SearchRequest
    // through it
    SearchRequest req = new SearchRequest(Duration.ofSeconds(10), ctx, BestEffortRetryStrategy.INSTANCE, null, null, null, null);
    // ChunkedSearchMessageHandler will try to encode() the SearchRequest.  Rather than mocking everything required
    // to get that working, just mock the encode method.
    SearchRequest spiedReq = spy(req);
    doReturn(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "localhost")).when(spiedReq).encode();
    doAnswer(v -> {
        EndpointContext endpointContext = new EndpointContext(ctx, new HostAndPort(null, 0), null, null, null, Optional.of("bucket"), null);
        BaseEndpoint endpoint = mock(BaseEndpoint.class);
        when(endpoint.context()).thenReturn(endpointContext);
        when(endpoint.pipelined()).thenReturn(false);
        // ChunkedSearchMessageHandler does most of the work in handling responses from the service
        ChunkedSearchMessageHandler handler = new ChunkedSearchMessageHandler(endpoint, endpointContext);
        // Netty's EmbeddedChannel lets us test ChannelHandlers like ChunkedSearchMessageHandler.  It's a Netty Channel
        // that doesn't touch the network at all.
        final EmbeddedChannel channel = new EmbeddedChannel(handler);
        // Writing the request is necessary to estabish some initial state inChunkedSearchMessageHandler
        channel.writeAndFlush(spiedReq);
        // Finally we can do the interesting bit of passing our fake FTS service response into
        // ChunkedSearchMessageHandler
        channel.writeInbound(resp);
        channel.writeInbound(content);
        return null;
    }).when(mockedCore).send(any());
    CompletableFuture<SearchResult> future = SearchAccessor.searchQueryAsync(mockedCore, req, DefaultJsonSerializer.create());
    SearchResult result = future.get();
    return result;
}
Also used : SearchRequest(com.couchbase.client.core.msg.search.SearchRequest) CoreContext(com.couchbase.client.core.CoreContext) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) JsonObject(com.couchbase.client.java.json.JsonObject) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) SearchResult(com.couchbase.client.java.search.result.SearchResult) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HostAndPort(com.couchbase.client.core.util.HostAndPort) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) Core(com.couchbase.client.core.Core)

Aggregations

HttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent)4 HttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse)4 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)3 DefaultHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse)3 DefaultLastHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent)3 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)3 Core (com.couchbase.client.core.Core)2 CoreContext (com.couchbase.client.core.CoreContext)2 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)2 DefaultHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent)2 LastHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent)2 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)2 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)2 BucketConfigStreamingResponse (com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse)2 Test (org.junit.jupiter.api.Test)2 UnsupportedResponseTypeReceivedEvent (com.couchbase.client.core.cnc.events.io.UnsupportedResponseTypeReceivedEvent)1 DefaultFullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)1 HttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest)1 IdleStateHandler (com.couchbase.client.core.deps.io.netty.handler.timeout.IdleStateHandler)1