use of com.github.ambry.account.Container in project ambry by linkedin.
the class HelixAccountServiceTest method testUpdateAccount.
/**
* Tests creating and updating accounts through {@link HelixAccountService} in various situations:
* 0. {@link Account}s already exists on ZooKeeper.
* 1. add a new {@link Account};
* 2. update existing {@link Account};
* 3. add a new {@link Container} to an existing {@link Account};
* 4. update existing {@link Container}s of existing {@link Account}s.
* @throws Exception Any unexpected exception.
*/
@Test
public void testUpdateAccount() throws Exception {
// pre-populate account metadata in ZK.
writeAccountsToHelixPropertyStore(idToRefAccountMap.values(), false);
accountService = mockHelixAccountServiceFactory.getAccountService();
assertAccountsInAccountService(idToRefAccountMap.values(), NUM_REF_ACCOUNT, accountService);
// add a new account
Account newAccountWithoutContainer = new AccountBuilder(refAccountId, refAccountName, refAccountStatus).build();
List<Account> accountsToUpdate = Collections.singletonList(newAccountWithoutContainer);
updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);
// update all existing reference accounts (not the new account)
accountsToUpdate = new ArrayList<>();
for (Account account : accountService.getAllAccounts()) {
AccountBuilder accountBuilder = new AccountBuilder(account);
accountBuilder.name(account.getName() + "-extra");
accountBuilder.status(account.getStatus().equals(AccountStatus.ACTIVE) ? AccountStatus.INACTIVE : AccountStatus.ACTIVE);
accountsToUpdate.add(accountBuilder.build());
}
updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);
// add one container to the new account
AccountBuilder accountBuilder = new AccountBuilder(accountService.getAccountById(refAccountId));
accountsToUpdate = Collections.singletonList(accountBuilder.addOrUpdateContainer(refContainer).build());
updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);
// update existing containers for all the reference accounts (not for the new account)
accountsToUpdate = new ArrayList<>();
for (Account account : accountService.getAllAccounts()) {
accountBuilder = new AccountBuilder(account);
for (Container container : account.getAllContainers()) {
ContainerBuilder containerBuilder = new ContainerBuilder(container);
containerBuilder.setId((short) (-1 * (container.getId())));
containerBuilder.setName(container.getName() + "-extra");
containerBuilder.setStatus(container.getStatus().equals(ContainerStatus.ACTIVE) ? ContainerStatus.INACTIVE : ContainerStatus.ACTIVE);
containerBuilder.setDescription(container.getDescription() + "--extra");
containerBuilder.setReplicationPolicy(container.getReplicationPolicy() + "---extra");
containerBuilder.setTtlRequired(!container.isTtlRequired());
accountBuilder.addOrUpdateContainer(containerBuilder.build());
}
accountsToUpdate.add(accountBuilder.build());
}
updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);
}
use of com.github.ambry.account.Container in project ambry by linkedin.
the class HelixAccountServiceTest method testGetContainerByStatus.
/**
* Tests {@link AccountService#getContainersByStatus(ContainerStatus)} with generated {@links Container}s
*/
@Test
public void testGetContainerByStatus() throws Exception {
// a set that records the account ids that have already been taken.
Set<Short> accountIdSet = new HashSet<>();
// generate a single reference account and container that can be referenced by refAccount and refContainer respectively.
refAccountId = Utils.getRandomShort(random);
accountIdSet.add(refAccountId);
generateRefAccounts(idToRefAccountMap, idToRefContainerMap, accountIdSet, 5, 2);
accountService = mockHelixAccountServiceFactory.getAccountService();
accountService.updateAccounts(idToRefAccountMap.values());
assertAccountsInAccountService(idToRefAccountMap.values(), 5, accountService);
List<Account> accountsToUpdate = new ArrayList<>();
int cnt = 0;
for (Account account : accountService.getAllAccounts()) {
AccountBuilder accountBuilder = new AccountBuilder(account);
for (Container container : account.getAllContainers()) {
if (cnt % 2 == 0) {
ContainerBuilder containerBuilder = new ContainerBuilder(container);
containerBuilder.setId((short) (-1 * (container.getId())));
containerBuilder.setName(container.getName() + "-extra");
containerBuilder.setStatus(ContainerStatus.DELETE_IN_PROGRESS);
containerBuilder.setDescription(container.getDescription() + "--extra");
containerBuilder.setReplicationPolicy(container.getReplicationPolicy() + "---extra");
containerBuilder.setTtlRequired(container.isTtlRequired());
accountBuilder.addOrUpdateContainer(containerBuilder.build());
}
cnt++;
}
accountsToUpdate.add(accountBuilder.build());
}
updateAccountsAndAssertAccountExistence(accountsToUpdate, 5, true);
Set<Container> containerList = accountService.getContainersByStatus(ContainerStatus.DELETE_IN_PROGRESS);
assertEquals("Wrong number of containers in containerList", 5, containerList.size());
for (Container container : containerList) {
assertSame("Container status mismatch", container.getStatus(), ContainerStatus.DELETE_IN_PROGRESS);
}
}
use of com.github.ambry.account.Container 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();
}
use of com.github.ambry.account.Container in project ambry by linkedin.
the class AccountDao method getContainerByName.
/**
* Gets container by its name and parent account Id.
* @param accountId the id for the parent account.
* @param containerName name of the container.
* @return {@link Container} if found in mysql db or {@code null} if it doesn't exist.
* @throws SQLException
*/
public synchronized Container getContainerByName(int accountId, String containerName) throws SQLException {
long startTimeMs = System.currentTimeMillis();
ResultSet rs = null;
try {
PreparedStatement getContainerByNameStatement = dataAccessor.getPreparedStatement(getContainerByNameSql, false);
getContainerByNameStatement.setInt(1, accountId);
getContainerByNameStatement.setString(2, containerName);
rs = getContainerByNameStatement.executeQuery();
List<Container> containers = convertContainersResultSet(rs);
dataAccessor.onSuccess(Read, System.currentTimeMillis() - startTimeMs);
return containers.isEmpty() ? null : containers.get(0);
} catch (SQLException e) {
dataAccessor.onException(e, Read);
throw e;
} finally {
closeQuietly(rs);
}
}
use of com.github.ambry.account.Container in project ambry by linkedin.
the class AccountDao method getContainers.
/**
* Gets the containers in a specified account.
* @param accountId the id for the parent account.
* @return a list of {@link Container}s.
* @throws SQLException
*/
public synchronized List<Container> getContainers(int accountId) throws SQLException {
long startTimeMs = System.currentTimeMillis();
ResultSet rs = null;
try {
PreparedStatement getByAccountStatement = dataAccessor.getPreparedStatement(getContainersByAccountSql, false);
getByAccountStatement.setInt(1, accountId);
rs = getByAccountStatement.executeQuery();
List<Container> containers = convertContainersResultSet(rs);
dataAccessor.onSuccess(Read, System.currentTimeMillis() - startTimeMs);
return containers;
} catch (SQLException e) {
dataAccessor.onException(e, Read);
throw e;
} finally {
closeQuietly(rs);
}
}
Aggregations