Example usage for io.netty.channel ChannelOption WRITE_BUFFER_LOW_WATER_MARK

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

Introduction

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

Prototype

ChannelOption WRITE_BUFFER_LOW_WATER_MARK

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

Click Source Link

Usage

From source file:alluxio.worker.grpc.GrpcDataServer.java

License:Apache License

private GrpcServerBuilder createServerBuilder(String hostName, SocketAddress bindAddress, ChannelType type) {
    GrpcServerBuilder builder = GrpcServerBuilder.forAddress(hostName, bindAddress,
            ServerConfiguration.global(), ServerUserState.global());
    int bossThreadCount = ServerConfiguration.getInt(PropertyKey.WORKER_NETWORK_NETTY_BOSS_THREADS);

    // If number of worker threads is 0, Netty creates (#processors * 2) threads by default.
    int workerThreadCount = ServerConfiguration.getInt(PropertyKey.WORKER_NETWORK_NETTY_WORKER_THREADS);
    String dataServerEventLoopNamePrefix = "data-server-"
            + ((mSocketAddress instanceof DomainSocketAddress) ? "domain-socket" : "tcp-socket");
    mBossGroup = NettyUtils.createEventLoop(type, bossThreadCount, dataServerEventLoopNamePrefix + "-boss-%d",
            true);/*w w w . jav a 2s.com*/
    mWorkerGroup = NettyUtils.createEventLoop(type, workerThreadCount,
            dataServerEventLoopNamePrefix + "-worker-%d", true);
    Class<? extends ServerChannel> socketChannelClass = NettyUtils
            .getServerChannelClass(mSocketAddress instanceof DomainSocketAddress, ServerConfiguration.global());
    if (type == ChannelType.EPOLL) {
        builder.withChildOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    }
    return builder.bossEventLoopGroup(mBossGroup).workerEventLoopGroup(mWorkerGroup)
            .channelType(socketChannelClass)
            .withChildOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            // set write buffer
            // this is the default, but its recommended to set it in case of change in future netty.
            .withChildOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK,
                    (int) ServerConfiguration.getBytes(PropertyKey.WORKER_NETWORK_NETTY_WATERMARK_HIGH))
            .withChildOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK,
                    (int) ServerConfiguration.getBytes(PropertyKey.WORKER_NETWORK_NETTY_WATERMARK_LOW));
}

From source file:alluxio.worker.netty.NettyDataServer.java

License:Apache License

private ServerBootstrap createBootstrap() {
    final ServerBootstrap boot = createBootstrapOfType(
            Configuration.getEnum(PropertyKey.WORKER_NETWORK_NETTY_CHANNEL, ChannelType.class));

    // use pooled buffers
    boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    boot.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

    // set write buffer
    // this is the default, but its recommended to set it in case of change in future netty.
    boot.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK,
            (int) Configuration.getBytes(PropertyKey.WORKER_NETWORK_NETTY_WATERMARK_HIGH));
    boot.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK,
            (int) Configuration.getBytes(PropertyKey.WORKER_NETWORK_NETTY_WATERMARK_LOW));

    // more buffer settings on Netty socket option, one can tune them by specifying
    // properties, e.g.:
    // alluxio.worker.network.netty.backlog=50
    // alluxio.worker.network.netty.buffer.send=64KB
    // alluxio.worker.network.netty.buffer.receive=64KB
    if (Configuration.containsKey(PropertyKey.WORKER_NETWORK_NETTY_BACKLOG)) {
        boot.option(ChannelOption.SO_BACKLOG, Configuration.getInt(PropertyKey.WORKER_NETWORK_NETTY_BACKLOG));
    }/* www .  ja v  a 2 s . c o m*/
    if (Configuration.containsKey(PropertyKey.WORKER_NETWORK_NETTY_BUFFER_SEND)) {
        boot.option(ChannelOption.SO_SNDBUF,
                (int) Configuration.getBytes(PropertyKey.WORKER_NETWORK_NETTY_BUFFER_SEND));
    }
    if (Configuration.containsKey(PropertyKey.WORKER_NETWORK_NETTY_BUFFER_RECEIVE)) {
        boot.option(ChannelOption.SO_RCVBUF,
                (int) Configuration.getBytes(PropertyKey.WORKER_NETWORK_NETTY_BUFFER_RECEIVE));
    }
    return boot;
}

