Search in sources :

Example 1 with HttpPipelinedResponse

use of org.opensearch.http.HttpPipelinedResponse in project OpenSearch by opensearch-project.

the class NioHttpPipeliningHandlerTests method testPipeliningRequestsAreReleased.

public void testPipeliningRequestsAreReleased() {
    final int numberOfRequests = 10;
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new NioHttpPipeliningHandler(logger, numberOfRequests + 1));
    for (int i = 0; i < numberOfRequests; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + i));
    }
    HttpPipelinedRequest inbound;
    ArrayList<HttpPipelinedRequest> requests = new ArrayList<>();
    while ((inbound = embeddedChannel.readInbound()) != null) {
        requests.add(inbound);
    }
    ArrayList<ChannelPromise> promises = new ArrayList<>();
    for (int i = 1; i < requests.size(); ++i) {
        ChannelPromise promise = embeddedChannel.newPromise();
        promises.add(promise);
        HttpPipelinedRequest pipelinedRequest = requests.get(i);
        HttpPipelinedResponse resp = pipelinedRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
        embeddedChannel.writeAndFlush(resp, promise);
    }
    for (ChannelPromise promise : promises) {
        assertFalse(promise.isDone());
    }
    embeddedChannel.close().syncUninterruptibly();
    for (ChannelPromise promise : promises) {
        assertTrue(promise.isDone());
        assertTrue(promise.cause() instanceof ClosedChannelException);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) HttpPipelinedResponse(org.opensearch.http.HttpPipelinedResponse) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpPipelinedRequest(org.opensearch.http.HttpPipelinedRequest) ChannelPromise(io.netty.channel.ChannelPromise)

Example 2 with HttpPipelinedResponse

use of org.opensearch.http.HttpPipelinedResponse in project OpenSearch by opensearch-project.

the class NioHttpPipeliningHandler method write.

@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
    assert msg instanceof HttpPipelinedResponse : "Invalid message type: " + msg.getClass();
    HttpPipelinedResponse response = (HttpPipelinedResponse) msg;
    boolean success = false;
    try {
        NettyListener listener = NettyListener.fromChannelPromise(promise);
        List<Tuple<HttpPipelinedResponse, NettyListener>> readyResponses = aggregator.write(response, listener);
        success = true;
        for (Tuple<HttpPipelinedResponse, NettyListener> responseToWrite : readyResponses) {
            ctx.write(responseToWrite.v1().getDelegateRequest(), responseToWrite.v2());
        }
    } catch (IllegalStateException e) {
        ctx.channel().close();
    } finally {
        if (success == false) {
            promise.setFailure(new ClosedChannelException());
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) HttpPipelinedResponse(org.opensearch.http.HttpPipelinedResponse) Tuple(org.opensearch.common.collect.Tuple)

Example 3 with HttpPipelinedResponse

use of org.opensearch.http.HttpPipelinedResponse in project OpenSearch by opensearch-project.

the class NioHttpPipeliningHandler method close.

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
    List<Tuple<HttpPipelinedResponse, NettyListener>> inflightResponses = aggregator.removeAllInflightResponses();
    if (inflightResponses.isEmpty() == false) {
        ClosedChannelException closedChannelException = new ClosedChannelException();
        for (Tuple<HttpPipelinedResponse, NettyListener> inflightResponse : inflightResponses) {
            try {
                inflightResponse.v2().setFailure(closedChannelException);
            } catch (RuntimeException e) {
                logger.error("unexpected error while releasing pipelined http responses", e);
            }
        }
    }
    ctx.close(promise);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) HttpPipelinedResponse(org.opensearch.http.HttpPipelinedResponse) Tuple(org.opensearch.common.collect.Tuple)

Example 4 with HttpPipelinedResponse

use of org.opensearch.http.HttpPipelinedResponse in project OpenSearch by opensearch-project.

the class HttpReadWriteHandlerTests method emptyGetResponse.

private static HttpPipelinedResponse emptyGetResponse(int sequence) {
    DefaultFullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    HttpPipelinedRequest httpRequest = new HttpPipelinedRequest(sequence, new NioHttpRequest(nettyRequest));
    HttpPipelinedResponse httpResponse = httpRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
    httpResponse.addHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), "0");
    return httpResponse;
}
Also used : HttpPipelinedResponse(org.opensearch.http.HttpPipelinedResponse) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpPipelinedRequest(org.opensearch.http.HttpPipelinedRequest)

Example 5 with HttpPipelinedResponse

use of org.opensearch.http.HttpPipelinedResponse in project OpenSearch by opensearch-project.

the class Netty4HttpPipeliningHandlerTests method testPipeliningRequestsAreReleased.

public void testPipeliningRequestsAreReleased() throws InterruptedException {
    final int numberOfRequests = 10;
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new Netty4HttpPipeliningHandler(logger, numberOfRequests + 1));
    for (int i = 0; i < numberOfRequests; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + i));
    }
    HttpPipelinedRequest inbound;
    ArrayList<HttpPipelinedRequest> requests = new ArrayList<>();
    while ((inbound = embeddedChannel.readInbound()) != null) {
        requests.add(inbound);
    }
    ArrayList<ChannelPromise> promises = new ArrayList<>();
    for (int i = 1; i < requests.size(); ++i) {
        ChannelPromise promise = embeddedChannel.newPromise();
        promises.add(promise);
        HttpPipelinedRequest pipelinedRequest = requests.get(i);
        HttpPipelinedResponse resp = pipelinedRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
        embeddedChannel.writeAndFlush(resp, promise);
    }
    for (ChannelPromise promise : promises) {
        assertFalse(promise.isDone());
    }
    embeddedChannel.close().syncUninterruptibly();
    for (ChannelPromise promise : promises) {
        assertTrue(promise.isDone());
        assertTrue(promise.cause() instanceof ClosedChannelException);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) HttpPipelinedResponse(org.opensearch.http.HttpPipelinedResponse) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpPipelinedRequest(org.opensearch.http.HttpPipelinedRequest) ChannelPromise(io.netty.channel.ChannelPromise)

Aggregations

HttpPipelinedResponse (org.opensearch.http.HttpPipelinedResponse)8 ClosedChannelException (java.nio.channels.ClosedChannelException)6 ChannelPromise (io.netty.channel.ChannelPromise)5 Tuple (org.opensearch.common.collect.Tuple)4 HttpPipelinedRequest (org.opensearch.http.HttpPipelinedRequest)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 ArrayList (java.util.ArrayList)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 BiConsumer (java.util.function.BiConsumer)1 FlushOperation (org.opensearch.nio.FlushOperation)1 SocketChannelContext (org.opensearch.nio.SocketChannelContext)1