Search in sources :

Example 31 with ChannelPromise

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

the class ConnectionTest method testCRCCorruption.

@Test
public void testCRCCorruption() throws Throwable {
    test((inbound, outbound, endpoint) -> {
        int version = outbound.settings().acceptVersions.max;
        if (version < VERSION_40)
            return;
        unsafeSetSerializer(Verb._TEST_1, () -> new IVersionedSerializer<Object>() {

            public void serialize(Object o, DataOutputPlus out, int version) throws IOException {
                out.writeInt((Integer) o);
            }

            public Object deserialize(DataInputPlus in, int version) throws IOException {
                return in.readInt();
            }

            public long serializedSize(Object o, int version) {
                return Integer.BYTES;
            }
        });
        connect(outbound);
        outbound.unsafeGetChannel().pipeline().addFirst(new ChannelOutboundHandlerAdapter() {

            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                ByteBuf bb = (ByteBuf) msg;
                bb.setByte(0, 0xAB);
                ctx.write(msg, promise);
            }
        });
        outbound.enqueue(Message.out(Verb._TEST_1, 0xffffffff));
        CompletableFuture.runAsync(() -> {
            while (outbound.isConnected() && !Thread.interrupted()) {
            }
        }).get(10, SECONDS);
        Assert.assertFalse(outbound.isConnected());
        // TODO: count corruptions
        connect(outbound);
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) UnknownColumnException(org.apache.cassandra.exceptions.UnknownColumnException) IOException(java.io.IOException) Test(org.junit.Test)

Example 32 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise 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 33 with ChannelPromise

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

the class StreamingMultiplexedChannelTest method onControlMessageComplete_Exception.

@Test
public void onControlMessageComplete_Exception() throws InterruptedException, ExecutionException, TimeoutException {
    Assert.assertTrue(channel.isOpen());
    Assert.assertTrue(sender.connected());
    ChannelPromise promise = channel.newPromise();
    promise.setFailure(new RuntimeException("this is just a testing exception"));
    Future f = sender.onMessageComplete(promise, new CompleteMessage());
    f.get(5, TimeUnit.SECONDS);
    Assert.assertFalse(channel.isOpen());
    Assert.assertFalse(sender.connected());
    Assert.assertEquals(StreamSession.State.FAILED, session.state());
}
Also used : CompleteMessage(org.apache.cassandra.streaming.messages.CompleteMessage) Future(java.util.concurrent.Future) StreamResultFuture(org.apache.cassandra.streaming.StreamResultFuture) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.Test)

Example 34 with ChannelPromise

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

the class NettyMessage method writeToChannel.

// ------------------------------------------------------------------------
void writeToChannel(ChannelOutboundInvoker out, ChannelPromise promise, ByteBufAllocator allocator, Consumer<ByteBuf> consumer, byte id, int length) throws IOException {
    ByteBuf byteBuf = null;
    try {
        byteBuf = allocateBuffer(allocator, id, length);
        consumer.accept(byteBuf);
        out.write(byteBuf, promise);
    } catch (Throwable t) {
        handleException(byteBuf, null, t);
    }
}
Also used : CompositeByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)

Example 35 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise 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)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)223 Test (org.junit.jupiter.api.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)63 ChannelFuture (io.netty.channel.ChannelFuture)62 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)58 ByteBuf (io.netty.buffer.ByteBuf)56 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)30 Test (org.junit.Test)25 Channel (io.netty.channel.Channel)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 ClosedChannelException (java.nio.channels.ClosedChannelException)20 ChannelFutureListener (io.netty.channel.ChannelFutureListener)19 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)18 InvocationOnMock (org.mockito.invocation.InvocationOnMock)18 AsciiString (io.netty.util.AsciiString)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Bootstrap (io.netty.bootstrap.Bootstrap)12