public static List merge()

in iep-servergroups/src/main/java/com/netflix/iep/servergroups/ServerGroup.java [301:348]


  public static List<ServerGroup> merge(Collection<ServerGroup> gs1, Collection<ServerGroup> gs2) {

    Map<String, ServerGroup> nimbleGroups = new HashMap<>();
    for (ServerGroup g : gs1) {
      if (g.getApp().startsWith(NIMBLE_PREFIX)) {
        nimbleGroups.put(g.id, g);
      }
    }

    Map<String, ServerGroup> otherGroups = new HashMap<>();
    Map<String, ServerGroup> otherNimbleGroups = new HashMap<>();
    for (ServerGroup g : gs2) {
      otherGroups.put(g.id, g);
      String nimbleId = g.nimbleId();
      if (nimbleGroups.containsKey(nimbleId)) {
        otherNimbleGroups.put(nimbleId, g);
      }
    }

    List<ServerGroup> merged = new ArrayList<>(gs1.size());

    // Groups in gs1 only or in both lists
    for (ServerGroup g1 : gs1) {
      if (!g1.group.startsWith(NIMBLE_PREFIX)) {
        ServerGroup g2 = otherGroups.remove(g1.id);
        merged.add(g2 == null ? g1 : g1.merge(g2));
      }
    }

    // Add groups only in gs2
    merged.addAll(otherGroups.values());

    // Merge for nimble groups that report the wrong information to sources like Eureka
    for (ServerGroup g1 : nimbleGroups.values()) {
      ServerGroup g2 = otherNimbleGroups.remove(g1.id);
      if (g2 == null) {
        merged.add(g1);
      } else {
        Set<String> instanceIds = new HashSet<>();
        for (Instance instance : g1.getInstances()) {
          instanceIds.add(instance.getNode());
        }
        merged.add(g1.merge(g2.toNimbleGroup(instanceIds)));
      }
    }

    return merged;
  }