Example usage for io.netty.buffer PooledByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer PooledByteBufAllocator DEFAULT

Introduction

In this page you can find the example usage for io.netty.buffer PooledByteBufAllocator DEFAULT.

Prototype

PooledByteBufAllocator DEFAULT

To view the source code for io.netty.buffer PooledByteBufAllocator DEFAULT.

Click Source Link

Usage

From source file:NettyHttpListner.java

License:Apache License

public void start() {

    System.out.println("Starting the server...");
    System.out.println("Starting Inbound Http Listner on Port " + this.port);

    // Configure SSL.
    SslContext sslCtx = null;/*w w  w.  jav a2  s  . c  om*/
    if (SSL) {
        try {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
        } catch (CertificateException ex) {
            Logger.getLogger(NettyHttpListner.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SSLException ex) {
            Logger.getLogger(NettyHttpListner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    commonEventLoopGroup = new NioEventLoopGroup(bossGroupSize);
    //        bossGroup = new NioEventLoopGroup(bossGroupSize);
    //        workerGroup = new NioEventLoopGroup(workerGroupSize);

    try {
        ServerBootstrap b = new ServerBootstrap();

        //            b.commonEventLoopGroup(bossGroup, workerGroup)
        b.group(commonEventLoopGroup).channel(NioServerSocketChannel.class)
                .childHandler(
                        new NettyHttpTransportHandlerInitializer(HOST, HOST_PORT, maxConnectionsQueued, sslCtx))
                .childOption(ChannelOption.AUTO_READ, false);

        b.option(ChannelOption.TCP_NODELAY, true);
        b.childOption(ChannelOption.TCP_NODELAY, true);

        b.option(ChannelOption.SO_BACKLOG, maxConnectionsQueued);

        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 15000);

        b.option(ChannelOption.SO_SNDBUF, 1048576);
        b.option(ChannelOption.SO_RCVBUF, 1048576);
        b.childOption(ChannelOption.SO_RCVBUF, 1048576);
        b.childOption(ChannelOption.SO_SNDBUF, 1048576);

        b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

        Channel ch = null;
        try {
            ch = b.bind(port).sync().channel();
            ch.closeFuture().sync();
            System.out.println("Inbound Listner Started");
        } catch (InterruptedException e) {
            System.out.println("Exception caught");
        }
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:NettyHttpTransportSourceHandler.java

License:Apache License

/**
 * activating registered handler to accept events.
 *
 * @param ctx//from w  ww. j a va2  s.c  o m
 * @throws Exception
 */
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {

    final Channel inboundChannel = ctx.channel();

    Bootstrap b = new Bootstrap();
    b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass());
    b.handler(new NettyTargetHandlerInitilizer(inboundChannel)).option(ChannelOption.AUTO_READ, false);

    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.option(ChannelOption.TCP_NODELAY, true);
    b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 15000);

    b.option(ChannelOption.SO_SNDBUF, 1048576);
    b.option(ChannelOption.SO_RCVBUF, 1048576);

    ChannelFuture f = b.connect(NettyHttpListner.HOST, NettyHttpListner.HOST_PORT);

    outboundChannel = f.channel();
    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                // connection complete start to read first data
                inboundChannel.read();
            } else {
                // Close the connection if the connection attempt has failed.
                inboundChannel.close();
            }
        }
    });

}

From source file:alluxio.client.block.stream.BlockOutStream.java

License:Apache License

/**
 * @return a newly allocated byte buffer of the user defined default size
 *//*from   ww w. ja va2s.co m*/
private ByteBuf allocateBuffer() {
    return PooledByteBufAllocator.DEFAULT.buffer(mPacketWriters.get(0).packetSize());
}

From source file:alluxio.client.netty.NettyClient.java

License:Apache License

/**
 * Creates and returns a new Netty client bootstrap for clients to connect to remote servers.
 *
 * @param handler the handler that should be added to new channel pipelines
 * @return the new client {@link Bootstrap}
 *//*from  ww w  . j av  a  2 s .  c  o m*/
public static Bootstrap createClientBootstrap(final ClientHandler handler) {
    final Bootstrap boot = new Bootstrap();

    boot.group(WORKER_GROUP).channel(CLIENT_CHANNEL_CLASS);
    boot.option(ChannelOption.SO_KEEPALIVE, true);
    boot.option(ChannelOption.TCP_NODELAY, true);
    boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

    boot.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();

            pipeline.addLast(RPCMessage.createFrameDecoder());
            pipeline.addLast(ENCODER);
            pipeline.addLast(DECODER);
            pipeline.addLast(handler);
        }
    });

    return boot;
}

From source file:alluxio.grpc.GrpcSerializationUtils.java

License:Apache License

/**
 * Gets a Netty buffer directly from a gRPC ReadableBuffer.
 *
 * @param buffer the input buffer/* w w w.  j  a v  a 2 s.c om*/
 * @return the raw ByteBuf, or null if the ByteBuf cannot be extracted
 */
