Search in sources :

Example 81 with Container

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

the class RestUtils method convertToStr.

/**
 * Convert the specified {@link RestRequest} object to a string representation.
 * @param restRequest {@link RestRequest} object.
 * @return String representation of the {@link RestRequest} object.
 */
public static String convertToStr(RestRequest restRequest) {
    if (Objects.isNull(restRequest)) {
        return "RestRequest: null";
    }
    StringBuilder sb = new StringBuilder();
    sb.append("RestRequest: [");
    sb.append("Method: " + ((restRequest.getRestMethod() == null) ? "null" : restRequest.getRestMethod().name()));
    sb.append(", Path: " + ((restRequest.getPath() == null) ? "null" : restRequest.getPath()));
    sb.append(", Uri: " + ((restRequest.getUri() == null) ? "null" : restRequest.getUri()));
    Account account = null;
    try {
        account = RestUtils.getAccountFromArgs(restRequest.getArgs());
    } catch (RestServiceException restServiceException) {
    }
    sb.append(", Account: " + ((account == null) ? "null" : account.toString()));
    Container container = null;
    try {
        container = RestUtils.getContainerFromArgs(restRequest.getArgs());
    } catch (RestServiceException restServiceException) {
    }
    sb.append(", Container: " + ((container == null) ? "null" : container.toString()));
    sb.append("]");
    return sb.toString();
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Example 82 with Container

use of com.github.ambry.account.Container 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 83 with Container

use of com.github.ambry.account.Container 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 84 with Container

use of com.github.ambry.account.Container 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 85 with Container

use of com.github.ambry.account.Container 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)

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