Java Code Examples for io.netty.buffer.PooledByteBufAllocator#DEFAULT

The following examples show how to use io.netty.buffer.PooledByteBufAllocator#DEFAULT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AllocatorStatsGenerator.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static AllocatorStats generate(String allocatorName) {
    PooledByteBufAllocator allocator = null;
    if ("default".equals(allocatorName)) {
        allocator = PooledByteBufAllocator.DEFAULT;
    } else if ("ml-cache".equals(allocatorName)) {
        allocator = EntryCacheImpl.ALLOCATOR;
    } else {
        throw new IllegalArgumentException("Invalid allocator name : " + allocatorName);
    }

    AllocatorStats stats = new AllocatorStats();
    stats.directArenas = allocator.metric().directArenas().stream()
        .map(AllocatorStatsGenerator::newPoolArenaStats)
        .collect(Collectors.toList());
    stats.heapArenas = allocator.metric().heapArenas().stream()
        .map(AllocatorStatsGenerator::newPoolArenaStats)
        .collect(Collectors.toList());

    stats.numDirectArenas = allocator.metric().numDirectArenas();
    stats.numHeapArenas = allocator.metric().numHeapArenas();
    stats.numThreadLocalCaches = allocator.metric().numThreadLocalCaches();
    stats.normalCacheSize = allocator.metric().normalCacheSize();
    stats.smallCacheSize = allocator.metric().smallCacheSize();
    stats.tinyCacheSize = allocator.metric().tinyCacheSize();
    return stats;
}
 
Example 2
Source File: RedisEncoderBenchmark.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
    byte[] bytes = new byte[256];
    content = Unpooled.buffer(bytes.length);
    content.writeBytes(bytes);
    ByteBuf testContent = Unpooled.unreleasableBuffer(content.asReadOnly());

    List<RedisMessage> rList = new ArrayList<RedisMessage>(arraySize);
    for (int i = 0; i < arraySize; ++i) {
        rList.add(new FullBulkStringRedisMessage(testContent));
    }
    redisArray = new ArrayRedisMessage(rList);
    encoder = new RedisEncoder();
    context = new EmbeddedChannelWriteReleaseHandlerContext(pooledAllocator ? PooledByteBufAllocator.DEFAULT :
            UnpooledByteBufAllocator.DEFAULT, encoder) {
        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
}
 
Example 3
Source File: JsonSerializerTest.java    From dapeng-soa with Apache License 2.0 6 votes vote down vote up
private static <REQ> ByteBuf buildRequestBuf(String service, String version, String method, int seqid, REQ request, BeanSerializer<REQ> requestSerializer) throws SoaException {
    AbstractByteBufAllocator allocator =
            SoaSystemEnvProperties.SOA_POOLED_BYTEBUF ?
                    PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT;
    final ByteBuf requestBuf = allocator.buffer(8192);

    SoaMessageBuilder<REQ> builder = new SoaMessageBuilder<>();

    try {
        SoaHeader header = SoaHeaderHelper.buildHeader(service, version, method);

        ByteBuf buf = builder.buffer(requestBuf)
                .header(header)
                .body(request, requestSerializer)
                .seqid(seqid)
                .build();
        return buf;
    } catch (TException e) {
        e.printStackTrace();
    }

    return null;
}
 
Example 4
Source File: NettyClient.java    From dapeng-soa with Apache License 2.0 6 votes vote down vote up
protected Bootstrap initBootstrap() {
    AbstractByteBufAllocator allocator =
            SoaSystemEnvProperties.SOA_POOLED_BYTEBUF ?
                    PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT;
    bootstrap = new Bootstrap();
    bootstrap.group(workerGroup);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.ALLOCATOR, allocator);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds),
                    new SoaFrameDecoder(), //粘包和断包处理
                    new SoaIdleHandler(),
                    new SoaClientHandler(callBack));
        }
    });
    return bootstrap;
}
 
