Search in sources :

Example 46 with EpollEventLoopGroup

use of org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.epoll.EpollEventLoopGroup in project java by wavefrontHQ.

the class RawLogsIngester method listen.

public void listen() throws InterruptedException {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    EventLoopGroup acceptorGroup;
    EventLoopGroup handlerGroup;
    Class<? extends ServerChannel> socketChannelClass;
    if (Epoll.isAvailable()) {
        logger.fine("Using native socket transport for port " + port);
        acceptorGroup = new EpollEventLoopGroup(2);
        handlerGroup = new EpollEventLoopGroup(10);
        socketChannelClass = EpollServerSocketChannel.class;
    } else {
        logger.fine("Using NIO socket transport for port " + port);
        acceptorGroup = new NioEventLoopGroup(2);
        handlerGroup = new NioEventLoopGroup(10);
        socketChannelClass = NioServerSocketChannel.class;
    }
    serverBootstrap.group(acceptorGroup, handlerGroup).channel(socketChannelClass).childHandler(new SocketInitializer()).option(ChannelOption.SO_BACKLOG, 5).option(ChannelOption.SO_KEEPALIVE, true);
    serverBootstrap.bind(port).sync();
}
Also used : EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 47 with EpollEventLoopGroup

use of org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.epoll.EpollEventLoopGroup in project herddb by diennea.

the class NetworkChannelTest method test.

@Test
public void test() throws Exception {
    try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", 1111, true)) {
        acceptor.setEnableJVMNetwork(false);
        acceptor.setAcceptor(new ServerSideConnectionAcceptor() {

            @Override
            public ServerSideConnection createConnection(Channel channel) {
                channel.setMessagesReceiver(new ChannelEventListener() {

                    @Override
                    public void messageReceived(Message message, Channel channel) {
                        channel.sendReplyMessage(message, Message.ACK("ciao"));
                    }

                    @Override
                    public void channelClosed(Channel channel) {
                    }
                });
                return new ServerSideConnection() {

                    @Override
                    public long getConnectionId() {
                        return new Random().nextLong();
                    }
                };
            }
        });
        acceptor.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        try (Channel client = NettyConnector.connect("localhost", 1111, true, 0, 0, new ChannelEventListener() {

            @Override
            public void messageReceived(Message message, Channel channel) {
                System.out.println("client messageReceived " + message);
            }

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, new NioEventLoopGroup(10, executor), new DefaultEventLoopGroup())) {
            for (int i = 0; i < 100; i++) {
                Message result = client.sendMessageWithReply(Message.ACK("clientId"), 10000);
                assertEquals(Message.TYPE_ACK, result.type);
            // System.out.println("result:" + result);
            }
        } finally {
            executor.shutdown();
        }
    }
    if (NetworkUtils.isEnableEpoolNative()) {
        try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", 1111, true)) {
            acceptor.setEnableJVMNetwork(false);
            acceptor.setAcceptor(new ServerSideConnectionAcceptor() {

                @Override
                public ServerSideConnection createConnection(Channel channel) {
                    channel.setMessagesReceiver(new ChannelEventListener() {

                        @Override
                        public void messageReceived(Message message, Channel channel) {
                            channel.sendReplyMessage(message, Message.ACK("ciao"));
                        }

                        @Override
                        public void channelClosed(Channel channel) {
                        }
                    });
                    return new ServerSideConnection() {

                        @Override
                        public long getConnectionId() {
                            return new Random().nextLong();
                        }
                    };
                }
            });
            acceptor.start();
            ExecutorService executor = Executors.newCachedThreadPool();
            try (Channel client = NettyConnector.connect("localhost", 1111, true, 0, 0, new ChannelEventListener() {

                @Override
                public void messageReceived(Message message, Channel channel) {
                    System.out.println("client messageReceived " + message);
                }

                @Override
                public void channelClosed(Channel channel) {
                    System.out.println("client channelClosed");
                }
            }, executor, new EpollEventLoopGroup(10, executor), new DefaultEventLoopGroup())) {
                for (int i = 0; i < 100; i++) {
                    Message result = client.sendMessageWithReply(Message.ACK("clientId"), 10000);
                    assertEquals(Message.TYPE_ACK, result.type);
                // System.out.println("result:" + result);
                }
            } finally {
                executor.shutdown();
            }
        }
    }
}
Also used : DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Random(java.util.Random) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) NettyChannelAcceptor(herddb.network.netty.NettyChannelAcceptor) ExecutorService(java.util.concurrent.ExecutorService) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 48 with EpollEventLoopGroup

use of org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.epoll.EpollEventLoopGroup in project pravega by pravega.

the class AbstractConnectionListener method startListening.

/**
 * Initializes the connection listener internals and starts listening.
 */
public void startListening() {
    final AtomicReference<SslContext> sslCtx = this.enableTls ? new AtomicReference<>(TLSHelper.newServerSslContext(pathToTlsCertFile, pathToTlsKeyFile, tlsProtocolVersion)) : null;
    boolean nio = false;
    try {
        bossGroup = new EpollEventLoopGroup(1);
        workerGroup = new EpollEventLoopGroup();
    } catch (ExceptionInInitializerError | UnsatisfiedLinkError | NoClassDefFoundError e) {
        nio = true;
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
    }
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(nio ? NioServerSocketChannel.class : EpollServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) {
            ChannelPipeline p = ch.pipeline();
            // Add SslHandler to the channel's pipeline, if TLS is enabled.
            if (enableTls) {
                SslHandler sslHandler = sslCtx.get().newHandler(ch.alloc());
                // We add a name to SSL/TLS handler, unlike the other handlers added later, to make it
                // easier to find and replace the handler.
                p.addLast(TLSHelper.TLS_HANDLER_NAME, sslHandler);
            }
            // Configure the class-specific encoder stack and request processors.
            ServerConnectionInboundHandler lsh = new ServerConnectionInboundHandler();
            createEncodingStack(ch.remoteAddress().toString()).forEach(p::addLast);
            lsh.setRequestProcessor(createRequestProcessor(new TrackedConnection(lsh, connectionTracker)));
            p.addLast(lsh);
        }
    });
    if (enableTls && enableTlsReload) {
        enableTlsContextReload(sslCtx);
    }
    // Start the server.
    serverChannel = b.bind(host, port).awaitUninterruptibly().channel();
    if (healthServiceManager != null) {
        healthServiceManager.register(new ConnectionListenerHealthContributor(this));
    }
}
Also used : EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 49 with EpollEventLoopGroup

