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();
}
}
}
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();
}
}
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);
}
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();
}
}
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);
}
Aggregations