public static ByteBuf getByteBufFromReadableBuffer(ReadableBuffer buffer) {
    if (!sZeroCopyReceiveSupported) {
        return null;
    }
    try {
        if (buffer instanceof CompositeReadableBuffer) {
            Queue<ReadableBuffer> buffers = (Queue<ReadableBuffer>) sCompositeBuffers.get(buffer);
            if (buffers.size() == 1) {
                return getByteBufFromReadableBuffer(buffers.peek());
            } else {
                CompositeByteBuf buf = PooledByteBufAllocator.DEFAULT.compositeBuffer();
                for (ReadableBuffer readableBuffer : buffers) {
                    ByteBuf subBuffer = getByteBufFromReadableBuffer(readableBuffer);
                    if (subBuffer == null) {
                        return null;
                    }
                    buf.addComponent(true, subBuffer);
                }
                return buf;
            }
        } else if (buffer.getClass().equals(sReadableByteBuf.getDeclaringClass())) {
            return (ByteBuf) sReadableByteBuf.get(buffer);
        }
    } catch (Exception e) {
        LOG.warn("Failed to get data buffer from stream: {}.", e.getMessage());
        return null;
    }
    return null;
}

From source file:alluxio.network.netty.NettyClient.java

License:Apache License

/**
 * Creates and returns a new Netty client bootstrap for clients to connect to remote servers.
 *
 * @param address the socket address/*from   w w w  . ja  v  a2 s .co  m*/
 * @return the new client {@link Bootstrap}
 */
public static Bootstrap createClientBootstrap(SocketAddress address) {
    final Bootstrap boot = new Bootstrap();

    boot.group(WORKER_GROUP).channel(
            NettyUtils.getClientChannelClass(NettyUtils.CHANNEL_TYPE, !(address instanceof InetSocketAddress)));
    boot.option(ChannelOption.SO_KEEPALIVE, true);
    boot.option(ChannelOption.TCP_NODELAY, true);
    boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    if (NettyUtils.CHANNEL_TYPE == ChannelType.EPOLL) {
        boot.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    }

    // After 10 missed heartbeat attempts and no write activity, the server will close the channel.
    final long timeoutMs = Configuration.getMs(PropertyKey.NETWORK_NETTY_HEARTBEAT_TIMEOUT_MS);
    final long heartbeatPeriodMs = Math.max(timeoutMs / 10, 1);
    boot.handler(new ChannelInitializer<Channel>() {
        @Override
        public void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();

            pipeline.addLast(RPCMessage.createFrameDecoder());
            pipeline.addLast(ENCODER);
            pipeline.addLast(DECODER);
            pipeline.addLast(new IdleStateHandler(0, heartbeatPeriodMs, 0, TimeUnit.MILLISECONDS));
            pipeline.addLast(new IdleWriteHandler());
        }
    });

    return boot;
}

From source file:alluxio.worker.block.UnderFileSystemBlockReaderTest.java

License:Apache License

@Test
public void transferFullBlock() throws Exception {
    mReader = UnderFileSystemBlockReader.create(mUnderFileSystemBlockMeta, 0, false, mAlluxioBlockStore);
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) TEST_BLOCK_SIZE * 2, (int) TEST_BLOCK_SIZE * 2);
    try {//from   w ww .  jav  a  2 s .  com
        while (buf.writableBytes() > 0 && mReader.transferTo(buf) != -1) {
        }
        Assert.assertTrue(BufferUtils.equalIncreasingByteBuffer((int) TEST_BLOCK_SIZE, (int) TEST_BLOCK_SIZE,
                buf.nioBuffer()));
        mReader.close();
    } finally {
        buf.release();
    }
}

From source file:alluxio.worker.block.UnderFileSystemBlockReaderTest.java

License:Apache License

@Test
public void transferPartialBlock() throws Exception {
    mReader = UnderFileSystemBlockReader.create(mUnderFileSystemBlockMeta, 0, false, mAlluxioBlockStore);
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) TEST_BLOCK_SIZE / 2, (int) TEST_BLOCK_SIZE / 2);
    try {/*  w ww  .ja  v a  2  s . c o  m*/
        while (buf.writableBytes() > 0 && mReader.transferTo(buf) != -1) {
        }
        Assert.assertTrue(BufferUtils.equalIncreasingByteBuffer((int) TEST_BLOCK_SIZE,
                (int) TEST_BLOCK_SIZE / 2, buf.nioBuffer()));
        mReader.close();
    } finally {
        buf.release();
    }
}

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.  j  a v  a2 s .co m
    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.AbstractWriteHandlerTest.java

License:Apache License

/**
 * @param len length of the data buffer/*from w  w  w  .ja  va 2 s.c o m*/
 * @return a newly created data buffer
 */
protected DataBuffer newDataBuffer(int len) {
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(len);
    for (int i = 0; i < len; i++) {
        byte value = (byte) (RANDOM.nextInt() % Byte.MAX_VALUE);
        buf.writeByte(value);
    }
    return new DataNettyBufferV2(buf);
}