diff --git a/helix-core/src/main/java/org/apache/helix/cloud/topology/FaultZoneBasedVirtualGroupAssignmentAlgorithm.java b/helix-core/src/main/java/org/apache/helix/cloud/topology/FaultZoneBasedVirtualGroupAssignmentAlgorithm.java index 4338770c35..872fa9352f 100644 --- a/helix-core/src/main/java/org/apache/helix/cloud/topology/FaultZoneBasedVirtualGroupAssignmentAlgorithm.java +++ b/helix-core/src/main/java/org/apache/helix/cloud/topology/FaultZoneBasedVirtualGroupAssignmentAlgorithm.java @@ -73,6 +73,8 @@ public Map> computeAssignment(int numGroups, String virtualG return constructResult(newAssignment, zoneMapping); } + // 2. Find unassigned zones. If there is any, incrementally assign them to the least-loaded + // virtual group. // Build instance-to-zone mapping for quick zone lookups. Map instanceToZoneMapping = new HashMap<>(); for (Map.Entry> entry : zoneMapping.entrySet()) { @@ -82,7 +84,7 @@ public Map> computeAssignment(int numGroups, String virtualG } // Copy zoneMapping for tracking which zones are unassigned. - Map> unassignedZoneToInstances = copyZoneMapping(zoneMapping); + Set unassignedZones = zoneMapping.keySet(); // Build virtual group -> zone mapping and remove assigned zones from the unassigned list Map> virtualGroupToZoneMapping = new HashMap<>(); @@ -91,18 +93,18 @@ public Map> computeAssignment(int numGroups, String virtualG for (String instance : entry.getValue()) { String zone = instanceToZoneMapping.get(instance); virtualGroupToZoneMapping.get(entry.getKey()).add(zone); - unassignedZoneToInstances.remove(zone); + unassignedZones.remove(zone); } } // If there are no unassigned zones, return the result as is. - if (unassignedZoneToInstances.isEmpty()) { + if (unassignedZones.isEmpty()) { return constructResult(virtualGroupToZoneMapping, zoneMapping); } - // 2. Distribute unassigned zones to keep the overall distribution balanced. - distributeUnassignedZones(virtualGroupToZoneMapping, - new ArrayList<>(unassignedZoneToInstances.keySet()), zoneMapping); + // Distribute unassigned zones to keep the overall distribution balanced. + distributeUnassignedZones(virtualGroupToZoneMapping, new ArrayList<>(unassignedZones), + zoneMapping); return constructResult(virtualGroupToZoneMapping, zoneMapping); } @@ -149,20 +151,6 @@ private void distributeUnassignedZones( } } - /** - * Creates a deep copy of the given map - * - * @param zoneMapping Original map to copy. - * @return A fully independent copy of the given map. - */ - private Map> copyZoneMapping(Map> zoneMapping) { - Map> copy = new HashMap<>(); - for (Map.Entry> entry : zoneMapping.entrySet()) { - copy.put(entry.getKey(), new HashSet<>(entry.getValue())); - } - return copy; - } - /** * Constructs the final result by mapping virtual groups to their instances. *