From source file:com.addthis.hydra.query.web.QueryServer.java

License:Apache License

public void run() throws Exception {
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    executorGroup = new DefaultEventExecutorGroup(AggregateConfig.FRAME_READER_THREADS,
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("frame-reader-%d").build());
    for (EventExecutor executor : executorGroup) {
        executor.execute(new NextQueryTask(queryQueue, executor));
    }//from   w ww . j  a va 2  s . c  o  m
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, new DefaultMessageSizeEstimator(200));
    b.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 100000000);
    b.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 50000000);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(queryServerInitializer);
    b.bind(webPort).sync();
}

From source file:com.addthis.meshy.Meshy.java

License:Apache License

protected Meshy() {
    if (HOSTNAME != null) {
        uuid = HOSTNAME + "-" + Long.toHexString(System.currentTimeMillis() & 0xffffff);
    } else {//from ww  w. j  av a  2 s. c om
        uuid = Long.toHexString(UUID.randomUUID().getMostSignificantBits());
    }
    workerGroup = new NioEventLoopGroup();
    clientBootstrap = new Bootstrap().option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
            .option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATERMARK)
            .option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATERMARK).channel(NioSocketChannel.class)
            .group(workerGroup).handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ChannelState(Meshy.this, ch));
                }
            });
    updateLastEventTime();
}

From source file:com.addthis.meshy.MeshyServer.java

License:Apache License

public MeshyServer(final int port, final File rootDir, @Nullable String[] netif, final MeshyServerGroup group)
        throws IOException {
    super();/*from   w  w  w .j a  va  2 s. c  o m*/
    this.group = group;
    this.rootDir = rootDir;
    this.filesystems = loadFileSystems(rootDir);
    this.serverPeers = new AtomicInteger(0);
    bossGroup = new NioEventLoopGroup(1);
    ServerBootstrap bootstrap = new ServerBootstrap()
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.SO_BACKLOG, 1024).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
            .option(ChannelOption.SO_REUSEADDR, true)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, HIGH_WATERMARK)
            .childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, LOW_WATERMARK)
            .channel(NioServerSocketChannel.class).group(bossGroup, workerGroup)
            .childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ChannelState(MeshyServer.this, ch));
                }
            });
    /* bind to one or more interfaces, if supplied, otherwise all */
    if ((netif == null) || (netif.length == 0)) {
        ServerSocketChannel serverChannel = (ServerSocketChannel) bootstrap.bind(new InetSocketAddress(port))
                .syncUninterruptibly().channel();
        serverLocal = serverChannel.localAddress();
    } else {
        InetSocketAddress primaryServerLocal = null;
        for (String net : netif) {
            NetworkInterface nicif = NetworkInterface.getByName(net);
            if (nicif == null) {
                log.warn("missing speficied NIC: {}", net);
                continue;
            }
            for (InterfaceAddress addr : nicif.getInterfaceAddresses()) {
                InetAddress inAddr = addr.getAddress();
                if (inAddr.getAddress().length != 4) {
                    log.trace("skip non-ipV4 address: {}", inAddr);
                    continue;
                }
                ServerSocketChannel serverChannel = (ServerSocketChannel) bootstrap
                        .bind(new InetSocketAddress(inAddr, port)).syncUninterruptibly().channel();
                if (primaryServerLocal != null) {
                    log.info("server [{}-*] binding to extra address: {}", super.getUUID(), primaryServerLocal);
                }
                primaryServerLocal = serverChannel.localAddress();
            }
        }
        if (primaryServerLocal == null) {
            throw new IllegalArgumentException("no valid interface / port specified");
        }
        serverLocal = primaryServerLocal;
    }
    this.serverNetIf = NetworkInterface.getByInetAddress(serverLocal.getAddress());
    this.serverPort = serverLocal.getPort();
    if (serverNetIf != null) {
        serverUuid = super.getUUID() + "-" + serverPort + "-" + serverNetIf.getName();
    } else {
        serverUuid = super.getUUID() + "-" + serverPort;
    }
    log.info("server [{}] on {} @ {}", getUUID(), serverLocal, rootDir);
    closeFuture = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
    workerGroup.terminationFuture().addListener((Future<Object> workerFuture) -> {
        bossGroup.terminationFuture().addListener((Future<Object> bossFuture) -> {
            if (!workerFuture.isSuccess()) {
                closeFuture.tryFailure(workerFuture.cause());
            } else if (!bossFuture.isSuccess()) {
                closeFuture.tryFailure(bossFuture.cause());
            } else {
                closeFuture.trySuccess(null);
            }
        });
    });
    addMessageFileSystemPaths();
    group.join(this);
    if (autoMesh) {
        startAutoMesh(serverPort, autoMeshTimeout);
    }
}

