Example usage for io.netty.channel FixedRecvByteBufAllocator FixedRecvByteBufAllocator

List of usage examples for io.netty.channel FixedRecvByteBufAllocator FixedRecvByteBufAllocator

Introduction

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

Prototype

public FixedRecvByteBufAllocator(int bufferSize) 

Source Link

Document

Creates a new predictor that always returns the same prediction of the specified buffer size.

Usage

From source file:org.apache.activemq.transport.netty.NettyTcpTransport.java

License:Apache License

private void configureNetty(Bootstrap bootstrap, NettyTransportOptions options) {
    bootstrap.option(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeout());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
    bootstrap.option(ChannelOption.SO_LINGER, options.getSoLinger());

    if (options.getSendBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, options.getSendBufferSize());
    }//from   w w w  . j a  v  a 2s  .c  om

    if (options.getReceiveBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, options.getReceiveBufferSize());
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }

    if (options.getTrafficClass() != -1) {
        bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
    }
}

From source file:org.apache.flink.runtime.io.network.netty.NettyConnectionManager.java

License:Apache License

@Override
public void start(ChannelManager channelManager) throws IOException {
    LOG.info(String.format("Starting with %d incoming and %d outgoing connection threads.", numInThreads,
            numOutThreads));//  ww  w  .  java2 s  .c  o m
    LOG.info(String.format("Setting low water mark to %d and high water mark to %d bytes.", lowWaterMark,
            highWaterMark));
    LOG.info(String.format("Close channels after idle for %d ms.", closeAfterIdleForMs));

    final BufferProviderBroker bufferProviderBroker = channelManager;
    final EnvelopeDispatcher envelopeDispatcher = channelManager;

    int numHeapArenas = 0;
    int numDirectArenas = numInThreads + numOutThreads;
    int pageSize = bufferSize << 1;
    int chunkSize = 16 << 20; // 16 MB

    // shift pageSize maxOrder times to get to chunkSize
    int maxOrder = (int) (Math.log(chunkSize / pageSize) / Math.log(2));

    PooledByteBufAllocator pooledByteBufAllocator = new PooledByteBufAllocator(true, numHeapArenas,
            numDirectArenas, pageSize, maxOrder);

    String msg = String.format(
            "Instantiated PooledByteBufAllocator with direct arenas: %d, heap arenas: %d, "
                    + "page size (bytes): %d, chunk size (bytes): %d.",
            numDirectArenas, numHeapArenas, pageSize, (pageSize << maxOrder));
    LOG.info(msg);

    // --------------------------------------------------------------------
    // server bootstrap (incoming connections)
    // --------------------------------------------------------------------
    in = new ServerBootstrap();
    in.group(new NioEventLoopGroup(numInThreads)).channel(NioServerSocketChannel.class)
            .localAddress(bindAddress, bindPort).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel channel) throws Exception {
                    channel.pipeline().addLast(new InboundEnvelopeDecoder(bufferProviderBroker))
                            .addLast(new InboundEnvelopeDispatcher(envelopeDispatcher));
                }
            }).option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(pageSize))
            .option(ChannelOption.ALLOCATOR, pooledByteBufAllocator);

    // --------------------------------------------------------------------
    // client bootstrap (outgoing connections)
    // --------------------------------------------------------------------
    out = new Bootstrap();
    out.group(new NioEventLoopGroup(numOutThreads)).channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel channel) throws Exception {
                    channel.pipeline().addLast(new OutboundEnvelopeEncoder());
                }
            }).option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, lowWaterMark)
            .option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, highWaterMark)
            .option(ChannelOption.ALLOCATOR, pooledByteBufAllocator).option(ChannelOption.TCP_NODELAY, false)
            .option(ChannelOption.SO_KEEPALIVE, true);

    try {
        in.bind().sync();
    } catch (InterruptedException e) {
        throw new IOException(e);
    }

    if (LOG.isDebugEnabled()) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Date date = new Date();

                while (true) {
                    try {
                        Thread.sleep(DEBUG_PRINT_QUEUED_ENVELOPES_EVERY_MS);

                        date.setTime(System.currentTimeMillis());

                        System.out.println(date);
                        System.out.println(getNonZeroNumQueuedEnvelopes());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

From source file:org.apache.hyracks.http.server.HttpServer.java

License:Apache License

protected void doStart() throws InterruptedException {
    /*//from   w  w  w .  ja  v  a 2s  .  c  om
     * This is a hacky way to ensure that IServlets with more specific paths are checked first.
     * For example:
     * "/path/to/resource/"
     * is checked before
     * "/path/to/"
     * which in turn is checked before
     * "/path/"
     * Note that it doesn't work for the case where multiple paths map to a single IServlet
     */
    Collections.sort(servlets, (l1, l2) -> l2.getPaths()[0].length() - l1.getPaths()[0].length());
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(RECEIVE_BUFFER_SIZE))
            .childOption(ChannelOption.AUTO_READ, Boolean.FALSE)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WRITE_BUFFER_WATER_MARK)
            .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new HttpServerInitializer(this));
    channel = b.bind(port).sync().channel();
}

