Search in sources :

Example 1 with ChannelInboundHandler

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

the class RequestDecoderDispatcher method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ChannelInboundHandler delegate = protocol.select(decoders);
    if (delegate == null) {
        log.warn("Unregistered handler for protocol %s", protocol);
        /*
             * Since we cannot process this message further we need to release the message as per netty doc
             * see http://netty.io/wiki/reference-counted-objects.html#inbound-messages
             */
        ReferenceCountUtil.release(msg);
        return;
    }
    delegate.channelRead(ctx, msg);
}
Also used : ChannelInboundHandler(io.netty.channel.ChannelInboundHandler)

Example 2 with ChannelInboundHandler

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

the class RequestDecoderDispatcherTest method shouldDispatchToRegisteredDecoder.

@Test
public void shouldDispatchToRegisteredDecoder() throws Exception {
    // given
    RequestDecoderDispatcher<State> dispatcher = new RequestDecoderDispatcher<>(protocol, logProvider);
    ChannelInboundHandler delegateOne = mock(ChannelInboundHandler.class);
    ChannelInboundHandler delegateTwo = mock(ChannelInboundHandler.class);
    ChannelInboundHandler delegateThree = mock(ChannelInboundHandler.class);
    dispatcher.register(State.one, delegateOne);
    dispatcher.register(State.two, delegateTwo);
    dispatcher.register(State.three, delegateThree);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    Object msg = new Object();
    // when
    dispatcher.channelRead(ctx, msg);
    // then
    verify(delegateTwo).channelRead(ctx, msg);
    verifyNoMoreInteractions(delegateTwo);
    verifyZeroInteractions(delegateOne, delegateThree);
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandler(io.netty.channel.ChannelInboundHandler) Test(org.junit.Test)

Example 3 with ChannelInboundHandler

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

the class BookieNettyServer method listenOn.

private void listenOn(InetSocketAddress address, BookieSocketAddress bookieAddress) throws InterruptedException {
    if (!conf.isDisableServerSocketBind()) {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
        bootstrap.group(eventLoopGroup, eventLoopGroup);
        bootstrap.childOption(ChannelOption.TCP_NODELAY, conf.getServerTcpNoDelay());
        bootstrap.childOption(ChannelOption.SO_LINGER, conf.getServerSockLinger());
        bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(conf.getRecvByteBufAllocatorSizeMin(), conf.getRecvByteBufAllocatorSizeInitial(), conf.getRecvByteBufAllocatorSizeMax()));
        if (eventLoopGroup instanceof EpollEventLoopGroup) {
            bootstrap.channel(EpollServerSocketChannel.class);
        } else {
            bootstrap.channel(NioServerSocketChannel.class);
        }
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                synchronized (suspensionLock) {
                    while (suspended) {
                        suspensionLock.wait();
                    }
                }
                BookieSideConnectionPeerContextHandler contextHandler = new BookieSideConnectionPeerContextHandler();
                ChannelPipeline pipeline = ch.pipeline();
                // For ByteBufList, skip the usual LengthFieldPrepender and have the encoder itself to add it
                pipeline.addLast("bytebufList", ByteBufList.ENCODER_WITH_SIZE);
                pipeline.addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4));
                pipeline.addLast("lengthprepender", new LengthFieldPrepender(4));
                pipeline.addLast("bookieProtoDecoder", new BookieProtoEncoding.RequestDecoder(registry));
                pipeline.addLast("bookieProtoEncoder", new BookieProtoEncoding.ResponseEncoder(registry));
                pipeline.addLast("bookieAuthHandler", new AuthHandler.ServerSideHandler(contextHandler.getConnectionPeer(), authProviderFactory));
                ChannelInboundHandler requestHandler = isRunning.get() ? new BookieRequestHandler(conf, requestProcessor, allChannels) : new RejectRequestHandler();
                pipeline.addLast("bookieRequestHandler", requestHandler);
                pipeline.addLast("contextHandler", contextHandler);
            }
        });
        // Bind and start to accept incoming connections
        Channel listen = bootstrap.bind(address.getAddress(), address.getPort()).sync().channel();
        if (listen.localAddress() instanceof InetSocketAddress) {
            if (conf.getBookiePort() == 0) {
                conf.setBookiePort(((InetSocketAddress) listen.localAddress()).getPort());
            }
        }
    }
    if (conf.isEnableLocalTransport()) {
        ServerBootstrap jvmBootstrap = new ServerBootstrap();
        jvmBootstrap.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
        jvmBootstrap.group(jvmEventLoopGroup, jvmEventLoopGroup);
        jvmBootstrap.childOption(ChannelOption.TCP_NODELAY, conf.getServerTcpNoDelay());
        jvmBootstrap.childOption(ChannelOption.SO_KEEPALIVE, conf.getServerSockKeepalive());
        jvmBootstrap.childOption(ChannelOption.SO_LINGER, conf.getServerSockLinger());
        jvmBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(conf.getRecvByteBufAllocatorSizeMin(), conf.getRecvByteBufAllocatorSizeInitial(), conf.getRecvByteBufAllocatorSizeMax()));
        if (jvmEventLoopGroup instanceof DefaultEventLoopGroup) {
            jvmBootstrap.channel(LocalServerChannel.class);
        } else if (jvmEventLoopGroup instanceof EpollEventLoopGroup) {
            jvmBootstrap.channel(EpollServerSocketChannel.class);
        } else {
            jvmBootstrap.channel(NioServerSocketChannel.class);
        }
        jvmBootstrap.childHandler(new ChannelInitializer<LocalChannel>() {

            @Override
            protected void initChannel(LocalChannel ch) throws Exception {
                synchronized (suspensionLock) {
                    while (suspended) {
                        suspensionLock.wait();
                    }
                }
                BookieSideConnectionPeerContextHandler contextHandler = new BookieSideConnectionPeerContextHandler();
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4));
                pipeline.addLast("lengthprepender", new LengthFieldPrepender(4));
                pipeline.addLast("bookieProtoDecoder", new BookieProtoEncoding.RequestDecoder(registry));
                pipeline.addLast("bookieProtoEncoder", new BookieProtoEncoding.ResponseEncoder(registry));
                pipeline.addLast("bookieAuthHandler", new AuthHandler.ServerSideHandler(contextHandler.getConnectionPeer(), authProviderFactory));
                ChannelInboundHandler requestHandler = isRunning.get() ? new BookieRequestHandler(conf, requestProcessor, allChannels) : new RejectRequestHandler();
                pipeline.addLast("bookieRequestHandler", requestHandler);
                pipeline.addLast("contextHandler", contextHandler);
            }
        });
        // use the same address 'name', so clients can find local Bookie still discovering them using ZK
        jvmBootstrap.bind(bookieAddress.getLocalAddress()).sync();
        LocalBookiesRegistry.registerLocalBookieAddress(bookieAddress);
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) LocalChannel(io.netty.channel.local.LocalChannel) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) AdaptiveRecvByteBufAllocator(io.netty.channel.AdaptiveRecvByteBufAllocator) ChannelInboundHandler(io.netty.channel.ChannelInboundHandler) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) Channel(io.netty.channel.Channel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) BookieException(org.apache.bookkeeper.bookie.BookieException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) ChannelPipeline(io.netty.channel.ChannelPipeline) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup)

