/** * Pre-populates two {@link Account}s: (id=1, name="a") and (id=2, name="b") in * {@link org.apache.helix.store.HelixPropertyStore}. * @throws Exception Any unexpected exception. */ private void writeAccountsForConflictTest() throws Exception { List<Account> existingAccounts = new ArrayList<>(); existingAccounts.add(new AccountBuilder((short) 1, "a", AccountStatus.INACTIVE).build()); existingAccounts.add(new AccountBuilder((short) 2, "b", AccountStatus.INACTIVE).build()); updateAccountsAndAssertAccountExistence(existingAccounts, 2, true); }
/** * Tests disabling the background thread. By setting the polling interval to 0ms, the accounts should not be fetched. * Therefore, after the {@link HelixAccountService} starts, there should be a single get call to the * {@link HelixPropertyStore}. * @throws Exception */ @Test public void testDisabledBackups() throws Exception { helixConfigProps.remove(HelixAccountServiceConfig.BACKUP_DIRECTORY_KEY); vHelixConfigProps = new VerifiableProperties(helixConfigProps); storeConfig = new HelixPropertyStoreConfig(vHelixConfigProps); String updaterThreadPrefix = UUID.randomUUID().toString(); MockHelixAccountServiceFactory mockHelixAccountServiceFactory = new MockHelixAccountServiceFactory(vHelixConfigProps, new MetricRegistry(), notifier, updaterThreadPrefix); accountService = mockHelixAccountServiceFactory.getAccountService(); updateAccountsAndAssertAccountExistence(Collections.singleton(refAccount), 1, true); }
/** * Tests receiving a bad message, it will not be recognized by {@link HelixAccountService}, but will also not * crash the service. * @throws Exception Any unexpected exception. */ @Test public void receiveBadMessage() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); updateAccountsAndAssertAccountExistence(idToRefAccountMap.values(), NUM_REF_ACCOUNT, true); notifier.publish(ACCOUNT_METADATA_CHANGE_TOPIC, "badMessage"); assertEquals("The number of account in HelixAccountService is different from expected", NUM_REF_ACCOUNT, accountService.getAllAccounts().size()); }
/** * Tests receiving a bad topic, it will not be recognized by {@link HelixAccountService}, but will also not * crash the service. * @throws Exception Any unexpected exception. */ @Test public void receiveBadTopic() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); updateAccountsAndAssertAccountExistence(idToRefAccountMap.values(), NUM_REF_ACCOUNT, true); notifier.publish("badTopic", FULL_ACCOUNT_METADATA_CHANGE_MESSAGE); assertEquals("The number of account in HelixAccountService is different from expected", NUM_REF_ACCOUNT, accountService.getAllAccounts().size()); }
/** * PrePopulates a collection of self-conflicting {@link Account}s, which will impact {@link HelixAccountService} * startup and update. * @param accounts The self-conflicting {@link Account}s. */ private void readAndUpdateBadRecord(Collection<Account> accounts) throws Exception { writeAccountsToHelixPropertyStore(accounts, false); accountService = mockHelixAccountServiceFactory.getAccountService(); assertEquals("Wrong number of accounts in helixAccountService", 0, accountService.getAllAccounts().size()); updateAccountsAndAssertAccountExistence(Collections.singletonList(refAccount), 0, false); writeAccountsToHelixPropertyStore(accounts, true); assertEquals("Number of account is wrong.", 0, accountService.getAllAccounts().size()); }
/** * Tests updating a {@link Account}, which has a new id and name different from any of the existing record. The * new record will replace the existing record. This test corresponds to case C specified in the JavaDoc of * {@link AccountService}. * @throws Exception Any unexpected exception. */ @Test public void testNonConflictingUpdateCaseC() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Collection<Account> nonConflictAccounts = Collections.singleton((new AccountBuilder((short) 3, "c", AccountStatus.ACTIVE).build())); updateAccountsAndAssertAccountExistence(nonConflictAccounts, 3, true); }
updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true); assertAccountUpdateConsumers(accountsToUpdate, numOfConsumers, updatedAccountsReceivedByConsumers); accountsToUpdate.add(accountBuilder.build()); updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true); assertAccountUpdateConsumers(accountsToUpdate, numOfConsumers, updatedAccountsReceivedByConsumers); 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);
/** * Does several operations: * 1. Writes a {@link ZNRecord} to {@link HelixPropertyStore} without notifying listeners; * 2. Starts up a {@link HelixAccountService} that should fetch the {@link ZNRecord}; * 3. Updates (creates) one more {@link Account} through the {@link HelixAccountService}; * 4. Writes the same {@link ZNRecord} to {@link HelixPropertyStore} and publishes a message for {@link Account} update; * * If the {@link ZNRecord} is good, it should not affect updating operation. * @param zNRecord A {@link ZNRecord} to write to {@link HelixPropertyStore}. * @param isGoodZNRecord {code true} to indicate the {@link ZNRecord} is good, which should not affect updating * operation; {@code false} otherwise. * @throws Exception Any unexpected exception. */ private void updateAndWriteZNRecord(ZNRecord zNRecord, boolean isGoodZNRecord) throws Exception { writeZNRecordToHelixPropertyStore(zNRecord, false); accountService = mockHelixAccountServiceFactory.getAccountService(); assertEquals("Number of account is wrong", 0, accountService.getAllAccounts().size()); updateAccountsAndAssertAccountExistence(Collections.singletonList(refAccount), isGoodZNRecord ? 1 : 0, isGoodZNRecord); writeZNRecordToHelixPropertyStore(zNRecord, true); if (isGoodZNRecord) { assertAccountInAccountService(refAccount, accountService); } else { assertEquals("Number of accounts is wrong.", 0, accountService.getAllAccounts().size()); } }
/** * 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); }
/** * 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); }
updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true); accountsToUpdate.add(accountBuilder.build()); updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true); updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true); updateAccountsAndAssertAccountExistence(accountsToUpdate, 1 + NUM_REF_ACCOUNT, true);
/** * Test updating an account with a conflicting expected snapshot version. */ @Test public void testConflictingSnapshotVersionUpdate() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Account expectedAccount = accountService.getAccountById((short) 1); int currentSnapshotVersion = expectedAccount.getSnapshotVersion(); for (int snapshotVersionOffset : new int[]{-2, -1, 1}) { int snapshotVersionToUse = currentSnapshotVersion + snapshotVersionOffset; Collection<Account> conflictAccounts = Collections.singleton( new AccountBuilder((short) 1, "c", AccountStatus.INACTIVE).snapshotVersion(snapshotVersionToUse).build()); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); assertEquals("Wrong account number in HelixAccountService", 2, accountService.getAllAccounts().size()); Account account = accountService.getAccountById((short) 1); assertEquals("Account should not have been updated", expectedAccount, account); assertEquals("Snapshot version should not have been updated", currentSnapshotVersion, account.getSnapshotVersion()); } Collection<Account> validAccounts = Collections.singleton( new AccountBuilder((short) 1, "c", AccountStatus.INACTIVE).snapshotVersion(currentSnapshotVersion).build()); updateAccountsAndAssertAccountExistence(validAccounts, 2, true); }