Example 5
Source File: NoPriorityByteDistributionBenchmark.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Setup(Level.Trial)
public void setupTrial() throws Exception {
    connection = new DefaultHttp2Connection(false);
    dataRefresherKey = connection.newKey();

    // Create the flow controller
    switch (algorithm) {
        case WFQ:
            distributor = new WeightedFairQueueByteDistributor(connection, 0);
            break;
        case UNIFORM:
            distributor = new UniformStreamByteDistributor(connection);
            break;
    }
    controller = new DefaultHttp2RemoteFlowController(connection, new ByteCounter(distributor));
    connection.remote().flowController(controller);
    Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
        .encoderEnforceMaxConcurrentStreams(false).validateHeaders(false)
        .frameListener(new Http2FrameAdapter())
        .connection(connection)
        .build();
    ctx = new EmbeddedChannelWriteReleaseHandlerContext(PooledByteBufAllocator.DEFAULT, handler) {
        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
    handler.handlerAdded(ctx);
    handler.channelActive(ctx);

    // Create the streams, each initialized with MAX_INT bytes.
    for (int i = 0; i < numStreams; ++i) {
        Http2Stream stream = connection.local().createStream(toStreamId(i), false);
        addData(stream, Integer.MAX_VALUE);
        stream.setProperty(dataRefresherKey, new DataRefresher(stream));
    }
}
 
Example 6
Source File: HttpObjectEncoderBenchmark.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
    byte[] bytes = new byte[256];
    content = Unpooled.buffer(bytes.length);
    content.writeBytes(bytes);
    ByteBuf testContent = Unpooled.unreleasableBuffer(content.asReadOnly());
    HttpHeaders headersWithChunked = new DefaultHttpHeaders(false);
    headersWithChunked.add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    HttpHeaders headersWithContentLength = new DefaultHttpHeaders(false);
    headersWithContentLength.add(HttpHeaderNames.CONTENT_LENGTH, testContent.readableBytes());

    fullRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index", testContent,
            headersWithContentLength, EmptyHttpHeaders.INSTANCE);
    contentLengthRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index",
            headersWithContentLength);
    chunkedRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index", headersWithChunked);
    lastContent = new DefaultLastHttpContent(testContent, false);

    encoder = new HttpRequestEncoder();
    context = new EmbeddedChannelWriteReleaseHandlerContext(pooledAllocator ? PooledByteBufAllocator.DEFAULT :
            UnpooledByteBufAllocator.DEFAULT, encoder) {
        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
}
 
Example 7
Source File: Http2FrameWriterDataBenchmark.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
    writer = new DefaultHttp2FrameWriter();
    oldWriter = new OldDefaultHttp2FrameWriter();
    payload = pooled ? PooledByteBufAllocator.DEFAULT.buffer(payloadSize) : Unpooled.buffer(payloadSize);
    payload.writeZero(payloadSize);
    ctx = new EmbeddedChannelWriteReleaseHandlerContext(
            pooled ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT,
            new ChannelInboundHandlerAdapter()) {
        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
}
 
Example 8
Source File: BufAllocator.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 创建缓存的缓冲区
 *
 * @param url
 * @return
 */
protected static ByteBufAllocator createPooled(final URL url) {
    String preferDirect = url.getString(BUFFER_PREFER_DIRECT_KEY);
    if ("true".equalsIgnoreCase(preferDirect)) {
        return new PooledByteBufAllocator(true);
    } else if ("false".equalsIgnoreCase(preferDirect)) {
        return new PooledByteBufAllocator(false);
    } else {
        return PooledByteBufAllocator.DEFAULT;
    }
}
 
