Search in sources :

Example 6 with Http2ClientMetrics

use of com.github.ambry.network.http2.Http2ClientMetrics 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 7 with Http2ClientMetrics

use of com.github.ambry.network.http2.Http2ClientMetrics 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 8 with Http2ClientMetrics

use of com.github.ambry.network.http2.Http2ClientMetrics 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 9 with Http2ClientMetrics

use of com.github.ambry.network.http2.Http2ClientMetrics 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)

Example 10 with Http2ClientMetrics

use of com.github.ambry.network.http2.Http2ClientMetrics in project ambry by linkedin.

the class CloudRouterFactory method getCompositeNetworkClientFactory.

/**
 * @param requestHandlerPool the pool to connect to {@link LocalNetworkClientFactory}.
 * @return a {@link CompositeNetworkClientFactory} that can be used to talk to cloud managed services or ambry server
 *         nodes.
 */
private CompositeNetworkClientFactory getCompositeNetworkClientFactory(RequestHandlerPool requestHandlerPool) {
    NetworkClientFactory cloudNetworkClientFactory = new LocalNetworkClientFactory((LocalRequestResponseChannel) requestHandlerPool.getChannel(), networkConfig, networkMetrics, time);
    NetworkClientFactory diskNetworkClientFactory = null;
    if (routerConfig.routerEnableHttp2NetworkClient) {
        diskNetworkClientFactory = new Http2NetworkClientFactory(http2ClientMetrics, http2ClientConfig, sslFactory, time);
    } else {
        diskNetworkClientFactory = new SocketNetworkClientFactory(networkMetrics, networkConfig, sslFactory, routerConfig.routerScalingUnitMaxConnectionsPerPortPlainText, routerConfig.routerScalingUnitMaxConnectionsPerPortSsl, routerConfig.routerConnectionCheckoutTimeoutMs, time);
    }
    Map<ReplicaType, NetworkClientFactory> childFactories = new EnumMap<>(ReplicaType.class);
    childFactories.put(ReplicaType.CLOUD_BACKED, cloudNetworkClientFactory);
    childFactories.put(ReplicaType.DISK_BACKED, diskNetworkClientFactory);
    return new CompositeNetworkClientFactory(childFactories);
}
Also used : LocalNetworkClientFactory(com.github.ambry.network.LocalNetworkClientFactory) CompositeNetworkClientFactory(com.github.ambry.network.CompositeNetworkClientFactory) SocketNetworkClientFactory(com.github.ambry.network.SocketNetworkClientFactory) ReplicaType(com.github.ambry.clustermap.ReplicaType) Http2NetworkClientFactory(com.github.ambry.network.http2.Http2NetworkClientFactory) LocalNetworkClientFactory(com.github.ambry.network.LocalNetworkClientFactory) SocketNetworkClientFactory(com.github.ambry.network.SocketNetworkClientFactory) NetworkClientFactory(com.github.ambry.network.NetworkClientFactory) CompositeNetworkClientFactory(com.github.ambry.network.CompositeNetworkClientFactory) Http2NetworkClientFactory(com.github.ambry.network.http2.Http2NetworkClientFactory) EnumMap(java.util.EnumMap)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)10 Test (org.junit.Test)9 MultiplexedChannelRecordTest (com.github.ambry.network.http2.MultiplexedChannelRecordTest)7 ChannelPool (io.netty.channel.pool.ChannelPool)7 SocketChannel (io.netty.channel.socket.SocketChannel)7 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)7 Http2ClientConfig (com.github.ambry.config.Http2ClientConfig)5 Http2ClientMetrics (com.github.ambry.network.http2.Http2ClientMetrics)5 Channel (io.netty.channel.Channel)5 Http2StreamChannel (io.netty.handler.codec.http2.Http2StreamChannel)5 DefaultPromise (io.netty.util.concurrent.DefaultPromise)5 ArrayList (java.util.ArrayList)5 NettySslHttp2Factory (com.github.ambry.commons.NettySslHttp2Factory)4 SSLFactory (com.github.ambry.commons.SSLFactory)4 Port (com.github.ambry.network.Port)4 IOException (java.io.IOException)4 DataNodeId (com.github.ambry.clustermap.DataNodeId)3 VerifiableProperties (com.github.ambry.config.VerifiableProperties)3 BlobProperties (com.github.ambry.messageformat.BlobProperties)3 Properties (java.util.Properties)3