Search in sources :

Example 61 with Account

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

the class AccountDaoTest method testBatchOperations.

@Test
public void testBatchOperations() throws SQLException {
    List<AccountUpdateInfo> accountUpdateInfos = new ArrayList<>();
    int size = 11;
    int batchSize = 5;
    // test batch account inserts
    for (int i = 1; i <= size; i++) {
        Account account = new AccountBuilder((short) i, "test account " + i, Account.AccountStatus.ACTIVE).build();
        accountUpdateInfos.add(new AccountUpdateInfo(account, true, false, new ArrayList<>(), new ArrayList<>()));
    }
    accountDao.updateAccounts(accountUpdateInfos, batchSize);
    verify(mockAccountInsertStatement, times(size)).addBatch();
    verify(mockAccountInsertStatement, times(size / batchSize + 1)).executeBatch();
    // test batch account updates
    accountUpdateInfos.clear();
    for (int i = 1; i <= size; i++) {
        Account account = new AccountBuilder((short) i, "test account " + i, Account.AccountStatus.ACTIVE).snapshotVersion(1).build();
        accountUpdateInfos.add(new AccountUpdateInfo(account, false, true, new ArrayList<>(), new ArrayList<>()));
    }
    accountDao.updateAccounts(accountUpdateInfos, batchSize);
    verify(mockAccountUpdateStatement, times(size)).addBatch();
    verify(mockAccountUpdateStatement, times(size / batchSize + 1)).executeBatch();
    Account account = new AccountBuilder((short) 1, "test account " + 1, Account.AccountStatus.ACTIVE).build();
    List<Container> containers = new ArrayList<>();
    for (int i = 1; i <= size; i++) {
        containers.add(new ContainerBuilder((short) i, "test container " + i, Container.ContainerStatus.ACTIVE, "", (short) 1).build());
    }
    // test batch container inserts
    accountUpdateInfos.clear();
    accountUpdateInfos.add(new AccountUpdateInfo(account, false, false, containers, new ArrayList<>()));
    accountDao.updateAccounts(accountUpdateInfos, batchSize);
    verify(mockContainerInsertStatement, times(size)).addBatch();
    // Execute batch should be invoked only once since all containers belong to same account
    verify(mockContainerInsertStatement, times(1)).executeBatch();
    // test batch container updates
    accountUpdateInfos.clear();
    accountUpdateInfos.add(new AccountUpdateInfo(account, false, false, new ArrayList<>(), containers));
    accountDao.updateAccounts(accountUpdateInfos, batchSize);
    verify(mockContainerUpdateStatement, times(size)).addBatch();
    // Execute batch should be invoked only once since all containers belong to same account
    verify(mockContainerUpdateStatement, times(1)).executeBatch();
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) AccountUpdateInfo(com.github.ambry.account.AccountUtils.AccountUpdateInfo) ArrayList(java.util.ArrayList) AccountBuilder(com.github.ambry.account.AccountBuilder) Test(org.junit.Test)

Example 62 with Account

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

the class AccountDaoTest method testGetAccounts.

@Test
public void testGetAccounts() throws Exception {
    List<Account> accountList = accountDao.getNewAccounts(0L);
    assertEquals(1, accountList.size());
    Account testAccountNoContainers = new AccountBuilder(testAccount).containers(null).build();
    assertEquals(testAccountNoContainers, accountList.get(0));
    assertEquals("Read success count should be 1", 1, metrics.readSuccessCount.getCount());
}
Also used : Account(com.github.ambry.account.Account) AccountBuilder(com.github.ambry.account.AccountBuilder) Test(org.junit.Test)

Example 63 with Account

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

the class AccountDao method getNewAccounts.

/**
 * Gets all accounts that have been created or modified since the specified time.
 * @param updatedSince the last modified time used to filter.
 * @return a list of {@link Account}s.
 * @throws SQLException
 */
public synchronized List<Account> getNewAccounts(long updatedSince) throws SQLException {
    long startTimeMs = System.currentTimeMillis();
    Timestamp sinceTime = new Timestamp(updatedSince);
    ResultSet rs = null;
    try {
        PreparedStatement getSinceStatement = dataAccessor.getPreparedStatement(getAccountsSinceSql, false);
        getSinceStatement.setTimestamp(1, sinceTime);
        rs = getSinceStatement.executeQuery();
        List<Account> accounts = convertAccountsResultSet(rs);
        dataAccessor.onSuccess(Read, System.currentTimeMillis() - startTimeMs);
        return accounts;
    } catch (SQLException e) {
        dataAccessor.onException(e, Read);
        throw e;
    } finally {
        closeQuietly(rs);
    }
}
Also used : Account(com.github.ambry.account.Account) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 64 with Account

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

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

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