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);
}
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);
}
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);
}
}
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);
}
}
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
}
Aggregations