Search in sources :

Example 56 with Account

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

the class HelixAccountServiceTest method testNonConflictingUpdateCaseB.

/**
 * Tests updating a {@link Account}, which has the same id as an existing record and a non-conflicting name with any
 * of the existing record. The new record will replace the existing record. This test corresponds to case B specified
 * in the JavaDoc of {@link AccountService}.
 * @throws Exception Any unexpected exception.
 */
@Test
public void testNonConflictingUpdateCaseB() throws Exception {
    accountService = mockHelixAccountServiceFactory.getAccountService();
    // write two accounts (1, "a") and (2, "b")
    writeAccountsForConflictTest();
    Account accountToUpdate = accountService.getAccountById((short) 1);
    Collection<Account> nonConflictAccounts = Collections.singleton((new AccountBuilder(accountToUpdate).status(AccountStatus.ACTIVE).build()));
    updateAccountsAndAssertAccountExistence(nonConflictAccounts, 2, true);
}
Also used : Account(com.github.ambry.account.Account) Test(org.junit.Test)

Example 57 with Account

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

the class HelixAccountServiceTest method testAccountUpdateConsumer.

/**
 * Tests adding/removing {@link Consumer}.
 * @throws Exception
 */
@Test
public void testAccountUpdateConsumer() throws Exception {
    // pre-populate account metadata in ZK.
    writeAccountsToHelixPropertyStore(idToRefAccountMap.values(), false);
    accountService = mockHelixAccountServiceFactory.getAccountService();
    assertAccountsInAccountService(idToRefAccountMap.values(), NUM_REF_ACCOUNT, accountService);
    // add consumer
    int numOfConsumers = 10;
    List<Collection<Account>> updatedAccountsReceivedByConsumers = new ArrayList<>();
    List<Consumer<Collection<Account>>> accountUpdateConsumers = IntStream.range(0, numOfConsumers).mapToObj(i -> (Consumer<Collection<Account>>) updatedAccountsReceivedByConsumers::add).peek(accountService::addAccountUpdateConsumer).collect(Collectors.toList());
    // listen to adding a new account
    Account newAccount = new AccountBuilder(refAccountId, refAccountName, refAccountStatus).build();
    Set<Account> accountsToUpdate = Collections.singleton(newAccount);
    updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);
    assertAccountUpdateConsumers(accountsToUpdate, numOfConsumers, updatedAccountsReceivedByConsumers);
    // listen to modification of existing accounts. Only updated accounts will be received by consumers.
    updatedAccountsReceivedByConsumers.clear();
    accountsToUpdate = new HashSet<>();
    for (Account account : accountService.getAllAccounts()) {
        AccountBuilder accountBuilder = new AccountBuilder(account);
        accountBuilder.name(account.getName() + "-extra");
        accountsToUpdate.add(accountBuilder.build());
    }
    updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);
    assertAccountUpdateConsumers(accountsToUpdate, numOfConsumers, updatedAccountsReceivedByConsumers);
    // removes the consumers so the consumers will not be informed.
    updatedAccountsReceivedByConsumers.clear();
    for (Consumer<Collection<Account>> accountUpdateConsumer : accountUpdateConsumers) {
        accountService.removeAccountUpdateConsumer(accountUpdateConsumer);
    }
    Account account = accountService.getAccountById(refAccountId);
    Account updatedAccount = new AccountBuilder(account).name(account.getName() + "-extra").build();
    accountsToUpdate = new HashSet<>(Collections.singleton(updatedAccount));
    updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);
    assertAccountUpdateConsumers(Collections.emptySet(), 0, updatedAccountsReceivedByConsumers);
}
Also used : Account(com.github.ambry.account.Account) Consumer(java.util.function.Consumer) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Test(org.junit.Test)

Example 58 with Account

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

the class HelixAccountServiceTest method checkBackupFileWithVersion.

/**
 * Check that the provided backup file matches the data in the corresponding serialized accounts.
 * @param expectedAccounts the expected {@link Account}s.
 * @param backupPath the {@link Path} to the backup file.
 * @throws JSONException
 */
private void checkBackupFileWithVersion(Collection<Account> expectedAccounts, Path backupPath) throws JSONException, IOException {
    try (BufferedReader reader = Files.newBufferedReader(backupPath)) {
        Account[] accounts = objectMapper.readValue(reader, Account[].class);
        assertEquals("unexpected array size", expectedAccounts.size(), accounts.length);
        Set<Account> expectedAccountSet = new HashSet<>(expectedAccounts);
        for (int i = 0; i < accounts.length; i++) {
            Account account = accounts[i];
            assertTrue("unexpected account in array: " + account.toString(), expectedAccountSet.contains(account));
        }
    }
}
Also used : Account(com.github.ambry.account.Account) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Example 59 with Account

use of com.github.ambry.account.Account 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);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 60 with Account

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

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