From source file:com.allanbank.mongodb.netty.NettyTransportFactory.java

License:Apache License

/**
 * Creates a new {@link Bootstrap} for creating a Netty client
 * {@link Channel channels}.//from  w  ww  .j a  va  2 s .c  o m
 *
 * @param config
 *            The configuration for the client.
 * @return The {@link Bootstrap} to create Netty client channels.
 */
protected Bootstrap createBootstrap(final MongoClientConfiguration config) {
    final Bootstrap bootstrap = new Bootstrap();

    bootstrap.channel(NioSocketChannel.class);
    bootstrap.option(ChannelOption.ALLOCATOR, myBufferAllocator);
    bootstrap.group(myGroup);

    // Suggested defaults.
    bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
    bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);

    // Settings from the config.
    bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.valueOf(config.isUsingSoKeepalive()));
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Integer.valueOf(config.getConnectTimeout()));

    return bootstrap;
}

From source file:com.caricah.iotracah.server.netty.ServerImpl.java

License:Apache License

/**
 * The @link configure method is responsible for starting the implementation server processes.
 * The implementation should return once the server has started this allows
 * the launcher to maintain the life of the application.
 *
 * @throws UnRetriableException//from   w  w  w. j av a 2s  .co  m
 */
public void initiate() throws UnRetriableException {

    log.info(" configure : initiating the netty server.");

    try {

        int countOfAvailableProcessors = Runtime.getRuntime().availableProcessors() + 1;

        if (Epoll.isAvailable()) {
            bossEventLoopGroup = new EpollEventLoopGroup(2, getExecutorService());
            workerEventLoopGroup = new EpollEventLoopGroup(countOfAvailableProcessors, getExecutorService());

        } else {
            bossEventLoopGroup = new NioEventLoopGroup(2, getExecutorService());
            workerEventLoopGroup = new NioEventLoopGroup(countOfAvailableProcessors, getExecutorService());
        }

        //Initialize listener for TCP
        ServerBootstrap tcpBootstrap = new ServerBootstrap();
        tcpBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
        tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);

        tcpBootstrap = tcpBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);

        if (Epoll.isAvailable()) {
            tcpBootstrap = tcpBootstrap.channel(EpollServerSocketChannel.class);
        } else {
            tcpBootstrap = tcpBootstrap.channel(NioServerSocketChannel.class);
        }

        tcpBootstrap = tcpBootstrap.handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(getServerInitializer(this, getConnectionTimeout()));

        ChannelFuture tcpChannelFuture = tcpBootstrap.bind(getTcpPort()).sync();
        tcpChannel = tcpChannelFuture.channel();

        if (isSslEnabled()) {
            //Initialize listener for SSL
            ServerBootstrap sslBootstrap = new ServerBootstrap();
            sslBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
            sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
            sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);

            sslBootstrap = sslBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);

            if (Epoll.isAvailable()) {
                sslBootstrap = sslBootstrap.channel(EpollServerSocketChannel.class);
            } else {
                sslBootstrap = sslBootstrap.channel(NioServerSocketChannel.class);
            }

            sslBootstrap = sslBootstrap.handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(getServerInitializer(this, getConnectionTimeout(), getSslHandler()));

            ChannelFuture sslChannelFuture = sslBootstrap.bind(getSslPort()).sync();
            sslChannel = sslChannelFuture.channel();
        }

    } catch (InterruptedException e) {

        log.error(" configure : Initialization issues ", e);

        throw new UnRetriableException(e);

    }

}

From source file:com.couchbase.client.core.io.endpoint.AbstractEndpoint.java

License:Open Source License

