Search in sources :

Example 41 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project jersey by jersey.

the class JerseyClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        final HttpResponse response = (HttpResponse) msg;
        final ClientResponse jerseyResponse = new ClientResponse(new Response.StatusType() {

            @Override
            public int getStatusCode() {
                return response.status().code();
            }

            @Override
            public Response.Status.Family getFamily() {
                return Response.Status.Family.familyOf(response.status().code());
            }

            @Override
            public String getReasonPhrase() {
                return response.status().reasonPhrase();
            }
        }, jerseyRequest);
        for (Map.Entry<String, String> entry : response.headers().entries()) {
            jerseyResponse.getHeaders().add(entry.getKey(), entry.getValue());
        }
        // request entity handling.
        if ((response.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(response) > 0) || HttpUtil.isTransferEncodingChunked(response)) {
            ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {

                @Override
                public void operationComplete(Future<? super Void> future) throws Exception {
                    isList.add(NettyInputStream.END_OF_INPUT_ERROR);
                }
            });
            jerseyResponse.setEntityStream(new NettyInputStream(isList));
        } else {
            jerseyResponse.setEntityStream(new InputStream() {

                @Override
                public int read() throws IOException {
                    return -1;
                }
            });
        }
        if (asyncConnectorCallback != null) {
            connector.executorService.execute(new Runnable() {

                @Override
                public void run() {
                    asyncConnectorCallback.response(jerseyResponse);
                    future.complete(jerseyResponse);
                }
            });
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;
        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            // copy bytes - when netty reads last chunk, it automatically closes the channel, which invalidates all
            // relates ByteBuffs.
            byte[] bytes = new byte[content.readableBytes()];
            content.getBytes(content.readerIndex(), bytes);
            isList.add(new ByteArrayInputStream(bytes));
        }
        if (msg instanceof LastHttpContent) {
            isList.add(NettyInputStream.END_OF_INPUT);
        }
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) InputStream(java.io.InputStream) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) IOException(java.io.IOException) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.netty.util.concurrent.Future) Map(java.util.Map) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 42 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateServerHandlerTest method testQueryUnknownKvStateID.

/**
	 * Tests the failure response with {@link UnknownKvStateID} as cause on
	 * queries for unregistered KvStateIDs.
	 */
@Test
public void testQueryUnknownKvStateID() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
    EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);
    long requestId = Integer.MAX_VALUE + 182828L;
    ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), requestId, new KvStateID(), new byte[0]);
    // Write the request and wait for the response
    channel.writeInbound(request);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
    assertEquals(requestId, response.getRequestId());
    assertTrue("Did not respond with expected failure cause", response.getCause() instanceof UnknownKvStateID);
    assertEquals(1, stats.getNumRequests());
    assertEquals(1, stats.getNumFailed());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) KvStateRequestFailure(org.apache.flink.runtime.query.netty.message.KvStateRequestFailure) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) KvStateID(org.apache.flink.runtime.query.KvStateID) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 43 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateServerHandlerTest method testFailureOnGetSerializedValue.

/**
	 * Tests the failure response on a failure on the {@link InternalKvState#getSerializedValue(byte[])}
	 * call.
	 */
@Test
public void testFailureOnGetSerializedValue() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
    EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);
    // Failing KvState
    InternalKvState<?> kvState = mock(InternalKvState.class);
    when(kvState.getSerializedValue(any(byte[].class))).thenThrow(new RuntimeException("Expected test Exception"));
    KvStateID kvStateId = registry.registerKvState(new JobID(), new JobVertexID(), new KeyGroupRange(0, 0), "vanilla", kvState);
    ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 282872, kvStateId, new byte[0]);
    // Write the request and wait for the response
    channel.writeInbound(request);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
    assertTrue(response.getCause().getMessage().contains("Expected test Exception"));
    assertEquals(1, stats.getNumRequests());
    assertEquals(1, stats.getNumFailed());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) KvStateRequestFailure(org.apache.flink.runtime.query.netty.message.KvStateRequestFailure) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) KvStateID(org.apache.flink.runtime.query.KvStateID) ByteBuf(io.netty.buffer.ByteBuf) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 44 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateServerHandlerTest method testCloseChannelOnExceptionCaught.

/**
	 * Tests that the channel is closed if an Exception reaches the channel
	 * handler.
	 */
@Test
public void testCloseChannelOnExceptionCaught() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    Throwable response = KvStateRequestSerializer.deserializeServerFailure(buf);
    assertTrue(response.getMessage().contains("Expected test Exception"));
    channel.closeFuture().await(READ_TIMEOUT_MILLIS);
    assertFalse(channel.isActive());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 45 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateServerHandlerTest method testUnexpectedMessage.

/**
	 * Tests response on unexpected messages.
	 */
@Test
public void testUnexpectedMessage() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
    EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);
    // Write the request and wait for the response
    ByteBuf unexpectedMessage = Unpooled.buffer(8);
    unexpectedMessage.writeInt(4);
    unexpectedMessage.writeInt(123238213);
    channel.writeInbound(unexpectedMessage);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    Throwable response = KvStateRequestSerializer.deserializeServerFailure(buf);
    assertEquals(0, stats.getNumRequests());
    assertEquals(0, stats.getNumFailed());
    unexpectedMessage = KvStateRequestSerializer.serializeKvStateRequestResult(channel.alloc(), 192, new byte[0]);
    channel.writeInbound(unexpectedMessage);
    buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    response = KvStateRequestSerializer.deserializeServerFailure(buf);
    assertTrue("Unexpected failure cause " + response.getClass().getName(), response instanceof IllegalArgumentException);
    assertEquals(0, stats.getNumRequests());
    assertEquals(0, stats.getNumFailed());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)5135 Test (org.junit.Test)1813 Test (org.junit.jupiter.api.Test)717 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)392 ArrayList (java.util.ArrayList)312 IOException (java.io.IOException)309 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)209 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)183 ByteBuffer (java.nio.ByteBuffer)174 InetSocketAddress (java.net.InetSocketAddress)156 List (java.util.List)146 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)144 Channel (io.netty.channel.Channel)141 Test (org.testng.annotations.Test)140 ChannelFuture (io.netty.channel.ChannelFuture)132 Map (java.util.Map)118 CountDownLatch (java.util.concurrent.CountDownLatch)108 MatchEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder)107 Position (org.traccar.model.Position)105 DeviceSession (org.traccar.DeviceSession)100