Search in sources :

Example 16 with Container

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

the class BlobStoreStatsTest method getContainerStorageStats.

/**
 * Go over the referenceIndex to collect valid data size information per container. The result is used for
 * verification purposes.
 * @param deleteReferenceTimeInMs the reference time in ms until which deletes are relevant
 * @param expiryReferenceTimeInMs the reference time in ms until which expirations are relevant
 * @param deleteTombstoneStats a hashmap that tracks stats related delete tombstones in log segments.
 * @return a nested {@link Map} of serviceId to containerId to valid data size
 */
private Map<Short, Map<Short, ContainerStorageStats>> getContainerStorageStats(long deleteReferenceTimeInMs, long expiryReferenceTimeInMs, Map<String, Pair<AtomicLong, AtomicLong>> deleteTombstoneStats) {
    Map<Short, Map<Short, ContainerStorageStats>> containerStorageStats = new HashMap<>();
    Map<Short, Map<Short, Long>> validSizeMap = new HashMap<>();
    Map<Short, Map<Short, Long>> physicalSizeMap = new HashMap<>();
    Map<Short, Map<Short, Set<StoreKey>>> storeKeyMap = new HashMap<>();
    Pair<Set<MockId>, Set<MockId>> expiredDeletes = new Pair<>(new HashSet<>(), new HashSet<>());
    for (Offset indSegStartOffset : state.referenceIndex.keySet()) {
        state.getValidIndexEntriesForIndexSegment(indSegStartOffset, deleteReferenceTimeInMs, expiryReferenceTimeInMs, null, deleteTombstoneStats, expiredDeletes, true, (entry, isValid) -> {
            IndexValue indexValue = entry.getValue();
            if (indexValue.isPut() && isValid) {
                StatsUtils.updateNestedMapHelper(validSizeMap, indexValue.getAccountId(), indexValue.getContainerId(), indexValue.getSize());
            }
            StatsUtils.updateNestedMapHelper(physicalSizeMap, indexValue.getAccountId(), indexValue.getContainerId(), indexValue.getSize());
            storeKeyMap.computeIfAbsent(indexValue.getAccountId(), k -> new HashMap<>()).computeIfAbsent(indexValue.getContainerId(), k -> new HashSet<>()).add(entry.getKey());
        });
    }
    for (short accountId : validSizeMap.keySet()) {
        for (short containerId : validSizeMap.get(accountId).keySet()) {
            containerStorageStats.computeIfAbsent(accountId, k -> new HashMap<>()).put(containerId, new ContainerStorageStats(containerId, validSizeMap.get(accountId).get(containerId), physicalSizeMap.get(accountId).get(containerId), storeKeyMap.get(accountId).get(containerId).size()));
        }
    }
    return containerStorageStats;
}
Also used : Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) StoreStats(com.github.ambry.store.StoreStats) HashMap(java.util.HashMap) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils(com.github.ambry.utils.TestUtils) Map(java.util.Map) After(org.junit.After) SystemTime(com.github.ambry.utils.SystemTime) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assume(org.junit.Assume) CuratedLogIndexState(com.github.ambry.store.CuratedLogIndexState) Time(com.github.ambry.utils.Time) EnumSet(java.util.EnumSet) Parameterized(org.junit.runners.Parameterized) Container(com.github.ambry.account.Container) MetricRegistry(com.codahale.metrics.MetricRegistry) Pair(com.github.ambry.utils.Pair) Set(java.util.Set) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) Test(org.junit.Test) NavigableMap(java.util.NavigableMap) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Throttler(com.github.ambry.utils.Throttler) MockTime(com.github.ambry.utils.MockTime) Account(com.github.ambry.account.Account) Optional(java.util.Optional) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) Set(java.util.Set) HashMap(java.util.HashMap) ContainerStorageStats(com.github.ambry.server.storagestats.ContainerStorageStats) HashMap(java.util.HashMap) Map(java.util.Map) NavigableMap(java.util.NavigableMap) Pair(com.github.ambry.utils.Pair) HashSet(java.util.HashSet)

Example 17 with Container

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

the class AmbrySecurityServiceTest method testGetBlob.

/**
 * Tests {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for a Get blob
 * with the passed in {@link BlobInfo} and {@link ByteRange}
 * @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
 * @param range the {@link ByteRange} for the {@link RestRequest}
 * @throws Exception
 */
private void testGetBlob(BlobInfo blobInfo, ByteRange range) throws Exception {
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    JSONObject headers = range != null ? new JSONObject().put(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range)) : null;
    RestRequest restRequest = createRestRequest(RestMethod.GET, "/", headers);
    Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
    insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
    securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
    Assert.assertEquals("ProcessResponse status should have been set", range == null ? ResponseStatus.Ok : ResponseStatus.PartialContent, restResponseChannel.getStatus());
    verifyHeadersForGetBlob(restRequest, blobInfo, accountAndContainer, range, 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)

