Source:
private static Set<AssignableReplica> findToBeAssignedReplicasByClusterChanges( Map<String, Set<AssignableReplica>> replicaMap, Set<String> activeInstances, Set<String> liveInstances, Map<HelixConstants.ChangeType, Set<String>> clusterChanges, Map<String, ResourceAssignment> currentAssignment, Map<String, Set<AssignableReplica>> allocatedReplicas) { Set<AssignableReplica> toBeAssignedReplicas = new HashSet<>(); Set<String> newlyConnectedNodes = clusterChanges .getOrDefault(HelixConstants.ChangeType.LIVE_INSTANCE, Collections.emptySet()); newlyConnectedNodes.retainAll(liveInstances); if (clusterChanges.containsKey(HelixConstants.ChangeType.CLUSTER_CONFIG) || clusterChanges .containsKey(HelixConstants.ChangeType.INSTANCE_CONFIG) || !newlyConnectedNodes.isEmpty()) { toBeAssignedReplicas .addAll(replicaMap.values().stream().flatMap(Set::stream).collect(Collectors.toSet())); } else { for (String resourceName : replicaMap.keySet()) { Set<AssignableReplica> replicas = replicaMap.get(resourceName); if (clusterChanges .getOrDefault(HelixConstants.ChangeType.RESOURCE_CONFIG, Collections.emptySet()) .contains(resourceName) || clusterChanges .getOrDefault(HelixConstants.ChangeType.IDEAL_STATE, Collections.emptySet()) .contains(resourceName) || !currentAssignment.containsKey(resourceName)) { toBeAssignedReplicas.addAll(replicas); continue; } else { Map<String, Map<String, Set<String>>> stateMap = getValidStateInstanceMap(currentAssignment.get(resourceName), activeInstances); for (AssignableReplica replica : replicas) { Set<String> validInstances = stateMap.getOrDefault(replica.getPartitionName(), Collections.emptyMap()) .getOrDefault(replica.getReplicaState(), Collections.emptySet()); if (validInstances.isEmpty()) { toBeAssignedReplicas.add(replica); continue; } else { Iterator<String> iter = validInstances.iterator(); String instanceName = iter.next(); iter.remove(); allocatedReplicas.computeIfAbsent(instanceName, key -> new HashSet<>()).add(replica); } } } } } return toBeAssignedReplicas; }

Target:
private static Set<AssignableReplica> findToBeAssignedReplicasByClusterChanges( Map<String, Set<AssignableReplica>> replicaMap, Set<String> activeInstances, Set<String> liveInstances, Map<HelixConstants.ChangeType, Set<String>> clusterChanges, Map<String, ResourceAssignment> currentAssignment, Map<String, Set<AssignableReplica>> allocatedReplicas) { Set<AssignableReplica> toBeAssignedReplicas = new HashSet<>(); Set<String> newlyConnectedNodes = clusterChanges .getOrDefault(HelixConstants.ChangeType.LIVE_INSTANCE, Collections.emptySet()); newlyConnectedNodes.retainAll(liveInstances); if (clusterChanges.containsKey(HelixConstants.ChangeType.CLUSTER_CONFIG) || clusterChanges .containsKey(HelixConstants.ChangeType.INSTANCE_CONFIG) || !newlyConnectedNodes.isEmpty()) { toBeAssignedReplicas .addAll(replicaMap.values().stream().flatMap(Set::stream).collect(Collectors.toSet())); } else { for (Map.Entry<String, Set<AssignableReplica>> replicaMapEntry : replicaMap.entrySet()) { String resourceName = replicaMapEntry.getKey(); Set<AssignableReplica> replicas = replicaMapEntry.getValue(); if (clusterChanges .getOrDefault(HelixConstants.ChangeType.RESOURCE_CONFIG, Collections.emptySet()) .contains(resourceName) || clusterChanges .getOrDefault(HelixConstants.ChangeType.IDEAL_STATE, Collections.emptySet()) .contains(resourceName) || !currentAssignment.containsKey(resourceName)) { toBeAssignedReplicas.addAll(replicas); continue; } else { Map<String, Map<String, Set<String>>> stateMap = getValidStateInstanceMap(currentAssignment.get(resourceName), activeInstances); for (AssignableReplica replica : replicas) { Set<String> validInstances = stateMap.getOrDefault(replica.getPartitionName(), Collections.emptyMap()) .getOrDefault(replica.getReplicaState(), Collections.emptySet()); if (validInstances.isEmpty()) { toBeAssignedReplicas.add(replica); continue; } else { Iterator<String> iter = validInstances.iterator(); String instanceName = iter.next(); iter.remove(); allocatedReplicas.computeIfAbsent(instanceName, key -> new HashSet<>()).add(replica); } } } } } return toBeAssignedReplicas; }