@Test public void testUpdateNameConflictingAccounts() throws IOException { AccountServiceMetrics accountServiceMetrics = new AccountServiceMetrics(new MetricRegistry()); mySqlAccountService = new MySqlAccountService(accountServiceMetrics, new MySqlAccountServiceConfig(new VerifiableProperties(mySqlConfigProps)), mock(MySqlAccountStore.class)); List<Account> conflictAccounts = new ArrayList<>(); conflictAccounts.add(new AccountBuilder((short) 1, "a", Account.AccountStatus.INACTIVE).build()); conflictAccounts.add(new AccountBuilder((short) 2, "a", Account.AccountStatus.INACTIVE).build()); assertFalse("Wrong return value from update operation.", mySqlAccountService.updateAccounts(conflictAccounts)); assertEquals("UpdateAccountErrorCount in metrics should be 1", 1, accountServiceMetrics.updateAccountErrorCount.getCount()); }
@Override public boolean updateAccounts(Collection<Account> accounts) { Objects.requireNonNull(accounts, "accounts cannot be null"); if (accounts.isEmpty()) { logger.debug("Empty account collection to update."); return false; } if (mySqlAccountStore == null) { logger.warn("MySql Account DB store is not accessible"); return false; } if (config.updateDisabled) { logger.info("Updates have been disabled"); return false; } if (hasDuplicateAccountIdOrName(accounts)) { logger.error("Duplicate account id or name exist in the accounts to update"); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.readLock().lock(); try { AccountInfoMap accountInfoMap = accountInfoMapRef.get(); if (accountInfoMap.hasConflictingAccount(accounts)) { logger.error("Accounts={} conflict with the accounts in local cache. Cancel the update operation.", accounts); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } for (Account account : accounts) { if (accountInfoMap.hasConflictingContainer(account.getAllContainers(), account.getId())) { logger.error( "Containers={} under Account={} conflict with the containers in local cache. Cancel the update operation.", account.getAllContainers(), account.getId()); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } } } finally { infoMapLock.readLock().unlock(); } try { updateAccountsWithMySqlStore(accounts); } catch (SQLException e) { logger.error("Failed updating accounts={} in MySql DB", accounts, e); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.writeLock().lock(); try { accountInfoMapRef.get().addOrUpdateAccounts(accounts); } finally { infoMapLock.writeLock().unlock(); } return true; }
MySqlAccountService extends AbstractAccountService { @Override public boolean updateAccounts(Collection<Account> accounts) { Objects.requireNonNull(accounts, "accounts cannot be null"); if (accounts.isEmpty()) { logger.debug("Empty account collection to update."); return false; } if (mySqlAccountStore == null) { logger.warn("MySql Account DB store is not accessible"); return false; } if (config.updateDisabled) { logger.info("Updates have been disabled"); return false; } if (hasDuplicateAccountIdOrName(accounts)) { logger.error("Duplicate account id or name exist in the accounts to update"); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.readLock().lock(); try { AccountInfoMap accountInfoMap = accountInfoMapRef.get(); if (accountInfoMap.hasConflictingAccount(accounts)) { logger.error("Accounts={} conflict with the accounts in local cache. Cancel the update operation.", accounts); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } for (Account account : accounts) { if (accountInfoMap.hasConflictingContainer(account.getAllContainers(), account.getId())) { logger.error( "Containers={} under Account={} conflict with the containers in local cache. Cancel the update operation.", account.getAllContainers(), account.getId()); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } } } finally { infoMapLock.readLock().unlock(); } try { updateAccountsWithMySqlStore(accounts); } catch (SQLException e) { logger.error("Failed updating accounts={} in MySql DB", accounts, e); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.writeLock().lock(); try { accountInfoMapRef.get().addOrUpdateAccounts(accounts); } finally { infoMapLock.writeLock().unlock(); } return true; } }
MySqlAccountService extends AbstractAccountService { @Override public boolean updateAccounts(Collection<Account> accounts) { Objects.requireNonNull(accounts, "accounts cannot be null"); if (accounts.isEmpty()) { logger.debug("Empty account collection to update."); return false; } if (mySqlAccountStore == null) { logger.warn("MySql Account DB store is not accessible"); return false; } if (config.updateDisabled) { logger.info("Updates have been disabled"); return false; } if (hasDuplicateAccountIdOrName(accounts)) { logger.error("Duplicate account id or name exist in the accounts to update"); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.readLock().lock(); try { AccountInfoMap accountInfoMap = accountInfoMapRef.get(); if (accountInfoMap.hasConflictingAccount(accounts)) { logger.error("Accounts={} conflict with the accounts in local cache. Cancel the update operation.", accounts); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } for (Account account : accounts) { if (accountInfoMap.hasConflictingContainer(account.getAllContainers(), account.getId())) { logger.error( "Containers={} under Account={} conflict with the containers in local cache. Cancel the update operation.", account.getAllContainers(), account.getId()); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } } } finally { infoMapLock.readLock().unlock(); } try { updateAccountsWithMySqlStore(accounts); } catch (SQLException e) { logger.error("Failed updating accounts={} in MySql DB", accounts, e); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.writeLock().lock(); try { accountInfoMapRef.get().addOrUpdateAccounts(accounts); } finally { infoMapLock.writeLock().unlock(); } return true; } MySqlAccountService(AccountServiceMetrics accountServiceMetrics, MySqlAccountServiceConfig config,
      MySqlAccountStore mySqlAccountStore); }
MySqlAccountService extends AbstractAccountService { @Override public boolean updateAccounts(Collection<Account> accounts) { Objects.requireNonNull(accounts, "accounts cannot be null"); if (accounts.isEmpty()) { logger.debug("Empty account collection to update."); return false; } if (mySqlAccountStore == null) { logger.warn("MySql Account DB store is not accessible"); return false; } if (config.updateDisabled) { logger.info("Updates have been disabled"); return false; } if (hasDuplicateAccountIdOrName(accounts)) { logger.error("Duplicate account id or name exist in the accounts to update"); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.readLock().lock(); try { AccountInfoMap accountInfoMap = accountInfoMapRef.get(); if (accountInfoMap.hasConflictingAccount(accounts)) { logger.error("Accounts={} conflict with the accounts in local cache. Cancel the update operation.", accounts); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } for (Account account : accounts) { if (accountInfoMap.hasConflictingContainer(account.getAllContainers(), account.getId())) { logger.error( "Containers={} under Account={} conflict with the containers in local cache. Cancel the update operation.", account.getAllContainers(), account.getId()); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } } } finally { infoMapLock.readLock().unlock(); } try { updateAccountsWithMySqlStore(accounts); } catch (SQLException e) { logger.error("Failed updating accounts={} in MySql DB", accounts, e); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.writeLock().lock(); try { accountInfoMapRef.get().addOrUpdateAccounts(accounts); } finally { infoMapLock.writeLock().unlock(); } return true; } MySqlAccountService(AccountServiceMetrics accountServiceMetrics, MySqlAccountServiceConfig config,
      MySqlAccountStore mySqlAccountStore); @Override Account getAccountById(short accountId); @Override Account getAccountByName(String accountName); @Override boolean updateAccounts(Collection<Account> accounts); @Override Collection<Account> getAllAccounts(); @Override boolean addAccountUpdateConsumer(Consumer<Collection<Account>> accountUpdateConsumer); @Override boolean removeAccountUpdateConsumer(Consumer<Collection<Account>> accountUpdateConsumer); @Override void selectInactiveContainersAndMarkInZK(StatsSnapshot statsSnapshot); @Override void close(); }
MySqlAccountService extends AbstractAccountService { @Override public boolean updateAccounts(Collection<Account> accounts) { Objects.requireNonNull(accounts, "accounts cannot be null"); if (accounts.isEmpty()) { logger.debug("Empty account collection to update."); return false; } if (mySqlAccountStore == null) { logger.warn("MySql Account DB store is not accessible"); return false; } if (config.updateDisabled) { logger.info("Updates have been disabled"); return false; } if (hasDuplicateAccountIdOrName(accounts)) { logger.error("Duplicate account id or name exist in the accounts to update"); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.readLock().lock(); try { AccountInfoMap accountInfoMap = accountInfoMapRef.get(); if (accountInfoMap.hasConflictingAccount(accounts)) { logger.error("Accounts={} conflict with the accounts in local cache. Cancel the update operation.", accounts); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } for (Account account : accounts) { if (accountInfoMap.hasConflictingContainer(account.getAllContainers(), account.getId())) { logger.error( "Containers={} under Account={} conflict with the containers in local cache. Cancel the update operation.", account.getAllContainers(), account.getId()); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } } } finally { infoMapLock.readLock().unlock(); } try { updateAccountsWithMySqlStore(accounts); } catch (SQLException e) { logger.error("Failed updating accounts={} in MySql DB", accounts, e); accountServiceMetrics.updateAccountErrorCount.inc(); return false; } infoMapLock.writeLock().lock(); try { accountInfoMapRef.get().addOrUpdateAccounts(accounts); } finally { infoMapLock.writeLock().unlock(); } return true; } MySqlAccountService(AccountServiceMetrics accountServiceMetrics, MySqlAccountServiceConfig config,
      MySqlAccountStore mySqlAccountStore); @Override Account getAccountById(short accountId); @Override Account getAccountByName(String accountName); @Override boolean updateAccounts(Collection<Account> accounts); @Override Collection<Account> getAllAccounts(); @Override boolean addAccountUpdateConsumer(Consumer<Collection<Account>> accountUpdateConsumer); @Override boolean removeAccountUpdateConsumer(Consumer<Collection<Account>> accountUpdateConsumer); @Override void selectInactiveContainersAndMarkInZK(StatsSnapshot statsSnapshot); @Override void close();  }