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();
}
}
Aggregations