Example 9
Source File: CopyByteBufHandlerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void copiesAndReleasesPooledByteBuf() {
    ByteBufAllocator pooledAllocator = PooledByteBufAllocator.DEFAULT;
    ByteBufAllocator unpooledAllocator = UnpooledByteBufAllocator.DEFAULT;
    CopyByteBufHandler handler = new CopyByteBufHandler(unpooledAllocator);

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    ArgumentCaptor<ByteBuf> valueCapture = ArgumentCaptor.forClass(ByteBuf.class);
    doReturn(ctx).when(ctx).fireChannelRead(valueCapture.capture());

    ByteBuf pooledBuf = pooledAllocator.buffer(4);
    assertThat(pooledBuf.alloc(), is(pooledAllocator));
    try {
        assertThat(writeAscii(pooledBuf, "test"), is(4));
        handler.channelRead(ctx, pooledBuf);
        assertThat(pooledBuf.refCnt(), is(0));

        ByteBuf unpooledBuf = valueCapture.getValue();
        assertThat(unpooledBuf, is(not(sameInstance(pooledBuf))));
        assertThat(unpooledBuf.alloc(), is(unpooledAllocator));
        assertThat(unpooledBuf.toString(US_ASCII), equalTo("test"));
    } finally {
        if (pooledBuf.refCnt() > 0) {
            pooledBuf.release();
        }
    }
}
 
Example 10
Source File: PoolConfiguration.java    From AsyncDao with MIT License 5 votes vote down vote up
public Configuration getConfig() {
    return new Configuration(
            username,
            host,
            port,
            Option.apply(password),
            Option.apply(database),
            SSLConfiguration.apply(buildSslConfig()),
            charset,
            16777216,
            PooledByteBufAllocator.DEFAULT,
            Duration.apply(connectTimeout, TimeUnit.MILLISECONDS),
            Duration.apply(testTimeout, TimeUnit.MILLISECONDS),
            Option.apply(Duration.apply(queryTimeout, TimeUnit.MILLISECONDS)));
}
 
Example 11
Source File: SoaConnectionImpl.java    From dapeng-soa with Apache License 2.0 5 votes vote down vote up
@Override
protected <REQ> ByteBuf buildRequestBuf(String service, String version, String method, int seqid, REQ request, BeanSerializer<REQ> requestSerializer) throws SoaException {
    AbstractByteBufAllocator allocator =
            SoaSystemEnvProperties.SOA_POOLED_BYTEBUF ?
                    PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT;
    final ByteBuf requestBuf = allocator.buffer(8192);

    SoaMessageBuilder<REQ> builder = new SoaMessageBuilder<>();

    try {
        SoaHeader header = SoaHeaderHelper.buildHeader(service, version, method);

        ByteBuf buf = builder.buffer(requestBuf)
                .header(header)
                .body(request, requestSerializer)
                .seqid(seqid)
                .build();
        return buf;
    } catch (TException e) {
        LOGGER.error(e.getMessage(), e);
        requestBuf.release();
        if (e instanceof SoaException) {
            throw (SoaException)e;
        } else {
            throw new SoaException(e);
        }
    }
}
 
Example 12
Source File: MySqlAsyncConnection.java    From ob1k with Apache License 2.0 5 votes vote down vote up
public static Configuration createConfiguration(final String host, final int port, final Optional<String> database, final String userName, final Optional<String> password,
                                                final long connectTimeoutMilliSeconds, final long queryTimeoutMilliSeconds) {
  final Optional<Duration> queryTimeout = queryTimeoutMilliSeconds == -1 ?
    Optional.empty() :
    Optional.of(Duration.ofMillis(queryTimeoutMilliSeconds));

  return new Configuration(userName, host, port, password.orElse(null), database.orElse(null), new SSLConfiguration(), CharsetUtil.UTF_8, MAXIMUM_MESSAGE_SIZE,
    PooledByteBufAllocator.DEFAULT, Duration.ofMillis(connectTimeoutMilliSeconds), Duration.ofSeconds(4),
    queryTimeout.orElse(null));
}
 
Example 13
Source File: ByteBufLeakTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Bean
public DataBufferFactory dataBufferFactory() {
    return new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT) {
        // This method will be called when emitting string from Mono/Flux.
        @Override
        public NettyDataBuffer allocateBuffer(int initialCapacity) {
            final NettyDataBuffer buffer = super.allocateBuffer(initialCapacity);
            // Keep allocated buffers.
            allocatedBuffers.offer(buffer);
            return buffer;
        }
    };
}
 
