Example usage for io.netty.channel ChannelOption SO_RCVBUF

List of usage examples for io.netty.channel ChannelOption SO_RCVBUF

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption SO_RCVBUF.

Prototype

ChannelOption SO_RCVBUF

To view the source code for io.netty.channel ChannelOption SO_RCVBUF.

Click Source Link

Usage

From source file:org.apache.rocketmq.remoting.netty.NettyRemotingClient.java

License:Apache License

@Override
public void start() {
    this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(//
            nettyClientConfig.getClientWorkerThreads(), //
            new ThreadFactory() {

                private AtomicInteger threadIndex = new AtomicInteger(0);

                @Override// ww  w  .j  a  v a 2s.  c  o m
                public Thread newThread(Runnable r) {
                    return new Thread(r, "NettyClientWorkerThread_" + this.threadIndex.incrementAndGet());
                }
            });

    Bootstrap handler = this.bootstrap.group(this.eventLoopGroupWorker).channel(NioSocketChannel.class)//
            .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, false)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, nettyClientConfig.getConnectTimeoutMillis())
            .option(ChannelOption.SO_SNDBUF, nettyClientConfig.getClientSocketSndBufSize())
            .option(ChannelOption.SO_RCVBUF, nettyClientConfig.getClientSocketRcvBufSize())
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(defaultEventExecutorGroup, new NettyEncoder(), new NettyDecoder(),
                            new IdleStateHandler(0, 0, nettyClientConfig.getClientChannelMaxIdleTimeSeconds()),
                            new NettyConnectManageHandler(), new NettyClientHandler());
                }
            });

    this.timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            try {
                NettyRemotingClient.this.scanResponseTable();
            } catch (Exception e) {
                log.error("scanResponseTable exception", e);
            }
        }
    }, 1000 * 3, 1000);

    if (this.channelEventListener != null) {
        this.nettyEventExecuter.start();
    }
}

From source file:org.apache.rocketmq.remoting.netty.NettyRemotingServer.java

License:Apache License

@Override
public void start() {
    this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(nettyServerConfig.getServerWorkerThreads(),
            new ThreadFactory() {

                private AtomicInteger threadIndex = new AtomicInteger(0);

                @Override//from  w  w  w . ja  va  2  s .com
                public Thread newThread(Runnable r) {
                    return new Thread(r, "NettyServerCodecThread_" + this.threadIndex.incrementAndGet());
                }
            });

    ServerBootstrap childHandler = this.serverBootstrap
            .group(this.eventLoopGroupBoss, this.eventLoopGroupSelector).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, 1024).option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_KEEPALIVE, false).childOption(ChannelOption.TCP_NODELAY, true)
            .option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize())
            .option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize())
            .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort()))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(defaultEventExecutorGroup, new NettyEncoder(), new NettyDecoder(),
                            new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()),
                            new NettyConnetManageHandler(), new NettyServerHandler());
                }
            });

    if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) {
        childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    }

    try {
        ChannelFuture sync = this.serverBootstrap.bind().sync();
        InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress();
        this.port = addr.getPort();
    } catch (InterruptedException e1) {
        throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1);
    }

    if (this.channelEventListener != null) {
        this.nettyEventExecuter.start();
    }

    this.timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                NettyRemotingServer.this.scanResponseTable();
            } catch (Exception e) {
                log.error("scanResponseTable exception", e);
            }
        }
    }, 1000 * 3, 1000);
}

From source file:org.apache.spark.network.client.TransportClientFactory.java

License:Apache License

/** Create a completely new {@link TransportClient} to the remote address. */
private TransportClient createClient(InetSocketAddress address) throws IOException, InterruptedException {
    logger.debug("Creating new connection to {}", address);

    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(workerGroup).channel(socketChannelClass)
            // Disable Nagle's Algorithm since we don't want packets to wait
            .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, conf.connectionTimeoutMs())
            .option(ChannelOption.ALLOCATOR, pooledAllocator);

    if (conf.receiveBuf() > 0) {
        bootstrap.option(ChannelOption.SO_RCVBUF, conf.receiveBuf());
    }// w  w  w  . jav  a 2  s.  c o m

    if (conf.sendBuf() > 0) {
        bootstrap.option(ChannelOption.SO_SNDBUF, conf.sendBuf());
    }

    final AtomicReference<TransportClient> clientRef = new AtomicReference<>();
    final AtomicReference<Channel> channelRef = new AtomicReference<>();

    bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) {
            TransportChannelHandler clientHandler = context.initializePipeline(ch);
            clientRef.set(clientHandler.getClient());
            channelRef.set(ch);
        }
    });

    // Connect to the remote server
    long preConnect = System.nanoTime();
    ChannelFuture cf = bootstrap.connect(address);
    if (!cf.await(conf.connectionTimeoutMs())) {
        throw new IOException(
                String.format("Connecting to %s timed out (%s ms)", address, conf.connectionTimeoutMs()));
    } else if (cf.cause() != null) {
        throw new IOException(String.format("Failed to connect to %s", address), cf.cause());
    }

    TransportClient client = clientRef.get();
    Channel channel = channelRef.get();
    assert client != null : "Channel future completed successfully with null client";

    // Execute any client bootstraps synchronously before marking the Client as successful.
    long preBootstrap = System.nanoTime();
    logger.debug("Connection to {} successful, running bootstraps...", address);
    try {
        for (TransportClientBootstrap clientBootstrap : clientBootstraps) {
            clientBootstrap.doBootstrap(client, channel);
        }
    } catch (Exception e) { // catch non-RuntimeExceptions too as bootstrap may be written in Scala
        long bootstrapTimeMs = (System.nanoTime() - preBootstrap) / 1000000;
        logger.error("Exception while bootstrapping client after " + bootstrapTimeMs + " ms", e);
        client.close();
        throw Throwables.propagate(e);
    }
    long postBootstrap = System.nanoTime();

    logger.info("Successfully created connection to {} after {} ms ({} ms spent in bootstraps)", address,
            (postBootstrap - preConnect) / 1000000, (postBootstrap - preBootstrap) / 1000000);

    return client;
}

