private Map<String, Set<String>> organizeHostsByRack(Set<String> hosts, String rackYamlFile) {
      try {
        Map<String, String> hostToRackMap = getHostToRackMap(rackYamlFile);
        Map<String, Set<String>> rackToHostsMap = new HashMap<>();
        for (String host : hosts) {
          if (hostToRackMap.containsKey(host)) {
            String rack = hostToRackMap.get(host);
            if (!rackToHostsMap.containsKey(rack)) {
              rackToHostsMap.put(rack, new HashSet<>());
            }
            rackToHostsMap.get(rack).add(host);
          } else {
            throw new RuntimeException(String.format("Rack mapping is not present for host name: %s", host));
          }
        }
        return rackToHostsMap;
      } catch (Exception e) {
        throw new RuntimeException(
                String.format("Failed to generate Rack to Hosts mapping. filePath: %s", rackYamlFile), e);
      }
    }
Utility method to organize and return Rack to Hosts mapping for given rack yaml file

@param hosts        : All hosts that are part of current group
@param rackYamlFile : file path for yaml containing rack to hosts mapping
                       e.g ambari-server/src/examples/rack_hosts.yaml
@return
ambari-server/src/main/java/org/apache/ambari/server/stack/upgrade/Grouping.java