use of com.github.ambry.commons.BlobId in project ambry by linkedin.
the class BlobIdTest method testBlobIdFlag.
/**
* Test that the BlobId flag is honored by V3 and above.
* @throws Exception
*/
@Test
public void testBlobIdFlag() throws Exception {
boolean[] isEncryptedValues = { true, false };
if (version >= BLOB_ID_V3) {
for (BlobIdType type : BlobIdType.values()) {
for (boolean isEncrypted : isEncryptedValues) {
BlobId blobId = new BlobId(version, type, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, isEncrypted, referenceDataType);
BlobId blobIdSerDed = new BlobId(new DataInputStream(new ByteArrayInputStream(blobId.toBytes())), referenceClusterMap);
assertEquals("The type should match the original's type", type, blobIdSerDed.getType());
assertEquals("The isEncrypted should match the original", version != BLOB_ID_V3 && isEncrypted, BlobId.isEncrypted(blobId.getID()));
}
}
}
if (version >= BLOB_ID_V5) {
for (BlobDataType dataType : BlobDataType.values()) {
BlobId blobId = new BlobId(version, BlobIdType.NATIVE, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, false, dataType);
BlobId blobIdSerDed = new BlobId(new DataInputStream(new ByteArrayInputStream(blobId.toBytes())), referenceClusterMap);
assertEquals("The data type should match the original's", dataType, blobIdSerDed.getBlobDataType());
}
}
}
use of com.github.ambry.commons.BlobId in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method doExternalServicesBadInputTest.
/**
* Does the tests to check for exception pipelining for exceptions returned/thrown by external services.
* @param restMethods the {@link RestMethod} types for which the test has to be run.
* @param expectedExceptionMsg the expected exception message.
* @param expectRouterCall if the router should have returned a value before the exception occurs.
* @throws JSONException
*/
private void doExternalServicesBadInputTest(RestMethod[] restMethods, String expectedExceptionMsg, boolean expectRouterCall) throws JSONException {
for (RestMethod restMethod : restMethods) {
if (restMethod.equals(RestMethod.UNKNOWN)) {
continue;
}
JSONObject headers = new JSONObject();
List<ByteBuffer> contents = null;
if (restMethod.equals(RestMethod.POST)) {
setAmbryHeadersForPut(headers, 7200, !refContainer.isCacheable(), "doExternalServicesBadInputTest", "application/octet-stream", "doExternalServicesBadInputTest", refAccount.getName(), refContainer.getName(), null);
contents = new ArrayList<>(1);
contents.add(null);
}
String blobIdStr = new BlobId(blobIdVersion, BlobId.BlobIdType.NATIVE, (byte) -1, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, clusterMap.getAllPartitionIds(null).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
try {
doOperation(createRestRequest(restMethod, blobIdStr, headers, contents), new MockRestResponseChannel());
fail("Operation " + restMethod + " should have failed because an external service would have thrown an exception");
} catch (Exception e) {
assertEquals("Unexpected exception message", expectedExceptionMsg, e.getMessage());
if (expectRouterCall && restMethod == RestMethod.GET) {
assertNotNull("expected router.getBlob() call to provide a response to responseHandler", responseHandler.getResponse());
}
}
}
}
use of com.github.ambry.commons.BlobId in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method updateTtlRejectedTest.
/**
* Tests the case when the TTL update is rejected
* @throws Exception
*/
@Test
public void updateTtlRejectedTest() throws Exception {
FrontendTestRouter testRouter = new FrontendTestRouter();
String exceptionMsg = TestUtils.getRandomString(10);
testRouter.exceptionToReturn = new RouterException(exceptionMsg, RouterErrorCode.BlobUpdateNotAllowed);
testRouter.exceptionOpType = FrontendTestRouter.OpType.UpdateBlobTtl;
frontendRestRequestService = new FrontendRestRequestService(frontendConfig, frontendMetrics, testRouter, clusterMap, idConverterFactory, securityServiceFactory, urlSigningService, idSigningService, null, accountService, accountAndContainerInjector, datacenterName, hostname, clusterName, accountStatsStore, QUOTA_MANAGER);
frontendRestRequestService.setupResponseHandler(responseHandler);
frontendRestRequestService.start();
String blobId = new BlobId(blobIdVersion, BlobId.BlobIdType.NATIVE, (byte) -1, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, clusterMap.getAllPartitionIds(null).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
JSONObject headers = new JSONObject();
setUpdateTtlHeaders(headers, blobId, "updateTtlRejectedTest");
RestRequest restRequest = createRestRequest(RestMethod.PUT, Operations.UPDATE_TTL, headers, null);
MockRestResponseChannel restResponseChannel = verifyOperationFailure(restRequest, RestServiceErrorCode.NotAllowed);
assertEquals("Unexpected response status", ResponseStatus.MethodNotAllowed, restResponseChannel.getStatus());
assertEquals("Unexpected value for the 'allow' header", FrontendRestRequestService.TTL_UPDATE_REJECTED_ALLOW_HEADER_VALUE, restResponseChannel.getHeader(RestUtils.Headers.ALLOW));
}
use of com.github.ambry.commons.BlobId in project ambry by linkedin.
the class FrontendUtilsTest method testGetBlobIdFromString.
/**
* Tests {@link FrontendUtils#getBlobIdFromString(String, ClusterMap)}
* @throws IOException
* @throws RestServiceException
*/
@Test
public void testGetBlobIdFromString() throws IOException, RestServiceException {
// good path
byte[] bytes = new byte[2];
ClusterMap referenceClusterMap = new MockClusterMap();
TestUtils.RANDOM.nextBytes(bytes);
BlobId.BlobIdType referenceType = TestUtils.RANDOM.nextBoolean() ? BlobId.BlobIdType.NATIVE : BlobId.BlobIdType.CRAFTED;
TestUtils.RANDOM.nextBytes(bytes);
byte referenceDatacenterId = bytes[0];
short referenceAccountId = getRandomShort(TestUtils.RANDOM);
short referenceContainerId = getRandomShort(TestUtils.RANDOM);
PartitionId referencePartitionId = referenceClusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0);
boolean referenceIsEncrypted = TestUtils.RANDOM.nextBoolean();
List<Short> versions = Arrays.stream(BlobId.getAllValidVersions()).filter(version -> version >= BlobId.BLOB_ID_V3).collect(Collectors.toList());
for (short version : versions) {
BlobId blobId = new BlobId(version, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted, BlobId.BlobDataType.DATACHUNK);
BlobId regeneratedBlobId = FrontendUtils.getBlobIdFromString(blobId.getID(), referenceClusterMap);
assertEquals("BlobId mismatch", blobId, regeneratedBlobId);
assertBlobIdFieldValues(regeneratedBlobId, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, version >= BlobId.BLOB_ID_V4 && referenceIsEncrypted);
// bad path
try {
FrontendUtils.getBlobIdFromString(blobId.getID().substring(1), referenceClusterMap);
fail("Should have thrown exception for bad blobId ");
} catch (RestServiceException e) {
assertEquals("RestServiceErrorCode mismatch", RestServiceErrorCode.BadRequest, e.getErrorCode());
}
}
}
use of com.github.ambry.commons.BlobId in project ambry by linkedin.
the class MessageInfoAndMetadataListSerde method deserializeMessageInfoAndMetadataList.
/**
* Deserialize the given stream and return the MessageInfo and Metadata lists.
* @param stream the stream to deserialize from.
* @param map the clustermap to use.
* @param versionToDeserializeIn the SerDe version to use to deserialize.
* @return the deserialized {@link MessageInfoAndMetadataListSerde}.
* @throws IOException if an I/O error occurs while reading from the stream.
*/
static MessageInfoAndMetadataListSerde deserializeMessageInfoAndMetadataList(DataInputStream stream, ClusterMap map, short versionToDeserializeIn) throws IOException {
if (versionToDeserializeIn >= VERSION_5) {
short versionFromStream = stream.readShort();
if (versionFromStream != versionToDeserializeIn) {
throw new IllegalArgumentException("Argument provided [" + versionToDeserializeIn + "] and stream [" + versionFromStream + "] disagree on version");
}
} else {
versionToDeserializeIn = versionToDeserializeIn == DETERMINE_VERSION ? stream.readShort() : versionToDeserializeIn;
}
int messageCount = stream.readInt();
ArrayList<MessageInfo> messageInfoList = new ArrayList<>(messageCount);
ArrayList<MessageMetadata> messageMetadataList = new ArrayList<>(messageCount);
for (int i = 0; i < messageCount; i++) {
BlobId id = new BlobId(stream, map);
long size = stream.readLong();
long ttl = stream.readLong();
boolean isDeleted = stream.readByte() == UPDATED;
boolean isTtlUpdated = false;
boolean isUndeleted = false;
short lifeVersion = 0;
Long crc = null;
short accountId = Account.UNKNOWN_ACCOUNT_ID;
short containerId = Container.UNKNOWN_CONTAINER_ID;
long operationTime = Utils.Infinite_Time;
if (versionToDeserializeIn < VERSION_1 || versionToDeserializeIn > VERSION_MAX) {
throw new IllegalArgumentException("Unknown version to deserialize MessageInfoList " + versionToDeserializeIn);
}
if (versionToDeserializeIn >= VERSION_5) {
isTtlUpdated = stream.readByte() == UPDATED;
}
if (versionToDeserializeIn >= VERSION_6) {
isUndeleted = stream.readByte() == UPDATED;
}
if (versionToDeserializeIn > VERSION_1) {
crc = stream.readByte() == FIELD_PRESENT ? stream.readLong() : null;
}
if (versionToDeserializeIn > VERSION_2) {
accountId = stream.readShort();
containerId = stream.readShort();
operationTime = stream.readLong();
}
if (versionToDeserializeIn >= VERSION_6) {
lifeVersion = stream.readShort();
}
messageInfoList.add(new MessageInfo(id, size, isDeleted, isTtlUpdated, isUndeleted, ttl, crc, accountId, containerId, operationTime, lifeVersion));
if (versionToDeserializeIn > VERSION_3) {
MessageMetadata messageMetadata = stream.readByte() == FIELD_PRESENT ? MessageMetadata.deserializeMessageMetadata(stream) : null;
messageMetadataList.add(messageMetadata);
} else {
messageMetadataList.add(null);
}
}
return new MessageInfoAndMetadataListSerde(messageInfoList, messageMetadataList, versionToDeserializeIn);
}
Aggregations