From source file:org.apache.spark.network.netty.FileServer.java

License:Apache License

FileServer(PathResolver pResolver, int port) {
    InetSocketAddress addr = new InetSocketAddress(port);

    // Configure the server.
    bossGroup = new OioEventLoopGroup();
    workerGroup = new OioEventLoopGroup();

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(OioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, 100).option(ChannelOption.SO_RCVBUF, 1500)
            .childHandler(new FileServerChannelInitializer(pResolver));
    // Start the server.
    channelFuture = bootstrap.bind(addr);
    try {/*from ww w .j  ava  2s .c o  m*/
        // Get the address we bound to.
        InetSocketAddress boundAddress = ((InetSocketAddress) channelFuture.sync().channel().localAddress());
        this.port = boundAddress.getPort();
    } catch (InterruptedException ie) {
        this.port = 0;
    }
}

From source file:org.apache.spark.network.server.TransportServer.java

License:Apache License

private void init(String hostToBind, int portToBind) {

    IOMode ioMode = IOMode.valueOf(conf.ioMode());
    EventLoopGroup bossGroup = NettyUtils.createEventLoop(ioMode, conf.serverThreads(),
            conf.getModuleName() + "-server");
    EventLoopGroup workerGroup = bossGroup;

    PooledByteBufAllocator allocator = NettyUtils.createPooledByteBufAllocator(conf.preferDirectBufs(),
            true /* allowCache */, conf.serverThreads());

    bootstrap = new ServerBootstrap().group(bossGroup, workerGroup)
            .channel(NettyUtils.getServerChannelClass(ioMode)).option(ChannelOption.ALLOCATOR, allocator)
            .option(ChannelOption.SO_REUSEADDR, !SystemUtils.IS_OS_WINDOWS)
            .childOption(ChannelOption.ALLOCATOR, allocator);

    this.metrics = new NettyMemoryMetrics(allocator, conf.getModuleName() + "-server", conf);

    if (conf.backLog() > 0) {
        bootstrap.option(ChannelOption.SO_BACKLOG, conf.backLog());
    }//  w ww  . j  a  v  a  2 s . co m

    if (conf.receiveBuf() > 0) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, conf.receiveBuf());
    }

    if (conf.sendBuf() > 0) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, conf.sendBuf());
    }

    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) {
            RpcHandler rpcHandler = appRpcHandler;
            for (TransportServerBootstrap bootstrap : bootstraps) {
                rpcHandler = bootstrap.doBootstrap(ch, rpcHandler);
            }
            context.initializePipeline(ch, rpcHandler);
        }
    });

    InetSocketAddress address = hostToBind == null ? new InetSocketAddress(portToBind)
            : new InetSocketAddress(hostToBind, portToBind);
    channelFuture = bootstrap.bind(address);
    channelFuture.syncUninterruptibly();

    port = ((InetSocketAddress) channelFuture.channel().localAddress()).getPort();
    logger.debug("Shuffle server started on port: {}", port);
}

From source file:org.apache.tajo.rpc.NettyClientBase.java

License:Apache License

protected void init(ChannelInitializer<Channel> initializer) {
    this.bootstrap = new Bootstrap();
    this.bootstrap.channel(NioSocketChannel.class).handler(initializer)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECTION_TIMEOUT)
            .option(ChannelOption.SO_RCVBUF, 1048576 * 10).option(ChannelOption.TCP_NODELAY, true);
}

From source file:org.apache.tajo.rpc.NettyServerBase.java

License:Apache License

public void init(ChannelInitializer<Channel> initializer, int workerNum) {
    for (RpcEventListener listener : listeners) {
        listener.onBeforeInit(this);
    }//from www. j av  a2  s .  c  o m

    bootstrap = RpcChannelFactory.createServerChannelFactory(serviceName, workerNum);

    this.initializer = initializer;
    bootstrap.channel(NioServerSocketChannel.class).childHandler(initializer)
            .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
            .childOption(ChannelOption.SO_RCVBUF, 1048576 * 10);

    for (RpcEventListener listener : listeners) {
        listener.onAfterInit(this);
    }
}

