List of usage examples for io.netty.buffer PooledByteBufAllocator DEFAULT
PooledByteBufAllocator DEFAULT
To view the source code for io.netty.buffer PooledByteBufAllocator DEFAULT.
Click Source Link
From source file:org.apache.distributedlog.io.LZ4CompressionCodec.java
License:Apache License
@Override // length parameter is ignored here because of the way the fastDecompressor works. public ByteBuf decompress(ByteBuf compressed, int decompressedSize) { checkNotNull(compressed);//from www . j a v a 2s . c o m checkArgument(compressed.readableBytes() >= 0); checkArgument(decompressedSize >= 0); ByteBuf uncompressed = PooledByteBufAllocator.DEFAULT.buffer(decompressedSize, decompressedSize); ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, decompressedSize); ByteBuffer compressedNio = compressed.nioBuffer(compressed.readerIndex(), compressed.readableBytes()); decompressor.decompress(compressedNio, compressedNio.position(), uncompressedNio, uncompressedNio.position(), uncompressedNio.remaining()); uncompressed.writerIndex(decompressedSize); return uncompressed; }
From source file:org.apache.distributedlog.io.TestCompressionCodec.java
License:Apache License
private void testCompressionCodec2(CompressionCodec codec) throws Exception { ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(32, 4 * 1024 * 1024); for (int i = 0; i < 100; i++) { ByteBuffer record = ByteBuffer.wrap(("record-" + i).getBytes(UTF_8)); buffer.writeInt(record.remaining()); buffer.writeBytes(record);/*ww w.j a va 2 s . com*/ } byte[] uncompressedData = new byte[buffer.readableBytes()]; buffer.slice().readBytes(uncompressedData); ByteBuf compressedBuf = codec.compress(buffer, 0); byte[] compressedData = new byte[compressedBuf.readableBytes()]; compressedBuf.slice().readBytes(compressedData); ByteBuf decompressedBuf = codec.decompress(compressedBuf, uncompressedData.length); byte[] decompressedData = new byte[decompressedBuf.readableBytes()]; decompressedBuf.slice().readBytes(decompressedData); buffer.release(); compressedBuf.release(); decompressedBuf.release(); }
From source file:org.apache.dubbo.remoting.transport.netty4.NettyClient.java
License:Apache License
/** * Init bootstrap//from ww w . ja v a 2 s . c o m * * @throws Throwable */ @Override protected void doOpen() throws Throwable { final NettyClientHandler nettyClientHandler = new NettyClientHandler(getUrl(), this); bootstrap = new Bootstrap(); bootstrap.group(nioEventLoopGroup).option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) //.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getTimeout()) .channel(NioSocketChannel.class); if (getConnectTimeout() < 3000) { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000); } else { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout()); } bootstrap.handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { int heartbeatInterval = UrlUtils.getHeartbeat(getUrl()); NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug .addLast("decoder", adapter.getDecoder()).addLast("encoder", adapter.getEncoder()) .addLast("client-idle-handler", new IdleStateHandler(heartbeatInterval, 0, 0, MILLISECONDS)) .addLast("handler", nettyClientHandler); String socksProxyHost = ConfigUtils.getProperty(SOCKS_PROXY_HOST); if (socksProxyHost != null) { int socksProxyPort = Integer .parseInt(ConfigUtils.getProperty(SOCKS_PROXY_PORT, DEFAULT_SOCKS_PROXY_PORT)); Socks5ProxyHandler socks5ProxyHandler = new Socks5ProxyHandler( new InetSocketAddress(socksProxyHost, socksProxyPort)); ch.pipeline().addFirst(socks5ProxyHandler); } } }); }
From source file:org.apache.dubbo.remoting.transport.netty4.NettyServer.java
License:Apache License
/** * Init and start netty server//from w w w. jav a2 s . c o m * * @throws Throwable */ @Override protected void doOpen() throws Throwable { bootstrap = new ServerBootstrap(); bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("NettyServerBoss", true)); workerGroup = new NioEventLoopGroup( getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS), new DefaultThreadFactory("NettyServerWorker", true)); final NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this); channels = nettyServerHandler.getChannels(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE) .childOption(ChannelOption.SO_REUSEADDR, Boolean.TRUE) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childHandler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel ch) throws Exception { // FIXME: should we use getTimeout()? int idleTimeout = UrlUtils.getIdleTimeout(getUrl()); NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this); ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug .addLast("decoder", adapter.getDecoder()).addLast("encoder", adapter.getEncoder()) .addLast("server-idle-handler", new IdleStateHandler(0, 0, idleTimeout, MILLISECONDS)) .addLast("handler", nettyServerHandler); } }); // bind ChannelFuture channelFuture = bootstrap.bind(getBindAddress()); channelFuture.syncUninterruptibly(); channel = channelFuture.channel(); }
From source file:org.apache.geode.redis.GeodeRedisServer.java
License:Apache License
/** * Helper method to start the server listening for connections. The server is bound to the port * specified by {@link GeodeRedisServer#serverPort} * /*from ww w.j a v a 2s . c om*/ * @throws IOException * @throws InterruptedException */ private void startRedisServer() throws IOException, InterruptedException { ThreadFactory selectorThreadFactory = new ThreadFactory() { private final AtomicInteger counter = new AtomicInteger(); @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("GeodeRedisServer-SelectorThread-" + counter.incrementAndGet()); t.setDaemon(true); return t; } }; ThreadFactory workerThreadFactory = new ThreadFactory() { private final AtomicInteger counter = new AtomicInteger(); @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("GeodeRedisServer-WorkerThread-" + counter.incrementAndGet()); return t; } }; bossGroup = null; workerGroup = null; Class<? extends ServerChannel> socketClass = null; if (singleThreadPerConnection) { bossGroup = new OioEventLoopGroup(Integer.MAX_VALUE, selectorThreadFactory); workerGroup = new OioEventLoopGroup(Integer.MAX_VALUE, workerThreadFactory); socketClass = OioServerSocketChannel.class; } else { bossGroup = new NioEventLoopGroup(this.numSelectorThreads, selectorThreadFactory); workerGroup = new NioEventLoopGroup(this.numWorkerThreads, workerThreadFactory); socketClass = NioServerSocketChannel.class; } InternalDistributedSystem system = (InternalDistributedSystem) cache.getDistributedSystem(); String pwd = system.getConfig().getRedisPassword(); final byte[] pwdB = Coder.stringToBytes(pwd); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(socketClass).childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { if (logger.fineEnabled()) logger.fine("GeodeRedisServer-Connection established with " + ch.remoteAddress()); ChannelPipeline p = ch.pipeline(); p.addLast(ByteToCommandDecoder.class.getSimpleName(), new ByteToCommandDecoder()); p.addLast(ExecutionHandlerContext.class.getSimpleName(), new ExecutionHandlerContext(ch, cache, regionCache, GeodeRedisServer.this, pwdB)); } }).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, getBufferSize()) .childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, GeodeRedisServer.connectTimeoutMillis) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(new InetSocketAddress(getBindAddress(), serverPort)).sync(); if (this.logger.infoEnabled()) { String logMessage = "GeodeRedisServer started {" + getBindAddress() + ":" + serverPort + "}, Selector threads: " + this.numSelectorThreads; if (this.singleThreadPerConnection) logMessage += ", One worker thread per connection"; else logMessage += ", Worker threads: " + this.numWorkerThreads; this.logger.info(logMessage); } this.serverChannel = f.channel(); }
From source file:org.apache.hadoop.hbase.ipc.NettyRpcServer.java
License:Apache License
public NettyRpcServer(final Server server, final String name, final List<BlockingServiceAndInterface> services, final InetSocketAddress bindAddress, Configuration conf, RpcScheduler scheduler) throws IOException { super(server, name, services, bindAddress, conf, scheduler); this.bindAddress = bindAddress; boolean useEpoll = useEpoll(conf); int workerCount = conf.getInt("hbase.netty.rpc.server.worker.count", Runtime.getRuntime().availableProcessors() / 4); EventLoopGroup bossGroup = null;//from ww w. ja v a 2 s. co m EventLoopGroup workerGroup = null; if (useEpoll) { bossGroup = new EpollEventLoopGroup(1); workerGroup = new EpollEventLoopGroup(workerCount); } else { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(workerCount); } ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup); if (useEpoll) { bootstrap.channel(EpollServerSocketChannel.class); } else { bootstrap.channel(NioServerSocketChannel.class); } bootstrap.childOption(ChannelOption.TCP_NODELAY, tcpNoDelay); bootstrap.childOption(ChannelOption.SO_KEEPALIVE, tcpKeepAlive); bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); bootstrap.childHandler(new Initializer(maxRequestSize)); try { serverChannel = bootstrap.bind(this.bindAddress).sync().channel(); LOG.info("NettyRpcServer bind to address=" + serverChannel.localAddress() + ", hbase.netty.rpc.server.worker.count=" + workerCount + ", useEpoll=" + useEpoll); allChannels.add(serverChannel); } catch (InterruptedException e) { throw new InterruptedIOException(e.getMessage()); } initReconfigurable(conf); this.scheduler.init(new RpcSchedulerContext(this)); }
From source file:org.apache.hyracks.http.HttpTestUtil.java
License:Apache License
public static synchronized void printMemUsage() { StringBuilder report = new StringBuilder(); /* TODO: unsupported APIs after java 8 report.append("sun.misc.VM.maxDirectMemory: "); report.append(sun.misc.VM.maxDirectMemory()); report.append('\n');//from w ww . java 2 s .c o m report.append("sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getMemoryUsed(): "); report.append(sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getMemoryUsed()); report.append('\n'); report.append("sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getTotalCapacity(): "); report.append(sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getTotalCapacity()); */ report.append('\n'); report.append("ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(): "); report.append(ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage()); report.append('\n'); report.append("---------------------------- Beans ----------------------------"); report.append('\n'); List<MemoryPoolMXBean> memPoolBeans = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean bean : memPoolBeans) { if (bean.isValid() && bean.getType() == MemoryType.NON_HEAP) { report.append(bean.getName()); report.append(": "); report.append(bean.getUsage()); report.append('\n'); } } report.append("---------------------------- Netty ----------------------------"); report.append('\n'); try { Field field = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_COUNTER"); field.setAccessible(true); AtomicLong usedDirectMemory = (AtomicLong) field.get(null); long used = usedDirectMemory.get(); report.append("Current PlatformDependent.DIRECT_MEMORY_COUNTER: "); report.append(used); report.append('\n'); report.append("Maximum PlatformDependent.DIRECT_MEMORY_COUNTER: "); maxMemUsage = Math.max(maxMemUsage, used); report.append(maxMemUsage); report.append('\n'); report.append('\n'); } catch (Throwable th) { // NOSONAR LOGGER.log(Level.WARN, "Failed to access PlatformDependent.DIRECT_MEMORY_COUNTER", th); return; } report.append("--------------- PooledByteBufAllocator.DEFAULT ----------------"); report.append(PooledByteBufAllocator.DEFAULT.dumpStats()); LOGGER.log(Level.INFO, report.toString()); }
From source file:org.apache.hyracks.http.server.HttpServer.java
License:Apache License
protected void doStart() throws InterruptedException { /*// www . j a v a 2 s.co m * 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.hyracks.http.server.utils.HttpUtil.java
License:Apache License
@SuppressWarnings("restriction") public static synchronized void printMemUsage() { StringBuilder report = new StringBuilder(); report.append("sun.misc.VM.maxDirectMemory: "); report.append(sun.misc.VM.maxDirectMemory()); report.append('\n'); report.append("sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getMemoryUsed(): "); report.append(sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getMemoryUsed()); report.append('\n'); report.append("sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getTotalCapacity(): "); report.append(sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getTotalCapacity()); report.append('\n'); report.append("ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(): "); report.append(ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage()); report.append('\n'); report.append("---------------------------- Beans ----------------------------"); report.append('\n'); List<MemoryPoolMXBean> memPoolBeans = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean bean : memPoolBeans) { if (bean.isValid() && bean.getType() == MemoryType.NON_HEAP) { report.append(bean.getName()); report.append(": "); report.append(bean.getUsage()); report.append('\n'); }/* w w w . jav a 2s . co m*/ } report.append("---------------------------- Netty ----------------------------"); report.append('\n'); try { Field field = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_COUNTER"); field.setAccessible(true); AtomicLong usedDirectMemory = (AtomicLong) field.get(null); long used = usedDirectMemory.get(); report.append("Current PlatformDependent.DIRECT_MEMORY_COUNTER: "); report.append(used); report.append('\n'); report.append("Maximum PlatformDependent.DIRECT_MEMORY_COUNTER: "); maxMemUsage = Math.max(maxMemUsage, used); report.append(maxMemUsage); report.append('\n'); report.append('\n'); } catch (Throwable th) { // NOSONAR LOGGER.log(Level.WARNING, "Failed to access PlatformDependent.DIRECT_MEMORY_COUNTER", th); return; } report.append("--------------- PooledByteBufAllocator.DEFAULT ----------------"); report.append(PooledByteBufAllocator.DEFAULT.dumpStats()); LOGGER.log(Level.INFO, report.toString()); }
From source file:org.apache.pulsar.broker.admin.impl.PersistentTopicsBase.java
License:Apache License
protected Response internalPeekNthMessage(String subName, int messagePosition, boolean authoritative) { if (topicName.isGlobal()) { validateGlobalNamespaceOwnership(namespaceName); }/*from ww w .j a v a 2 s . co m*/ PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative); if (partitionMetadata.partitions > 0) { throw new RestException(Status.METHOD_NOT_ALLOWED, "Peek messages on a partitioned topic is not allowed"); } validateAdminAccessForSubscriber(subName, authoritative); if (!(getTopicReference(topicName) instanceof PersistentTopic)) { log.error("[{}] Not supported operation of non-persistent topic {} {}", clientAppId(), topicName, subName); throw new RestException(Status.METHOD_NOT_ALLOWED, "Skip messages on a non-persistent topic is not allowed"); } PersistentTopic topic = (PersistentTopic) getTopicReference(topicName); PersistentReplicator repl = null; PersistentSubscription sub = null; Entry entry = null; if (subName.startsWith(topic.replicatorPrefix)) { repl = getReplicatorReference(subName, topic); } else { sub = (PersistentSubscription) getSubscriptionReference(subName, topic); } try { if (subName.startsWith(topic.replicatorPrefix)) { entry = repl.peekNthMessage(messagePosition).get(); } else { entry = sub.peekNthMessage(messagePosition).get(); } checkNotNull(entry); PositionImpl pos = (PositionImpl) entry.getPosition(); ByteBuf metadataAndPayload = entry.getDataBuffer(); // moves the readerIndex to the payload MessageMetadata metadata = Commands.parseMessageMetadata(metadataAndPayload); ResponseBuilder responseBuilder = Response.ok(); responseBuilder.header("X-Pulsar-Message-ID", pos.toString()); for (KeyValue keyValue : metadata.getPropertiesList()) { responseBuilder.header("X-Pulsar-PROPERTY-" + keyValue.getKey(), keyValue.getValue()); } if (metadata.hasPublishTime()) { responseBuilder.header("X-Pulsar-publish-time", DateFormatter.format(metadata.getPublishTime())); } if (metadata.hasEventTime()) { responseBuilder.header("X-Pulsar-event-time", DateFormatter.format(metadata.getEventTime())); } if (metadata.hasNumMessagesInBatch()) { responseBuilder.header("X-Pulsar-num-batch-message", metadata.getNumMessagesInBatch()); } // Decode if needed CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(metadata.getCompression()); ByteBuf uncompressedPayload = codec.decode(metadataAndPayload, metadata.getUncompressedSize()); // Copy into a heap buffer for output stream compatibility ByteBuf data = PooledByteBufAllocator.DEFAULT.heapBuffer(uncompressedPayload.readableBytes(), uncompressedPayload.readableBytes()); data.writeBytes(uncompressedPayload); uncompressedPayload.release(); StreamingOutput stream = new StreamingOutput() { @Override public void write(OutputStream output) throws IOException, WebApplicationException { output.write(data.array(), data.arrayOffset(), data.readableBytes()); data.release(); } }; return responseBuilder.entity(stream).build(); } catch (NullPointerException npe) { throw new RestException(Status.NOT_FOUND, "Message not found"); } catch (Exception exception) { log.error("[{}] Failed to get message at position {} from {} {}", clientAppId(), messagePosition, topicName, subName, exception); throw new RestException(exception); } finally { if (entry != null) { entry.release(); } } }