Search in sources :

Example 51 with Account

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

the class HelixAccountServiceTest method testNonConflictingUpdateCaseA.

/**
 * Tests updating a {@link Account}, which has the same id and name as an existing record, and will replace the
 * existing record. This test corresponds to case A specified in the JavaDoc of {@link AccountService}.
 * @throws Exception Any unexpected exception.
 */
@Test
public void testNonConflictingUpdateCaseA() 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 52 with Account

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

the class HelixAccountServiceTest method testReadConflictAccountDataFromHelixPropertyStoreCase2.

/**
 * Tests reading conflicting {@link Account} metadata from {@link org.apache.helix.store.HelixPropertyStore}.
 * @throws Exception Any unexpected exception.
 */
@Test
public void testReadConflictAccountDataFromHelixPropertyStoreCase2() throws Exception {
    List<Account> conflictAccounts = new ArrayList<>();
    Account account1 = new AccountBuilder((short) 1, "a", AccountStatus.INACTIVE).build();
    Account account2 = new AccountBuilder((short) 2, "a", AccountStatus.INACTIVE).build();
    Account account3 = new AccountBuilder((short) 2, "b", AccountStatus.INACTIVE).build();
    Account account4 = new AccountBuilder((short) 3, "b", AccountStatus.INACTIVE).build();
    conflictAccounts.add(account1);
    conflictAccounts.add(account2);
    conflictAccounts.add(account3);
    conflictAccounts.add(account4);
    readAndUpdateBadRecord(conflictAccounts);
}
Also used : Account(com.github.ambry.account.Account) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 53 with Account

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

the class HelixAccountServiceTest method writeAccountsToHelixPropertyStore.

/**
 * Pre-populates a collection of {@link Account}s to the underlying {@link org.apache.helix.store.HelixPropertyStore}
 * using {@link com.github.ambry.clustermap.HelixStoreOperator} (not through the {@link HelixAccountService}). This method
 * does not check any conflict among the {@link Account}s to write.
 * @throws Exception Any unexpected exception.
 */
private void writeAccountsToHelixPropertyStore(Collection<Account> accounts, boolean shouldNotify) throws Exception {
    HelixStoreOperator storeOperator = new HelixStoreOperator(mockHelixAccountServiceFactory.getHelixStore(ZK_CONNECT_STRING, storeConfig));
    ZNRecord zNRecord = new ZNRecord(String.valueOf(System.currentTimeMillis()));
    Map<String, String> accountMap = new HashMap<>();
    for (Account account : accounts) {
        accountMap.put(String.valueOf(account.getId()), objectMapper.writeValueAsString(new AccountBuilder(account).snapshotVersion(refAccount.getSnapshotVersion() + 1).build()));
    }
    if (useNewZNodePath) {
        String blobID = RouterStore.writeAccountMapToRouter(accountMap, mockRouter);
        List<String> list = Collections.singletonList(new RouterStore.BlobIDAndVersion(blobID, 1).toJson());
        zNRecord.setListField(RouterStore.ACCOUNT_METADATA_BLOB_IDS_LIST_KEY, list);
        storeOperator.write(RouterStore.ACCOUNT_METADATA_BLOB_IDS_PATH, zNRecord);
    } else {
        zNRecord.setMapField(LegacyMetadataStore.ACCOUNT_METADATA_MAP_KEY, accountMap);
        // Write account metadata into HelixPropertyStore.
        storeOperator.write(LegacyMetadataStore.FULL_ACCOUNT_METADATA_PATH, zNRecord);
    }
    if (shouldNotify) {
        notifier.publish(ACCOUNT_METADATA_CHANGE_TOPIC, FULL_ACCOUNT_METADATA_CHANGE_MESSAGE);
    }
}
Also used : HelixStoreOperator(com.github.ambry.clustermap.HelixStoreOperator) Account(com.github.ambry.account.Account) HashMap(java.util.HashMap) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord)

Example 54 with Account

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

the class AccountDao method convertAccountsResultSet.

/**
 * Convert a query result set to a list of accounts.
 * @param resultSet the result set.
 * @return a list of {@link Account}s.
 * @throws SQLException
 */
private List<Account> convertAccountsResultSet(ResultSet resultSet) throws SQLException {
    List<Account> accounts = new ArrayList<>();
    while (resultSet.next()) {
        String accountJson = resultSet.getString(ACCOUNT_INFO);
        Timestamp lastModifiedTime = resultSet.getTimestamp(LAST_MODIFIED_TIME);
        int version = resultSet.getInt(VERSION);
        try {
            Account account = new AccountBuilder(objectMapper.readValue(accountJson, Account.class)).lastModifiedTime(lastModifiedTime.getTime()).snapshotVersion(version).build();
            accounts.add(account);
        } catch (IOException e) {
            throw new SQLException(String.format("Faild to deserialize string [{}] to account object", accountJson), e);
        }
    }
    return accounts;
}
Also used : Account(com.github.ambry.account.Account) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) AccountBuilder(com.github.ambry.account.AccountBuilder) IOException(java.io.IOException) Timestamp(java.sql.Timestamp)

Example 55 with Account

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

the class HelixAccountServiceTest method assertAccountUpdateConsumers.

/**
 * Asserts the {@link Account}s received by the {@link Consumer} are as expected.
 * @param expectedAccounts The expected collection of {@link Account}s that should be received by the {@link Consumer}s.
 * @param expectedNumberOfConsumers The expected number of {@link Consumer}s.
 * @param accountsInConsumers A list of collection of {@link Account}s, where each collection of {@link Account}s are
 *                            received by one {@link Consumer}.
 */
private void assertAccountUpdateConsumers(Set<Account> expectedAccounts, int expectedNumberOfConsumers, List<Collection<Account>> accountsInConsumers) throws Exception {
    assertEquals("Wrong number of consumers", expectedNumberOfConsumers, accountsInConsumers.size());
    for (Collection<Account> accounts : accountsInConsumers) {
        assertEquals("Wrong number of updated accounts received by consumers", expectedAccounts.size(), accounts.size());
        for (Account account : accounts) {
            assertTrue("Account update not received by consumers", expectedAccounts.contains(account));
        }
        TestUtils.assertException(UnsupportedOperationException.class, () -> accounts.add(InMemoryUnknownAccountService.UNKNOWN_ACCOUNT), null);
    }
}
Also used : Account(com.github.ambry.account.Account)

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