private HostsType.HighAvailabilityHosts findMasterAndSecondaries(NameService nameService, Set<String> componentHosts) throws ClassifyNameNodeException {
    String master = null;
    List<String> secondaries = new ArrayList<>();
    for (NameService.NameNode nameNode : nameService.getNameNodes()) {
      checkForDualNetworkCards(componentHosts, nameNode);
      String state = queryJmxBeanValue(nameNode.getHost(), nameNode.getPort(), "Hadoop:service=NameNode,name=NameNodeStatus", "State", true, nameNode.isEncrypted());
      if (Status.ACTIVE.toString().equalsIgnoreCase(state)) {
        master = nameNode.getHost();
      } else if (Status.STANDBY.toString().equalsIgnoreCase(state)) {
        secondaries.add(nameNode.getHost());
      } else {
        LOG.error(String.format("Could not retrieve state for NameNode %s from property %s by querying JMX.", nameNode.getHost(), nameNode.getPropertyName()));
      }
    }
    if (masterAndSecondariesAreFound(componentHosts, master, secondaries)) {
      return new HostsType.HighAvailabilityHosts(master, secondaries);
    }
    throw new ClassifyNameNodeException(nameService);
  }
Find the master and secondary namenode(s) based on JMX NameNodeStatus.
ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java