Search in sources :

Example 16 with ChannelPool

use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.

the class Http2MultiplexedChannelPoolTest method minConnectionTest.

/**
 * Minimum Connection is required.
 */
@Test
public void minConnectionTest() throws Exception {
    int minConnections = 2;
    int totalStreamToAcquire = 4;
    List<Channel> channels = new ArrayList<>();
    try {
        ChannelPool connectionPool = Mockito.mock(ChannelPool.class);
        OngoingStubbing<Future<Channel>> mockito = Mockito.when(connectionPool.acquire());
        for (int i = 0; i < minConnections; i++) {
            Channel channel = newHttp2Channel();
            channels.add(channel);
            loopGroup.register(channel).awaitUninterruptibly();
            Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
            channelPromise.setSuccess(channel);
            mockito = mockito.thenReturn(channelPromise);
        }
        Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, new HashSet<>(), null, http2ClientConfigForTwoConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
        List<Channel> toRelease = new ArrayList<>();
        for (int i = 0; i < minConnections; i++) {
            Channel streamChannel = h2Pool.acquire().awaitUninterruptibly().getNow();
            toRelease.add(streamChannel);
            assertTrue(streamChannel instanceof Http2StreamChannel);
            Mockito.verify(connectionPool, Mockito.times(i + 1)).acquire();
        }
        for (int i = minConnections; i < totalStreamToAcquire; i++) {
            Channel streamChannel = h2Pool.acquire().awaitUninterruptibly().getNow();
            toRelease.add(streamChannel);
            assertTrue(streamChannel instanceof Http2StreamChannel);
            // No more parent channel acquisition
            Mockito.verify(connectionPool, Mockito.times(minConnections)).acquire();
        }
        for (Channel streamChannel : toRelease) {
            h2Pool.release(streamChannel).getNow();
        }
    } finally {
        for (Channel channel : channels) {
            channel.close();
        }
    }
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) ArrayList(java.util.ArrayList) DefaultPromise(io.netty.util.concurrent.DefaultPromise) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.netty.util.concurrent.Future) FailedFuture(io.netty.util.concurrent.FailedFuture) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) MultiplexedChannelRecordTest(com.github.ambry.network.http2.MultiplexedChannelRecordTest) Test(org.junit.Test)

Example 17 with ChannelPool

use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.

the class Http2MultiplexedChannelPoolTest method closeWaitsForConnectionToBeReleasedBeforeClosingConnectionPool.

/**
 * Connection pool is released first and then close upon h2 pool close.
 */
@Test
public void closeWaitsForConnectionToBeReleasedBeforeClosingConnectionPool() {
    SocketChannel channel = new NioSocketChannel();
    try {
        loopGroup.register(channel).awaitUninterruptibly();
        ChannelPool connectionPool = mock(ChannelPool.class);
        ArgumentCaptor<Promise> releasePromise = ArgumentCaptor.forClass(Promise.class);
        when(connectionPool.release(eq(channel), releasePromise.capture())).thenAnswer(invocation -> {
            Promise<?> promise = releasePromise.getValue();
            promise.setSuccess(null);
            return promise;
        });
        MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 8, null, streamChannelInitializer);
        Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.singleton(record), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
        h2Pool.close();
        InOrder inOrder = Mockito.inOrder(connectionPool);
        inOrder.verify(connectionPool).release(eq(channel), isA(Promise.class));
        inOrder.verify(connectionPool).close();
    } finally {
        channel.close().awaitUninterruptibly();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelPool(io.netty.channel.pool.ChannelPool) Promise(io.netty.util.concurrent.Promise) DefaultPromise(io.netty.util.concurrent.DefaultPromise) InOrder(org.mockito.InOrder) MetricRegistry(com.codahale.metrics.MetricRegistry) MultiplexedChannelRecordTest(com.github.ambry.network.http2.MultiplexedChannelRecordTest) Test(org.junit.Test)

Example 18 with ChannelPool

use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.

the class Http2MultiplexedChannelPoolTest method acquireAfterCloseFails.

/**
 * Acquire should fail if pool is closed.
 */
@Test
public void acquireAfterCloseFails() throws InterruptedException {
    ChannelPool connectionPool = mock(ChannelPool.class);
    Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup.next(), new HashSet<>(), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
    h2Pool.close();
    Future<Channel> acquireResult = h2Pool.acquire().await();
    assertFalse(acquireResult.isSuccess());
    assertTrue(acquireResult.cause() instanceof IOException);
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool) MetricRegistry(com.codahale.metrics.MetricRegistry) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) IOException(java.io.IOException) MultiplexedChannelRecordTest(com.github.ambry.network.http2.MultiplexedChannelRecordTest) Test(org.junit.Test)

