Search in sources :

Example 71 with Container

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

the class AccountDao method updateAccounts.

/**
 * Adds/Updates accounts and their containers to the database in batches atomically using transaction.
 * @param accountsInfo information of updated Accounts
 * @param batchSize number of statements to be executed in one batch
 * @throws SQLException
 */
public synchronized void updateAccounts(List<AccountUpdateInfo> accountsInfo, int batchSize) throws SQLException {
    try {
        long startTimeMs = System.currentTimeMillis();
        AccountUpdateBatch accountUpdateBatch = new AccountUpdateBatch(dataAccessor.getPreparedStatement(insertAccountsSql, true), dataAccessor.getPreparedStatement(updateAccountsSql, true), dataAccessor.getPreparedStatement(insertContainersSql, true), dataAccessor.getPreparedStatement(updateContainersSql, true));
        // Disable auto commits
        dataAccessor.setAutoCommit(false);
        int batchCount = 0;
        for (AccountUpdateInfo accountUpdateInfo : accountsInfo) {
            // Get account and container changes information
            Account account = accountUpdateInfo.getAccount();
            boolean isAccountAdded = accountUpdateInfo.isAdded();
            boolean isAccountUpdated = accountUpdateInfo.isUpdated();
            List<Container> addedContainers = accountUpdateInfo.getAddedContainers();
            List<Container> updatedContainers = accountUpdateInfo.getUpdatedContainers();
            // Number of changes in the account.
            int accountUpdateCount = (isAccountAdded ? 1 : 0) + (isAccountUpdated ? 1 : 0) + addedContainers.size() + updatedContainers.size();
            // b) Adding account and its containers in current iteration to total batch count > configured batch size
            if (batchCount >= batchSize || (batchCount > 0 && batchCount + accountUpdateCount > batchSize)) {
                accountUpdateBatch.maybeExecuteBatch();
                dataAccessor.commit();
                batchCount = 0;
            }
            // Add account to insert/update batch if it was either added or modified.
            if (isAccountAdded) {
                accountUpdateBatch.addAccount(account);
            } else if (isAccountUpdated) {
                accountUpdateBatch.updateAccount(account);
            }
            // Add new containers for batch inserts
            for (Container container : addedContainers) {
                accountUpdateBatch.addContainer(account.getId(), container);
            }
            // Add updated containers for batch updates
            for (Container container : updatedContainers) {
                accountUpdateBatch.updateContainer(account.getId(), container);
            }
            batchCount += accountUpdateCount;
        }
        // Commit transaction with pending batch inserts/updates
        if (batchCount > 0) {
            accountUpdateBatch.maybeExecuteBatch();
            dataAccessor.commit();
        }
        dataAccessor.onSuccess(Write, System.currentTimeMillis() - startTimeMs);
    } catch (SQLException e) {
        // rollback the current transaction.
        dataAccessor.rollback();
        dataAccessor.onException(e, Write);
        throw e;
    } finally {
        // Close the connection to ensure subsequent queries are made in a new transaction and return the latest data
        dataAccessor.closeActiveConnection();
    }
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) AccountUpdateInfo(com.github.ambry.account.AccountUtils.AccountUpdateInfo) SQLException(java.sql.SQLException)

Example 72 with Container

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

the class AccountContainerTest method testContainerBuilder.

/**
 * Tests building a {@link Container} using {@link ContainerBuilder}.
 * @throws JSONException
 */
