Search in sources :

Example 56 with EpollEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup in project herddb by diennea.

the class NettyChannelAcceptor method start.

public void start() throws Exception {
    if (ssl) {
        if (sslCertFile == null) {
            LOGGER.log(Level.INFO, "start SSL with self-signed auto-generated certificate");
            if (sslCiphers != null) {
                LOGGER.log(Level.INFO, "required sslCiphers " + sslCiphers);
            }
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            try {
                sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).ciphers(sslCiphers).build();
            } finally {
                ssc.delete();
            }
        } else {
            LOGGER.log(Level.INFO, "start SSL with certificate " + sslCertFile.getAbsolutePath() + " chain file " + sslCertChainFile.getAbsolutePath());
            if (sslCiphers != null) {
                LOGGER.log(Level.INFO, "required sslCiphers " + sslCiphers);
            }
            sslCtx = SslContextBuilder.forServer(sslCertChainFile, sslCertFile, sslCertPassword).ciphers(sslCiphers).build();
        }
    }
    if (callbackThreads == 0) {
        callbackExecutorQueue = new SynchronousQueue<Runnable>();
        callbackExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, callbackExecutorQueue, threadFactory);
    } else {
        callbackExecutorQueue = new LinkedBlockingQueue<Runnable>();
        callbackExecutor = new ThreadPoolExecutor(callbackThreads, callbackThreads, 0L, TimeUnit.MILLISECONDS, callbackExecutorQueue, threadFactory);
    }
    statsLogger.registerGauge("callbacksqueue", new Gauge<Integer>() {

        @Override
        public Integer getDefaultValue() {
            return 0;
        }

        @Override
        public Integer getSample() {
            return callbackExecutorQueue.size();
        }
    });
    InetSocketAddress address = new InetSocketAddress(host, port);
    if (enableRealNetwork) {
        LOGGER.log(Level.INFO, "Starting HerdDB network server at {0}:{1}", new Object[] { host, port + "" });
    }
    if (enableRealNetwork && address.isUnresolved()) {
        throw new IOException("Bind address " + host + ":" + port + " cannot be resolved");
    }
    ChannelInitializer<io.netty.channel.Channel> channelInitialized = new ChannelInitializer<io.netty.channel.Channel>() {

        @Override
        public void initChannel(io.netty.channel.Channel ch) throws Exception {
            NettyChannel session = new NettyChannel("unnamed", ch, callbackExecutor);
            if (acceptor != null) {
                acceptor.createConnection(session);
            }
            // Add SSL handler first to encrypt and decrypt everything.
            if (ssl) {
                ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()));
            }
            ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4));
            ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            // 
            ch.pipeline().addLast("messagedecoder", new ProtocolMessageDecoder());
            ch.pipeline().addLast(new ServerInboundMessageHandler(session));
        }
    };
    if (enableRealNetwork) {
        if (NetworkUtils.isEnableEpoolNative()) {
            bossGroup = new EpollEventLoopGroup(workerThreads);
            workerGroup = new EpollEventLoopGroup(workerThreads);
            LOGGER.log(Level.FINE, "Using netty-native-epoll network type");
        } else {
            bossGroup = new NioEventLoopGroup(workerThreads);
            workerGroup = new NioEventLoopGroup(workerThreads);
            LOGGER.log(Level.FINE, "Using nio network type");
        }
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NetworkUtils.isEnableEpoolNative() ? EpollServerSocketChannel.class : NioServerSocketChannel.class).childHandler(channelInitialized).option(ChannelOption.SO_BACKLOG, 128);
        ChannelFuture f = b.bind(address).sync();
        this.channel = f.channel();
    }
    if (enableJVMNetwork) {
        jvmhostAddress = NetworkUtils.getAddress(address);
        LocalServerRegistry.registerLocalServer(jvmhostAddress, port, localVMChannelAcceptor);
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) InetSocketAddress(java.net.InetSocketAddress) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ChannelInitializer(io.netty.channel.ChannelInitializer) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) IOException(java.io.IOException) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 57 with EpollEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup in project turbo-rpc by hank-whu.

the class NettyRpcServer method start.

