@Override public Account getAccountById(short id) { checkOpen(); return accountInfoMapRef.get().getAccountById(id); }
/** * Checks a collection of {@link Account}s if there is any conflict between the {@link Account}s to update and the * {@link Account}s in the local cache. * * @param accountsToSet The collection of {@link Account}s to check with the local cache. * @return {@code true} if there is at least one {@link Account} in {@code accountsToSet} conflicts with the * {@link Account}s in the cache, {@code false} otherwise. */ private boolean hasConflictWithCache(Collection<Account> accountsToSet) { return hasConflictingAccount(accountsToSet, accountInfoMapRef.get()); }
@Override public AccountService getAccountService() { try { long startTimeMs = System.currentTimeMillis(); logger.info("Starting a HelixAccountService"); HelixPropertyStore<ZNRecord> helixStore = CommonUtils.createHelixPropertyStore(accountServiceConfig.zkClientConnectString, storeConfig, null); logger.info("HelixPropertyStore started with zkClientConnectString={}, zkClientSessionTimeoutMs={}, " + "zkClientConnectionTimeoutMs={}, rootPath={}", accountServiceConfig.zkClientConnectString, storeConfig.zkClientSessionTimeoutMs, storeConfig.zkClientConnectionTimeoutMs, storeConfig.rootPath); ScheduledExecutorService scheduler = accountServiceConfig.updaterPollingIntervalMs > 0 ? Utils.newScheduler(1, HELIX_ACCOUNT_UPDATER_PREFIX, false) : null; HelixAccountService helixAccountService = new HelixAccountService(helixStore, accountServiceMetrics, notifier, scheduler, accountServiceConfig); long spentTimeMs = System.currentTimeMillis() - startTimeMs; logger.info("HelixAccountService started, took {} ms", spentTimeMs); accountServiceMetrics.startupTimeInMs.update(spentTimeMs); return helixAccountService; } catch (Exception e) { throw new IllegalStateException("Could not instantiate HelixAccountService", e); } } }
checkOpen(); Objects.requireNonNull(accounts, "accounts cannot be null"); if (hasDuplicateAccountIdOrName(accounts)) { if (hasConflictWithCache(accounts)) { logger.debug("Accounts={} conflict with the accounts in local cache. Cancel the update operation.", accounts); accountServiceMetrics.updateAccountErrorCount.inc();
/** * To be used to subscribe to a {@link Notifier} topic. Upon receiving a * {@link #FULL_ACCOUNT_METADATA_CHANGE_MESSAGE}, it will check for any account updates. * @param topic The topic. * @param message The message for the topic. */ private void onAccountChangeMessage(String topic, String message) { if (!open.get()) { // take no action instead of throwing an exception to silence noisy log messages when a message is received while // closing the AccountService. return; } logger.trace("Start to process message={} for topic={}", message, topic); try { switch (message) { case FULL_ACCOUNT_METADATA_CHANGE_MESSAGE: logger.trace("Start processing message={} for topic={}", message, topic); readFullAccountAndUpdateCache(FULL_ACCOUNT_METADATA_PATH, true); logger.trace("Completed processing message={} for topic={}", message, topic); break; default: accountServiceMetrics.unrecognizedMessageErrorCount.inc(); throw new RuntimeException("Could not understand message=" + message + " for topic=" + topic); } } catch (Exception e) { logger.error("Exception occurred when processing message={} for topic={}.", message, topic, e); accountServiceMetrics.remoteDataCorruptionErrorCount.inc(); accountServiceMetrics.fetchRemoteAccountErrorCount.inc(); } }
AccountInfoMap newAccountInfoMap = new AccountInfoMap(remoteAccountMap); AccountInfoMap oldAccountInfoMap = accountInfoMapRef.getAndSet(newAccountInfoMap); notifyAccountUpdateConsumers(newAccountInfoMap, oldAccountInfoMap, isCalledFromListener);
@Override public AccountService getAccountService() { try { ScheduledExecutorService scheduler = accountServiceConfig.updaterPollingIntervalMs > 0 ? Utils.newScheduler(1, updaterThreadPrefix, false) : null; return new HelixAccountService(getHelixStore(accountServiceConfig.zkClientConnectString, storeConfig), accountServiceMetrics, notifier, scheduler, accountServiceConfig); } catch (Exception e) { throw new IllegalStateException("Could not instantiate HelixAccountService", e); } }
readFullAccountAndUpdateCache(FULL_ACCOUNT_METADATA_PATH, false); } catch (Exception e) { logger.error("Exception occurred when fetching remote account data", e);
@Override public Collection<Account> getAllAccounts() { checkOpen(); return accountInfoMapRef.get().getAccounts(); }
@Override public Account getAccountByName(String accountName) { checkOpen(); Objects.requireNonNull(accountName, "accountName cannot be null."); return accountInfoMapRef.get().getAccountByName(accountName); }
@Override public boolean addAccountUpdateConsumer(Consumer<Collection<Account>> accountUpdateConsumer) { checkOpen(); Objects.requireNonNull(accountUpdateConsumer, "accountUpdateConsumer to subscribe cannot be null"); return accountUpdateConsumers.add(accountUpdateConsumer); }
@Override public boolean removeAccountUpdateConsumer(Consumer<Collection<Account>> accountUpdateConsumer) { checkOpen(); Objects.requireNonNull(accountUpdateConsumer, "accountUpdateConsumer to unsubscribe cannot be null"); return accountUpdateConsumers.remove(accountUpdateConsumer); }