Search in sources :

Example 21 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class FrontendIntegrationTest method stitchedUploadTest.

/**
 * Test the stitched (multipart) upload flow. This includes generating signed chunk upload URLs, uploading chunks to
 * that URL, calling the /stitch API to create a stitched blob, and performing get/head/ttlUpdate/delete operations on
 * the stitched blob.
 * @throws Exception
 */
@Test
public void stitchedUploadTest() throws Exception {
    Account account = ACCOUNT_SERVICE.createAndAddRandomAccount();
    Container container = account.getContainerById(Container.DEFAULT_PRIVATE_CONTAINER_ID);
    Pair<List<String>, byte[]> idsAndContent = uploadDataChunksAndVerify(account, container, 50, 50, 50, 50, 17);
    stitchBlobAndVerify(account, container, idsAndContent.getFirst(), idsAndContent.getSecond(), 217);
    idsAndContent = uploadDataChunksAndVerify(account, container, 167);
    stitchBlobAndVerify(account, container, idsAndContent.getFirst(), idsAndContent.getSecond(), 167);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 22 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class AccountAndContainerInjector method injectTargetAccountAndContainerFromBlobId.

/**
 * Obtains the target {@link Account} and {@link Container} id from the blobId string, queries the {@link AccountService}
 * to get the corresponding {@link Account} and {@link Container}, and injects the target {@link Account} and
 * {@link Container} into the {@link RestRequest}.
 * @param blobId The blobId to get the target {@link Account} and {@link Container} id.
 * @param restRequest The rest request to insert the target {@link Account} and {@link Container}.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException if 1) either {@link Account} or {@link Container} could not be found; or 2)
 *                              either {@link Account} or {@link Container} IDs were explicitly specified as
 *                              {@link Account#UNKNOWN_ACCOUNT_ID} or {@link Container#UNKNOWN_CONTAINER_ID}.
 */
public void injectTargetAccountAndContainerFromBlobId(BlobId blobId, RestRequest restRequest, RestRequestMetricsGroup metricsGroup) throws RestServiceException {
    Account targetAccount = accountService.getAccountById(blobId.getAccountId());
    if (targetAccount == null) {
        frontendMetrics.getHeadDeleteUnrecognizedAccountCount.inc();
        // @todo The check can be removed once HelixAccountService is running with UNKNOWN_ACCOUNT created.
        if (blobId.getAccountId() != Account.UNKNOWN_ACCOUNT_ID) {
            throw new RestServiceException("Account from blobId=" + blobId.getID() + "with accountId=" + blobId.getAccountId() + " cannot be recognized", RestServiceErrorCode.InvalidAccount);
        } else {
            logger.debug("Account cannot be found for blobId={} with accountId={}. Setting targetAccount to UNKNOWN_ACCOUNT", blobId.getID(), blobId.getAccountId());
            targetAccount = accountService.getAccountById(Account.UNKNOWN_ACCOUNT_ID);
        }
    }
    Container targetContainer;
    try {
        targetContainer = accountService.getContainerById(blobId.getAccountId(), blobId.getContainerId());
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container with Id= " + blobId.getContainerId() + " from account " + targetAccount.getName() + "for blobId=" + blobId.getID(), RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    if (targetContainer == null) {
        frontendMetrics.getHeadDeleteUnrecognizedContainerCount.inc();
        throw new RestServiceException("Container from blobId=" + blobId.getID() + "with accountId=" + blobId.getAccountId() + " containerId=" + blobId.getContainerId() + " cannot be recognized", RestServiceErrorCode.InvalidContainer);
    }
    setTargetAccountAndContainerInRestRequest(restRequest, targetAccount, targetContainer, metricsGroup);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) AccountServiceException(com.github.ambry.account.AccountServiceException) Container(com.github.ambry.account.Container)

Example 23 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class AccountAndContainerInjector method ensureAccountAndContainerInjected.

/**
 * If a non-unknown {@link Account} and {@link Container} was not previously injected, inject them into the provided
 * {@link RestRequest}, based on the given {@link BlobProperties}' service ID and blob privacy setting. This is useful
 * for V1 blob IDs that do not directly encode the account/container ID.
 * @param restRequest The {@link RestRequest} to inject {@link Account} and {@link Container}.
 * @param blobProperties The {@link BlobProperties} that contains the service id and blob privacy setting.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException if no valid account or container could be identified for re-injection.
 */
public void ensureAccountAndContainerInjected(RestRequest restRequest, BlobProperties blobProperties, RestRequestMetricsGroup metricsGroup) throws RestServiceException {
    Account targetAccount = (Account) restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY);
    Container targetContainer = (Container) restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY);
    if (targetAccount == null || targetContainer == null) {
        throw new RestServiceException("Account and container were not injected by RestRequestService", RestServiceErrorCode.InternalServerError);
    } else if (targetAccount.getId() == Account.UNKNOWN_ACCOUNT_ID) {
        // This should only occur for V1 blobs, where the blob ID does not contain the actual account and container IDs.
        String serviceId = blobProperties.getServiceId();
        boolean isPrivate = blobProperties.isPrivate();
        injectAccountAndContainerUsingServiceId(restRequest, serviceId, isPrivate, metricsGroup);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 24 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class AmbrySecurityServiceTest method testGetNotModifiedBlob.

/**
 * Tests {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for a Get blob
 * with the passed in {@link BlobInfo} for a not modified response
 * @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
 * @param ifModifiedSinceMs the value (as a date string) of the {@link RestUtils.Headers#IF_MODIFIED_SINCE} header.
 * @throws Exception
 */
private void testGetNotModifiedBlob(BlobInfo blobInfo, long ifModifiedSinceMs) throws Exception {
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    JSONObject headers = new JSONObject();
    SimpleDateFormat dateFormat = new SimpleDateFormat(RestUtils.HTTP_DATE_FORMAT, Locale.ENGLISH);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = new Date(ifModifiedSinceMs);
    String dateStr = dateFormat.format(date);
    headers.put(RestUtils.Headers.IF_MODIFIED_SINCE, dateStr);
    RestRequest restRequest = createRestRequest(RestMethod.GET, "/abc", headers);
    Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
    insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
    securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
    if (ifModifiedSinceMs >= blobInfo.getBlobProperties().getCreationTimeInMs()) {
        Assert.assertEquals("Not modified response expected", ResponseStatus.NotModified, restResponseChannel.getStatus());
        verifyHeadersForGetBlobNotModified(restResponseChannel, accountAndContainer.getSecond().isCacheable());
    } else {
        Assert.assertEquals("Not modified response should not be returned", ResponseStatus.Ok, restResponseChannel.getStatus());
        verifyHeadersForGetBlob(restRequest, blobInfo, accountAndContainer, null, restResponseChannel);
    }
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 25 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class PostAccountContainersHandlerTest method validRequestsTest.

/**
 * Test valid request cases.
 * @throws Exception
 */
@Test
public void validRequestsTest() throws Exception {
    String accountName = theAccount.getName();
    short accountId = theAccount.getId();
    ThrowingConsumer<Collection<Container>> testAction = inputContainers -> {
        String requestBody = new String(AccountCollectionSerde.serializeContainersInJson(inputContainers));
        RestResponseChannel restResponseChannel = new MockRestResponseChannel();
        RestRequest request = createRestRequest(requestBody, accountName, null);
        ReadableStreamChannel responseChannel = sendRequestGetResponse(request, restResponseChannel);
        assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
        assertEquals("Content-length is not as expected", responseChannel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
        assertEquals("Account id in response header is not as expected", accountId, Short.parseShort((String) restResponseChannel.getHeader(RestUtils.Headers.TARGET_ACCOUNT_ID)));
        RetainingAsyncWritableChannel asyncWritableChannel = new RetainingAsyncWritableChannel((int) responseChannel.getSize());
        responseChannel.readInto(asyncWritableChannel, null).get();
        Collection<Container> outputContainers = AccountCollectionSerde.containersFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream(), accountId);
        assertEquals("Unexpected count returned", inputContainers.size(), outputContainers.size());
        for (Container container : outputContainers) {
            assertEquals("Container in account service not as expected", container, accountService.getContainerByName(accountName, container.getName()));
        }
    };
    // add new container
    testAction.accept(Collections.singleton(accountService.getRandomContainer(accountId)));
    // add multiple containers
    List<Container> containerList = new ArrayList<>();
    for (int j = 0; j < 10; j++) {
        containerList.add(new ContainerBuilder(Container.UNKNOWN_CONTAINER_ID, "Test-" + j, Container.ContainerStatus.ACTIVE, "", accountId).build());
    }
    testAction.accept(containerList);
// TODO: update existing containers when support is added
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) FutureResult(com.github.ambry.router.FutureResult) ContainerBuilder(com.github.ambry.account.ContainerBuilder) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) ArrayList(java.util.ArrayList) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) LinkedList(java.util.LinkedList) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) Container(com.github.ambry.account.Container) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) ArrayList(java.util.ArrayList) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Container(com.github.ambry.account.Container) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ContainerBuilder(com.github.ambry.account.ContainerBuilder) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

Container (com.github.ambry.account.Container)119 Account (com.github.ambry.account.Account)88 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)30 RestServiceException (com.github.ambry.rest.RestServiceException)20 ContainerBuilder (com.github.ambry.account.ContainerBuilder)17 JSONObject (org.json.JSONObject)17 VerifiableProperties (com.github.ambry.config.VerifiableProperties)16 HashSet (java.util.HashSet)15 HashMap (java.util.HashMap)14 Properties (java.util.Properties)14 AccountBuilder (com.github.ambry.account.AccountBuilder)13 RestRequest (com.github.ambry.rest.RestRequest)13 ByteBuffer (java.nio.ByteBuffer)13 Map (java.util.Map)13 MetricRegistry (com.codahale.metrics.MetricRegistry)12 TestUtils (com.github.ambry.utils.TestUtils)12 Collections (java.util.Collections)12 List (java.util.List)12 Assert (org.junit.Assert)12