private HostsType filterHosts(HostsType hostsType, String service, String component) {
    try {
      org.apache.ambari.server.state.Service svc = m_cluster.getService(service);
      ServiceComponent sc = svc.getServiceComponent(component);

      // !!! not really a fan of passing these around
      List<ServiceComponentHost> unhealthyHosts = new ArrayList<>();
      LinkedHashSet<String> upgradeHosts = new LinkedHashSet<>();

      for (String hostName : hostsType.getHosts()) {
        ServiceComponentHost sch = sc.getServiceComponentHost(hostName);
        Host host = sch.getHost();
        MaintenanceState maintenanceState = host.getMaintenanceState(sch.getClusterId());

        // !!! FIXME: only rely on maintenance state once the upgrade endpoint
        // is using the pre-req endpoint for determining if an upgrade is
        // possible
        if (maintenanceState != MaintenanceState.OFF) {
          unhealthyHosts.add(sch);
          continue;
        }

        if (sch.getUpgradeState() == UpgradeState.FAILED) {
          upgradeHosts.add(hostName);
          continue;
        }

        if(m_upgradeContext.getDirection() == Direction.UPGRADE){
          RepositoryVersionEntity targetRepositoryVersion = m_upgradeContext.getRepositoryVersion();
          if (!StringUtils.equals(targetRepositoryVersion.getVersion(), sch.getVersion())) {
            upgradeHosts.add(hostName);
          }

          continue;
        }

        // it's a downgrade ...
        RepositoryVersionEntity downgradeToRepositoryVersion = m_upgradeContext.getTargetRepositoryVersion(service);
        String downgradeToVersion = downgradeToRepositoryVersion.getVersion();
        if (!StringUtils.equals(downgradeToVersion, sch.getVersion())) {
          upgradeHosts.add(hostName);
          continue;
        }
      }

      hostsType.unhealthy = unhealthyHosts;
      hostsType.setHosts(upgradeHosts);

      return hostsType;
    } catch (AmbariException e) {
      // !!! better not
      LOG.warn("Could not determine host components to upgrade. Defaulting to saved hosts.", e);
      return hostsType;
    }
  }
Filters the supplied list of hosts in the following ways:
<ul>
<li>Compares the versions of a HostComponent to the version for the
resolver. Only versions that do not match are retained.</li>
<li>Removes unhealthy hosts in maintenance mode from the list of healthy
hosts</li>
</ul>

@param hostsType
         the hosts to resolve
@param service
         the service name
@param component
         the component name
@return the modified hosts instance with filtered and unhealthy hosts
        filled
ambari-server/src/main/java/org/apache/ambari/server/stack/MasterHostResolver.java