Search in sources :

Example 16 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 17 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 18 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 19 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 20 with Account

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

the class AccountContainerTest method testAccountBuilder.

/**
 * Tests building an {@link Account} using {@link AccountBuilder}.
 * @throws JSONException
 */
@Test
public void testAccountBuilder() throws JSONException {
    // build an account with arguments supplied
    AccountBuilder accountBuilder = new AccountBuilder(refAccountId, refAccountName, refAccountStatus, refQuotaResourceType).snapshotVersion(refAccountSnapshotVersion).aclInheritedByContainer(refAccountAclInheritedByContainer);
    Account accountByBuilder = accountBuilder.build();
    assertAccountAgainstReference(accountByBuilder, false, false);
    // set containers
    for (int i = 0; i < CONTAINER_COUNT; i++) {
        Container container = containerFromJson(containerJsonList.get(i), refAccountId);
        accountBuilder.addOrUpdateContainer(container);
    }
    accountByBuilder = accountBuilder.build();
    assertAccountAgainstReference(accountByBuilder, true, true);
    // build an account from existing account
    accountBuilder = new AccountBuilder(accountByBuilder);
    Account account2ByBuilder = accountBuilder.build();
    assertAccountAgainstReference(account2ByBuilder, true, true);
    // clear containers
    Account account3ByBuilder = new AccountBuilder(account2ByBuilder).containers(null).build();
    assertAccountAgainstReference(account3ByBuilder, false, false);
    assertTrue("Container list should be empty.", account3ByBuilder.getAllContainers().isEmpty());
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) 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