List of usage examples for io.netty.channel WriteBufferWaterMark WriteBufferWaterMark
public WriteBufferWaterMark(int low, int high)
From source file:io.grpc.netty.UtilsTest.java
License:Apache License
private static InternalChannelz.SocketOptions setAndValidateGeneric(Channel channel) { channel.config().setOption(ChannelOption.SO_LINGER, 3); // only applicable for OIO channels: channel.config().setOption(ChannelOption.SO_TIMEOUT, 250); // Test some arbitrarily chosen options with a non numeric values channel.config().setOption(ChannelOption.SO_KEEPALIVE, true); WriteBufferWaterMark writeBufWaterMark = new WriteBufferWaterMark(10, 20); channel.config().setOption(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufWaterMark); InternalChannelz.SocketOptions socketOptions = Utils.getSocketOptions(channel); assertEquals(3, (int) socketOptions.lingerSeconds); assertEquals("true", socketOptions.others.get("SO_KEEPALIVE")); assertEquals(writeBufWaterMark.toString(), socketOptions.others.get(ChannelOption.WRITE_BUFFER_WATER_MARK.toString())); return socketOptions; }
From source file:org.apache.bookkeeper.proto.BookieNettyServer.java
License:Apache License
private void listenOn(InetSocketAddress address, BookieSocketAddress bookieAddress) throws InterruptedException { if (!conf.isDisableServerSocketBind()) { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.option(ChannelOption.ALLOCATOR, allocator); bootstrap.childOption(ChannelOption.ALLOCATOR, allocator); bootstrap.group(eventLoopGroup, eventLoopGroup); bootstrap.childOption(ChannelOption.TCP_NODELAY, conf.getServerTcpNoDelay()); bootstrap.childOption(ChannelOption.SO_LINGER, conf.getServerSockLinger()); bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(conf.getRecvByteBufAllocatorSizeMin(), conf.getRecvByteBufAllocatorSizeInitial(), conf.getRecvByteBufAllocatorSizeMax())); bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark( conf.getServerWriteBufferLowWaterMark(), conf.getServerWriteBufferHighWaterMark())); if (eventLoopGroup instanceof EpollEventLoopGroup) { bootstrap.channel(EpollServerSocketChannel.class); } else {/*from w w w .jav a 2s. c om*/ bootstrap.channel(NioServerSocketChannel.class); } bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { synchronized (suspensionLock) { while (suspended) { suspensionLock.wait(); } } BookieSideConnectionPeerContextHandler contextHandler = new BookieSideConnectionPeerContextHandler(); ChannelPipeline pipeline = ch.pipeline(); // For ByteBufList, skip the usual LengthFieldPrepender and have the encoder itself to add it pipeline.addLast("bytebufList", ByteBufList.ENCODER_WITH_SIZE); pipeline.addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4)); pipeline.addLast("lengthprepender", new LengthFieldPrepender(4)); pipeline.addLast("bookieProtoDecoder", new BookieProtoEncoding.RequestDecoder(registry)); pipeline.addLast("bookieProtoEncoder", new BookieProtoEncoding.ResponseEncoder(registry)); pipeline.addLast("bookieAuthHandler", new AuthHandler.ServerSideHandler( contextHandler.getConnectionPeer(), authProviderFactory)); ChannelInboundHandler requestHandler = isRunning.get() ? new BookieRequestHandler(conf, requestProcessor, allChannels) : new RejectRequestHandler(); pipeline.addLast("bookieRequestHandler", requestHandler); pipeline.addLast("contextHandler", contextHandler); } }); // Bind and start to accept incoming connections Channel listen = bootstrap.bind(address.getAddress(), address.getPort()).sync().channel(); if (listen.localAddress() instanceof InetSocketAddress) { if (conf.getBookiePort() == 0) { conf.setBookiePort(((InetSocketAddress) listen.localAddress()).getPort()); } } } if (conf.isEnableLocalTransport()) { ServerBootstrap jvmBootstrap = new ServerBootstrap(); jvmBootstrap.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true)); jvmBootstrap.group(jvmEventLoopGroup, jvmEventLoopGroup); jvmBootstrap.childOption(ChannelOption.TCP_NODELAY, conf.getServerTcpNoDelay()); jvmBootstrap.childOption(ChannelOption.SO_KEEPALIVE, conf.getServerSockKeepalive()); jvmBootstrap.childOption(ChannelOption.SO_LINGER, conf.getServerSockLinger()); jvmBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(conf.getRecvByteBufAllocatorSizeMin(), conf.getRecvByteBufAllocatorSizeInitial(), conf.getRecvByteBufAllocatorSizeMax())); jvmBootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark( conf.getServerWriteBufferLowWaterMark(), conf.getServerWriteBufferHighWaterMark())); if (jvmEventLoopGroup instanceof DefaultEventLoopGroup) { jvmBootstrap.channel(LocalServerChannel.class); } else if (jvmEventLoopGroup instanceof EpollEventLoopGroup) { jvmBootstrap.channel(EpollServerSocketChannel.class); } else { jvmBootstrap.channel(NioServerSocketChannel.class); } jvmBootstrap.childHandler(new ChannelInitializer<LocalChannel>() { @Override protected void initChannel(LocalChannel ch) throws Exception { synchronized (suspensionLock) { while (suspended) { suspensionLock.wait(); } } BookieSideConnectionPeerContextHandler contextHandler = new BookieSideConnectionPeerContextHandler(); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4)); pipeline.addLast("lengthprepender", new LengthFieldPrepender(4)); pipeline.addLast("bookieProtoDecoder", new BookieProtoEncoding.RequestDecoder(registry)); pipeline.addLast("bookieProtoEncoder", new BookieProtoEncoding.ResponseEncoder(registry)); pipeline.addLast("bookieAuthHandler", new AuthHandler.ServerSideHandler( contextHandler.getConnectionPeer(), authProviderFactory)); ChannelInboundHandler requestHandler = isRunning.get() ? new BookieRequestHandler(conf, requestProcessor, allChannels) : new RejectRequestHandler(); pipeline.addLast("bookieRequestHandler", requestHandler); pipeline.addLast("contextHandler", contextHandler); } }); // use the same address 'name', so clients can find local Bookie still discovering them using ZK jvmBootstrap.bind(bookieAddress.getLocalAddress()).sync(); LocalBookiesRegistry.registerLocalBookieAddress(bookieAddress); } }
From source file:org.apache.bookkeeper.proto.PerChannelBookieClient.java
License:Apache License
protected ChannelFuture connect() { final long startTime = MathUtils.nowInNano(); if (LOG.isDebugEnabled()) { LOG.debug("Connecting to bookie: {}", addr); }/*from w w w.j av a 2 s . c om*/ // Set up the ClientBootStrap so we can create a new Channel connection to the bookie. Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup); if (eventLoopGroup instanceof EpollEventLoopGroup) { bootstrap.channel(EpollSocketChannel.class); } else if (eventLoopGroup instanceof DefaultEventLoopGroup) { bootstrap.channel(LocalChannel.class); } else { bootstrap.channel(NioSocketChannel.class); } bootstrap.option(ChannelOption.ALLOCATOR, this.allocator); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, conf.getClientConnectTimeoutMillis()); bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark( conf.getClientWriteBufferLowWaterMark(), conf.getClientWriteBufferHighWaterMark())); if (!(eventLoopGroup instanceof DefaultEventLoopGroup)) { bootstrap.option(ChannelOption.TCP_NODELAY, conf.getClientTcpNoDelay()); bootstrap.option(ChannelOption.SO_KEEPALIVE, conf.getClientSockKeepalive()); // if buffer sizes are 0, let OS auto-tune it if (conf.getClientSendBufferSize() > 0) { bootstrap.option(ChannelOption.SO_SNDBUF, conf.getClientSendBufferSize()); } if (conf.getClientReceiveBufferSize() > 0) { bootstrap.option(ChannelOption.SO_RCVBUF, conf.getClientReceiveBufferSize()); } } // In the netty pipeline, we need to split packets based on length, so we // use the {@link LengthFieldBasedFramDecoder}. Other than that all actions // are carried out in this class, e.g., making sense of received messages, // prepending the length to outgoing packets etc. bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("bytebufList", ByteBufList.ENCODER_WITH_SIZE); pipeline.addLast("lengthbasedframedecoder", new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4)); pipeline.addLast("lengthprepender", new LengthFieldPrepender(4)); pipeline.addLast("bookieProtoEncoder", new BookieProtoEncoding.RequestEncoder(extRegistry)); pipeline.addLast("bookieProtoDecoder", new BookieProtoEncoding.ResponseDecoder(extRegistry, useV2WireProtocol)); pipeline.addLast("authHandler", new AuthHandler.ClientSideHandler(authProviderFactory, txnIdGenerator, connectionPeer, useV2WireProtocol)); pipeline.addLast("mainhandler", PerChannelBookieClient.this); } }); SocketAddress bookieAddr = addr.getSocketAddress(); if (eventLoopGroup instanceof DefaultEventLoopGroup) { bookieAddr = addr.getLocalAddress(); } ChannelFuture future = bootstrap.connect(bookieAddr); future.addListener(contextPreservingListener(new ConnectionFutureListener(startTime))); future.addListener(x -> makeWritable()); return future; }
From source file:org.jupiter.transport.netty.NettyTcpAcceptor.java
License:Apache License
@Override protected void setOptions() { super.setOptions(); ServerBootstrap boot = bootstrap();//from w w w.j a va 2 s . c om // parent options NettyConfig.NettyTcpConfigGroup.ParentConfig parent = configGroup.parent(); boot.option(ChannelOption.SO_BACKLOG, parent.getBacklog()); boot.option(ChannelOption.SO_REUSEADDR, parent.isReuseAddress()); if (parent.getRcvBuf() > 0) { boot.option(ChannelOption.SO_RCVBUF, parent.getRcvBuf()); } // child options NettyConfig.NettyTcpConfigGroup.ChildConfig child = configGroup.child(); boot.childOption(ChannelOption.SO_REUSEADDR, child.isReuseAddress()) .childOption(ChannelOption.SO_KEEPALIVE, child.isKeepAlive()) .childOption(ChannelOption.TCP_NODELAY, child.isTcpNoDelay()) .childOption(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure()); if (child.getRcvBuf() > 0) { boot.childOption(ChannelOption.SO_RCVBUF, child.getRcvBuf()); } if (child.getSndBuf() > 0) { boot.childOption(ChannelOption.SO_SNDBUF, child.getSndBuf()); } if (child.getLinger() > 0) { boot.childOption(ChannelOption.SO_LINGER, child.getLinger()); } if (child.getIpTos() > 0) { boot.childOption(ChannelOption.IP_TOS, child.getIpTos()); } int bufLowWaterMark = child.getWriteBufferLowWaterMark(); int bufHighWaterMark = child.getWriteBufferHighWaterMark(); if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) { WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark); boot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark); } }
From source file:org.jupiter.transport.netty.NettyTcpConnector.java
License:Apache License
@Override protected void setOptions() { super.setOptions(); Bootstrap boot = bootstrap();/* w w w . j a va 2 s.com*/ NettyConfig.NettyTcpConfigGroup.ChildConfig child = childConfig; // child options boot.option(ChannelOption.SO_REUSEADDR, child.isReuseAddress()) .option(ChannelOption.SO_KEEPALIVE, child.isKeepAlive()) .option(ChannelOption.TCP_NODELAY, child.isTcpNoDelay()) .option(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure()); if (child.getRcvBuf() > 0) { boot.option(ChannelOption.SO_RCVBUF, child.getRcvBuf()); } if (child.getSndBuf() > 0) { boot.option(ChannelOption.SO_SNDBUF, child.getSndBuf()); } if (child.getLinger() > 0) { boot.option(ChannelOption.SO_LINGER, child.getLinger()); } if (child.getIpTos() > 0) { boot.option(ChannelOption.IP_TOS, child.getIpTos()); } if (child.getConnectTimeoutMillis() > 0) { boot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, child.getConnectTimeoutMillis()); } int bufLowWaterMark = child.getWriteBufferLowWaterMark(); int bufHighWaterMark = child.getWriteBufferHighWaterMark(); if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) { WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark); boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark); } }
From source file:org.jupiter.transport.netty.NettyUdtAcceptor.java
License:Apache License
@Override protected void setOptions() { super.setOptions(); ServerBootstrap boot = bootstrap();/*from w ww . j ava2 s. co m*/ // parent options NettyUdtConfigGroup.ParentConfig parent = configGroup.parent(); boot.option(ChannelOption.SO_BACKLOG, parent.getBacklog()); // child options NettyUdtConfigGroup.ChildConfig child = configGroup.child(); boot.childOption(ChannelOption.SO_REUSEADDR, child.isReuseAddress()); if (child.getRcvBuf() > 0) { boot.childOption(ChannelOption.SO_RCVBUF, child.getRcvBuf()); } if (child.getSndBuf() > 0) { boot.childOption(ChannelOption.SO_SNDBUF, child.getSndBuf()); } if (child.getLinger() > 0) { boot.childOption(ChannelOption.SO_LINGER, child.getLinger()); } int bufLowWaterMark = child.getWriteBufferLowWaterMark(); int bufHighWaterMark = child.getWriteBufferHighWaterMark(); if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) { WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark); boot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark); } }
From source file:org.jupiter.transport.netty.NettyUdtConnector.java
License:Apache License
@Override protected void setOptions() { super.setOptions(); Bootstrap boot = bootstrap();//ww w. ja v a2s . co m NettyUdtConfigGroup.ChildConfig child = childConfig; // child options boot.option(ChannelOption.SO_REUSEADDR, child.isReuseAddress()); if (child.getRcvBuf() > 0) { boot.option(ChannelOption.SO_RCVBUF, child.getRcvBuf()); } if (child.getSndBuf() > 0) { boot.option(ChannelOption.SO_SNDBUF, child.getSndBuf()); } if (child.getLinger() > 0) { boot.option(ChannelOption.SO_LINGER, child.getLinger()); } if (child.getConnectTimeoutMillis() > 0) { boot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, child.getConnectTimeoutMillis()); } int bufLowWaterMark = child.getWriteBufferLowWaterMark(); int bufHighWaterMark = child.getWriteBufferHighWaterMark(); if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) { WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark); boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark); } }
From source file:sailfish.remoting.channel.AbstractConfigurableExchangeChannelGroup.java
License:Apache License
private Bootstrap newBootstrap() { Bootstrap boot = new Bootstrap(); boot.channel(NettyPlatformIndependent.channelClass()); boot.option(ChannelOption.TCP_NODELAY, true); // replace by heart beat boot.option(ChannelOption.SO_KEEPALIVE, false); // default is pooled direct // ByteBuf(io.netty.util.internal.PlatformDependent.DIRECT_BUFFER_PREFERRED) boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); // 32kb(for massive long connections, See // http://www.infoq.com/cn/articles/netty-million-level-push-service-design-points) // 64kb(RocketMq remoting default value) boot.option(ChannelOption.SO_SNDBUF, 32 * 1024); boot.option(ChannelOption.SO_RCVBUF, 32 * 1024); // temporary settings, need more tests boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 32 * 1024)); //default is true, reduce thread context switching boot.option(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, true); return boot;//from w w w. java 2s . c o m }
From source file:sailfish.remoting.DefaultServer.java
License:Apache License
private ServerBootstrap newServerBootstrap() { ServerBootstrap serverBoot = new ServerBootstrap(); serverBoot.channel(NettyPlatformIndependent.serverChannelClass()); // connections wait for accept serverBoot.option(ChannelOption.SO_BACKLOG, 1024); serverBoot.option(ChannelOption.SO_REUSEADDR, true); // replace by heart beat serverBoot.childOption(ChannelOption.SO_KEEPALIVE, false); serverBoot.childOption(ChannelOption.TCP_NODELAY, true); serverBoot.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); serverBoot.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); // temporary settings, need more tests serverBoot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 32 * 1024)); serverBoot.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); //default is true, reduce thread context switching serverBoot.childOption(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, true); return serverBoot; }