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:alluxio.worker.netty.DataServerBlockWriteHandlerTest.java

License:Apache License

@Override
protected RPCProtoMessage buildWriteRequest(long offset, int len) {
    Protocol.WriteRequest writeRequest = Protocol.WriteRequest.newBuilder().setId(1L).setOffset(offset)
            .setSessionId(1L).setType(Protocol.RequestType.ALLUXIO_BLOCK).build();
    DataBuffer buffer = null;/*ww  w  .  ja va2  s .  com*/
    if (len > 0) {
        ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(len);
        for (int i = 0; i < len; i++) {
            byte value = (byte) (mRandom.nextInt() % Byte.MAX_VALUE);
            buf.writeByte(value);
            mChecksum += BufferUtils.byteToInt(value);
        }
        buffer = new DataNettyBufferV2(buf);
    }
    if (len == EOF) {
        writeRequest = writeRequest.toBuilder().setEof(true).build();
    }
    if (len == CANCEL) {
        writeRequest = writeRequest.toBuilder().setCancel(true).build();
    }
    return new RPCProtoMessage(new ProtoMessage(writeRequest), buffer);
}

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

License:Apache License

@Override
protected RPCProtoMessage buildWriteRequest(long offset, int len) {
    Protocol.WriteRequest writeRequest = Protocol.WriteRequest.newBuilder().setId(1L).setOffset(offset)
            .setType(Protocol.RequestType.UFS_FILE).build();
    DataBuffer buffer = null;//w  w  w .  j ava2  s.  c o  m
    if (len > 0) {
        ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(len);
        for (int i = 0; i < len; i++) {
            byte value = (byte) (mRandom.nextInt() % Byte.MAX_VALUE);
            buf.writeByte(value);
            mChecksum += BufferUtils.byteToInt(value);
        }
        buffer = new DataNettyBufferV2(buf);
    }
    if (len == EOF) {
        writeRequest = writeRequest.toBuilder().setEof(true).build();
    }
    if (len == CANCEL) {
        writeRequest = writeRequest.toBuilder().setCancel(true).build();
    }
    return new RPCProtoMessage(new ProtoMessage(writeRequest), buffer);
}

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));
    }/*from w w w  .  ja va 2 s.c om*/
    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:blazingcache.client.CacheClient.java

License:Apache License

public CacheClient(String clientId, String sharedSecret, ServerLocator brokerLocator) {
    this.brokerLocator = brokerLocator;
    this.sharedSecret = sharedSecret;
    this.coreThread = new Thread(new ConnectionManager(), "cache-connection-manager-" + clientId);
    this.coreThread.setDaemon(true);
    this.clientId = clientId + "_" + System.nanoTime();

    this.statisticsMXBean = new BlazingCacheClientStatisticsMXBean(this);
    this.statusMXBean = new BlazingCacheClientStatusMXBean(this);

    this.oldestEvictedKeyAge = new AtomicLong();
    this.clientPuts = new AtomicLong();
    this.clientLoads = new AtomicLong();
    this.clientTouches = new AtomicLong();
    this.clientGets = new AtomicLong();
    this.clientFetches = new AtomicLong();
    this.clientEvictions = new AtomicLong();
    this.clientInvalidations = new AtomicLong();
    this.clientHits = new AtomicLong();
    this.clientMissedGetsToSuccessfulFetches = new AtomicLong();
    this.clientMissedGetsToMissedFetches = new AtomicLong();
    this.allocator = PooledByteBufAllocator.DEFAULT;
}

From source file:c5db.client.C5NettyConnectionManager.java

License:Apache License

public C5NettyConnectionManager() {
    bootstrap.group(group);//from  ww w  .  j a va  2  s . c  om
    bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

    try {
        uri = new URI("ws://0.0.0.0:8080/websocket");
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

}

From source file:c5db.regionserver.RegionServerService.java

License:Apache License

public RegionServerService(EventLoopGroup acceptGroup, EventLoopGroup workerGroup, int port, C5Server server) {
    this.acceptGroup = acceptGroup;
    this.workerGroup = workerGroup;
    this.port = port;
    this.server = server;
    this.fiber = server.getFiberSupplier().getNewFiber(this::notifyFailed);
    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
}

From source file:cc.blynk.integration.model.websocket.WebSocketClient.java

License:Apache License

private static WebSocketFrame produceWebSocketFrame(MessageBase msg) {
    ByteBuf bb = PooledByteBufAllocator.DEFAULT.heapBuffer(5 + msg.length);
    bb.writeByte(msg.command);/*w  ww  . j a  v a  2 s  .co m*/
    bb.writeShort(msg.id);
    bb.writeShort(msg.length);
    byte[] data = msg.getBytes();
    if (data != null) {
        bb.writeBytes(data);
    }
    return new BinaryWebSocketFrame(bb);
}

From source file:cn.wcl.test.netty.server.netty.NettyHttpServer.java

License:Apache License

private void init() throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//  w w  w .  j  a  va2 s  .c o  m
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);
        b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        b.childHandler(new HttpUploadServerInitializer(sslCtx, applicationContext, needlogin));

        Channel ch = b.bind(port).sync().channel();

        System.err.println("Open your web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:"
                + port + '/');

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:code.google.nfs.rpc.netty.client.NettyClientFactory.java

License:Apache License

protected Client createClient(String targetIP, int targetPort, int connectTimeout, String key)
        throws Exception {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(workerGroup).channel(NioSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.TCP_NODELAY,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true")))
            .option(ChannelOption.SO_REUSEADDR,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true")));
    if (connectTimeout < 1000) {
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);
    } else {//  w  w  w.j  ava 2 s.c o  m
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    }
    final NettyClientHandler handler = new NettyClientHandler(this, key);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        protected void initChannel(SocketChannel channel) throws Exception {
            ChannelPipeline pipeline = channel.pipeline();
            pipeline.addLast("decoder", new NettyProtocolDecoder());
            pipeline.addLast("encoder", new NettyProtocolEncoder());
            pipeline.addLast("handler", handler);
        }

    });
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(targetIP, targetPort)).sync();
    future.awaitUninterruptibly(connectTimeout);
    if (!future.isDone()) {
        LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " timeout!");
        throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!");
    }
    if (future.isCancelled()) {
        LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!");
        throw new Exception("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!");
    }
    if (!future.isSuccess()) {
        LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " error", future.cause());
        throw new Exception("Create connection to " + targetIP + ":" + targetPort + " error", future.cause());
    }
    NettyClient client = new NettyClient(future, key, connectTimeout);
    handler.setClient(client);
    return client;
}

From source file:code.google.nfs.rpc.netty.server.NettyServer.java

License:Apache License

public NettyServer() {
    ThreadFactory serverBossTF = new NamedThreadFactory("NETTYSERVER-BOSS-");
    ThreadFactory serverWorkerTF = new NamedThreadFactory("NETTYSERVER-WORKER-");
    EventLoopGroup bossGroup = new NioEventLoopGroup(PROCESSORS, serverBossTF);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup(PROCESSORS * 2, serverWorkerTF);
    workerGroup.setIoRatio(Integer.parseInt(System.getProperty("nfs.rpc.io.ratio", "50")));
    bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.SO_REUSEADDR,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true")))
            .option(ChannelOption.TCP_NODELAY,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true")));
}