use of org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.epoll.EpollEventLoopGroup in project pravega by pravega.

the class ConnectionPoolingTest method setUp.

@Before
public void setUp() throws Exception {
    // Configure SSL.
    port = TestUtils.getAvailableListenPort();
    final SslContext sslCtx;
    if (ssl) {
        try {
            sslCtx = SslContextBuilder.forServer(new File(SecurityConfigDefaults.TLS_SERVER_CERT_PATH), new File(SecurityConfigDefaults.TLS_SERVER_PRIVATE_KEY_PATH)).build();
        } catch (SSLException e) {
            throw new RuntimeException(e);
        }
    } else {
        sslCtx = null;
    }
    boolean nio = false;
    EventLoopGroup bossGroup;
    EventLoopGroup workerGroup;
    try {
        bossGroup = new EpollEventLoopGroup(1);
        workerGroup = new EpollEventLoopGroup();
    } catch (ExceptionInInitializerError | UnsatisfiedLinkError | NoClassDefFoundError e) {
        nio = true;
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
    }
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(nio ? NioServerSocketChannel.class : EpollServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            if (sslCtx != null) {
                SslHandler handler = sslCtx.newHandler(ch.alloc());
                SSLEngine sslEngine = handler.engine();
                SSLParameters sslParameters = sslEngine.getSSLParameters();
                sslParameters.setEndpointIdentificationAlgorithm("LDAPS");
                sslEngine.setSSLParameters(sslParameters);
                p.addLast(handler);
            }
            p.addLast(new CommandEncoder(null, NO_OP_METRIC_NOTIFIER), new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4), new CommandDecoder(), new EchoServerHandler());
        }
    });
    // Start the server.
    serverChannel = b.bind("localhost", port).awaitUninterruptibly().channel();
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SSLEngine(javax.net.ssl.SSLEngine) CommandEncoder(io.pravega.shared.protocol.netty.CommandEncoder) SSLException(javax.net.ssl.SSLException) SSLParameters(javax.net.ssl.SSLParameters) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) CommandDecoder(io.pravega.shared.protocol.netty.CommandDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SSLException(javax.net.ssl.SSLException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) File(java.io.File) Before(org.junit.Before)

Example 50 with EpollEventLoopGroup

use of org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.epoll.EpollEventLoopGroup in project beam by apache.

the class ManagedChannelFactory method forDescriptor.

public ManagedChannel forDescriptor(ApiServiceDescriptor apiServiceDescriptor) {
    ManagedChannelBuilder<?> channelBuilder;
    switch(type) {
        case EPOLL:
            SocketAddress address = SocketAddressFactory.createFrom(apiServiceDescriptor.getUrl());
            channelBuilder = NettyChannelBuilder.forAddress(address).channelType(address instanceof DomainSocketAddress ? EpollDomainSocketChannel.class : EpollSocketChannel.class).eventLoopGroup(new EpollEventLoopGroup());
            break;
        case DEFAULT:
            channelBuilder = ManagedChannelBuilder.forTarget(apiServiceDescriptor.getUrl());
            break;
        case IN_PROCESS:
            channelBuilder = InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl());
            break;
        default:
            throw new IllegalStateException("Unknown type " + type);
    }
    channelBuilder = channelBuilder.usePlaintext().maxInboundMessageSize(Integer.MAX_VALUE).intercept(interceptors);
    if (directExecutor) {
        channelBuilder = channelBuilder.directExecutor();
    }
    return channelBuilder.build();
}
Also used : EpollEventLoopGroup(org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.epoll.EpollEventLoopGroup) EpollDomainSocketChannel(org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.epoll.EpollDomainSocketChannel) DomainSocketAddress(org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.unix.DomainSocketAddress) EpollSocketChannel(org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.epoll.EpollSocketChannel) SocketAddress(java.net.SocketAddress) DomainSocketAddress(org.apache.beam.vendor.grpc.v1p43p2.io.netty.channel.unix.DomainSocketAddress)

Aggregations

EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)55 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)46 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)20 EventLoopGroup (io.netty.channel.EventLoopGroup)19 EpollServerSocketChannel (io.netty.channel.epoll.EpollServerSocketChannel)14 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)14 SocketChannel (io.netty.channel.socket.SocketChannel)13 ChannelPipeline (io.netty.channel.ChannelPipeline)12 Channel (io.netty.channel.Channel)10 InetSocketAddress (java.net.InetSocketAddress)10 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)9 IOException (java.io.IOException)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 ChannelFuture (io.netty.channel.ChannelFuture)8 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)8 SslContext (io.netty.handler.ssl.SslContext)8 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)7 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)7 LoggingHandler (io.netty.handler.logging.LoggingHandler)6 SslHandler (io.netty.handler.ssl.SslHandler)6