Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkGaox committed Jan 26, 2025
1 parent db05d25 commit 7466f68
Showing 1 changed file with 8 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public Map<String, Set<String>> 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<String, String> instanceToZoneMapping = new HashMap<>();
for (Map.Entry<String, Set<String>> entry : zoneMapping.entrySet()) {
Expand All @@ -82,7 +84,7 @@ public Map<String, Set<String>> computeAssignment(int numGroups, String virtualG
}

// Copy zoneMapping for tracking which zones are unassigned.
Map<String, Set<String>> unassignedZoneToInstances = copyZoneMapping(zoneMapping);
Set<String> unassignedZones = zoneMapping.keySet();

// Build virtual group -> zone mapping and remove assigned zones from the unassigned list
Map<String, Set<String>> virtualGroupToZoneMapping = new HashMap<>();
Expand All @@ -91,18 +93,18 @@ public Map<String, Set<String>> 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);
}

Expand Down Expand Up @@ -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<String, Set<String>> copyZoneMapping(Map<String, Set<String>> zoneMapping) {
Map<String, Set<String>> copy = new HashMap<>();
for (Map.Entry<String, Set<String>> entry : zoneMapping.entrySet()) {
copy.put(entry.getKey(), new HashSet<>(entry.getValue()));
}
return copy;
}

/**
* Constructs the final result by mapping virtual groups to their instances.
*
Expand Down

0 comments on commit 7466f68

Please sign in to comment.