Search in sources :

Example 46 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionUnsubscribedAlreadyAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionUnsubscribedAlreadyAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final String addr = UUID.randomUUID().toString();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                subscriber.unsubscribe();
                final Subscription subscription = getresp.subscribe(subscriber);
                assertTrue(subscription.isUnsubscribed());
                subscriber.assertNoTerminalEvent();
                subscriber.assertNoValues();
            }
        });
        assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TestSubscriber(rx.observers.TestSubscriber) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Example 47 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project grpc-java by grpc.

the class AbstractBenchmark method setup.

/**
 * Initialize the environment for the executor.
 */
public void setup(ExecutorType clientExecutor, ExecutorType serverExecutor, MessageSize requestSize, MessageSize responseSize, FlowWindowSize windowSize, ChannelType channelType, int maxConcurrentStreams, int channelCount) throws Exception {
    ServerCredentials serverCreds = InsecureServerCredentials.create();
    NettyServerBuilder serverBuilder;
    NettyChannelBuilder channelBuilder;
    if (channelType == ChannelType.LOCAL) {
        LocalAddress address = new LocalAddress("netty-e2e-benchmark");
        serverBuilder = NettyServerBuilder.forAddress(address, serverCreds);
        serverBuilder.channelType(LocalServerChannel.class);
        channelBuilder = NettyChannelBuilder.forAddress(address);
        channelBuilder.channelType(LocalChannel.class);
    } else {
        ServerSocket sock = new ServerSocket();
        // Pick a port using an ephemeral socket.
        sock.bind(new InetSocketAddress(BENCHMARK_ADDR, 0));
        SocketAddress address = sock.getLocalSocketAddress();
        sock.close();
        serverBuilder = NettyServerBuilder.forAddress(address, serverCreds).channelType(NioServerSocketChannel.class);
        channelBuilder = NettyChannelBuilder.forAddress(address).channelType(NioSocketChannel.class);
    }
    if (serverExecutor == ExecutorType.DIRECT) {
        serverBuilder.directExecutor();
    }
    if (clientExecutor == ExecutorType.DIRECT) {
        channelBuilder.directExecutor();
    }
    // Always use a different worker group from the client.
    ThreadFactory serverThreadFactory = new DefaultThreadFactory("STF pool", true);
    serverBuilder.workerEventLoopGroup(new NioEventLoopGroup(0, serverThreadFactory));
    serverBuilder.bossEventLoopGroup(new NioEventLoopGroup(1, serverThreadFactory));
    // Always set connection and stream window size to same value
    serverBuilder.flowControlWindow(windowSize.bytes());
    channelBuilder.flowControlWindow(windowSize.bytes());
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
    serverBuilder.maxConcurrentCallsPerConnection(maxConcurrentStreams);
    // Create buffers of the desired size for requests and responses.
    PooledByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
    // Use a heap buffer for now, since MessageFramer doesn't know how to directly convert this
    // into a WritableBuffer
    // TODO(carl-mastrangelo): convert this into a regular buffer() call.  See
    // https://github.com/grpc/grpc-java/issues/2062#issuecomment-234646216
    request = alloc.heapBuffer(requestSize.bytes());
    request.writerIndex(request.capacity() - 1);
    response = alloc.heapBuffer(responseSize.bytes());
    response.writerIndex(response.capacity() - 1);
    // Simple method that sends and receives NettyByteBuf
    unaryMethod = MethodDescriptor.<ByteBuf, ByteBuf>newBuilder().setType(MethodType.UNARY).setFullMethodName("benchmark/unary").setRequestMarshaller(new ByteBufOutputMarshaller()).setResponseMarshaller(new ByteBufOutputMarshaller()).build();
    pingPongMethod = unaryMethod.toBuilder().setType(MethodType.BIDI_STREAMING).setFullMethodName("benchmark/pingPong").build();
    flowControlledStreaming = pingPongMethod.toBuilder().setFullMethodName("benchmark/flowControlledStreaming").build();
    // Server implementation of unary & streaming methods
    serverBuilder.addService(ServerServiceDefinition.builder(new ServiceDescriptor("benchmark", unaryMethod, pingPongMethod, flowControlledStreaming)).addMethod(unaryMethod, new ServerCallHandler<ByteBuf, ByteBuf>() {

        @Override
        public ServerCall.Listener<ByteBuf> startCall(final ServerCall<ByteBuf, ByteBuf> call, Metadata headers) {
            call.sendHeaders(new Metadata());
            call.request(1);
            return new ServerCall.Listener<ByteBuf>() {

                @Override
                public void onMessage(ByteBuf message) {
                    // no-op
                    message.release();
                    call.sendMessage(response.slice());
                }

                @Override
                public void onHalfClose() {
                    call.close(Status.OK, new Metadata());
                }

                @Override
                public void onCancel() {
                }

                @Override
                public void onComplete() {
                }
            };
        }
    }).addMethod(pingPongMethod, new ServerCallHandler<ByteBuf, ByteBuf>() {

        @Override
        public ServerCall.Listener<ByteBuf> startCall(final ServerCall<ByteBuf, ByteBuf> call, Metadata headers) {
            call.sendHeaders(new Metadata());
            call.request(1);
            return new ServerCall.Listener<ByteBuf>() {

                @Override
                public void onMessage(ByteBuf message) {
                    message.release();
                    call.sendMessage(response.slice());
                    // Request next message
                    call.request(1);
                }

                @Override
                public void onHalfClose() {
                    call.close(Status.OK, new Metadata());
                }

                @Override
                public void onCancel() {
                }

                @Override
                public void onComplete() {
                }
            };
        }
    }).addMethod(flowControlledStreaming, new ServerCallHandler<ByteBuf, ByteBuf>() {

        @Override
        public ServerCall.Listener<ByteBuf> startCall(final ServerCall<ByteBuf, ByteBuf> call, Metadata headers) {
            call.sendHeaders(new Metadata());
            call.request(1);
            return new ServerCall.Listener<ByteBuf>() {

                @Override
                public void onMessage(ByteBuf message) {
                    message.release();
                    while (call.isReady()) {
                        call.sendMessage(response.slice());
                    }
                    // Request next message
                    call.request(1);
                }

                @Override
                public void onHalfClose() {
                    call.close(Status.OK, new Metadata());
                }

                @Override
                public void onCancel() {
                }

                @Override
                public void onComplete() {
                }

                @Override
                public void onReady() {
                    while (call.isReady()) {
                        call.sendMessage(response.slice());
                    }
                }
            };
        }
    }).build());
    // Build and start the clients and servers
    server = serverBuilder.build();
    server.start();
    channels = new ManagedChannel[channelCount];
    ThreadFactory clientThreadFactory = new DefaultThreadFactory("CTF pool", true);
    for (int i = 0; i < channelCount; i++) {
        // Use a dedicated event-loop for each channel
        channels[i] = channelBuilder.eventLoopGroup(new NioEventLoopGroup(1, clientThreadFactory)).build();
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ServerCallHandler(io.grpc.ServerCallHandler) InetSocketAddress(java.net.InetSocketAddress) Metadata(io.grpc.Metadata) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ServiceDescriptor(io.grpc.ServiceDescriptor) ServerCall(io.grpc.ServerCall) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NettyServerBuilder(io.grpc.netty.NettyServerBuilder) LocalAddress(io.netty.channel.local.LocalAddress) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerCredentials(io.grpc.ServerCredentials) InsecureServerCredentials(io.grpc.InsecureServerCredentials) ServerSocket(java.net.ServerSocket) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ByteBufOutputMarshaller(io.grpc.benchmarks.ByteBufOutputMarshaller)

Example 48 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project incubator-pulsar by apache.

the class BookieClientsStatsGeneratorTest method testJvmDirectMemoryUsedMetric.

@Test
public void testJvmDirectMemoryUsedMetric() throws Exception {
    PooledByteBufAllocator allocator = new // 
    PooledByteBufAllocator(// preferDirect
    true, // nHeapArenas,
    0, // nDirectArena
    1, // pageSize
    8192, // maxOrder
    11, // tinyCacheSize
    64, // smallCacheSize
    32, // normalCacheSize
    8, // Cache all threads
    true);
    int allocateMemory = 17777216;
    long directMemory1 = JvmMetrics.getJvmDirectMemoryUsed();
    ByteBuf buf2 = allocator.directBuffer(allocateMemory, allocateMemory);
    long directMemory2 = JvmMetrics.getJvmDirectMemoryUsed();
    assertEquals(directMemory2, directMemory1 + allocateMemory);
    ByteBuf buf3 = allocator.directBuffer(allocateMemory, allocateMemory);
    long directMemory3 = JvmMetrics.getJvmDirectMemoryUsed();
    assertEquals(directMemory3, directMemory2 + allocateMemory);
    buf3.release();
    directMemory3 = JvmMetrics.getJvmDirectMemoryUsed();
    assertEquals(directMemory3, directMemory2);
    buf2.release();
    directMemory2 = JvmMetrics.getJvmDirectMemoryUsed();
    assertEquals(directMemory2, directMemory1);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.testng.annotations.Test)

Example 49 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project incubator-pulsar by apache.

the class ManagedLedgerCacheMetrics method generate.

@Override
public synchronized List<Metrics> generate() {
    // get the ML cache stats bean
    ManagedLedgerFactoryMXBean mlCacheStats = getManagedLedgerCacheStats();
    Metrics m = createMetrics();
    m.put("brk_ml_count", mlCacheStats.getNumberOfManagedLedgers());
    m.put("brk_ml_cache_used_size", mlCacheStats.getCacheUsedSize());
    m.put("brk_ml_cache_evictions", mlCacheStats.getNumberOfCacheEvictions());
    m.put("brk_ml_cache_hits_rate", mlCacheStats.getCacheHitsRate());
    m.put("brk_ml_cache_misses_rate", mlCacheStats.getCacheMissesRate());
    m.put("brk_ml_cache_hits_throughput", mlCacheStats.getCacheHitsThroughput());
    m.put("brk_ml_cache_misses_throughput", mlCacheStats.getCacheMissesThroughput());
    PooledByteBufAllocator allocator = EntryCacheImpl.ALLOCATOR;
    long activeAllocations = 0;
    long activeAllocationsTiny = 0;
    long activeAllocationsSmall = 0;
    long activeAllocationsNormal = 0;
    long activeAllocationsHuge = 0;
    long totalAllocated = 0;
    long totalUsed = 0;
    for (PoolArenaMetric arena : allocator.metric().directArenas()) {
        activeAllocations += arena.numActiveAllocations();
        activeAllocationsTiny += arena.numActiveTinyAllocations();
        activeAllocationsSmall += arena.numActiveSmallAllocations();
        activeAllocationsNormal += arena.numActiveNormalAllocations();
        activeAllocationsHuge += arena.numActiveHugeAllocations();
        for (PoolChunkListMetric list : arena.chunkLists()) {
            for (PoolChunkMetric chunk : list) {
                int size = chunk.chunkSize();
                int used = size - chunk.freeBytes();
                totalAllocated += size;
                totalUsed += used;
            }
        }
    }
    m.put("brk_ml_cache_pool_allocated", totalAllocated);
    m.put("brk_ml_cache_pool_used", totalUsed);
    m.put("brk_ml_cache_pool_active_allocations", activeAllocations);
    m.put("brk_ml_cache_pool_active_allocations_tiny", activeAllocationsTiny);
    m.put("brk_ml_cache_pool_active_allocations_small", activeAllocationsSmall);
    m.put("brk_ml_cache_pool_active_allocations_normal", activeAllocationsNormal);
    m.put("brk_ml_cache_pool_active_allocations_huge", activeAllocationsHuge);
    metrics.clear();
    metrics.add(m);
    return metrics;
}
Also used : PoolArenaMetric(io.netty.buffer.PoolArenaMetric) ManagedLedgerFactoryMXBean(org.apache.bookkeeper.mledger.ManagedLedgerFactoryMXBean) Metrics(org.apache.pulsar.common.stats.Metrics) PoolChunkListMetric(io.netty.buffer.PoolChunkListMetric) PoolChunkMetric(io.netty.buffer.PoolChunkMetric) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 50 with PooledByteBufAllocator

use of io.netty.buffer.PooledByteBufAllocator in project motan by weibocom.

the class NettyHttpRequestHandlerTest method buildHttpRequest.

private FullHttpRequest buildHttpRequest(String requestPath) throws Exception {
    PooledByteBufAllocator allocator = new PooledByteBufAllocator();
    ByteBuf buf = allocator.buffer(0);
    FullHttpRequest httpReqeust = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, requestPath, buf);
    return httpReqeust;
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Aggregations

PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)60 Test (org.junit.Test)37 LocalAddress (io.netty.channel.local.LocalAddress)29 ByteBuf (io.netty.buffer.ByteBuf)28 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)25 Subscription (rx.Subscription)25 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)24 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)24 DisposableWrapper (org.jocean.idiom.DisposableWrapper)20 IOException (java.io.IOException)18 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)17 ConnectException (java.net.ConnectException)17 CertificateException (java.security.cert.CertificateException)17 SSLException (javax.net.ssl.SSLException)17 TransportException (org.jocean.http.TransportException)17 TestSubscriber (rx.observers.TestSubscriber)16 Channel (io.netty.channel.Channel)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)8