use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class CloudTokenPersistorTest method basicTest.
@Test
public void basicTest() throws Exception {
Properties props = VcrTestUtil.createVcrProperties("DC1", "vcrClusterName", "zkConnectString", 12310, 12410, 12510, null);
props.setProperty("replication.cloud.token.factory", replicationCloudTokenFactory);
CloudConfig cloudConfig = new CloudConfig(new VerifiableProperties(props));
ClusterMapConfig clusterMapConfig = new ClusterMapConfig(new VerifiableProperties(props));
ClusterMap clusterMap = new MockClusterMap();
DataNodeId dataNodeId = new CloudDataNode(cloudConfig, clusterMapConfig);
Map<String, Set<PartitionInfo>> mountPathToPartitionInfoList = new HashMap<>();
BlobIdFactory blobIdFactory = new BlobIdFactory(clusterMap);
StoreFindTokenFactory factory = new StoreFindTokenFactory(blobIdFactory);
PartitionId partitionId = clusterMap.getAllPartitionIds(null).get(0);
ReplicaId cloudReplicaId = new CloudReplica(partitionId, dataNodeId);
List<? extends ReplicaId> peerReplicas = cloudReplicaId.getPeerReplicaIds();
List<RemoteReplicaInfo> remoteReplicas = new ArrayList<RemoteReplicaInfo>();
List<RemoteReplicaInfo.ReplicaTokenInfo> replicaTokenInfos = new ArrayList<>();
for (ReplicaId remoteReplica : peerReplicas) {
RemoteReplicaInfo remoteReplicaInfo = new RemoteReplicaInfo(remoteReplica, cloudReplicaId, null, factory.getNewFindToken(), 10, SystemTime.getInstance(), remoteReplica.getDataNodeId().getPortToConnectTo());
remoteReplicas.add(remoteReplicaInfo);
replicaTokenInfos.add(new RemoteReplicaInfo.ReplicaTokenInfo(remoteReplicaInfo));
}
PartitionInfo partitionInfo = new PartitionInfo(remoteReplicas, partitionId, null, cloudReplicaId);
mountPathToPartitionInfoList.computeIfAbsent(cloudReplicaId.getMountPath(), key -> ConcurrentHashMap.newKeySet()).add(partitionInfo);
LatchBasedInMemoryCloudDestination cloudDestination = new LatchBasedInMemoryCloudDestination(Collections.emptyList(), AzureCloudDestinationFactory.getReplicationFeedType(new VerifiableProperties(props)), clusterMap);
ReplicationConfig replicationConfig = new ReplicationConfig(new VerifiableProperties(props));
CloudTokenPersistor cloudTokenPersistor = new CloudTokenPersistor("replicaTokens", mountPathToPartitionInfoList, new ReplicationMetrics(new MetricRegistry(), Collections.emptyList()), clusterMap, new FindTokenHelper(blobIdFactory, replicationConfig), cloudDestination);
cloudTokenPersistor.persist(cloudReplicaId.getMountPath(), replicaTokenInfos);
List<RemoteReplicaInfo.ReplicaTokenInfo> retrievedReplicaTokenInfos = cloudTokenPersistor.retrieve(cloudReplicaId.getMountPath());
Assert.assertEquals("Number of tokens doesn't match.", replicaTokenInfos.size(), retrievedReplicaTokenInfos.size());
for (int i = 0; i < replicaTokenInfos.size(); i++) {
Assert.assertArrayEquals("Token is not correct.", replicaTokenInfos.get(i).getReplicaToken().toBytes(), retrievedReplicaTokenInfos.get(i).getReplicaToken().toBytes());
}
}
use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class FrontendRestRequestServiceFactoryTest method getFrontendRestRequestServiceTest.
/**
* Tests the instantiation of an {@link FrontendRestRequestService} instance through the
* {@link FrontendRestRequestServiceFactory}.
* @throws Exception
*/
@Test
public void getFrontendRestRequestServiceTest() throws Exception {
// dud properties. server should pick up defaults
JSONObject jsonObject = new JSONObject().put("POST", "http://uploadUrl:15158").put("GET", "http://downloadUrl:15158");
Properties properties = new Properties();
CommonTestUtils.populateRequiredRouterProps(properties);
properties.setProperty(FrontendConfig.URL_SIGNER_ENDPOINTS, jsonObject.toString());
properties.setProperty("clustermap.cluster.name", "Cluster-Name");
properties.setProperty("clustermap.datacenter.name", "Datacenter-Name");
properties.setProperty("clustermap.host.name", "localhost");
VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
FrontendRestRequestServiceFactory frontendRestRequestServiceFactory = new FrontendRestRequestServiceFactory(verifiableProperties, new MockClusterMap(), new InMemoryRouter(verifiableProperties, new MockClusterMap()), new InMemAccountService(false, true));
RestRequestService ambryRestRequestService = frontendRestRequestServiceFactory.getRestRequestService();
assertNotNull("No RestRequestService returned", ambryRestRequestService);
assertEquals("Did not receive an FrontendRestRequestService instance", FrontendRestRequestService.class.getCanonicalName(), ambryRestRequestService.getClass().getCanonicalName());
}
use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class RequestResponseTest method ttlUpdateRequestResponseTest.
/**
* Tests for {@link TtlUpdateRequest} and {@link TtlUpdateResponse}.
* @throws IOException
*/
@Test
public void ttlUpdateRequestResponseTest() throws IOException {
MockClusterMap clusterMap = new MockClusterMap();
int correlationId = TestUtils.RANDOM.nextInt();
long opTimeMs = Utils.getRandomLong(TestUtils.RANDOM, Long.MAX_VALUE);
long expiresAtMs = Utils.getRandomLong(TestUtils.RANDOM, Long.MAX_VALUE);
short accountId = Utils.getRandomShort(TestUtils.RANDOM);
short containerId = Utils.getRandomShort(TestUtils.RANDOM);
BlobId id1 = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, accountId, containerId, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK);
short[] versions = new short[] { TtlUpdateRequest.TTL_UPDATE_REQUEST_VERSION_1 };
for (short version : versions) {
TtlUpdateRequest ttlUpdateRequest = new TtlUpdateRequest(correlationId, "client", id1, expiresAtMs, opTimeMs, version);
DataInputStream requestStream = serAndPrepForRead(ttlUpdateRequest, -1, true);
TtlUpdateRequest deserializedTtlUpdateRequest = TtlUpdateRequest.readFrom(requestStream, clusterMap);
Assert.assertEquals("Request type mismatch", RequestOrResponseType.TtlUpdateRequest, deserializedTtlUpdateRequest.getRequestType());
Assert.assertEquals("Correlation ID mismatch", correlationId, deserializedTtlUpdateRequest.getCorrelationId());
Assert.assertEquals("Client ID mismatch", "client", deserializedTtlUpdateRequest.getClientId());
Assert.assertEquals("Blob ID mismatch", id1, deserializedTtlUpdateRequest.getBlobId());
Assert.assertEquals("AccountId mismatch ", id1.getAccountId(), deserializedTtlUpdateRequest.getAccountId());
Assert.assertEquals("ContainerId mismatch ", id1.getContainerId(), deserializedTtlUpdateRequest.getContainerId());
Assert.assertEquals("ExpiresAtMs mismatch ", expiresAtMs, deserializedTtlUpdateRequest.getExpiresAtMs());
Assert.assertEquals("DeletionTime mismatch ", opTimeMs, deserializedTtlUpdateRequest.getOperationTimeInMs());
ttlUpdateRequest.release();
TtlUpdateResponse response = new TtlUpdateResponse(correlationId, "client", ServerErrorCode.No_Error);
requestStream = serAndPrepForRead(response, -1, false);
TtlUpdateResponse deserializedTtlUpdateResponse = TtlUpdateResponse.readFrom(requestStream);
Assert.assertEquals("Response type mismatch", RequestOrResponseType.TtlUpdateResponse, deserializedTtlUpdateResponse.getRequestType());
Assert.assertEquals("Correlation ID mismatch", correlationId, deserializedTtlUpdateResponse.getCorrelationId());
Assert.assertEquals("Client ID mismatch", "client", deserializedTtlUpdateResponse.getClientId());
Assert.assertEquals("Server error code mismatch", ServerErrorCode.No_Error, deserializedTtlUpdateResponse.getError());
response.release();
}
}
use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class RequestResponseTest method doBlobStoreControlAdminRequestTest.
/**
* Does the actual test of ser/de of {@link BlobStoreControlAdminRequest} and checks for equality of fields with
* reference data.
* @param storeControlRequestType the type of store control request specified in {@link BlobStoreControlAdminRequest}.
* @throws IOException
*/
private void doBlobStoreControlAdminRequestTest(BlobStoreControlAction storeControlRequestType) throws IOException {
MockClusterMap clusterMap = new MockClusterMap();
PartitionId id = clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0);
int correlationId = 1234;
String clientId = "client";
// test BlobStore Control request
short numCaughtUpPerPartition = Utils.getRandomShort(TestUtils.RANDOM);
AdminRequest adminRequest = new AdminRequest(AdminRequestOrResponseType.BlobStoreControl, id, correlationId, clientId);
BlobStoreControlAdminRequest blobStoreControlAdminRequest = new BlobStoreControlAdminRequest(numCaughtUpPerPartition, storeControlRequestType, adminRequest);
DataInputStream requestStream = serAndPrepForRead(blobStoreControlAdminRequest, -1, true);
AdminRequest deserializedAdminRequest = deserAdminRequestAndVerify(requestStream, clusterMap, correlationId, clientId, AdminRequestOrResponseType.BlobStoreControl, id);
BlobStoreControlAdminRequest deserializedBlobStoreControlRequest = BlobStoreControlAdminRequest.readFrom(requestStream, deserializedAdminRequest);
Assert.assertEquals("Num caught up per partition not as set", numCaughtUpPerPartition, deserializedBlobStoreControlRequest.getNumReplicasCaughtUpPerPartition());
Assert.assertEquals("Control request type is not expected", storeControlRequestType, deserializedBlobStoreControlRequest.getStoreControlAction());
// test toString method
String correctString = "BlobStoreControlAdminRequest[ClientId=" + clientId + ", CorrelationId=" + correlationId + ", BlobStoreControlAction=" + deserializedBlobStoreControlRequest.getStoreControlAction() + ", NumReplicasCaughtUpPerPartition=" + deserializedBlobStoreControlRequest.getNumReplicasCaughtUpPerPartition() + ", PartitionId=" + deserializedBlobStoreControlRequest.getPartitionId() + "]";
Assert.assertEquals("The test of toString method fails", correctString, "" + deserializedBlobStoreControlRequest);
blobStoreControlAdminRequest.release();
}
use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class RequestResponseTest method doReplicaMetadataRequestTest.
private void doReplicaMetadataRequestTest(short responseVersionToUse, short requestVersionToUse, short messageInfoToUse, ReplicaType replicaType) throws IOException {
MessageInfoAndMetadataListSerde.AUTO_VERSION = messageInfoToUse;
MockClusterMap clusterMap = new MockClusterMap();
List<ReplicaMetadataRequestInfo> replicaMetadataRequestInfoList = new ArrayList<ReplicaMetadataRequestInfo>();
ReplicaMetadataRequestInfo replicaMetadataRequestInfo = new ReplicaMetadataRequestInfo(new MockPartitionId(), new MockFindToken(0, 1000), "localhost", "path", replicaType, requestVersionToUse);
replicaMetadataRequestInfoList.add(replicaMetadataRequestInfo);
ReplicaMetadataRequest request = new ReplicaMetadataRequest(1, "id", replicaMetadataRequestInfoList, 1000, requestVersionToUse);
DataInputStream requestStream = serAndPrepForRead(request, -1, true);
ReplicaMetadataRequest replicaMetadataRequestFromBytes = ReplicaMetadataRequest.readFrom(requestStream, new MockClusterMap(), new MockFindTokenHelper());
Assert.assertEquals(replicaMetadataRequestFromBytes.getMaxTotalSizeOfEntriesInBytes(), 1000);
Assert.assertEquals(replicaMetadataRequestFromBytes.getReplicaMetadataRequestInfoList().size(), 1);
request.release();
try {
new ReplicaMetadataRequest(1, "id", null, 12, requestVersionToUse);
Assert.fail("Serializing should have failed");
} catch (IllegalArgumentException e) {
// expected. Nothing to do
}
try {
new ReplicaMetadataRequestInfo(new MockPartitionId(), null, "localhost", "path", replicaType, requestVersionToUse);
Assert.fail("Construction should have failed");
} catch (IllegalArgumentException e) {
// expected. Nothing to do
}
long operationTimeMs = SystemTime.getInstance().milliseconds() + TestUtils.RANDOM.nextInt();
int numResponseInfos = 5;
int numMessagesInEachResponseInfo = 200;
List<ReplicaMetadataResponseInfo> replicaMetadataResponseInfoList = new ArrayList<>();
for (int j = 0; j < numResponseInfos; j++) {
List<MessageInfo> messageInfoList = new ArrayList<MessageInfo>();
int totalSizeOfAllMessages = 0;
for (int i = 0; i < numMessagesInEachResponseInfo; i++) {
int msgSize = TestUtils.RANDOM.nextInt(1000) + 1;
short accountId = Utils.getRandomShort(TestUtils.RANDOM);
short containerId = Utils.getRandomShort(TestUtils.RANDOM);
BlobId id = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, accountId, containerId, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK);
MessageInfo messageInfo = new MessageInfo(id, msgSize, false, false, true, Utils.Infinite_Time, null, accountId, containerId, operationTimeMs, (short) 1);
messageInfoList.add(messageInfo);
totalSizeOfAllMessages += msgSize;
}
ReplicaMetadataResponseInfo responseInfo = new ReplicaMetadataResponseInfo(clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), replicaType, new MockFindToken(0, 1000), messageInfoList, 1000, responseVersionToUse);
Assert.assertEquals("Total size of messages not as expected", totalSizeOfAllMessages, responseInfo.getTotalSizeOfAllMessages());
replicaMetadataResponseInfoList.add(responseInfo);
}
ReplicaMetadataResponse response = new ReplicaMetadataResponse(1234, "clientId", ServerErrorCode.No_Error, replicaMetadataResponseInfoList, responseVersionToUse);
requestStream = serAndPrepForRead(response, -1, false);
ReplicaMetadataResponse deserializedReplicaMetadataResponse = ReplicaMetadataResponse.readFrom(requestStream, new MockFindTokenHelper(), clusterMap);
Assert.assertEquals(deserializedReplicaMetadataResponse.getCorrelationId(), 1234);
Assert.assertEquals(deserializedReplicaMetadataResponse.getError(), ServerErrorCode.No_Error);
Assert.assertEquals("ReplicaMetadataResponse list size mismatch ", numResponseInfos, deserializedReplicaMetadataResponse.getReplicaMetadataResponseInfoList().size());
for (int j = 0; j < replicaMetadataResponseInfoList.size(); j++) {
ReplicaMetadataResponseInfo originalMetadataResponse = replicaMetadataResponseInfoList.get(j);
ReplicaMetadataResponseInfo replicaMetadataResponseInfo = deserializedReplicaMetadataResponse.getReplicaMetadataResponseInfoList().get(j);
Assert.assertEquals("MsgInfo list size in ReplicaMetadataResponse mismatch ", numMessagesInEachResponseInfo, replicaMetadataResponseInfo.getMessageInfoList().size());
Assert.assertEquals("Total size of messages not as expected", originalMetadataResponse.getTotalSizeOfAllMessages(), replicaMetadataResponseInfo.getTotalSizeOfAllMessages());
List<MessageInfo> deserializedMsgInfoList = replicaMetadataResponseInfo.getMessageInfoList();
for (int i = 0; i < originalMetadataResponse.getMessageInfoList().size(); i++) {
MessageInfo originalMsgInfo = originalMetadataResponse.getMessageInfoList().get(i);
MessageInfo msgInfo = deserializedMsgInfoList.get(i);
Assert.assertEquals("MsgInfo size mismatch ", originalMsgInfo.getSize(), msgInfo.getSize());
Assert.assertEquals("MsgInfo key mismatch ", originalMsgInfo.getStoreKey(), msgInfo.getStoreKey());
Assert.assertEquals("MsgInfo expiration value mismatch ", Utils.Infinite_Time, msgInfo.getExpirationTimeInMs());
if (response.getVersionId() >= ReplicaMetadataResponse.REPLICA_METADATA_RESPONSE_VERSION_V_3) {
Assert.assertEquals("AccountId mismatch ", originalMsgInfo.getAccountId(), msgInfo.getAccountId());
Assert.assertEquals("ContainerId mismatch ", originalMsgInfo.getContainerId(), msgInfo.getContainerId());
Assert.assertEquals("OperationTime mismatch ", operationTimeMs, msgInfo.getOperationTimeMs());
} else {
Assert.assertEquals("AccountId mismatch ", UNKNOWN_ACCOUNT_ID, msgInfo.getAccountId());
Assert.assertEquals("ContainerId mismatch ", UNKNOWN_CONTAINER_ID, msgInfo.getContainerId());
Assert.assertEquals("OperationTime mismatch ", Utils.Infinite_Time, msgInfo.getOperationTimeMs());
}
if (messageInfoToUse >= MessageInfoAndMetadataListSerde.VERSION_6) {
Assert.assertTrue(msgInfo.isUndeleted());
Assert.assertEquals("LifeVersion mismatch", (short) 1, msgInfo.getLifeVersion());
} else {
Assert.assertFalse(msgInfo.isUndeleted());
Assert.assertEquals("LifeVersion mismatch", (short) 0, msgInfo.getLifeVersion());
}
}
}
response.release();
// to ensure that the toString() representation does not go overboard, a random bound check is executed here.
// a rough estimate is that each response info should contribute about 500 chars to the toString() representation
int maxLength = 100 + numResponseInfos * 500;
Assert.assertTrue("toString() representation longer than " + maxLength + " characters", response.toString().length() < maxLength);
// test toString() of a ReplicaMetadataResponseInfo without any messages
ReplicaMetadataResponseInfo responseInfo = new ReplicaMetadataResponseInfo(clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), replicaType, new MockFindToken(0, 1000), Collections.emptyList(), 1000, responseVersionToUse);
Assert.assertTrue("Length of toString() should be > 0", responseInfo.toString().length() > 0);
// test toString() of a ReplicaMetadataResponse without any ReplicaMetadataResponseInfo
response = new ReplicaMetadataResponse(1234, "clientId", ServerErrorCode.No_Error, Collections.emptyList(), responseVersionToUse);
Assert.assertTrue("Length of toString() should be > 0", response.toString().length() > 0);
response.release();
}
Aggregations