List of usage examples for io.netty.buffer PooledByteBufAllocator PooledByteBufAllocator
@SuppressWarnings("deprecation") public PooledByteBufAllocator(boolean preferDirect)
From source file:net.NettyEngine4.ServerServiceImpl.java
License:Apache License
/** * jni native Epoll//from w ww . j ava2s .co m #NioEventLoopGroup EpollEventLoopGroup ---- EventLoopGroup ? #NioEventLoop EpollEventLoop #NioServerSocketChannel EpollServerSocketChannel #NioSocketChannel EpollSocketChannel ---- SocketChannel? ## RHEL/CentOS/Fedora: #sudo yum install autoconf automake libtool glibc-devel.i686 glibc-devel libgcc.i686 make ## Debian/Ubuntu: #sudo apt-get install autoconf automake libtool make gcc-multilib * @throws Exception */ @Override public void runNativeEpoll() throws Exception { EpollEventLoopGroup BossEventLoopGroup = new EpollEventLoopGroup(0x1, new PriorityThreadFactory("@+?", Thread.NORM_PRIORITY)); //mainReactor 1 EpollEventLoopGroup WorkerEventLoopGroup = new EpollEventLoopGroup( Runtime.getRuntime().availableProcessors() + 0x1, new PriorityThreadFactory("@+I/O", Thread.NORM_PRIORITY)); //subReactor ?cpu+1 ServerBootstrap serverBootstrap = new ServerBootstrap(); try { serverBootstrap.group(BossEventLoopGroup, WorkerEventLoopGroup).channel(EpollServerSocketChannel.class) .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.SO_REUSEADDR, true) //?? .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false))// heap buf 's better .childOption(ChannelOption.SO_RCVBUF, 1048576).childOption(ChannelOption.SO_SNDBUF, 1048576) .childHandler(new ServerChannelInitializer());//used to serve the request for the {@link Channel}'s // Bind and start to accept incoming connections. ChannelFuture channelFuture = serverBootstrap .bind(new InetSocketAddress(Config.DEFAULT_VALUE.SERVER_VALUE.gameserverPort)).sync(); if (LOGGER.isDebugEnabled()) LOGGER.debug("server??:" + Config.DEFAULT_VALUE.SERVER_VALUE.gameserverPort); // Wait until the server socket is closed. // In this server, this does not happen, but you can do that to gracefully // shut down your server. channelFuture.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. BossEventLoopGroup.shutdownGracefully(); WorkerEventLoopGroup.shutdownGracefully(); } }
From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java
License:Apache License
@Test @SuppressWarnings("unchecked") public void testLedgerCreateAdvByteBufRefCnt() throws Exception { long ledgerId = rng.nextLong(); ledgerId &= Long.MAX_VALUE; if (!baseConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)) { // since LongHierarchicalLedgerManager supports ledgerIds of // decimal length upto 19 digits but other // LedgerManagers only upto 10 decimals ledgerId %= 9999999999L;//w w w.j ava 2 s .com } final LedgerHandle lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null); final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true), new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true), new UnpooledByteBufAllocator(false)); long entryId = 0; for (AbstractByteBufAllocator alloc : allocs) { final ByteBuf data = alloc.buffer(10); data.writeBytes(("fragment0" + entryId).getBytes()); assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt()); CompletableFuture<Integer> cf = new CompletableFuture<>(); lh.asyncAddEntry(entryId, data, (rc, handle, eId, qwcLatency, ctx) -> { CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx; future.complete(rc); }, cf); int rc = cf.get(); assertEquals("rc code is OK", BKException.Code.OK, rc); for (int i = 0; i < 10; i++) { if (data.refCnt() == 0) { break; } TimeUnit.MILLISECONDS.sleep(250); // recycler runs asynchronously } assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0, data.refCnt()); org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId); assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes())); entryId++; } bkc.deleteLedger(lh.ledgerId); }
From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java
License:Apache License
@Test @SuppressWarnings("unchecked") public void testLedgerCreateByteBufRefCnt() throws Exception { final LedgerHandle lh = bkc.createLedger(5, 3, 2, digestType, ledgerPassword, null); final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true), new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true), new UnpooledByteBufAllocator(false)); int entryId = 0; for (AbstractByteBufAllocator alloc : allocs) { final ByteBuf data = alloc.buffer(10); data.writeBytes(("fragment0" + entryId).getBytes()); assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt()); CompletableFuture<Integer> cf = new CompletableFuture<>(); lh.asyncAddEntry(data, (rc, handle, eId, ctx) -> { CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx; future.complete(rc);//from ww w. j ava 2 s .com }, cf); int rc = cf.get(); assertEquals("rc code is OK", BKException.Code.OK, rc); for (int i = 0; i < 10; i++) { if (data.refCnt() == 0) { break; } TimeUnit.MILLISECONDS.sleep(250); // recycler runs asynchronously } assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0, data.refCnt()); org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId); assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes())); entryId++; } bkc.deleteLedger(lh.ledgerId); }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testPooled() { PooledByteBufAllocator pooledAlloc = new PooledByteBufAllocator(true); ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.PooledDirect) .pooledAllocator(pooledAlloc).build(); assertTrue(alloc.isDirectBufferPooled()); ByteBuf buf1 = alloc.buffer();//from w ww. java 2 s . c o m assertEquals(pooledAlloc, buf1.alloc()); assertFalse(buf1.hasArray()); buf1.release(); ByteBuf buf2 = alloc.directBuffer(); assertEquals(pooledAlloc, buf2.alloc()); assertFalse(buf2.hasArray()); buf2.release(); ByteBuf buf3 = alloc.heapBuffer(); assertEquals(pooledAlloc, buf3.alloc()); assertTrue(buf3.hasArray()); buf3.release(); }
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 {// w ww.jav a 2 s . c o m 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.giraph.conf.GiraphConfiguration.java
License:Apache License
/** * Used by netty client and server to create ByteBufAllocator * * @return ByteBufAllocator// www . j av a 2 s . c o m */ public ByteBufAllocator getNettyAllocator() { if (nettyBufferAllocator == null) { if (NETTY_USE_POOLED_ALLOCATOR.get(this)) { // Use pooled allocator nettyBufferAllocator = new PooledByteBufAllocator(NETTY_USE_DIRECT_MEMORY.get(this)); } else { // Use un-pooled allocator // Note: Current default settings create un-pooled heap allocator nettyBufferAllocator = new UnpooledByteBufAllocator(NETTY_USE_DIRECT_MEMORY.get(this)); } } return nettyBufferAllocator; }
From source file:org.ballerinalang.test.agent.server.WebServer.java
License:Open Source License
/** * Initializes the server, socket, and channel. * * @param loopGroup The event loop group. * @param serverChannelClass The socket channel class. * @throws InterruptedException on interruption. *///from www. j a v a 2 s. co m private void start(final EventLoopGroup loopGroup, final Class<? extends ServerChannel> serverChannelClass) throws InterruptedException { try { final InetSocketAddress inet = new InetSocketAddress(host, port); final ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024); serverBootstrap.option(ChannelOption.SO_REUSEADDR, true); serverBootstrap.group(loopGroup).channel(serverChannelClass).childHandler(new WebServerInitializer()); serverBootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE); serverBootstrap.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true)); serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true); serverBootstrap.childOption(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE); final Channel ch = serverBootstrap.bind(inet).sync().channel(); ch.closeFuture().sync(); } finally { loopGroup.shutdownGracefully().sync(); } }
From source file:org.jupiter.transport.netty.NettyAcceptor.java
License:Apache License
protected void setOptions() { JConfig parent = configGroup().parent(); // parent options JConfig child = configGroup().child(); // child options setIoRatio(parent.getOption(JOption.IO_RATIO), child.getOption(JOption.IO_RATIO)); boolean direct = child.getOption(JOption.PREFER_DIRECT); if (child.getOption(JOption.USE_POOLED_ALLOCATOR)) { if (direct) { allocator = new PooledByteBufAllocator(PlatformDependent.directBufferPreferred()); } else {//from w w w. j a va 2s . c om allocator = new PooledByteBufAllocator(false); } } else { if (direct) { allocator = new UnpooledByteBufAllocator(PlatformDependent.directBufferPreferred()); } else { allocator = new UnpooledByteBufAllocator(false); } } bootstrap.childOption(ChannelOption.ALLOCATOR, allocator).childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, JMessageSizeEstimator.DEFAULT); }
From source file:org.jupiter.transport.netty.NettyConnector.java
License:Apache License
protected void setOptions() { JConfig child = config();//from w w w .j ava 2 s. c o m setIoRatio(child.getOption(JOption.IO_RATIO)); boolean direct = child.getOption(JOption.PREFER_DIRECT); if (child.getOption(JOption.USE_POOLED_ALLOCATOR)) { if (direct) { allocator = new PooledByteBufAllocator(PlatformDependent.directBufferPreferred()); } else { allocator = new PooledByteBufAllocator(false); } } else { if (direct) { allocator = new UnpooledByteBufAllocator(PlatformDependent.directBufferPreferred()); } else { allocator = new UnpooledByteBufAllocator(false); } } bootstrap.option(ChannelOption.ALLOCATOR, allocator).option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, JMessageSizeEstimator.DEFAULT); }
From source file:org.springframework.core.codec.support.AbstractAllocatingTestCase.java
License:Apache License
@Parameterized.Parameters(name = "{0}") public static Object[][] allocators() { return new Object[][] { { new NettyDataBufferAllocator(new UnpooledByteBufAllocator(true)) }, { new NettyDataBufferAllocator(new UnpooledByteBufAllocator(false)) }, { new NettyDataBufferAllocator(new PooledByteBufAllocator(true)) }, { new NettyDataBufferAllocator(new PooledByteBufAllocator(false)) }, { new DefaultDataBufferAllocator(true) }, { new DefaultDataBufferAllocator(false) } };/*from w w w . j av a 2s . c o m*/ }