public static Collection<Host> getCandidateHosts(Cluster cluster, ExecuteHostType executeHostType,
      String serviceName, String componentName) {
    Collection<Host> candidates = cluster.getHosts();
    if (StringUtils.isNotBlank(serviceName) && StringUtils.isNotBlank(componentName)) {
      List<ServiceComponentHost> schs = cluster.getServiceComponentHosts(serviceName,componentName);
      candidates = schs.stream().map(sch -> sch.getHost()).collect(Collectors.toList());
    }

    if (candidates.isEmpty()) {
      return candidates;
    }

    // figure out where to add the new component
    List<Host> winners = Lists.newArrayList();
    switch (executeHostType) {
      case ALL:
        winners.addAll(candidates);
        break;
      case FIRST:
        winners.add(candidates.iterator().next());
        break;
      case MASTER:
        winners.add(candidates.iterator().next());
        break;
      case ANY:
        winners.add(candidates.iterator().next());
        break;
    }

    return winners;
  }
Gets hosts which match the supplied criteria.

@param cluster
@param executeHostType
@param serviceName
@param componentName
@return
ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java