pkuwm commented on a change in pull request #686: Add resource partition weight 
gauge
URL: https://github.com/apache/helix/pull/686#discussion_r369219403
 
 

 ##########
 File path: 
helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/ResourceUsageCalculator.java
 ##########
 @@ -142,4 +142,97 @@ public static double 
measureBaselineDivergence(Map<String, ResourceAssignment> b
     return numTotalBestPossibleReplicas == 0 ? 1.0d
         : (1.0d - (double) numMatchedReplicas / (double) 
numTotalBestPossibleReplicas);
   }
+
+  /**
+   * Calculates average partition weight per capacity key for a resource 
config. Example as below:
+   * Input =
+   * {
+   *   "partition1": {
+   *     "capacity1": 20,
+   *     "capacity2": 40
+   *   },
+   *   "partition2": {
+   *     "capacity1": 30,
+   *     "capacity2": 50
+   *   },
+   *   "partition3": {
+   *     "capacity1": 16,
+   *     "capacity2": 30
+   *   }
+   * }
+   *
+   * Total weight for key "capacity1" = 20 + 30 + 16 = 66;
+   * Total weight for key "capacity2" = 40 + 50 + 30 = 120;
+   * Total partitions = 3;
+   * Average partition weight for "capacity1" = 66 / 3 = 22;
+   * Average partition weight for "capacity2" = 120 / 3 = 40;
+   *
+   * Output =
+   * {
+   *   "capacity1": 22,
+   *   "capacity2": 40
+   * }
+   *
+   * @param partitionCapacityMap A map of partition capacity:
+   *        <PartitionName or DEFAULT_PARTITION_KEY, <Capacity Key, Capacity 
Number>>
+   * @return A map of partition weight: capacity key -> average partition 
weight
+   */
+  public static Map<String, Integer> calculateAveragePartitionWeight(
+      Map<String, Map<String, Integer>> partitionCapacityMap) {
+    Map<String, PartitionWeightCounterEntry> countPartitionWeightMap =
+        aggregatePartitionWeight(partitionCapacityMap);
+
+    // capacity key -> average partition weight
+    Map<String, Integer> averagePartitionWeightMap = new HashMap<>();
+
+    // Calculate average partition weight for each capacity key.
+    // Per capacity key level:
+    // average partition weight = (total partition weight) / (number of 
partitions)
+    for (Map.Entry<String, PartitionWeightCounterEntry> entry
+        : countPartitionWeightMap.entrySet()) {
+      String capacityKey = entry.getKey();
+      PartitionWeightCounterEntry weightEntry = entry.getValue();
+      int averageWeight = weightEntry.getWeight() / 
weightEntry.getPartitions();
+      averagePartitionWeightMap.put(capacityKey, averageWeight);
+    }
+
+    return averagePartitionWeightMap;
+  }
+
+  /*
+   * Aggregates partition weight for each capacity key.
+   */
+  private static Map<String, PartitionWeightCounterEntry> 
aggregatePartitionWeight(
+      Map<String, Map<String, Integer>> partitionCapacityMap) {
+    // capacity key -> [number of partitions, total weight per capacity key]
+    Map<String, PartitionWeightCounterEntry> countPartitionWeightMap = new 
HashMap<>();
+
+    partitionCapacityMap.values().forEach(partitionCapacityEntry ->
+        partitionCapacityEntry.forEach((capacityKey, weight) -> 
countPartitionWeightMap
+            .computeIfAbsent(capacityKey, counterEntry -> new 
PartitionWeightCounterEntry())
+            .increase(1, weight)));
+
+    return countPartitionWeightMap;
+  }
+
+  /*
+   * Represents total number of partitions and total partition weight for a 
capacity key.
+   */
+  private static class PartitionWeightCounterEntry {
+    private int partitions;
+    private int weight;
 
 Review comment:
   Done.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to