Example 19 with ChannelPool

use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.

the class Http2MultiplexedChannelPoolTest method releaseParentChannelsIfPoolIsClosed.

/**
 * Channel acquire should fail if pool is closed.
 */
@Test
public void releaseParentChannelsIfPoolIsClosed() {
    SocketChannel channel = new NioSocketChannel();
    try {
        loopGroup.register(channel).awaitUninterruptibly();
        ChannelPool connectionPool = mock(ChannelPool.class);
        ArgumentCaptor<Promise> releasePromise = ArgumentCaptor.forClass(Promise.class);
        when(connectionPool.release(eq(channel), releasePromise.capture())).thenAnswer(invocation -> {
            Promise<?> promise = releasePromise.getValue();
            promise.setSuccess(null);
            return promise;
        });
        MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 8, null, streamChannelInitializer);
        Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.singleton(record), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
        h2Pool.close();
        InOrder inOrder = Mockito.inOrder(connectionPool);
        inOrder.verify(connectionPool).release(eq(channel), isA(Promise.class));
        inOrder.verify(connectionPool).close();
    } finally {
        channel.close().awaitUninterruptibly();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelPool(io.netty.channel.pool.ChannelPool) Promise(io.netty.util.concurrent.Promise) DefaultPromise(io.netty.util.concurrent.DefaultPromise) InOrder(org.mockito.InOrder) MetricRegistry(com.codahale.metrics.MetricRegistry) MultiplexedChannelRecordTest(com.github.ambry.network.http2.MultiplexedChannelRecordTest) Test(org.junit.Test)

Aggregations

ChannelPool (io.netty.channel.pool.ChannelPool)19 Channel (io.netty.channel.Channel)13 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)13 SocketChannel (io.netty.channel.socket.SocketChannel)12 Test (org.junit.Test)12 Bootstrap (io.netty.bootstrap.Bootstrap)10 MetricRegistry (com.codahale.metrics.MetricRegistry)7 MultiplexedChannelRecordTest (com.github.ambry.network.http2.MultiplexedChannelRecordTest)7 InetSocketAddress (java.net.InetSocketAddress)7 EventLoopGroup (io.netty.channel.EventLoopGroup)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Consumer (java.util.function.Consumer)6 WrapperException (com.nike.backstopper.exception.WrapperException)4 Pair (com.nike.internal.util.Pair)4 DownstreamIdleChannelTimeoutHandler (com.nike.riposte.client.asynchttp.netty.downstreampipeline.DownstreamIdleChannelTimeoutHandler)4 DownstreamChannelClosedUnexpectedlyException (com.nike.riposte.server.error.exception.DownstreamChannelClosedUnexpectedlyException)4 DownstreamIdleChannelTimeoutException (com.nike.riposte.server.error.exception.DownstreamIdleChannelTimeoutException)4 HostnameResolutionException (com.nike.riposte.server.error.exception.HostnameResolutionException)4 NativeIoExceptionWrapper (com.nike.riposte.server.error.exception.NativeIoExceptionWrapper)4 DOWNSTREAM_CALL_CONNECTION_SETUP_TIME_NANOS_REQUEST_ATTR_KEY (com.nike.riposte.server.handler.ProxyRouterEndpointExecutionHandler.DOWNSTREAM_CALL_CONNECTION_SETUP_TIME_NANOS_REQUEST_ATTR_KEY)4