Search in sources :

Example 6 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project cassandra by apache.

the class EntireSSTableStreamingCorrectFilesCountTest method createMockNettyChannel.

private EmbeddedChannel createMockNettyChannel(ByteBuf serializedFile) {
    WritableByteChannel wbc = new WritableByteChannel() {

        private boolean isOpen = true;

        public int write(ByteBuffer src) {
            int size = src.limit();
            serializedFile.writeBytes(src);
            return size;
        }

        public boolean isOpen() {
            return isOpen;
        }

        public void close() {
            isOpen = false;
        }
    };
    return new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            ((SharedDefaultFileRegion) msg).transferTo(wbc, 0);
            super.write(ctx, msg, promise);
        }
    });
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuffer(java.nio.ByteBuffer) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) IOException(java.io.IOException)

Example 7 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project flink by apache.

the class ClientTransportErrorHandlingTest method testExceptionOnWrite.

/**
 * Verifies that failed client requests via {@link PartitionRequestClient} are correctly
 * attributed to the respective {@link RemoteInputChannel}.
 */
@Test
public void testExceptionOnWrite() throws Exception {
    NettyProtocol protocol = new NettyProtocol(mock(ResultPartitionProvider.class), mock(TaskEventDispatcher.class)) {

        @Override
        public ChannelHandler[] getServerChannelHandlers() {
            return new ChannelHandler[0];
        }
    };
    // We need a real server and client in this test, because Netty's EmbeddedChannel is
    // not failing the ChannelPromise of failed writes.
    NettyServerAndClient serverAndClient = initServerAndClient(protocol, createConfig());
    Channel ch = connect(serverAndClient);
    NetworkClientHandler handler = getClientHandler(ch);
    // Last outbound handler throws Exception after 1st write
    ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {

        int writeNum = 0;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (writeNum >= 1) {
                throw new RuntimeException("Expected test exception.");
            }
            writeNum++;
            ctx.write(msg, promise);
        }
    });
    PartitionRequestClient requestClient = new NettyPartitionRequestClient(ch, handler, mock(ConnectionID.class), mock(PartitionRequestClientFactory.class));
    // Create input channels
    RemoteInputChannel[] rich = new RemoteInputChannel[] { createRemoteInputChannel(), createRemoteInputChannel() };
    final CountDownLatch sync = new CountDownLatch(1);
    // Do this with explicit synchronization. Otherwise this is not robust against slow timings
    // of the callback (e.g. we cannot just verify that it was called once, because there is
    // a chance that we do this too early).
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            sync.countDown();
            return null;
        }
    }).when(rich[1]).onError(isA(LocalTransportException.class));
    // First request is successful
    requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[0], 0);
    // Second request is *not* successful
    requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[1], 0);
    // Wait for the notification and it could confirm all the request operations are done
    if (!sync.await(TestingUtils.TESTING_DURATION.toMillis(), TimeUnit.MILLISECONDS)) {
        fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION.toMillis() + " ms to be notified about the channel error.");
    }
    // Only the second channel should be notified about the error
    verify(rich[0], times(0)).onError(any(LocalTransportException.class));
    shutdown(serverAndClient);
}
Also used : ChannelHandlerContext(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext) ChannelPromise(org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise) PartitionRequestClient(org.apache.flink.runtime.io.network.PartitionRequestClient) ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) RemoteInputChannel(org.apache.flink.runtime.io.network.partition.consumer.RemoteInputChannel) ResultPartitionID(org.apache.flink.runtime.io.network.partition.ResultPartitionID) ResultPartitionProvider(org.apache.flink.runtime.io.network.partition.ResultPartitionProvider) NetworkClientHandler(org.apache.flink.runtime.io.network.NetworkClientHandler) RemoteInputChannel(org.apache.flink.runtime.io.network.partition.consumer.RemoteInputChannel) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) ChannelOutboundHandlerAdapter(org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter) LocalTransportException(org.apache.flink.runtime.io.network.netty.exception.LocalTransportException) CountDownLatch(java.util.concurrent.CountDownLatch) LocalTransportException(org.apache.flink.runtime.io.network.netty.exception.LocalTransportException) RemoteTransportException(org.apache.flink.runtime.io.network.netty.exception.RemoteTransportException) IOException(java.io.IOException) ConnectionID(org.apache.flink.runtime.io.network.ConnectionID) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TaskEventDispatcher(org.apache.flink.runtime.io.network.TaskEventDispatcher) NettyServerAndClient(org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient) Test(org.junit.Test)

