Search in sources :

Example 6 with AccountUpdateInfo

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

Aggregations

AccountUpdateInfo (com.github.ambry.account.AccountUtils.AccountUpdateInfo)6 ArrayList (java.util.ArrayList)5 MetricRegistry (com.codahale.metrics.MetricRegistry)3 Container (com.github.ambry.account.Container)3 Test (org.junit.Test)3 Account (com.github.ambry.account.Account)2 AccountDao (com.github.ambry.account.mysql.AccountDao)2 MySqlAccountStoreFactory (com.github.ambry.account.mysql.MySqlAccountStoreFactory)2 MySqlDataAccessor (com.github.ambry.mysql.MySqlDataAccessor)2 SQLException (java.sql.SQLException)2 AccountBuilder (com.github.ambry.account.AccountBuilder)1 ContainerBuilder (com.github.ambry.account.ContainerBuilder)1 MySqlAccountStore (com.github.ambry.account.mysql.MySqlAccountStore)1 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)1 MySqlAccountServiceConfig (com.github.ambry.config.MySqlAccountServiceConfig)1 VerifiableProperties (com.github.ambry.config.VerifiableProperties)1 MySqlMetrics (com.github.ambry.mysql.MySqlMetrics)1 MySqlUtils (com.github.ambry.mysql.MySqlUtils)1 AccountTestUtils (com.github.ambry.utils.AccountTestUtils)1 SystemTime (com.github.ambry.utils.SystemTime)1