Search in sources :

Example 36 with BlobId

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());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) BlobId(com.github.ambry.commons.BlobId) Test(org.junit.Test)

Example 37 with BlobId

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());
            }
        }
    }
}
Also used : JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ByteBuffer(java.nio.ByteBuffer) BlobId(com.github.ambry.commons.BlobId) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RestServiceException(com.github.ambry.rest.RestServiceException) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AccountServiceException(com.github.ambry.account.AccountServiceException) RestMethod(com.github.ambry.rest.RestMethod)

Example 38 with BlobId

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));
}
Also used : RouterException(com.github.ambry.router.RouterException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) BlobId(com.github.ambry.commons.BlobId) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 39 with BlobId

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());
        }
    }
}
Also used : Container(com.github.ambry.account.Container) Arrays(java.util.Arrays) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) ClusterMap(com.github.ambry.clustermap.ClusterMap) IOException(java.io.IOException) Test(org.junit.Test) Utils(com.github.ambry.utils.Utils) Collectors(java.util.stream.Collectors) List(java.util.List) RestServiceException(com.github.ambry.rest.RestServiceException) TestUtils(com.github.ambry.utils.TestUtils) Account(com.github.ambry.account.Account) Assert(org.junit.Assert) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) PartitionId(com.github.ambry.clustermap.PartitionId) BlobId(com.github.ambry.commons.BlobId) ClusterMap(com.github.ambry.clustermap.ClusterMap) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) PartitionId(com.github.ambry.clustermap.PartitionId) RestServiceException(com.github.ambry.rest.RestServiceException) BlobId(com.github.ambry.commons.BlobId) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) Test(org.junit.Test)

Example 40 with BlobId

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);
}
Also used : ArrayList(java.util.ArrayList) MessageInfo(com.github.ambry.store.MessageInfo) MessageMetadata(com.github.ambry.messageformat.MessageMetadata) BlobId(com.github.ambry.commons.BlobId)

Aggregations

BlobId (com.github.ambry.commons.BlobId)192 ArrayList (java.util.ArrayList)83 Test (org.junit.Test)75 PartitionId (com.github.ambry.clustermap.PartitionId)52 VerifiableProperties (com.github.ambry.config.VerifiableProperties)45 BlobProperties (com.github.ambry.messageformat.BlobProperties)45 IOException (java.io.IOException)43 ByteBuffer (java.nio.ByteBuffer)43 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)40 MockPartitionId (com.github.ambry.clustermap.MockPartitionId)40 HashMap (java.util.HashMap)40 DataInputStream (java.io.DataInputStream)39 MessageInfo (com.github.ambry.store.MessageInfo)36 List (java.util.List)34 Properties (java.util.Properties)34 CloudBlobMetadata (com.github.ambry.cloud.CloudBlobMetadata)31 Map (java.util.Map)30 GetResponse (com.github.ambry.protocol.GetResponse)28 PartitionRequestInfo (com.github.ambry.protocol.PartitionRequestInfo)28 StoreKey (com.github.ambry.store.StoreKey)26