From source file:org.apache.tajo.worker.Fetcher.java

License:Apache License

public Fetcher(TajoConf conf, URI uri, FileChunk chunk) {
    this.uri = uri;
    this.fileChunk = chunk;
    this.useLocalFile = !chunk.fromRemote();
    this.state = TajoProtos.FetcherState.FETCH_INIT;
    this.conf = conf;

    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    this.host = uri.getHost() == null ? "localhost" : uri.getHost();
    this.port = uri.getPort();
    if (port == -1) {
        if (scheme.equalsIgnoreCase("http")) {
            this.port = 80;
        } else if (scheme.equalsIgnoreCase("https")) {
            this.port = 443;
        }//from   w  w  w .  j  a v  a 2  s.  co  m
    }

    if (!useLocalFile) {
        bootstrap = new Bootstrap()
                .group(RpcChannelFactory.getSharedClientEventloopGroup(
                        RpcChannelFactory.ClientChannelId.FETCHER,
                        conf.getIntVar(TajoConf.ConfVars.SHUFFLE_RPC_CLIENT_WORKER_THREAD_NUM)))
                .channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // set 5 sec
                .option(ChannelOption.SO_RCVBUF, 1048576) // set 1M
                .option(ChannelOption.TCP_NODELAY, true);

        ChannelInitializer<Channel> initializer = new HttpClientChannelInitializer(fileChunk.getFile());
        bootstrap.handler(initializer);
    }
}

From source file:org.apache.tajo.worker.LocalFetcher.java

License:Apache License

@VisibleForTesting
public LocalFetcher(TajoConf conf, URI uri, String tableName) throws IOException {
    super(conf, uri);
    this.maxUrlLength = conf.getIntVar(ConfVars.PULLSERVER_FETCH_URL_MAX_LENGTH);
    this.tableName = tableName;
    this.localFileSystem = new LocalFileSystem();
    this.localDirAllocator = new LocalDirAllocator(ConfVars.WORKER_TEMPORAL_DIR.varname);
    this.pullServerService = null;

    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    this.host = uri.getHost() == null ? "localhost" : uri.getHost();
    this.port = uri.getPort();
    if (port == -1) {
        if (scheme.equalsIgnoreCase("http")) {
            this.port = 80;
        } else if (scheme.equalsIgnoreCase("https")) {
            this.port = 443;
        }/*from w  w  w .  j a v a 2  s  .  c  o  m*/
    }

    bootstrap = new Bootstrap()
            .group(NettyUtils.getSharedEventLoopGroup(NettyUtils.GROUP.FETCHER,
                    conf.getIntVar(ConfVars.SHUFFLE_RPC_CLIENT_WORKER_THREAD_NUM)))
            .channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, NettyUtils.ALLOCATOR)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
                    conf.getIntVar(ConfVars.SHUFFLE_FETCHER_CONNECT_TIMEOUT) * 1000)
            .option(ChannelOption.SO_RCVBUF, 1048576) // set 1M
            .option(ChannelOption.TCP_NODELAY, true);
}

From source file:org.apache.tajo.worker.LocalFetcher.java

License:Apache License

public LocalFetcher(TajoConf conf, URI uri, ExecutionBlockContext executionBlockContext, String tableName) {
    super(conf, uri);
    this.localFileSystem = executionBlockContext.getLocalFS();
    this.localDirAllocator = executionBlockContext.getLocalDirAllocator();
    this.maxUrlLength = conf.getIntVar(ConfVars.PULLSERVER_FETCH_URL_MAX_LENGTH);
    this.tableName = tableName;

    Optional<TajoPullServerService> optional = executionBlockContext.getSharedResource().getPullServerService();
    if (optional.isPresent()) {
        // local pull server service
        this.pullServerService = optional.get();
        this.host = null;
        this.bootstrap = null;

    } else if (PullServerUtil.useExternalPullServerService(conf)) {
        // external pull server service
        pullServerService = null;/*  www  . j  a v  a 2s  .  c o  m*/

        String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
        this.host = uri.getHost() == null ? "localhost" : uri.getHost();
        this.port = uri.getPort();
        if (port == -1) {
            if (scheme.equalsIgnoreCase("http")) {
                this.port = 80;
            } else if (scheme.equalsIgnoreCase("https")) {
                this.port = 443;
            }
        }

        bootstrap = new Bootstrap()
                .group(NettyUtils.getSharedEventLoopGroup(NettyUtils.GROUP.FETCHER,
                        conf.getIntVar(ConfVars.SHUFFLE_RPC_CLIENT_WORKER_THREAD_NUM)))
                .channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, NettyUtils.ALLOCATOR)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
                        conf.getIntVar(ConfVars.SHUFFLE_FETCHER_CONNECT_TIMEOUT) * 1000)
                .option(ChannelOption.SO_RCVBUF, 1048576) // set 1M
                .option(ChannelOption.TCP_NODELAY, true);
    } else {
        endFetch(FetcherState.FETCH_FAILED);
        throw new TajoInternalError("Pull server service is not initialized");
    }
}