From source file:org.apache.qpid.jms.transports.netty.NettyTcpTransport.java

License:Apache License

private void configureNetty(Bootstrap bootstrap, TransportOptions options) {
    bootstrap.option(ChannelOption.TCP_NODELAY, options.isTcpNoDelay());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeout());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, options.isTcpKeepAlive());
    bootstrap.option(ChannelOption.SO_LINGER, options.getSoLinger());
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);

    if (options.getSendBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, options.getSendBufferSize());
    }/*from www.  ja v a  2 s  . c om*/

    if (options.getReceiveBufferSize() != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, options.getReceiveBufferSize());
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(options.getReceiveBufferSize()));
    }

    if (options.getTrafficClass() != -1) {
        bootstrap.option(ChannelOption.IP_TOS, options.getTrafficClass());
    }
}

From source file:org.elasticsearch.hadoop.http.netty4.Netty4HttpServerTransport.java

License:Apache License

@Inject
public Netty4HttpServerTransport(Settings settings, NetworkService networkService, BigArrays bigArrays,
        ThreadPool threadPool) {/*  w ww. j  a  va2s  .  c  o m*/
    super(settings);
    this.networkService = networkService;
    this.bigArrays = bigArrays;
    this.threadPool = threadPool;

    ByteSizeValue maxContentLength = SETTING_HTTP_MAX_CONTENT_LENGTH.get(settings);
    this.maxChunkSize = SETTING_HTTP_MAX_CHUNK_SIZE.get(settings);
    this.maxHeaderSize = SETTING_HTTP_MAX_HEADER_SIZE.get(settings);
    this.maxInitialLineLength = SETTING_HTTP_MAX_INITIAL_LINE_LENGTH.get(settings);
    this.resetCookies = SETTING_HTTP_RESET_COOKIES.get(settings);
    this.maxCumulationBufferCapacity = SETTING_HTTP_NETTY_MAX_CUMULATION_BUFFER_CAPACITY.get(settings);
    this.maxCompositeBufferComponents = SETTING_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS.get(settings);
    this.workerCount = SETTING_HTTP_WORKER_COUNT.get(settings);
    this.blockingServer = SETTING_HTTP_TCP_BLOCKING_SERVER.get(settings);
    this.port = SETTING_HTTP_PORT.get(settings);
    this.bindHosts = SETTING_HTTP_BIND_HOST.get(settings).toArray(Strings.EMPTY_ARRAY);
    this.publishHosts = SETTING_HTTP_PUBLISH_HOST.get(settings).toArray(Strings.EMPTY_ARRAY);
    this.tcpNoDelay = SETTING_HTTP_TCP_NO_DELAY.get(settings);
    this.tcpKeepAlive = SETTING_HTTP_TCP_KEEP_ALIVE.get(settings);
    this.reuseAddress = SETTING_HTTP_TCP_REUSE_ADDRESS.get(settings);
    this.tcpSendBufferSize = SETTING_HTTP_TCP_SEND_BUFFER_SIZE.get(settings);
    this.tcpReceiveBufferSize = SETTING_HTTP_TCP_RECEIVE_BUFFER_SIZE.get(settings);
    this.detailedErrorsEnabled = SETTING_HTTP_DETAILED_ERRORS_ENABLED.get(settings);

    // See AdaptiveReceiveBufferSizePredictor#DEFAULT_XXX for default values in netty..., we can use higher ones for us, even fixed one
    ByteSizeValue receivePredictorMin = SETTING_HTTP_NETTY_RECEIVE_PREDICTOR_MIN.get(settings);
    ByteSizeValue receivePredictorMax = SETTING_HTTP_NETTY_RECEIVE_PREDICTOR_MAX.get(settings);
    if (receivePredictorMax.bytes() == receivePredictorMin.bytes()) {
        recvByteBufAllocator = new FixedRecvByteBufAllocator(Math.toIntExact(receivePredictorMax.bytes()));
    } else {
        recvByteBufAllocator = new AdaptiveRecvByteBufAllocator(Math.toIntExact(receivePredictorMin.bytes()),
                Math.toIntExact(receivePredictorMin.bytes()), Math.toIntExact(receivePredictorMax.bytes()));
    }

    this.compression = SETTING_HTTP_COMPRESSION.get(settings);
    this.compressionLevel = SETTING_HTTP_COMPRESSION_LEVEL.get(settings);
    this.pipelining = SETTING_PIPELINING.get(settings);
    this.pipeliningMaxEvents = SETTING_PIPELINING_MAX_EVENTS.get(settings);
    this.corsConfig = buildCorsConfig(settings);

    // validate max content length
    if (maxContentLength.bytes() > Integer.MAX_VALUE) {
        logger.warn("maxContentLength[{}] set to high value, resetting it to [100mb]", maxContentLength);
        maxContentLength = new ByteSizeValue(100, ByteSizeUnit.MB);
    }
    this.maxContentLength = maxContentLength;

    logger.debug(
            "using max_chunk_size[{}], max_header_size[{}], max_initial_line_length[{}], max_content_length[{}], "
                    + "receive_predictor[{}->{}], pipelining[{}], pipelining_max_events[{}]",
            maxChunkSize, maxHeaderSize, maxInitialLineLength, this.maxContentLength, receivePredictorMin,
            receivePredictorMax, pipelining, pipeliningMaxEvents);
}