Example 8 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.

the class MessageToMessageEncoderTest method testIntermediateWriteFailures.

@Test
public void testIntermediateWriteFailures() {
    ChannelHandler encoder = new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) {
            out.add(new Object());
            out.add(msg);
        }
    };
    final Exception firstWriteException = new Exception();
    ChannelHandler writeThrower = new ChannelOutboundHandlerAdapter() {

        private boolean firstWritten;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (firstWritten) {
                ctx.write(msg, promise);
            } else {
                firstWritten = true;
                promise.setFailure(firstWriteException);
            }
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(writeThrower, encoder);
    Object msg = new Object();
    ChannelFuture write = channel.writeAndFlush(msg);
    assertSame(firstWriteException, write.cause());
    assertSame(msg, channel.readOutbound());
    assertFalse(channel.finish());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.jupiter.api.Test)

Example 9 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.

the class ChunkedWriteHandlerTest method testFailureWhenLastChunkFailed.

// See https://github.com/netty/netty/issues/8700.
@Test
public void testFailureWhenLastChunkFailed() throws IOException {
    ChannelOutboundHandlerAdapter failLast = new ChannelOutboundHandlerAdapter() {

        private int passedWrites;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (++this.passedWrites < 4) {
                ctx.write(msg, promise);
            } else {
                ReferenceCountUtil.release(msg);
                promise.tryFailure(new RuntimeException());
            }
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(failLast, new ChunkedWriteHandler());
    // 4 chunks
    ChannelFuture r = ch.writeAndFlush(new ChunkedFile(TMP, 1024 * 16));
    assertTrue(ch.finish());
    assertFalse(r.isSuccess());
    assertTrue(r.cause() instanceof RuntimeException);
    // 3 out of 4 chunks were already written
    int read = 0;
    for (; ; ) {
        ByteBuf buffer = ch.readOutbound();
        if (buffer == null) {
            break;
        }
        read += buffer.readableBytes();
        buffer.release();
    }
    assertEquals(1024 * 16 * 3, read);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 10 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.

the class ChunkedWriteHandlerTest method checkSkipFailed.

private static void checkSkipFailed(Object input1, Object input2) {
    ChannelOutboundHandlerAdapter failFirst = new ChannelOutboundHandlerAdapter() {

        private boolean alreadyFailed;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (alreadyFailed) {
                ctx.write(msg, promise);
            } else {
                this.alreadyFailed = true;
                ReferenceCountUtil.release(msg);
                promise.tryFailure(new RuntimeException());
            }
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(failFirst, new ChunkedWriteHandler());
    ChannelFuture r1 = ch.write(input1);
    ChannelFuture r2 = ch.writeAndFlush(input2).awaitUninterruptibly();
    assertTrue(ch.finish());
    assertTrue(r1.cause() instanceof RuntimeException);
    assertTrue(r2.isSuccess());
    // note, that after we've "skipped" the first write,
    // we expect to see the second message, chunk by chunk
    int i = 0;
    int read = 0;
    for (; ; ) {
        ByteBuf buffer = ch.readOutbound();
        if (buffer == null) {
            break;
        }
        while (buffer.isReadable()) {
            assertEquals(BYTES[i++], buffer.readByte());
            read++;
            if (i == BYTES.length) {
                i = 0;
            }
        }
        buffer.release();
    }
    assertEquals(BYTES.length, read);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)32 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)32 ChannelPromise (io.netty.channel.ChannelPromise)30 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)18 Test (org.junit.jupiter.api.Test)17 ByteBuf (io.netty.buffer.ByteBuf)10 Test (org.junit.Test)9 IOException (java.io.IOException)7 ClosedChannelException (java.nio.channels.ClosedChannelException)7 ChannelFuture (io.netty.channel.ChannelFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Channel (io.netty.channel.Channel)3 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)3 SslContext (io.netty.handler.ssl.SslContext)3 ByteBuffer (java.nio.ByteBuffer)3 WritableByteChannel (java.nio.channels.WritableByteChannel)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ApiErrorWithMetadata (com.nike.backstopper.apierror.ApiErrorWithMetadata)2 NettyHttpClientRequestBuilder (com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder)2 NettyHttpClientResponse (com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse)2