public static int compareAccounts(Collection<Account> accountsInPrimary, Collection<Account> accountsInSecondary) {
    int mismatchCount = 0;
    Map<String, Account> secondaryAccountMap = new HashMap<>();
    accountsInSecondary.forEach(account -> secondaryAccountMap.put(account.getName(), account));

    Set<Account> accountsMissingInSecondary = accountsInPrimary.stream()
        .filter(account -> !secondaryAccountMap.containsKey(account.getName()))
        .collect(Collectors.toSet());

    Set<Account> accountsDifferentInSecondary = new HashSet<>(accountsInPrimary);
    accountsDifferentInSecondary.removeAll(accountsInSecondary);
    accountsDifferentInSecondary.removeAll(accountsMissingInSecondary);

    if (!accountsMissingInSecondary.isEmpty() || !accountsDifferentInSecondary.isEmpty()) {

      StringBuilder accountsInfo = new StringBuilder();

      try {
        if (!accountsMissingInSecondary.isEmpty()) {
          accountsInfo.append("[");
          for (Account account : accountsMissingInSecondary) {
            accountsInfo.append(new String(AccountCollectionSerde.serializeAccountsInJsonNoContainers(account)))
                .append(",");
          }
          accountsInfo.append("]");
          logger.warn("Accounts found in primary and absent in secondary = {}", accountsInfo.toString());
          mismatchCount += accountsMissingInSecondary.size();
        }

        if (!accountsDifferentInSecondary.isEmpty()) {
          accountsInfo.setLength(0);
          accountsInfo.append("[");
          for (Account account : accountsDifferentInSecondary) {
            accountsInfo.append("{Account = ")
                .append(account.toString())
                .append(", primary = ")
                .append(new String(AccountCollectionSerde.serializeAccountsInJsonNoContainers(account)))
                .append(", secondary = ")
                .append(new String(AccountCollectionSerde.serializeAccountsInJsonNoContainers(
                    secondaryAccountMap.get(account.getName()))));

            Set<Container> containersMissingInSecondary = account.getAllContainers()
                .stream()
                .filter(container -> secondaryAccountMap.get(account.getName()).getContainerByName(container.getName())
                    == null)
                .collect(Collectors.toSet());

            Set<Container> containersDifferentInSecondary = new HashSet<>(account.getAllContainers());
            containersDifferentInSecondary.removeAll(secondaryAccountMap.get(account.getName()).getAllContainers());
            containersDifferentInSecondary.removeAll(containersMissingInSecondary);

            if (!containersMissingInSecondary.isEmpty()) {
              accountsInfo.append(", Containers missing in secondary: [");
              for (Container container : containersMissingInSecondary) {
                accountsInfo.append(objectMapper.writeValueAsString(container)).append(",");
              }
              accountsInfo.append("]");
              mismatchCount += containersMissingInSecondary.size();
            }

            if (!containersDifferentInSecondary.isEmpty()) {
              accountsInfo.append(", Containers different in secondary: [");
              for (Container container : containersDifferentInSecondary) {
                accountsInfo.append("{container = ")
                    .append(container.toString())
                    .append(", primary = ")
                    .append(objectMapper.writeValueAsString(container))
                    .append(", secondary = ")
                    .append(objectMapper.writeValueAsString(
                        secondaryAccountMap.get(account.getName()).getContainerByName(container.getName())))
                    .append("},");
              }
              accountsInfo.append("]");
              mismatchCount += containersDifferentInSecondary.size();
            }
            accountsInfo.append("}");
          }
          accountsInfo.append("]");

          logger.warn("Accounts mismatch in primary and secondary = {}", accountsInfo.toString());
        }
      } catch (IOException e) {
        throw new RuntimeException("Fail to serialize accounts", e);
      }
    }
    return mismatchCount;
  }
Compares and logs differences (if any) in Accounts.
@param accountsInPrimary accounts in primary collection.
@param accountsInSecondary accounts in secondary collection.
@return count of accounts mismatching.
ambry-account/src/main/java/com/github/ambry/account/AccountUtils.java