/**
 * Create a new {@link AbstractEndpoint} and supply essential params.
 *
 * @param addr the socket address to connect to.
 * @param env the environment to attach to.
 * @param group the {@link EventLoopGroup} to use.
 *///from   w w w . jav  a  2 s.c  o m
protected AbstractEndpoint(final InetSocketAddress addr, final Environment env, final EventLoopGroup group) {
    this.env = env;
    endpointStateDeferred = Streams.defer(env, defaultPromiseEnv);
    endpointStateStream = endpointStateDeferred.compose();

    connectionBootstrap = new BootstrapAdapter(new Bootstrap().group(group).channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(final SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    if (LOGGER.isTraceEnabled()) {
                        pipeline.addLast(new LoggingHandler(LogLevel.TRACE));
                    }

                    customEndpointHandlers(pipeline);
                    pipeline.addLast(new GenericEndpointHandler<REQ, RES>());
                }
            }).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.TCP_NODELAY, false).option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1500)
            .remoteAddress(addr));
}

From source file:com.github.milenkovicm.kafka.connection.AbstractKafkaBroker.java

License:Apache License

public AbstractKafkaBroker(String hostname, int port, String topicName, EventLoopGroup workerGroup,
        ProducerProperties properties) {
    this.hostname = hostname;
    this.port = port;
    this.topicName = topicName;
    this.workerGroup = workerGroup;
    this.properties = properties;

    this.bootstrap = new Bootstrap();
    this.bootstrap.group(this.workerGroup);
    this.bootstrap.channel(NioSocketChannel.class);
    this.bootstrap.option(ChannelOption.TCP_NODELAY, true);
    this.bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK,
            properties.get(ProducerProperties.NETTY_HIGH_WATERMARK));
    this.bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK,
            properties.get(ProducerProperties.NETTY_LOW_WATERMARK));

    this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    if (properties.get(ProducerProperties.SO_TIMEOUT) > 0) {
        this.bootstrap.option(ChannelOption.SO_TIMEOUT, 0);
    }/*from  w w  w  .  j  a v a 2  s  .c  om*/

    if (properties.get(ProducerProperties.SO_RCVBUF) > 0) {
        this.bootstrap.option(ChannelOption.SO_RCVBUF, 0);
    }

    if (properties.get(ProducerProperties.SO_SNDBUF) > 0) {
        this.bootstrap.option(ChannelOption.SO_SNDBUF, 0);
    }

    this.bootstrap.handler(pipeline());
}

From source file:com.lambdaworks.redis.AbstractRedisClient.java

License:Apache License

/**
 * Populate connection builder with necessary resources.
 *
 * @param handler instance of a CommandHandler for writing redis commands
 * @param connection implementation of a RedisConnection
 * @param socketAddressSupplier address supplier for initial connect and re-connect
 * @param connectionBuilder connection builder to configure the connection
 * @param redisURI URI of the redis instance
 *//*from   w  w w  .  j av a2s .  c  o  m*/
protected void connectionBuilder(CommandHandler<?, ?> handler, RedisChannelHandler<?, ?> connection,
        Supplier<SocketAddress> socketAddressSupplier, ConnectionBuilder connectionBuilder, RedisURI redisURI) {

    Bootstrap redisBootstrap = new Bootstrap();
    redisBootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
    redisBootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
    redisBootstrap.option(ChannelOption.ALLOCATOR, BUF_ALLOCATOR);

    SocketOptions socketOptions = getOptions().getSocketOptions();

    redisBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            (int) socketOptions.getConnectTimeoutUnit().toMillis(socketOptions.getConnectTimeout()));

    if (LettuceStrings.isEmpty(redisURI.getSocket())) {
        redisBootstrap.option(ChannelOption.SO_KEEPALIVE, socketOptions.isKeepAlive());
        redisBootstrap.option(ChannelOption.TCP_NODELAY, socketOptions.isTcpNoDelay());
    }

    connectionBuilder.timeout(redisURI.getTimeout(), redisURI.getUnit());
    connectionBuilder.password(redisURI.getPassword());

    connectionBuilder.bootstrap(redisBootstrap);
    connectionBuilder.channelGroup(channels).connectionEvents(connectionEvents).timer(timer);
    connectionBuilder.commandHandler(handler).socketAddressSupplier(socketAddressSupplier)
            .connection(connection);
    connectionBuilder.workerPool(genericWorkerPool);
}