public HostsType getMasterAndHosts(String serviceName, String componentName) {
    if (serviceName == null || componentName == null) {
      return null;
    }
    LinkedHashSet<String> componentHosts = new LinkedHashSet<>(m_cluster.getHosts(serviceName, componentName));
    if (componentHosts.isEmpty()) {
      return null;
    }

    HostsType hostsType = HostsType.normal(componentHosts);

    try {
      switch (Service.fromString(serviceName)) {
        case HDFS:
          if (componentName.equalsIgnoreCase("NAMENODE") && componentHosts.size() >= 2) {
            try {
              hostsType = HostsType.federated(nameSpaces(componentHosts), componentHosts);
            } catch (ClassifyNameNodeException | IllegalArgumentException e) {
              if (componentHosts.size() == 2) { // in HA mode guess if cannot determine active/standby
                hostsType = HostsType.guessHighAvailability(componentHosts);
                LOG.warn(
                  "Could not determine the active/standby states from NameNodes {}. Using {} as active and {} as standbys.",
                  componentHosts, hostsType.getMasters(), hostsType.getSecondaries());
              } else {
                // XXX fallback to HostsType.normal unsure how to handle this
                LOG.warn("Could not determine the active/standby states of federated NameNode from NameNodes {}.", componentHosts);
              }
            }
          }
          break;
        case YARN:
          if (componentName.equalsIgnoreCase("RESOURCEMANAGER")) {
            hostsType = resolveResourceManagers(getCluster(), componentHosts);
          }
          break;
        case HBASE:
          if (componentName.equalsIgnoreCase("HBASE_MASTER")) {
            hostsType = resolveHBaseMasters(getCluster(), componentHosts);
          }
          break;
        default:
          break;
      }
    } catch (Exception err) {
      LOG.error("Unable to get master and hosts for Component " + componentName + ". Error: " + err.getMessage(), err);
    }
    return filterHosts(hostsType, serviceName, componentName);
  }
Get the master hostname of the given service and component.
@param serviceName Service
@param componentName Component
@return The hostname that is the master of the service and component if successful, null otherwise.
ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java