use of com.github.ambry.config.ConnectionPoolConfig in project ambry by linkedin.
the class BlockingChannelConnectionPoolTest method underSubscriptionTest.
private void underSubscriptionTest(Properties props, String host, Port port, int underSubscriptionCount) throws Exception {
AtomicReference<Exception> exception = new AtomicReference<Exception>();
BlockingChannelInfo channelInfo = new BlockingChannelInfo(new ConnectionPoolConfig(new VerifiableProperties(props)), host, port, new MetricRegistry(), sslSocketFactory, sslConfig);
CountDownLatch channelCount = new CountDownLatch(underSubscriptionCount);
CountDownLatch shouldRelease = new CountDownLatch(1);
CountDownLatch releaseComplete = new CountDownLatch(underSubscriptionCount);
for (int i = 0; i < underSubscriptionCount; i++) {
BlockingChannelInfoThread infoThread = new BlockingChannelInfoThread(channelInfo, channelCount, shouldRelease, releaseComplete, true, exception);
Thread t = new Thread(infoThread);
t.start();
}
shouldRelease.countDown();
awaitCountdown(releaseComplete, 2000, exception, "Timed out while waiting for channels to be released");
Assert.assertEquals(channelInfo.getNumberOfConnections(), underSubscriptionCount);
channelInfo.getBlockingChannel(1000);
Assert.assertEquals(channelInfo.getNumberOfConnections(), underSubscriptionCount);
channelInfo.cleanup();
Assert.assertEquals(channelInfo.getNumberOfConnections(), 0);
}
use of com.github.ambry.config.ConnectionPoolConfig in project ambry by linkedin.
the class BlockingChannelConnectionPoolTest method testBlockingChannelConnectionPool.
@Test
public void testBlockingChannelConnectionPool() throws Exception {
Properties props = new Properties();
props.put("connectionpool.max.connections.per.port.plain.text", "5");
props.put("connectionpool.max.connections.per.port.ssl", "5");
props.put("clustermap.cluster.name", "test");
props.put("clustermap.datacenter.name", "dc1");
props.put("clustermap.host.name", "localhost");
ConnectionPool connectionPool = new BlockingChannelConnectionPool(new ConnectionPoolConfig(new VerifiableProperties(props)), sslConfig, plainTextClusterMapConfig, new MetricRegistry());
connectionPool.start();
CountDownLatch shouldRelease = new CountDownLatch(1);
CountDownLatch releaseComplete = new CountDownLatch(10);
AtomicReference<Exception> exception = new AtomicReference<Exception>();
Map<String, CountDownLatch> channelCount = new HashMap<String, CountDownLatch>();
channelCount.put("localhost" + 6667, new CountDownLatch(5));
channelCount.put("localhost" + 6668, new CountDownLatch(5));
channelCount.put("localhost" + 6669, new CountDownLatch(5));
Map<String, Port> channelToPortMap = new HashMap<String, Port>();
channelToPortMap.put("localhost" + 6667, new Port(6667, PortType.PLAINTEXT));
channelToPortMap.put("localhost" + 6668, new Port(6668, PortType.PLAINTEXT));
channelToPortMap.put("localhost" + 6669, new Port(6669, PortType.PLAINTEXT));
for (int i = 0; i < 10; i++) {
ConnectionPoolThread connectionPoolThread = new ConnectionPoolThread(channelCount, channelToPortMap, connectionPool, false, shouldRelease, releaseComplete, exception);
Thread t = new Thread(connectionPoolThread);
t.start();
}
for (String channelStr : channelCount.keySet()) {
awaitCountdown(channelCount.get(channelStr), 1000, exception, "Timed out waiting for channel count to reach 5");
}
// reset
for (String channelStr : channelCount.keySet()) {
channelCount.put(channelStr, new CountDownLatch(5));
}
shouldRelease.countDown();
for (String channelStr : channelCount.keySet()) {
awaitCountdown(channelCount.get(channelStr), 1000, exception, "Timed out waiting for channel count to reach 5");
}
awaitCountdown(releaseComplete, 2000, exception, "Timed out while waiting for channels to be released");
connectionPool.shutdown();
}
use of com.github.ambry.config.ConnectionPoolConfig in project ambry by linkedin.
the class BlockingChannelConnectionPoolTest method testConnectionFailureCases.
/**
* Tests how connection failures are handled by BlockingChannelInfo.
*/
@Test
public void testConnectionFailureCases() throws InterruptedException, ConnectionPoolTimeoutException, IOException {
int port = 6680;
String host = "127.0.0.1";
SocketServer server = startServer(port);
Properties props = new Properties();
props.setProperty("clustermap.cluster.name", "test");
props.setProperty("clustermap.datacenter.name", "dc1");
props.setProperty("clustermap.host.name", "localhost");
BlockingChannelInfo channelInfo = new BlockingChannelInfo(new ConnectionPoolConfig(new VerifiableProperties(props)), host, new Port(port, PortType.PLAINTEXT), new MetricRegistry(), sslSocketFactory, sslConfig);
// ask for N no of connections
Assert.assertEquals(channelInfo.getNumberOfConnections(), 0);
BlockingChannel blockingChannel1 = channelInfo.getBlockingChannel(1000);
Assert.assertEquals(channelInfo.getNumberOfConnections(), 1);
BlockingChannel blockingChannel2 = channelInfo.getBlockingChannel(1000);
Assert.assertEquals(channelInfo.getNumberOfConnections(), 2);
BlockingChannel blockingChannel3 = channelInfo.getBlockingChannel(1000);
Assert.assertEquals(channelInfo.getNumberOfConnections(), 3);
// realease 2 of them back to pool
channelInfo.releaseBlockingChannel(blockingChannel2);
channelInfo.releaseBlockingChannel(blockingChannel3);
Assert.assertEquals("Available connections count mismatch ", 2, channelInfo.availableConnections.getValue().intValue());
// shutdown server
server.shutdown();
// destroy one of the connections and verify that the available connections cleaned up
channelInfo.destroyBlockingChannel(blockingChannel1);
Assert.assertEquals("Available connections should have not been cleaned up", 0, channelInfo.availableConnections.getValue().intValue());
// bring up the server
startServer(port);
// ask for 2 more connections
BlockingChannel blockingChannel4 = channelInfo.getBlockingChannel(1000);
Assert.assertEquals(channelInfo.getNumberOfConnections(), 1);
BlockingChannel blockingChannel5 = channelInfo.getBlockingChannel(1000);
Assert.assertEquals(channelInfo.getNumberOfConnections(), 2);
// release one of them back to pool
channelInfo.releaseBlockingChannel(blockingChannel4);
// verify that destroy connection will not trigger clean up of available connections
// as connection recreation should have passed
channelInfo.destroyBlockingChannel(blockingChannel5);
Assert.assertEquals("Available connections should not have been cleaned up", 2, channelInfo.availableConnections.getValue().intValue());
}
use of com.github.ambry.config.ConnectionPoolConfig in project ambry by linkedin.
the class BlockingChannelConnectionPoolTest method createAndReleaseSingleChannelTest.
private void createAndReleaseSingleChannelTest(Properties props, String host, Port port) throws InterruptedException, ConnectionPoolTimeoutException {
BlockingChannelInfo channelInfo = new BlockingChannelInfo(new ConnectionPoolConfig(new VerifiableProperties(props)), host, port, new MetricRegistry(), sslSocketFactory, sslConfig);
Assert.assertEquals(channelInfo.getNumberOfConnections(), 0);
BlockingChannel blockingChannel = channelInfo.getBlockingChannel(1000);
Assert.assertEquals(channelInfo.getNumberOfConnections(), 1);
channelInfo.releaseBlockingChannel(blockingChannel);
Assert.assertEquals(channelInfo.getNumberOfConnections(), 1);
}
use of com.github.ambry.config.ConnectionPoolConfig in project ambry by linkedin.
the class ServerTestUtil method endToEndReplicationWithMultiNodeMultiPartitionMultiDCTest.
static void endToEndReplicationWithMultiNodeMultiPartitionMultiDCTest(String sourceDatacenter, String sslEnabledDatacenters, PortType portType, MockCluster cluster, MockNotificationSystem notificationSystem, Properties routerProps) throws Exception {
Properties props = new Properties();
props.setProperty("router.hostname", "localhost");
props.setProperty("router.datacenter.name", sourceDatacenter);
props.setProperty("router.put.request.parallelism", "1");
props.setProperty("router.put.success.target", "1");
props.setProperty("clustermap.cluster.name", "test");
props.setProperty("clustermap.datacenter.name", sourceDatacenter);
props.setProperty("clustermap.host.name", "localhost");
props.setProperty("kms.default.container.key", TestUtils.getRandomKey(32));
props.putAll(routerProps);
VerifiableProperties verifiableProperties = new VerifiableProperties(props);
AccountService accountService = new InMemAccountService(false, true);
Router router = new NonBlockingRouterFactory(verifiableProperties, cluster.getClusterMap(), notificationSystem, getSSLFactoryIfRequired(verifiableProperties), accountService).getRouter();
int numberOfRequestsToSend = 15;
int numberOfVerifierThreads = 3;
final LinkedBlockingQueue<Payload> payloadQueue = new LinkedBlockingQueue<Payload>();
final AtomicReference<Exception> exceptionRef = new AtomicReference<>(null);
final CountDownLatch callbackLatch = new CountDownLatch(numberOfRequestsToSend);
List<Future<String>> putFutures = new ArrayList<>(numberOfRequestsToSend);
short accountId = Utils.getRandomShort(TestUtils.RANDOM);
short containerId = Utils.getRandomShort(TestUtils.RANDOM);
for (int i = 0; i < numberOfRequestsToSend; i++) {
int size = new Random().nextInt(5000);
final BlobProperties properties = new BlobProperties(size, "service1", "owner id check", "image/jpeg", false, TestUtils.TTL_SECS, cluster.time.milliseconds(), accountId, containerId, false, null, null, null);
final byte[] metadata = new byte[new Random().nextInt(1000)];
final byte[] blob = new byte[size];
TestUtils.RANDOM.nextBytes(metadata);
TestUtils.RANDOM.nextBytes(blob);
Future<String> future = router.putBlob(properties, metadata, new ByteBufferReadableStreamChannel(ByteBuffer.wrap(blob)), new PutBlobOptionsBuilder().build(), new Callback<String>() {
@Override
public void onCompletion(String result, Exception exception) {
if (exception == null) {
payloadQueue.add(new Payload(properties, metadata, blob, result));
} else {
exceptionRef.set(exception);
}
callbackLatch.countDown();
}
}, QUOTA_CHARGE_EVENT_LISTENER);
putFutures.add(future);
}
for (Future<String> future : putFutures) {
future.get(20, TimeUnit.SECONDS);
}
assertTrue("Did not receive all callbacks in time", callbackLatch.await(1, TimeUnit.SECONDS));
if (exceptionRef.get() != null) {
throw exceptionRef.get();
}
// put away for future use
Payload payload1 = payloadQueue.peek();
MockClusterMap clusterMap = cluster.getClusterMap();
BlobId blobId1 = new BlobId(payload1.blobId, clusterMap);
assertEquals("Did not put expected number of blobs", numberOfRequestsToSend, payloadQueue.size());
Properties sslProps = new Properties();
sslProps.putAll(routerProps);
sslProps.setProperty("clustermap.ssl.enabled.datacenters", sslEnabledDatacenters);
sslProps.setProperty("clustermap.cluster.name", "test");
sslProps.setProperty("clustermap.datacenter.name", sourceDatacenter);
sslProps.setProperty("clustermap.host.name", "localhost");
sslProps.setProperty("connectionpool.read.timeout.ms", "15000");
VerifiableProperties vProps = new VerifiableProperties(sslProps);
ConnectionPool connectionPool = new BlockingChannelConnectionPool(new ConnectionPoolConfig(vProps), new SSLConfig(vProps), new ClusterMapConfig(vProps), new MetricRegistry());
CountDownLatch verifierLatch = new CountDownLatch(numberOfVerifierThreads);
AtomicInteger totalRequests = new AtomicInteger(numberOfRequestsToSend);
AtomicInteger verifiedRequests = new AtomicInteger(0);
AtomicBoolean cancelTest = new AtomicBoolean(false);
for (int i = 0; i < numberOfVerifierThreads; i++) {
Thread thread = new Thread(new Verifier(payloadQueue, verifierLatch, totalRequests, verifiedRequests, cluster.getClusterMap(), cancelTest, portType, connectionPool, notificationSystem, cluster.time));
thread.start();
}
assertTrue("Did not verify in 2 minutes", verifierLatch.await(2, TimeUnit.MINUTES));
assertEquals(totalRequests.get(), verifiedRequests.get());
BlobIdFactory blobIdFactory = new BlobIdFactory(clusterMap);
MockDataNodeId dataNodeId = clusterMap.getDataNodes().get(0);
Port port = new Port(portType == PortType.PLAINTEXT ? dataNodeId.getPort() : dataNodeId.getSSLPort(), portType);
ConnectedChannel channel = connectionPool.checkOutConnection("localhost", port, 10000);
PartitionId partitionId = blobId1.getPartition();
// stop the store via AdminRequest
System.out.println("Begin to stop a BlobStore");
AdminRequest adminRequest = new AdminRequest(AdminRequestOrResponseType.BlobStoreControl, partitionId, 1, "clientid2");
BlobStoreControlAdminRequest controlRequest = new BlobStoreControlAdminRequest((short) 0, BlobStoreControlAction.StopStore, adminRequest);
DataInputStream stream = channel.sendAndReceive(controlRequest).getInputStream();
AdminResponse adminResponse = AdminResponse.readFrom(stream);
releaseNettyBufUnderneathStream(stream);
assertEquals("Stop store admin request should succeed", ServerErrorCode.No_Error, adminResponse.getError());
// put a blob on a stopped store, which should fail
byte[] userMetadata = new byte[1000];
byte[] data = new byte[3187];
BlobProperties properties = new BlobProperties(3187, "serviceid1", accountId, containerId, false, cluster.time.milliseconds());
BlobId blobId2 = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, clusterMap.getLocalDatacenterId(), accountId, containerId, partitionId, false, BlobId.BlobDataType.DATACHUNK);
PutRequest putRequest2 = new PutRequest(1, "clientId2", blobId2, properties, ByteBuffer.wrap(userMetadata), Unpooled.wrappedBuffer(data), properties.getBlobSize(), BlobType.DataBlob, null);
DataInputStream putResponseStream = channel.sendAndReceive(putRequest2).getInputStream();
PutResponse response2 = PutResponse.readFrom(putResponseStream);
releaseNettyBufUnderneathStream(putResponseStream);
assertEquals("Put blob on stopped store should fail", ServerErrorCode.Replica_Unavailable, response2.getError());
// get a blob properties on a stopped store, which should fail
ArrayList<BlobId> ids = new ArrayList<>();
ids.add(blobId1);
ArrayList<PartitionRequestInfo> partitionRequestInfoList = new ArrayList<>();
PartitionRequestInfo partitionRequestInfo = new PartitionRequestInfo(partitionId, ids);
partitionRequestInfoList.add(partitionRequestInfo);
GetRequest getRequest1 = new GetRequest(1, "clientId1", MessageFormatFlags.BlobProperties, partitionRequestInfoList, GetOption.None);
stream = channel.sendAndReceive(getRequest1).getInputStream();
GetResponse resp1 = GetResponse.readFrom(stream, clusterMap);
assertEquals("Get blob properties on stopped store should fail", ServerErrorCode.Replica_Unavailable, resp1.getPartitionResponseInfoList().get(0).getErrorCode());
releaseNettyBufUnderneathStream(stream);
// delete a blob on a stopped store, which should fail
DeleteRequest deleteRequest = new DeleteRequest(1, "clientId1", blobId1, System.currentTimeMillis());
stream = channel.sendAndReceive(deleteRequest).getInputStream();
DeleteResponse deleteResponse = DeleteResponse.readFrom(stream);
releaseNettyBufUnderneathStream(stream);
assertEquals("Delete blob on stopped store should fail", ServerErrorCode.Replica_Unavailable, deleteResponse.getError());
// start the store via AdminRequest
System.out.println("Begin to restart the BlobStore");
adminRequest = new AdminRequest(AdminRequestOrResponseType.BlobStoreControl, partitionId, 1, "clientId");
controlRequest = new BlobStoreControlAdminRequest((short) 0, BlobStoreControlAction.StartStore, adminRequest);
stream = channel.sendAndReceive(controlRequest).getInputStream();
adminResponse = AdminResponse.readFrom(stream);
releaseNettyBufUnderneathStream(stream);
assertEquals("Start store admin request should succeed", ServerErrorCode.No_Error, adminResponse.getError());
List<? extends ReplicaId> replicaIds = partitionId.getReplicaIds();
for (ReplicaId replicaId : replicaIds) {
// forcibly mark replicas and disks as up.
MockReplicaId mockReplicaId = (MockReplicaId) replicaId;
mockReplicaId.markReplicaDownStatus(false);
((MockDiskId) mockReplicaId.getDiskId()).setDiskState(HardwareState.AVAILABLE, false);
}
// put a blob on a restarted store , which should succeed
putRequest2 = new PutRequest(1, "clientId2", blobId2, properties, ByteBuffer.wrap(userMetadata), Unpooled.wrappedBuffer(data), properties.getBlobSize(), BlobType.DataBlob, null);
putResponseStream = channel.sendAndReceive(putRequest2).getInputStream();
response2 = PutResponse.readFrom(putResponseStream);
releaseNettyBufUnderneathStream(putResponseStream);
assertEquals("Put blob on restarted store should succeed", ServerErrorCode.No_Error, response2.getError());
// verify the put blob has been replicated successfully.
notificationSystem.awaitBlobCreations(blobId2.getID());
// get a blob on a restarted store , which should succeed
ids = new ArrayList<BlobId>();
ids.add(blobId2);
partitionRequestInfoList = new ArrayList<PartitionRequestInfo>();
partitionRequestInfo = new PartitionRequestInfo(partitionId, ids);
partitionRequestInfoList.add(partitionRequestInfo);
GetRequest getRequest2 = new GetRequest(1, "clientId2", MessageFormatFlags.All, partitionRequestInfoList, GetOption.None);
stream = channel.sendAndReceive(getRequest2).getInputStream();
GetResponse resp2 = GetResponse.readFrom(stream, clusterMap);
InputStream responseStream = resp2.getInputStream();
BlobAll blobAll = MessageFormatRecord.deserializeBlobAll(responseStream, blobIdFactory);
byte[] actualBlobData = getBlobDataAndRelease(blobAll.getBlobData());
assertArrayEquals("Content mismatch.", data, actualBlobData);
releaseNettyBufUnderneathStream(stream);
// delete a blob on a restarted store , which should succeed
deleteRequest = new DeleteRequest(1, "clientId2", blobId2, System.currentTimeMillis());
stream = channel.sendAndReceive(deleteRequest).getInputStream();
deleteResponse = DeleteResponse.readFrom(stream);
releaseNettyBufUnderneathStream(stream);
assertEquals("Delete blob on restarted store should succeed", ServerErrorCode.No_Error, deleteResponse.getError());
router.close();
connectionPool.shutdown();
}
Aggregations