sreejasahithi commented on code in PR #11037:
URL: https://github.com/apache/ozone/pull/11037#discussion_r3802577302


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerClusterAnalyzer.java:
##########
@@ -0,0 +1,249 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.scm.container.balancer;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.DatanodeUsageInfoProto;
+
+/**
+ * Classifies datanode usage protos using the same rules as
+ * {@code ContainerBalancerTask.initializeIteration()}.
+ */
+public final class ContainerBalancerClusterAnalyzer {
+
+  private static final int TOP_NODE_LIMIT = 5;
+
+  private ContainerBalancerClusterAnalyzer() {
+  }
+
+  /**
+   * Core shared formula: (totalCapacity - totalRemaining) / totalCapacity.
+   */
+  public static double calculateAvgUtilization(long totalCapacity, long 
totalRemaining) {
+    if (totalCapacity == 0) {
+      return 0;
+    }
+    return (totalCapacity - totalRemaining) / (double) totalCapacity;
+  }
+
+  /**
+   * Cluster average from datanode usage protos.
+   */
+  public static double calculateAvgUtilization(List<DatanodeUsageInfoProto> 
nodes) {
+    if (nodes.isEmpty()) {
+      return 0;
+    }
+    long totalCapacity = 0;
+    long totalRemaining = 0;
+    for (DatanodeUsageInfoProto node : nodes) {
+      totalCapacity += node.getCapacity();
+      totalRemaining += node.getRemaining();
+    }
+    return calculateAvgUtilization(totalCapacity, totalRemaining);
+  }
+
+  /**
+   * Builds a cluster snapshot after applying include/exclude filters.
+   *
+   * @param nodes all nodes from getDatanodeUsageInfo (typically healthy 
IN_SERVICE)
+   * @param thresholdRatio threshold as ratio, e.g. 0.10 for 10%
+   * @param includeNodes empty = all included; non-empty = allow-list
+   * @param excludeNodes nodes to skip
+   */
+  public static ContainerBalancerClusterSnapshot analyze(
+      List<DatanodeUsageInfoProto> nodes,
+      double thresholdRatio,
+      Set<String> includeNodes,
+      Set<String> excludeNodes) {
+    List<DatanodeUsageInfoProto> eligible = filterEligibleNodes(nodes, 
includeNodes, excludeNodes);
+
+    if (eligible.isEmpty()) {
+      return emptySnapshot(thresholdRatio);
+    }
+
+    long clusterCapacityBytes = 0;
+    for (DatanodeUsageInfoProto node : eligible) {
+      clusterCapacityBytes += node.getCapacity();
+    }

Review Comment:
   we can compute the clusterCapacityBytes in below for loop where we classify 
the source and target nodes itself as we are already going through the nodes 
there.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to