Search in sources :

Example 71 with Account

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

the class QuotaResource method fromRestRequest.

/**
 * Create {@link QuotaResource} for the specified {@link RestRequest}.
 * @param restRequest {@link RestRequest} object.
 * @return QuotaResource extracted from headers of {@link RestRequest}.
 * @throws QuotaException if appropriate headers aren't found in the {@link RestRequest}.
 */
public static QuotaResource fromRestRequest(RestRequest restRequest) throws QuotaException {
    try {
        final Account account = RestUtils.getAccountFromArgs(restRequest.getArgs());
        if (account.getQuotaResourceType() == QuotaResourceType.ACCOUNT) {
            return QuotaResource.fromAccountId(account.getId());
        }
        Container container = RestUtils.getContainerFromArgs(restRequest.getArgs());
        return QuotaResource.fromContainerId(account.getId(), container.getId());
    } catch (RestServiceException rEx) {
        LOGGER.error("Could not get quota resource for request: {} due to {}", RestUtils.convertToStr(restRequest), rEx.getMessage());
        throw new QuotaException("Could not get quota resource for request: " + RestUtils.convertToStr(restRequest), false);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 72 with Account

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

the class ReplicationSkipPredicate method test.

/**
 * Determines if {@link MessageInfo} container in the status of DELETED_IN_PROGRESS or INACTIVE.
 * DELETED_IN_PROGRESS containers won't be skipper from replication within the container deletion retention time.
 * @param messageInfo A message info class that contains basic info about a blob
 * @return {@code true} if the blob associates with the deprecated container, {@code false} otherwise.
 * Deprecated containers status include DELETE_IN_PROGRESS and INACTIVE.
 */
@Override
public boolean test(MessageInfo messageInfo) {
    if (accountService != null) {
        Account account = accountService.getAccountById(messageInfo.getAccountId());
        if (account == null) {
            logger.trace("Can't get account through accountService : {}", accountService);
            return false;
        }
        Container container = account.getContainerById(messageInfo.getContainerId());
        if (container == null) {
            logger.trace("Can't get container through account : {}", account);
            return false;
        }
        Container.ContainerStatus status = container.getStatus();
        if (status == Container.ContainerStatus.DELETE_IN_PROGRESS && container.getDeleteTriggerTime() + TimeUnit.DAYS.toMillis(replicationConfig.replicationContainerDeletionRetentionDays) > System.currentTimeMillis()) {
            logger.debug("Container {} is not qualified as it’s still within retention time", container);
            return false;
        }
        if (status == Container.ContainerStatus.DELETE_IN_PROGRESS || status == Container.ContainerStatus.INACTIVE) {
            logger.trace("Container {} will be skipped during replication", container);
            return true;
        } else {
            logger.debug("Container {} is Active", container);
            return false;
        }
    } else {
        logger.debug("Current accountService : {}", accountService);
        return false;
    }
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 73 with Account

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

the class RestUtilsTest method verifyBlobPropertiesConstructionSuccess.

/**
 * Verifies that a request with headers defined by {@code headers} builds {@link BlobProperties} successfully and
 * matches the values of the properties with those in {@code headers}.
 * @param headers the headers that need to go with the request that is used to construct {@link BlobProperties}.
 * @throws Exception
 */
private void verifyBlobPropertiesConstructionSuccess(JSONObject headers) throws Exception {
    RestRequest restRequest = createRestRequest(RestMethod.POST, "/", headers);
    Account account = (Account) headers.get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY);
    Container container = (Container) headers.get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY);
    restRequest.setArg(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY, account);
    restRequest.setArg(RestUtils.InternalKeys.TARGET_CONTAINER_KEY, container);
    BlobProperties blobProperties = RestUtils.buildBlobProperties(restRequest.getArgs());
    long expectedTTL = Utils.Infinite_Time;
    if (headers.has(RestUtils.Headers.TTL) && !JSONObject.NULL.equals(headers.get(RestUtils.Headers.TTL))) {
        expectedTTL = headers.getLong(RestUtils.Headers.TTL);
    }
    assertEquals("Blob TTL does not match", expectedTTL, blobProperties.getTimeToLiveInSeconds());
    assertEquals("Blob isPrivate does not match", !container.isCacheable(), blobProperties.isPrivate());
    assertEquals("Blob service ID does not match", headers.getString(RestUtils.Headers.SERVICE_ID), blobProperties.getServiceId());
    assertEquals("Blob content type does not match", headers.getString(RestUtils.Headers.AMBRY_CONTENT_TYPE), blobProperties.getContentType());
    if (headers.has(RestUtils.Headers.OWNER_ID) && !JSONObject.NULL.equals(headers.get(RestUtils.Headers.OWNER_ID))) {
        assertEquals("Blob owner ID does not match", headers.getString(RestUtils.Headers.OWNER_ID), blobProperties.getOwnerId());
    }
    assertEquals("Target account id does not match", account.getId(), blobProperties.getAccountId());
    assertEquals("Target container id does not match", container.getId(), blobProperties.getContainerId());
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) BlobProperties(com.github.ambry.messageformat.BlobProperties)

Example 74 with Account

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

the class RestUtilsTest method convertToStrTest.

@Test
public void convertToStrTest() throws Exception {
    String template = "RestRequest: [Method: %s, Path: %s, Uri: %s, Account: %s, Container: %s]";
    String method = RestMethod.GET.name();
    String uri = "/";
    Account account = InMemAccountService.UNKNOWN_ACCOUNT;
    Container container = Container.UNKNOWN_CONTAINER;
    // Test with no null values.
    RestRequest restRequest = createRestRequest(method, "/", InMemAccountService.UNKNOWN_ACCOUNT, Container.UNKNOWN_CONTAINER);
    String s = RestUtils.convertToStr(restRequest);
    assertEquals(String.format(template, method, uri, uri, account.toString(), container.toString()), s);
    // Test with null method.
    restRequest = createRestRequest(null, "/", InMemAccountService.UNKNOWN_ACCOUNT, Container.UNKNOWN_CONTAINER);
    s = RestUtils.convertToStr(restRequest);
    assertEquals(String.format(template, "null", uri, uri, account.toString(), container.toString()), s);
    // Test with null uri.
    restRequest = createRestRequest(method, null, InMemAccountService.UNKNOWN_ACCOUNT, Container.UNKNOWN_CONTAINER);
    s = RestUtils.convertToStr(restRequest);
    assertEquals(String.format(template, method, "null", "null", account.toString(), container.toString()), s);
    // Test with null account.
    restRequest = createRestRequest(method, uri, null, Container.UNKNOWN_CONTAINER);
    s = RestUtils.convertToStr(restRequest);
    assertEquals(String.format(template, method, uri, uri, "null", container.toString()), s);
    // Test with null container.
    restRequest = createRestRequest(method, uri, account, null);
    s = RestUtils.convertToStr(restRequest);
    assertEquals(String.format(template, method, uri, uri, account, "null"), s);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 75 with Account

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

the class AccountAndContainerInjector method injectAccountAndContainerForNamedBlob.

/**
 * Injects target {@link Account} and {@link Container} for named blob requests. This will treat the request path as
 * a named blob path that includes the account and container names.
 * @param restRequest The Put {@link RestRequest}.
 * @param metricsGroup The {@link RestRequestMetricsGroup} to use to set up {@link ContainerMetrics}, or {@code null}
 *                     if {@link ContainerMetrics} instantiation is not needed.
 * @throws RestServiceException
 */
public void injectAccountAndContainerForNamedBlob(RestRequest restRequest, RestRequestMetricsGroup metricsGroup) throws RestServiceException {
    accountAndContainerSanityCheck(restRequest);
    NamedBlobPath namedBlobPath = NamedBlobPath.parse(getRequestPath(restRequest), restRequest.getArgs());
    String accountName = namedBlobPath.getAccountName();
    Account targetAccount = accountService.getAccountByName(accountName);
    if (targetAccount == null) {
        frontendMetrics.unrecognizedAccountNameCount.inc();
        throw new RestServiceException("Account cannot be found for accountName=" + accountName + " in put request with account and container headers.", RestServiceErrorCode.InvalidAccount);
    }
    ensureAccountNameMatch(targetAccount, restRequest);
    String containerName = namedBlobPath.getContainerName();
    Container targetContainer;
    try {
        targetContainer = accountService.getContainerByName(accountName, containerName);
    } catch (AccountServiceException e) {
        throw new RestServiceException("Failed to get container " + containerName + " from account " + accountName + " in put request with account and container headers.", RestServiceErrorCode.getRestServiceErrorCode(e.getErrorCode()));
    }
    if (targetContainer == null) {
        frontendMetrics.unrecognizedContainerNameCount.inc();
        throw new RestServiceException("Container cannot be found for accountName=" + accountName + " and containerName=" + containerName + " in put request with account and container headers.", RestServiceErrorCode.InvalidContainer);
    }
    if (targetContainer.getNamedBlobMode() == Container.NamedBlobMode.DISABLED) {
        throw new RestServiceException("Named blob APIs disabled for this container. account=" + accountName + ", container=" + containerName, RestServiceErrorCode.BadRequest);
    }
    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)

Aggregations

Account (com.github.ambry.account.Account)114 Container (com.github.ambry.account.Container)87 Test (org.junit.Test)67 RestServiceException (com.github.ambry.rest.RestServiceException)24 ArrayList (java.util.ArrayList)22 RestRequest (com.github.ambry.rest.RestRequest)18 JSONObject (org.json.JSONObject)18 MockRestRequest (com.github.ambry.rest.MockRestRequest)17 VerifiableProperties (com.github.ambry.config.VerifiableProperties)16 HashMap (java.util.HashMap)15 HashSet (java.util.HashSet)15 AccountBuilder (com.github.ambry.account.AccountBuilder)14 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)14 ContainerBuilder (com.github.ambry.account.ContainerBuilder)13 Properties (java.util.Properties)13 MetricRegistry (com.codahale.metrics.MetricRegistry)12 InMemAccountService (com.github.ambry.account.InMemAccountService)12 ByteBuffer (java.nio.ByteBuffer)12 RestMethod (com.github.ambry.rest.RestMethod)11 Map (java.util.Map)11