From source file:org.elasticsearch.hadoop.transport.netty4.Netty4Transport.java

License:Apache License

@Inject
public Netty4Transport(Settings settings, ThreadPool threadPool, NetworkService networkService,
        BigArrays bigArrays, NamedWriteableRegistry namedWriteableRegistry,
        CircuitBreakerService circuitBreakerService) {
    super("netty", settings, threadPool, bigArrays, circuitBreakerService, namedWriteableRegistry,
            networkService);/*  w w  w  . ja v  a2  s  . c o  m*/
    this.workerCount = WORKER_COUNT.get(settings);
    this.maxCumulationBufferCapacity = NETTY_MAX_CUMULATION_BUFFER_CAPACITY.get(settings);
    this.maxCompositeBufferComponents = NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS.get(settings);

    // See AdaptiveReceiveBufferSizePredictor#DEFAULT_XXX for default values in netty..., we can use higher ones for us, even fixed one
    this.receivePredictorMin = NETTY_RECEIVE_PREDICTOR_MIN.get(settings);
    this.receivePredictorMax = NETTY_RECEIVE_PREDICTOR_MAX.get(settings);
    if (receivePredictorMax.bytes() == receivePredictorMin.bytes()) {
        recvByteBufAllocator = new FixedRecvByteBufAllocator((int) receivePredictorMax.bytes());
    } else {
        recvByteBufAllocator = new AdaptiveRecvByteBufAllocator((int) receivePredictorMin.bytes(),
                (int) receivePredictorMin.bytes(), (int) receivePredictorMax.bytes());
    }
}

From source file:org.graylog2.inputs.transports.UdpTransport.java

License:Open Source License

@VisibleForTesting
Bootstrap getBootstrap(MessageInput input) {
    LOG.debug("Setting UDP receive buffer size to {} bytes", getRecvBufferSize());
    final NettyTransportType transportType = nettyTransportConfiguration.getType();

    eventLoopGroup = eventLoopGroupFactory.create(workerThreads, localRegistry, "workers");

    return new Bootstrap().group(eventLoopGroup).channelFactory(new DatagramChannelFactory(transportType))
            .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(getRecvBufferSize()))
            .option(ChannelOption.SO_RCVBUF, getRecvBufferSize()).option(UnixChannelOption.SO_REUSEPORT, true)
            .handler(getChannelInitializer(getChannelHandlers(input))).validate();
}

