/** * Tests starting up a {@link HelixAccountService}, when the corresponding {@code ZooKeeper} has account metadata * already stored on it. * @throws Exception Any unexpected exception. */ @Test public void testStartUpWithMetadataExists() throws Exception { // pre-populate account metadata in ZK. writeAccountsToHelixPropertyStore(idToRefAccountMap.values(), false); // When start, the helixAccountService should get the account metadata. accountService = mockHelixAccountServiceFactory.getAccountService(); assertAccountsInAccountService(idToRefAccountMap.values(), NUM_REF_ACCOUNT, accountService); }
/** * Tests a clean startup of {@link HelixAccountService}, when the corresponding {@code ZooKeeper} does not * have any {@link ZNRecord} on it. * @throws Exception Any unexpected exception. */ @Test public void testStartUpWithoutMetadataExists() { accountService = mockHelixAccountServiceFactory.getAccountService(); // At time zero, no account metadata exists. assertEquals("The number of account in HelixAccountService is incorrect after clean startup", 0, accountService.getAllAccounts().size()); }
/** * 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 creating a number of new {@link Account} through {@link HelixAccountService}, where there is no {@link ZNRecord} * exists on the {@code ZooKeeper}. */ @Test public void testCreateAccount() { accountService = mockHelixAccountServiceFactory.getAccountService(); assertEquals("The number of account in HelixAccountService is incorrect", 0, accountService.getAllAccounts().size()); boolean res = accountService.updateAccounts(idToRefAccountMap.values()); assertTrue("Failed to update accounts", res); assertAccountsInAccountService(idToRefAccountMap.values(), NUM_REF_ACCOUNT, accountService); }
/** * 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}. */ @Test public void testDisableBackgroundUpdater() { helixConfigProps.setProperty(HelixAccountServiceConfig.UPDATER_POLLING_INTERVAL_MS_KEY, "0"); 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(); assertEquals("Wrong number of thread for account updater.", 0, numThreadsByThisName(updaterThreadPrefix)); }
/** * 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()); }
/** * 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 updating a collection of {@link Account}s, where the {@link Account}s are conflicting among themselves * in name. * @throws Exception Any unexpected exception. */ @Test public void testUpdateNameConflictingAccounts() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); List<Account> conflictAccounts = new ArrayList<>(); conflictAccounts.add(new AccountBuilder((short) 1, "a", AccountStatus.INACTIVE).build()); conflictAccounts.add(new AccountBuilder((short) 2, "a", AccountStatus.INACTIVE).build()); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); }
/** * Tests updating a collection of {@link Account}s, where there are duplicate {@link Account}s in id and name. * @throws Exception Any unexpected exception. */ @Test public void testUpdateDuplicateAccounts() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); List<Account> conflictAccounts = new ArrayList<>(); conflictAccounts.add(new AccountBuilder((short) 1, "a", AccountStatus.INACTIVE).build()); conflictAccounts.add(new AccountBuilder((short) 1, "a", AccountStatus.INACTIVE).build()); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); }
/** * 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 collection of {@link Account}s, where the {@link Account}s are conflicting among themselves * in id. * @throws Exception Any unexpected exception. */ @Test public void testUpdateIdConflictingAccounts() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); List<Account> conflictAccounts = new ArrayList<>(); conflictAccounts.add(new AccountBuilder((short) 1, "a", AccountStatus.INACTIVE).build()); conflictAccounts.add(new AccountBuilder((short) 1, "b", AccountStatus.INACTIVE).build()); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); }
/** * Tests the background updater for updating accounts from remote. During the initialization of * {@link HelixAccountService}, its internal {@link HelixPropertyStore} will be read to first time get account data. * Because of the background account updater, it should continuously make get calls to the {@link HelixPropertyStore}, * even no notification for account updates is received. Therefore, there will be more than 1 get calls to the * {@link HelixPropertyStore}. * @throws Exception */ @Test public void testBackgroundUpdater() throws Exception { helixConfigProps.setProperty(HelixAccountServiceConfig.UPDATER_POLLING_INTERVAL_MS_KEY, "1"); 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(); CountDownLatch latch = new CountDownLatch(1); mockHelixAccountServiceFactory.getHelixStore(ZK_CONNECT_STRING, storeConfig).setReadLatch(latch); assertEquals("Wrong number of thread for account updater.", 1, numThreadsByThisName(updaterThreadPrefix)); awaitLatchOrTimeout(latch, 100); }
/** * 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); }
/** * 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); }
/** * 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 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); }
/** * Tests updating a {@link Account}, which has a new id but a name conflicting with an existing record. The update * operation will fail. This test corresponds to case D specified in the JavaDoc of {@link AccountService}. * @throws Exception Any unexpected exception. */ @Test public void testConflictingUpdateCaseD() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Collection<Account> conflictAccounts = Collections.singleton((new AccountBuilder((short) 3, "a", AccountStatus.INACTIVE).build())); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); assertEquals("Wrong account number in HelixAccountService", 2, accountService.getAllAccounts().size()); assertNull("Wrong account got from HelixAccountService", accountService.getAccountById((short) 3)); }
/** * Tests updating a {@link Account}, which has the same id as an existing record, but the name conflicting with * another existing record. The update operation will fail. This test corresponds to case E specified in the JavaDoc * of {@link AccountService}. * @throws Exception Any unexpected exception. */ @Test public void testConflictingUpdateCaseE() throws Exception { accountService = mockHelixAccountServiceFactory.getAccountService(); // write two accounts (1, "a") and (2, "b") writeAccountsForConflictTest(); Collection<Account> conflictAccounts = Collections.singleton((new AccountBuilder((short) 1, "b", AccountStatus.INACTIVE).build())); assertFalse("Wrong return value from update operation.", accountService.updateAccounts(conflictAccounts)); assertEquals("Wrong account number in HelixAccountService", 2, accountService.getAllAccounts().size()); assertEquals("Wrong account name got from HelixAccountService", "a", accountService.getAccountById((short) 1).getName()); }
/** * Tests a series of operations. * 1. PrePopulates account (1, "a"); * 2. Starts up a {@link HelixAccountService}; * 3. Remote copy adds a new account (2, "b"), and the update has not been propagated to the {@link HelixAccountService}; * 4. The {@link HelixAccountService} attempts to update an account (3, "b"), which should fail because it will eventually * conflict with the remote copy; * @throws Exception Any unexpected exception. */ @Test public void testReadConflictAccountDataFromHelixPropertyStoreCase3() throws Exception { Account account1 = new AccountBuilder((short) 1, "a", AccountStatus.INACTIVE).build(); List<Account> accounts = Collections.singletonList(account1); writeAccountsToHelixPropertyStore(accounts, false); accountService = mockHelixAccountServiceFactory.getAccountService(); assertAccountInAccountService(account1, accountService); Account account2 = new AccountBuilder((short) 2, "b", AccountStatus.INACTIVE).build(); accounts = Collections.singletonList(account2); writeAccountsToHelixPropertyStore(accounts, false); Account conflictingAccount = new AccountBuilder((short) 3, "b", AccountStatus.INACTIVE).build(); accounts = Collections.singletonList(conflictingAccount); assertFalse(accountService.updateAccounts(accounts)); assertEquals("Number of account is wrong.", 1, accountService.getAllAccounts().size()); assertAccountInAccountService(account1, accountService); }
/** * 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); }