Example 14
Source File: BaseSqlTest.java    From AsyncDao with MIT License 4 votes vote down vote up
@Test
public void origin() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    Vertx vertx = Vertx.vertx();

    Configuration configuration = new Configuration(
            "root",
            "localhost",
            3306,
            Option.apply("admin"),
            Option.apply("test"),
            SSLConfiguration.apply(Map$.MODULE$.empty()),
            Charset.forName("UTF-8"),
            16777216,
            PooledByteBufAllocator.DEFAULT,
            Duration.apply(10000, TimeUnit.MILLISECONDS),
            Duration.apply(10000, TimeUnit.MILLISECONDS),
            Option.apply(Duration.apply(10000, TimeUnit.MILLISECONDS)));

    Connection connection = new MySQLConnection(configuration,
            CharsetMapper.Instance(),
            vertx.nettyEventLoopGroup().next(),
            VertxEventLoopExecutionContext.create(vertx));

    System.out.println(connection.isConnected());

    connection.connect().onComplete(ScalaUtils.toFunction1(asyncResult -> {

        if(asyncResult.failed()){
            asyncResult.cause().printStackTrace();
            latch.countDown();
        }

        Future<QueryResult> qr = asyncResult.result().sendQuery("insert into T_User(username) values('twogoods')");
        qr.onComplete(ScalaUtils.toFunction1(ar -> {
            if (ar.succeeded()) {
                QueryResult queryResult = ar.result();
                System.out.println("rowsAffected: " + queryResult.rowsAffected());
                System.out.println("insert id: " + ((MySQLQueryResult) queryResult).lastInsertId());

                System.out.println(queryResult.rows().get().columnNames().toList());
                queryResult.rows().get().foreach(new AbstractFunction1<RowData, Void>() {
                    @Override
                    public Void apply(RowData row) {
                        row.foreach(new AbstractFunction1<Object, Void>() {
                            @Override
                            public Void apply(Object value) {
                                System.out.println("value" + value);
                                return null;
                            }
                        });
                        latch.countDown();
                        return null;
                    }
                });
            } else {
                ar.cause().printStackTrace();
            }
        }), VertxEventLoopExecutionContext.create(vertx));

    }), VertxEventLoopExecutionContext.create(vertx));

    latch.await();
}
 
Example 15
Source File: DefaultClientRequestContext.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBufAllocator alloc() {
    final Channel channel = channel();
    return channel != null ? channel.alloc() : PooledByteBufAllocator.DEFAULT;
}
 
Example 16
Source File: ArmeriaServerHttpResponseTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void requestInvalidDemand() throws Exception {
    final ConcurrentLinkedQueue<NettyDataBuffer> allocatedBuffers = new ConcurrentLinkedQueue<>();
    final DataBufferFactoryWrapper<NettyDataBufferFactory> factoryWrapper = new DataBufferFactoryWrapper<>(
            new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT) {
                @Override
                public NettyDataBuffer allocateBuffer() {
                    final NettyDataBuffer buffer = super.allocateBuffer();
                    allocatedBuffers.offer(buffer);
                    return buffer;
                }
            });
    final CompletableFuture<HttpResponse> future = new CompletableFuture<>();
    final ArmeriaServerHttpResponse response =
            new ArmeriaServerHttpResponse(ctx, future, factoryWrapper, null);
    response.writeWith(Mono.just(factoryWrapper.delegate().allocateBuffer().write("foo".getBytes())))
            .then(Mono.defer(response::setComplete)).subscribe();
    await().until(future::isDone);
    assertThat(future.isCompletedExceptionally()).isFalse();

    final AtomicBoolean completed = new AtomicBoolean();
    final AtomicReference<Throwable> error = new AtomicReference<>();
    future.get().subscribe(new Subscriber<HttpObject>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(0);
        }

        @Override
        public void onNext(HttpObject httpObject) {
            // Do nothing.
        }

        @Override
        public void onError(Throwable t) {
            error.compareAndSet(null, t);
            completed.set(true);
        }

        @Override
        public void onComplete() {
            completed.set(true);
        }
    });

    await().untilTrue(completed);
    assertThat(error.get()).isInstanceOf(IllegalArgumentException.class)
                           .hasMessageContaining("Reactive Streams specification rule 3.9");
    await().untilAsserted(() -> {
        assertThat(allocatedBuffers).hasSize(1);
        assertThat(allocatedBuffers.peek().getNativeBuffer().refCnt()).isZero();
        allocatedBuffers.poll();
    });
}
 