@Test
public void testContainerBuilder() throws JSONException {
    for (int i = 0; i < CONTAINER_COUNT; i++) {
        // build a container with arguments supplied
        ContainerBuilder containerBuilder = new ContainerBuilder(refContainerIds.get(i), refContainerNames.get(i), refContainerStatuses.get(i), refContainerDescriptions.get(i), refAccountId).setEncrypted(refContainerEncryptionValues.get(i)).setPreviouslyEncrypted(refContainerPreviousEncryptionValues.get(i)).setCacheable(refContainerCachingValues.get(i)).setBackupEnabled(refContainerBackupEnabledValues.get(i)).setMediaScanDisabled(refContainerMediaScanDisabledValues.get(i)).setReplicationPolicy(refContainerReplicationPolicyValues.get(i)).setTtlRequired(refContainerTtlRequiredValues.get(i)).setSecurePathRequired(refContainerSignedPathRequiredValues.get(i)).setContentTypeWhitelistForFilenamesOnDownload(refContainerContentTypeAllowListForFilenamesOnDownloadValues.get(i)).setOverrideAccountAcl(refContainerOverrideAccountAcls.get(i)).setNamedBlobMode(refContainerNamedBlobModes.get(i)).setDeleteTriggerTime(refContainerDeleteTriggerTime.get(i)).setLastModifiedTime(refContainerLastModifiedTimes.get(i)).setSnapshotVersion(refContainerSnapshotVersions.get(i)).setAccessControlAllowOrigin(refAccessControlAllowOriginValues.get(i));
        Container containerFromBuilder = containerBuilder.build();
        assertContainer(containerFromBuilder, i);
        // build a container from existing container
        containerBuilder = new ContainerBuilder(containerFromBuilder);
        containerFromBuilder = containerBuilder.build();
        assertContainer(containerFromBuilder, i);
        boolean previouslyEncrypted = containerFromBuilder.wasPreviouslyEncrypted();
        // turn off encryption, check that previouslyEncrypted is the same as the previous value.
        containerFromBuilder = new ContainerBuilder(containerFromBuilder).setEncrypted(false).build();
        assertEncryptionSettings(containerFromBuilder, false, previouslyEncrypted);
        // turn off encryption, by turning it on and off again.
        containerFromBuilder = new ContainerBuilder(containerFromBuilder).setEncrypted(true).setEncrypted(false).build();
        assertEncryptionSettings(containerFromBuilder, false, previouslyEncrypted);
        // turn it back on, previouslyEncrypted should be set.
        containerFromBuilder = new ContainerBuilder(containerFromBuilder).setEncrypted(true).build();
        assertEncryptionSettings(containerFromBuilder, true, true);
        // turn off again, previouslyEncrypted should still be set.
        containerFromBuilder = new ContainerBuilder(containerFromBuilder).setEncrypted(false).build();
        assertEncryptionSettings(containerFromBuilder, false, true);
    }
}
Also used : Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 73 with Container

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

the class AccountContainerTest method testUnknownAccountAndContainer.

/**
 * Tests for {@link InMemAccountService#UNKNOWN_ACCOUNT}, {@link Container#UNKNOWN_CONTAINER},
 * {@link Container#DEFAULT_PUBLIC_CONTAINER}, and {@link Container#DEFAULT_PRIVATE_CONTAINER}.
 */