Example 4 with ChannelInboundHandler

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

the class RouterHandler method routed.

private void routed(ChannelHandlerContext channelHandlerContext, RouteResult<?> routeResult, HttpRequest httpRequest) {
    ChannelInboundHandler handler = (ChannelInboundHandler) routeResult.target();
    // The handler may have been added (keep alive)
    ChannelPipeline pipeline = channelHandlerContext.pipeline();
    ChannelHandler addedHandler = pipeline.get(ROUTED_HANDLER_NAME);
    if (handler != addedHandler) {
        if (addedHandler == null) {
            pipeline.addAfter(ROUTER_HANDLER_NAME, ROUTED_HANDLER_NAME, handler);
        } else {
            pipeline.replace(addedHandler, ROUTED_HANDLER_NAME, handler);
        }
    }
    RoutedRequest<?> request = new RoutedRequest<>(routeResult, httpRequest);
    channelHandlerContext.fireChannelRead(request.retain());
}
Also used : ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) SimpleChannelInboundHandler(org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler) ChannelInboundHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandler) ChannelPipeline(org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline)

Example 5 with ChannelInboundHandler

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

the class SocketFileRegionTest method testFileRegion0.

private static void testFileRegion0(ServerBootstrap sb, Bootstrap cb, boolean voidPromise, final boolean autoRead, boolean defaultFileRegion) throws Throwable {
    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);
    final int bufferSize = 1024;
    final File file = PlatformDependent.createTempFile("netty-", ".tmp", null);
    file.deleteOnExit();
    final FileOutputStream out = new FileOutputStream(file);
    final Random random = PlatformDependent.threadLocalRandom();
    // Prepend random data which will not be transferred, so that we can test non-zero start offset
    final int startOffset = random.nextInt(8192);
    for (int i = 0; i < startOffset; i++) {
        out.write(random.nextInt());
    }
    // .. and here comes the real data to transfer.
    out.write(data, bufferSize, data.length - bufferSize);
    // .. and then some extra data which is not supposed to be transferred.
    for (int i = random.nextInt(8192); i > 0; i--) {
        out.write(random.nextInt());
    }
    out.close();
    ChannelInboundHandler ch = new SimpleChannelInboundHandler<Object>() {

        @Override
        public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            if (!autoRead) {
                ctx.read();
            }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
        }
    };
    TestHandler sh = new TestHandler(autoRead);
    sb.childHandler(sh);
    cb.handler(ch);
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect(sc.localAddress()).sync().channel();
    FileRegion region = new DefaultFileRegion(new RandomAccessFile(file, "r").getChannel(), startOffset, data.length - bufferSize);
    FileRegion emptyRegion = new DefaultFileRegion(new RandomAccessFile(file, "r").getChannel(), 0, 0);
    if (!defaultFileRegion) {
        region = new FileRegionWrapper(region);
        emptyRegion = new FileRegionWrapper(emptyRegion);
    }
    // https://github.com/netty/netty/issues/2964
    if (voidPromise) {
        assertEquals(cc.voidPromise(), cc.write(Unpooled.wrappedBuffer(data, 0, bufferSize), cc.voidPromise()));
        assertEquals(cc.voidPromise(), cc.write(emptyRegion, cc.voidPromise()));
        assertEquals(cc.voidPromise(), cc.writeAndFlush(region, cc.voidPromise()));
    } else {
        assertNotEquals(cc.voidPromise(), cc.write(Unpooled.wrappedBuffer(data, 0, bufferSize)));
        assertNotEquals(cc.voidPromise(), cc.write(emptyRegion));
        assertNotEquals(cc.voidPromise(), cc.writeAndFlush(region));
    }
    while (sh.counter < data.length) {
        if (sh.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    sh.channel.close().sync();
    cc.close().sync();
    sc.close().sync();
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    // Make sure we did not receive more than we expected.
    assertThat(sh.counter, is(data.length));
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) Channel(io.netty.channel.Channel) WritableByteChannel(java.nio.channels.WritableByteChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) IOException(java.io.IOException) DefaultFileRegion(io.netty.channel.DefaultFileRegion) Random(java.util.Random) RandomAccessFile(java.io.RandomAccessFile) FileOutputStream(java.io.FileOutputStream) FileRegion(io.netty.channel.FileRegion) DefaultFileRegion(io.netty.channel.DefaultFileRegion) ChannelInboundHandler(io.netty.channel.ChannelInboundHandler) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

ChannelInboundHandler (io.netty.channel.ChannelInboundHandler)10 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 Channel (io.netty.channel.Channel)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 Test (org.junit.Test)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)3 LocalChannel (io.netty.channel.local.LocalChannel)3 LocalServerChannel (io.netty.channel.local.LocalServerChannel)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 ChannelInboundHandler (org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandler)3 ClientTlsProtocolNegotiator (io.grpc.netty.ProtocolNegotiators.ClientTlsProtocolNegotiator)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelHandler (io.netty.channel.ChannelHandler)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 LocalAddress (io.netty.channel.local.LocalAddress)2 File (java.io.File)2