From source file:org.graylog2.plugin.inputs.transports.AbstractTcpTransport.java

License:Open Source License

protected ServerBootstrap getBootstrap(MessageInput input) {
    final LinkedHashMap<String, Callable<? extends ChannelHandler>> parentHandlers = getChannelHandlers(input);
    final LinkedHashMap<String, Callable<? extends ChannelHandler>> childHandlers = getChildChannelHandlers(
            input);//from  w ww  .j a  va2  s . c  o m

    childEventLoopGroup = eventLoopGroupFactory.create(workerThreads, localRegistry, "workers");

    return new ServerBootstrap().group(parentEventLoopGroup, childEventLoopGroup)
            .channelFactory(new ServerSocketChannelFactory(nettyTransportConfiguration.getType()))
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(8192))
            .option(ChannelOption.SO_RCVBUF, getRecvBufferSize())
            .childOption(ChannelOption.SO_RCVBUF, getRecvBufferSize())
            .childOption(ChannelOption.SO_KEEPALIVE, tcpKeepalive)
            .handler(getChannelInitializer(parentHandlers)).childHandler(getChannelInitializer(childHandlers));
}

From source file:org.midonet.util.netty.ServerFrontEnd.java

License:Apache License

@Override
protected void doStart() {

    try {/*  w w  w  . j a  va  2 s  . co  m*/
        if (datagram) {
            log.info("Starting Netty UDP server on port {}", port);
            Bootstrap boot = new Bootstrap();
            boot.group(wrkr).channel(NioDatagramChannel.class).handler(adapter);
            if (rcvbufSize != null)
                boot.option(ChannelOption.SO_RCVBUF, rcvbufSize).option(ChannelOption.RCVBUF_ALLOCATOR,
                        new FixedRecvByteBufAllocator(rcvbufSize));
            sock = boot.bind(port).sync();
        } else {
            log.info("Starting Netty TCP server on port {}", port);
            ServerBootstrap boot = new ServerBootstrap();
            boot.group(boss, wrkr).channel(NioServerSocketChannel.class).childHandler(adapter)
                    .option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_KEEPALIVE, true);
            sock = boot.bind(port).sync();
        }
        log.info("Netty server started");
        notifyStarted();
    } catch (InterruptedException e) {
        log.warn("Netty server start interrupted");
        Thread.currentThread().interrupt();
        notifyFailed(e);
    }
}

From source file:org.opendaylight.sxp.core.service.ConnectFacade.java

License:Open Source License

/**
 * Create new Connection to Peer//from  ww w  . j  a v  a 2  s  .  c o  m
 *
 * @param node       SxpNode containing Security options
 * @param connection SxpConnection containing connection details
 * @param hf         HandlerFactory providing handling of communication
 * @return ChannelFuture callback
 */
public static ChannelFuture createClient(SxpNode node, SxpConnection connection, final HandlerFactory hf) {
    if (!Epoll.isAvailable()) {
        throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
    }
    Bootstrap bootstrap = new Bootstrap();
    if (connection.getPassword() != null && !connection.getPassword().isEmpty()) {
        bootstrap.option(EpollChannelOption.TCP_MD5SIG,
                Collections.singletonMap(connection.getDestination().getAddress(),
                        connection.getPassword().getBytes(StandardCharsets.US_ASCII)));
    }
    bootstrap.channel(EpollSocketChannel.class);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Configuration.NETTY_CONNECT_TIMEOUT_MILLIS);
    RecvByteBufAllocator recvByteBufAllocator = new FixedRecvByteBufAllocator(
            Configuration.getConstants().getMessageLengthMax());
    bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.localAddress(node.getSourceIp().getHostAddress(), 0);
    bootstrap.group(eventLoopGroup);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(hf.getDecoders());
            ch.pipeline().addLast(hf.getEncoders());
        }
    });
    return bootstrap.connect(connection.getDestination());
}