Example 18 with Container

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

the class FrontendIntegrationTest method updateContainersAndVerify.

// accountApiTest() helpers
/**
 * Call the {@code POST /accounts/updateContainers} API to update account metadata and verify that the update succeeded.
 * @param account the account in which to update containers.
 * @param containers the containers to update.
 */
private void updateContainersAndVerify(Account account, Container... containers) throws Exception {
    byte[] containersUpdateJson = AccountCollectionSerde.serializeContainersInJson(Arrays.asList(containers));
    String accountName = account.getName();
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(RestUtils.Headers.TARGET_ACCOUNT_NAME, accountName);
    FullHttpRequest request = buildRequest(HttpMethod.POST, Operations.ACCOUNTS_CONTAINERS, headers, ByteBuffer.wrap(containersUpdateJson));
    ResponseParts responseParts = nettyClient.sendRequest(request, null, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
    // verify regular response header
    assertEquals("Unexpected account id in response header", account.getId(), Short.parseShort(response.headers().get(RestUtils.Headers.TARGET_ACCOUNT_ID)));
    assertEquals("Unexpected content type in response header", RestUtils.JSON_CONTENT_TYPE, response.headers().get(RestUtils.Headers.CONTENT_TYPE));
    verifyTrackingHeaders(response);
    ByteBuffer content = getContent(responseParts.queue, HttpUtil.getContentLength(response));
    Collection<Container> outputContainers = AccountCollectionSerde.containersFromInputStreamInJson(new ByteArrayInputStream(content.array()), account.getId());
    for (Container container : outputContainers) {
        assertEquals("Update not reflected in AccountService", container, ACCOUNT_SERVICE.getContainerByName(accountName, container.getName()));
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Container(com.github.ambry.account.Container) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) ByteBuffer(java.nio.ByteBuffer)

Example 19 with Container

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

the class FrontendIntegrationTest method accountApiTest.

/**
 * Tests for the account/container get/update API.
 */
@Test
public void accountApiTest() throws Exception {
    verifyGetAccountsAndContainer();
    // update and add accounts
    Map<Short, Account> accountsById = ACCOUNT_SERVICE.getAllAccounts().stream().collect(Collectors.toMap(Account::getId, Function.identity()));
    Account editedAccount = accountsById.values().stream().findAny().get();
    Container editedContainer = editedAccount.getAllContainers().stream().findAny().get();
    editedContainer = new ContainerBuilder(editedContainer).setDescription("new description abcdefgh").build();
    editedAccount = new AccountBuilder(editedAccount).addOrUpdateContainer(editedContainer).build();
    updateAccountsAndVerify(ACCOUNT_SERVICE, editedAccount, ACCOUNT_SERVICE.generateRandomAccount());
    verifyGetAccountsAndContainer();
    // Test adding a container to the account
    Container newContainer = ACCOUNT_SERVICE.getRandomContainer(editedAccount.getId());
    updateContainersAndVerify(editedAccount, newContainer);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) AccountBuilder(com.github.ambry.account.AccountBuilder) Test(org.junit.Test)

Example 20 with Container

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

the class FrontendIntegrationTest method multipartPostGetHeadUpdateDeleteUndeleteTest.

/**
 * Tests multipart POST and verifies it via GET operations.
 * @throws Exception
 */
@Test
public void multipartPostGetHeadUpdateDeleteUndeleteTest() throws Exception {
    Account refAccount = ACCOUNT_SERVICE.createAndAddRandomAccount();
    Container refContainer = refAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID);
    doPostGetHeadUpdateDeleteUndeleteTest(0, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
    doPostGetHeadUpdateDeleteUndeleteTest((int) FRONTEND_CONFIG.chunkedGetResponseThresholdInBytes * 3, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
    // failure case
    // size of content being POSTed is higher than what is allowed via multipart/form-data
    long maxAllowedSizeBytes = new NettyConfig(FRONTEND_VERIFIABLE_PROPS).nettyMultipartPostMaxSizeBytes;
    ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes((int) maxAllowedSizeBytes + 1));
    HttpHeaders headers = new DefaultHttpHeaders();
    setAmbryHeadersForPut(headers, TTL_SECS, !refContainer.isCacheable(), refAccount.getName(), "application/octet-stream", null, refAccount.getName(), refContainer.getName());
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, ByteBuffer.allocate(0));
    ResponseParts responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status());
    assertTrue("No Date header", response.headers().getTimeMillis(HttpHeaderNames.DATE, -1) != -1);
    assertFalse("Channel should not be active", HttpUtil.isKeepAlive(response));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Account(com.github.ambry.account.Account) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Container(com.github.ambry.account.Container) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) NettyConfig(com.github.ambry.config.NettyConfig) ByteBuffer(java.nio.ByteBuffer) 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