Example 17
Source File: DriftNettyServerModule.java    From drift with Apache License 2.0 4 votes vote down vote up
public DriftNettyServerModule()
{
    this(PooledByteBufAllocator.DEFAULT);
}
 
Example 18
Source File: LoadServer.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
LoadServer(Control.ServerConfig config) throws Exception {
  log.log(Level.INFO, "Server Config \n" + config.toString());
  port = config.getPort() ==  0 ? Utils.pickUnusedPort() : config.getPort();
  ServerBuilder<?> serverBuilder = ServerBuilder.forPort(port);
  int asyncThreads = config.getAsyncServerThreads() == 0
      ? Runtime.getRuntime().availableProcessors()
      : config.getAsyncServerThreads();
  // The concepts of sync & async server are quite different in the C impl and the names
  // chosen for the enum are based on that implementation. We use 'sync' to mean
  // the direct executor case in Java even though the service implementations are always
  // fully async.
  switch (config.getServerType()) {
    case ASYNC_SERVER: {
      serverBuilder.executor(getExecutor(asyncThreads));
      break;
    }
    case SYNC_SERVER: {
      serverBuilder.directExecutor();
      break;
    }
    case ASYNC_GENERIC_SERVER: {
      serverBuilder.executor(getExecutor(asyncThreads));
      // Create buffers for the generic service
      PooledByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
      genericResponse = alloc.buffer(config.getPayloadConfig().getBytebufParams().getRespSize());
      if (genericResponse.capacity() > 0) {
        genericResponse.writerIndex(genericResponse.capacity() - 1);
      }
      break;
    }
    default: {
      throw new IllegalArgumentException();
    }
  }
  if (config.hasSecurityParams()) {
    File cert = TestUtils.loadCert("server1.pem");
    File key = TestUtils.loadCert("server1.key");
    serverBuilder.useTransportSecurity(cert, key);
  }
  benchmarkService = new AsyncServer.BenchmarkServiceImpl();
  if (config.getServerType() == Control.ServerType.ASYNC_GENERIC_SERVER) {
    serverBuilder.addService(
        ServerServiceDefinition
            .builder(new ServiceDescriptor(BenchmarkServiceGrpc.SERVICE_NAME,
                GENERIC_STREAMING_PING_PONG_METHOD))
            .addMethod(GENERIC_STREAMING_PING_PONG_METHOD, new GenericServiceCallHandler())
            .build());
  } else {
    serverBuilder.addService(benchmarkService);
  }
  server = serverBuilder.build();

  List<OperatingSystemMXBean> beans =
      ManagementFactory.getPlatformMXBeans(OperatingSystemMXBean.class);
  if (!beans.isEmpty()) {
    osBean = beans.get(0);
  } else {
    osBean = null;
  }
}
 
Example 19
Source File: RSocketBrokerResponderHandler.java    From alibaba-rsocket-broker with Apache License 2.0 2 votes vote down vote up
/**
 * payload with data encoding if
 *
 * @param payload payload
 * @return payload
 */
public Payload payloadWithDataEncoding(Payload payload) {
    CompositeByteBuf compositeByteBuf = new CompositeByteBuf(PooledByteBufAllocator.DEFAULT, true, 2, payload.metadata(), this.defaultEncodingBytebuf.retainedDuplicate());
    return ByteBufPayload.create(payload.data(), compositeByteBuf);
}
 
Example 20
Source File: LeakAwareDataBufferFactory.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Creates a new {@code LeakAwareDataBufferFactory} by wrapping a
 * {@link DefaultDataBufferFactory}.
 */
public LeakAwareDataBufferFactory() {
	this(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT));
}