public void start() throws InterruptedException {
    InetSocketAddress inet = new InetSocketAddress(hostPort.host, hostPort.port);
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(eventLoopGroup);
    bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.SO_RCVBUF, 256 * 1024);
    if (eventLoopGroup instanceof EpollEventLoopGroup) {
        bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
        bootstrap.channel(EpollServerSocketChannel.class);
    } else if (eventLoopGroup instanceof NioEventLoopGroup) {
        bootstrap.channel(NioServerSocketChannel.class);
    }
    bootstrap.childHandler(new NettyRpcChannelInitializer(invokerFactory, serializer, filters));
    bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.SO_RCVBUF, 256 * 1024);
    bootstrap.childOption(ChannelOption.SO_SNDBUF, 256 * 1024);
    // 
    bootstrap.childOption(// 
    ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 2048 * 1024));
    channel = bootstrap.bind(inet).sync().channel();
    System.out.println("TurboRpcServer started. Listening on: " + hostPort);
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NettyRpcChannelInitializer(rpc.turbo.transport.server.rpc.handler.NettyRpcChannelInitializer)

Example 58 with EpollEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup in project turbo-rpc by hank-whu.

the class NettyClientConnector method connect.

void connect() throws InterruptedException {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(eventLoopGroup);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.SO_RCVBUF, 256 * 1024);
    bootstrap.option(ChannelOption.SO_SNDBUF, 256 * 1024);
    // 
    bootstrap.option(// 
    ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 2048 * 1024));
    if (eventLoopGroup instanceof EpollEventLoopGroup) {
        bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
        bootstrap.channel(EpollSocketChannel.class);
    } else if (eventLoopGroup instanceof NioEventLoopGroup) {
        bootstrap.channel(NioSocketChannel.class);
    }
    bootstrap.handler(new TurboChannelInitializer(serializer));
    Sender[] newSenders = new Sender[connectCount];
    for (int i = 0; i < connectCount; i++) {
        Channel channel = bootstrap.connect(serverAddress.host, serverAddress.port).sync().channel();
        newSenders[i] = new BatchSender(channel);
        if (logger.isInfoEnabled()) {
            logger.info(serverAddress + " connect " + i + "/" + connectCount);
        }
        if (i == 0) {
            InetSocketAddress insocket = (InetSocketAddress) channel.localAddress();
            clientAddress = new HostPort(insocket.getAddress().getHostAddress(), 0);
        }
    }
    Sender[] old = senders;
    senders = newSenders;
    if (old != null) {
        for (int i = 0; i < old.length; i++) {
            try {
                old[i].close();
            } catch (Exception e) {
                if (logger.isWarnEnabled()) {
                    logger.warn("关闭出错", e);
                }
            }
        }
    }
}
Also used : BatchSender(rpc.turbo.transport.client.sender.BatchSender) InetSocketAddress(java.net.InetSocketAddress) TurboChannelInitializer(rpc.turbo.transport.client.handler.TurboChannelInitializer) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) HostPort(rpc.turbo.config.HostPort) IOException(java.io.IOException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) BatchSender(rpc.turbo.transport.client.sender.BatchSender) Sender(rpc.turbo.transport.client.sender.Sender) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 59 with EpollEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollEventLoopGroup in project turbo-rpc by hank-whu.

the class NettyRestServer method start.

public void start() throws InterruptedException {
    InetSocketAddress inet = new InetSocketAddress(hostPort.host, hostPort.port);
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(eventLoopGroup);
    bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.SO_RCVBUF, 256 * 1024);
    if (eventLoopGroup instanceof EpollEventLoopGroup) {
        bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
        bootstrap.channel(EpollServerSocketChannel.class);
    } else if (eventLoopGroup instanceof NioEventLoopGroup) {
        bootstrap.channel(NioServerSocketChannel.class);
    }
    bootstrap.childHandler(new NettyRestChannelInitializer(invokerFactory, jsonMapper, filters));
    bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.SO_RCVBUF, 256 * 1024);
    bootstrap.childOption(ChannelOption.SO_SNDBUF, 256 * 1024);
    // 
    bootstrap.childOption(// 
    ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 2048 * 1024));
    channel = bootstrap.bind(inet).sync().channel();
    System.out.println("NettyRestServer started. Listening on: " + hostPort);
}
Also used : NettyRestChannelInitializer(rpc.turbo.transport.server.rest.handler.NettyRestChannelInitializer) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)56 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)47 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 WriteBufferWaterMark (io.netty.channel.WriteBufferWaterMark)6 LoggingHandler (io.netty.handler.logging.LoggingHandler)6