@Test
public void testUnknownAccountAndContainer() {
    Account unknownAccount = InMemAccountService.UNKNOWN_ACCOUNT;
    Container unknownContainer = Container.UNKNOWN_CONTAINER;
    Container unknownPublicContainer = Container.DEFAULT_PUBLIC_CONTAINER;
    Container unknownPrivateContainer = Container.DEFAULT_PRIVATE_CONTAINER;
    // UNKNOWN_CONTAINER
    assertEquals("Wrong id for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_ID, unknownContainer.getId());
    assertEquals("Wrong name for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_NAME, unknownContainer.getName());
    assertEquals("Wrong status for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_STATUS, unknownContainer.getStatus());
    assertEquals("Wrong description for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_DESCRIPTION, unknownContainer.getDescription());
    assertEquals("Wrong parent account id for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_PARENT_ACCOUNT_ID, unknownContainer.getParentAccountId());
    assertEquals("Wrong cacheable setting for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_CACHEABLE_SETTING, unknownContainer.isCacheable());
    assertEquals("Wrong encrypted setting for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_ENCRYPTED_SETTING, unknownContainer.isEncrypted());
    assertEquals("Wrong previouslyEncrypted setting for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_PREVIOUSLY_ENCRYPTED_SETTING, unknownContainer.wasPreviouslyEncrypted());
    assertEquals("Wrong mediaScanDisabled setting for UNKNOWN_CONTAINER", Container.UNKNOWN_CONTAINER_MEDIA_SCAN_DISABLED_SETTING, unknownContainer.isMediaScanDisabled());
    // DEFAULT_PUBLIC_CONTAINER
    assertEquals("Wrong id for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_ID, unknownPublicContainer.getId());
    assertEquals("Wrong name for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_NAME, unknownPublicContainer.getName());
    assertEquals("Wrong status for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_STATUS, unknownPublicContainer.getStatus());
    assertEquals("Wrong description for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_DESCRIPTION, unknownPublicContainer.getDescription());
    assertEquals("Wrong parent account id for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_PARENT_ACCOUNT_ID, unknownPublicContainer.getParentAccountId());
    assertEquals("Wrong cacheable setting for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_CACHEABLE_SETTING, unknownPublicContainer.isCacheable());
    assertEquals("Wrong encrypted setting for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_ENCRYPTED_SETTING, unknownPublicContainer.isEncrypted());
    assertEquals("Wrong previouslyEncrypted setting for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_PREVIOUSLY_ENCRYPTED_SETTING, unknownPublicContainer.wasPreviouslyEncrypted());
    assertEquals("Wrong mediaScanDisabled setting for DEFAULT_PUBLIC_CONTAINER", Container.DEFAULT_PUBLIC_CONTAINER_MEDIA_SCAN_DISABLED_SETTING, unknownPublicContainer.isMediaScanDisabled());
    // DEFAULT_PRIVATE_CONTAINER
    assertEquals("Wrong id for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_ID, unknownPrivateContainer.getId());
    assertEquals("Wrong name for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_NAME, unknownPrivateContainer.getName());
    assertEquals("Wrong status for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_STATUS, unknownPrivateContainer.getStatus());
    assertEquals("Wrong description for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_DESCRIPTION, unknownPrivateContainer.getDescription());
    assertEquals("Wrong parent account id for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_PARENT_ACCOUNT_ID, unknownPrivateContainer.getParentAccountId());
    assertEquals("Wrong cacheable setting for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_CACHEABLE_SETTING, unknownPrivateContainer.isCacheable());
    assertEquals("Wrong encrypted setting for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_ENCRYPTED_SETTING, unknownPrivateContainer.isEncrypted());
    assertEquals("Wrong previouslyEncrypted setting for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_PREVIOUSLY_ENCRYPTED_SETTING, unknownPrivateContainer.wasPreviouslyEncrypted());
    assertEquals("Wrong mediaScanDisabled setting for DEFAULT_PRIVATE_CONTAINER", Container.DEFAULT_PRIVATE_CONTAINER_MEDIA_SCAN_DISABLED_SETTING, unknownPrivateContainer.isMediaScanDisabled());
    // UNKNOWN_ACCOUNT
    assertEquals("Wrong id for UNKNOWN_ACCOUNT", Account.UNKNOWN_ACCOUNT_ID, unknownAccount.getId());
    assertEquals("Wrong name for UNKNOWN_ACCOUNT", Account.UNKNOWN_ACCOUNT_NAME, unknownAccount.getName());
    assertEquals("Wrong status for UNKNOWN_ACCOUNT", AccountStatus.ACTIVE, unknownAccount.getStatus());
    assertEquals("Wrong number of containers for UNKNOWN_ACCOUNT", 3, unknownAccount.getAllContainers().size());
    assertEquals("Wrong unknown container get from UNKNOWN_ACCOUNT", Container.UNKNOWN_CONTAINER, unknownAccount.getContainerById(Container.UNKNOWN_CONTAINER_ID));
    assertEquals("Wrong unknown public container get from UNKNOWN_ACCOUNT", Container.DEFAULT_PUBLIC_CONTAINER, unknownAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID));
    assertEquals("Wrong unknown private container get from UNKNOWN_ACCOUNT", Container.DEFAULT_PRIVATE_CONTAINER, unknownAccount.getContainerById(Container.DEFAULT_PRIVATE_CONTAINER_ID));
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 74 with Container

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

the class AccountContainerTest method accountFromJson.

/**
 * Deserialize a {@link JSONObject} to an {@link Account}.
 * @param metadata The {@link JSONObject}.
 * @return A {@link Account}.
 * @throws JSONException
 */
private Account accountFromJson(JSONObject metadata) throws JSONException {
    if (metadata == null) {
        throw new IllegalArgumentException("metadata cannot be null.");
    }
    short metadataVersion = (short) metadata.getInt(Account.JSON_VERSION_KEY);
    switch(metadataVersion) {
        case Account.JSON_VERSION_1:
            short id = (short) metadata.getInt(ACCOUNT_ID_KEY);
            String name = metadata.getString(ACCOUNT_NAME_KEY);
            AccountStatus status = AccountStatus.valueOf(metadata.getString(Account.STATUS_KEY));
            int snapshotVersion = metadata.optInt(Account.SNAPSHOT_VERSION_KEY, Account.SNAPSHOT_VERSION_DEFAULT_VALUE);
            long lastModifiedTime = metadata.optLong(Account.LAST_MODIFIED_TIME_KEY, Account.LAST_MODIFIED_TIME_DEFAULT_VALUE);
            boolean aclInheritedByContainer = metadata.optBoolean(ACL_INHERITED_BY_CONTAINER_KEY, ACL_INHERITED_BY_CONTAINER_DEFAULT_VALUE);
            if (name == null || status == null) {
                throw new IllegalStateException("Either of required fields name=" + name + " or status=" + status + " is null");
            }
            JSONArray containerArray = metadata.optJSONArray(CONTAINERS_KEY);
            List<Container> containers = new ArrayList<>();
            if (containerArray != null) {
                for (int index = 0; index < containerArray.length(); index++) {
                    containers.add(containerFromJson(containerArray.getJSONObject(index), id));
                }
            }
            QuotaResourceType quotaResourceType = metadata.optEnum(QuotaResourceType.class, QUOTA_RESOURCE_TYPE_KEY, QUOTA_RESOURCE_TYPE_DEFAULT_VALUE);
            return new AccountBuilder(id, name, status, quotaResourceType).snapshotVersion(snapshotVersion).lastModifiedTime(lastModifiedTime).aclInheritedByContainer(aclInheritedByContainer).containers(containers).build();
        default:
            throw new IllegalStateException("Unsupported account json version=" + metadataVersion);
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Container(com.github.ambry.account.Container) QuotaResourceType(com.github.ambry.quota.QuotaResourceType)

Example 75 with Container

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

the class AccountContainerTest method testRemovingContainers.

/**
 * Tests removing containers in AccountBuilder.
 */
@Test
public void testRemovingContainers() throws JSONException {
    Account origin = accountFromJson(refAccountJson);
    AccountBuilder accountBuilder = new AccountBuilder(origin);
    // first, remove 10 containers
    ArrayList<Container> containers = new ArrayList<>(origin.getAllContainers());
    Set<Container> removed = new HashSet<>();
    while (removed.size() < 10) {
        Container container = containers.get(random.nextInt(containers.size()));
        removed.add(container);
        accountBuilder.removeContainer(container);
    }
    Account account = accountBuilder.build();
    assertEquals("Wrong number of containers", CONTAINER_COUNT - 10, account.getAllContainers().size());
    for (Container removedContainer : removed) {
        assertNull("Container not removed ", account.getContainerById(removedContainer.getId()));
        assertNull("Container not removed ", account.getContainerByName(removedContainer.getName()));
    }
    // then, remove the rest containers
    for (Container container : origin.getAllContainers()) {
        accountBuilder.removeContainer(container);
    }
    account = accountBuilder.build();
    assertEquals("Wrong container number.", 0, account.getAllContainers().size());
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) 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