sreejasahithi commented on code in PR #11037: URL: https://github.com/apache/ozone/pull/11037#discussion_r3796033586
########## hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerClusterAnalyzer.java: ########## @@ -0,0 +1,245 @@ +/* + * 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(); + } + + double clusterAvgUtilization = calculateAvgUtilization(eligible); + double upperLimit = clusterAvgUtilization + thresholdRatio; + double lowerLimit = clusterAvgUtilization - thresholdRatio; + + List<NodeClassification> sources = new ArrayList<>(); + List<NodeClassification> targets = new ArrayList<>(); + double maxUtilization = Double.NEGATIVE_INFINITY; + double minUtilization = Double.POSITIVE_INFINITY; + long totalOverUtilizedBytes = 0; + long totalUnderUtilizedBytes = 0; + + for (DatanodeUsageInfoProto node : eligible) { + long capacity = node.getCapacity(); + double utilization = calculateNodeUtilization(node); + + maxUtilization = Math.max(maxUtilization, utilization); + minUtilization = Math.min(minUtilization, utilization); + + String hostname = getDisplayHostname(node); + if (Double.compare(utilization, upperLimit) > 0) { + long overBytes = ratioToBytes(capacity, utilization) + - ratioToBytes(capacity, upperLimit); + totalOverUtilizedBytes += overBytes; + sources.add(new NodeClassification(hostname, utilization)); + } else if (Double.compare(utilization, lowerLimit) < 0) { + long underBytes = ratioToBytes(capacity, lowerLimit) + - ratioToBytes(capacity, utilization); + totalUnderUtilizedBytes += underBytes; + targets.add(new NodeClassification(hostname, utilization)); + } + } + + sources.sort(Comparator.comparingDouble(NodeClassification::getUtilization).reversed()); + targets.sort(Comparator.comparingDouble(NodeClassification::getUtilization)); + + double imbalance = maxUtilization - minUtilization; + + return new ContainerBalancerClusterSnapshot( + eligible.size(), + clusterAvgUtilization, + clusterCapacityBytes, + maxUtilization, + minUtilization, + upperLimit, + lowerLimit, + sources.size(), + targets.size(), + totalOverUtilizedBytes, + totalUnderUtilizedBytes, + totalOverUtilizedBytes, Review Comment: if we are using sum(excess bytes per over-utilized DN) as the byets to move instead of sending totalOverUtilizedBytes twice, just re-use it. ########## hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerClusterAnalyzer.java: ########## @@ -0,0 +1,245 @@ +/* + * 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(); + } + + double clusterAvgUtilization = calculateAvgUtilization(eligible); + double upperLimit = clusterAvgUtilization + thresholdRatio; + double lowerLimit = clusterAvgUtilization - thresholdRatio; + + List<NodeClassification> sources = new ArrayList<>(); + List<NodeClassification> targets = new ArrayList<>(); + double maxUtilization = Double.NEGATIVE_INFINITY; + double minUtilization = Double.POSITIVE_INFINITY; + long totalOverUtilizedBytes = 0; + long totalUnderUtilizedBytes = 0; + + for (DatanodeUsageInfoProto node : eligible) { + long capacity = node.getCapacity(); + double utilization = calculateNodeUtilization(node); + + maxUtilization = Math.max(maxUtilization, utilization); + minUtilization = Math.min(minUtilization, utilization); + + String hostname = getDisplayHostname(node); + if (Double.compare(utilization, upperLimit) > 0) { + long overBytes = ratioToBytes(capacity, utilization) + - ratioToBytes(capacity, upperLimit); + totalOverUtilizedBytes += overBytes; + sources.add(new NodeClassification(hostname, utilization)); + } else if (Double.compare(utilization, lowerLimit) < 0) { + long underBytes = ratioToBytes(capacity, lowerLimit) + - ratioToBytes(capacity, utilization); + totalUnderUtilizedBytes += underBytes; + targets.add(new NodeClassification(hostname, utilization)); + } + } + + sources.sort(Comparator.comparingDouble(NodeClassification::getUtilization).reversed()); + targets.sort(Comparator.comparingDouble(NodeClassification::getUtilization)); + + double imbalance = maxUtilization - minUtilization; + + return new ContainerBalancerClusterSnapshot( + eligible.size(), + clusterAvgUtilization, + clusterCapacityBytes, + maxUtilization, + minUtilization, + upperLimit, + lowerLimit, + sources.size(), + targets.size(), + totalOverUtilizedBytes, + totalUnderUtilizedBytes, + totalOverUtilizedBytes, + imbalance, + topHostnames(sources), + topHostnames(targets)); + } + + /** + * Same rules as {@code ContainerBalancer.shouldExcludeDatanode}. + */ + public static boolean shouldExcludeDatanode( Review Comment: I think we should extract this as well instead of having two copies of the same implementation. ########## hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerTask.java: ########## @@ -1130,15 +1129,14 @@ public static double calculateAvgUtilization(List<DatanodeUsageInfo> nodes) { "ContainerBalancer."); return 0; } - SCMNodeStat aggregatedStats = new SCMNodeStat( - 0, 0, 0, 0, 0, 0); + long totalCapacity = 0; + long totalRemaining = 0; for (DatanodeUsageInfo node : nodes) { - aggregatedStats.add(node.getScmNodeStat()); + totalCapacity += node.getScmNodeStat().getCapacity().get(); + totalRemaining += node.getScmNodeStat().getRemaining().get(); } - long clusterCapacity = aggregatedStats.getCapacity().get(); - long clusterRemaining = aggregatedStats.getRemaining().get(); - - return (clusterCapacity - clusterRemaining) / (double) clusterCapacity; + return ContainerBalancerClusterAnalyzer.calculateAvgUtilization( + totalCapacity, totalRemaining); } Review Comment: keeps its existing `List<DatanodeUsageInfo>` wrapper, aggregates node stats, and delegate to the shared core method. ########## hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerClusterAnalyzer.java: ########## @@ -0,0 +1,245 @@ +/* + * 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(); + } + + double clusterAvgUtilization = calculateAvgUtilization(eligible); + double upperLimit = clusterAvgUtilization + thresholdRatio; + double lowerLimit = clusterAvgUtilization - thresholdRatio; + + List<NodeClassification> sources = new ArrayList<>(); + List<NodeClassification> targets = new ArrayList<>(); + double maxUtilization = Double.NEGATIVE_INFINITY; + double minUtilization = Double.POSITIVE_INFINITY; + long totalOverUtilizedBytes = 0; + long totalUnderUtilizedBytes = 0; + + for (DatanodeUsageInfoProto node : eligible) { + long capacity = node.getCapacity(); + double utilization = calculateNodeUtilization(node); + + maxUtilization = Math.max(maxUtilization, utilization); + minUtilization = Math.min(minUtilization, utilization); + + String hostname = getDisplayHostname(node); + if (Double.compare(utilization, upperLimit) > 0) { + long overBytes = ratioToBytes(capacity, utilization) + - ratioToBytes(capacity, upperLimit); + totalOverUtilizedBytes += overBytes; + sources.add(new NodeClassification(hostname, utilization)); + } else if (Double.compare(utilization, lowerLimit) < 0) { + long underBytes = ratioToBytes(capacity, lowerLimit) + - ratioToBytes(capacity, utilization); + totalUnderUtilizedBytes += underBytes; + targets.add(new NodeClassification(hostname, utilization)); + } + } + + sources.sort(Comparator.comparingDouble(NodeClassification::getUtilization).reversed()); + targets.sort(Comparator.comparingDouble(NodeClassification::getUtilization)); + + double imbalance = maxUtilization - minUtilization; + + return new ContainerBalancerClusterSnapshot( + eligible.size(), + clusterAvgUtilization, + clusterCapacityBytes, + maxUtilization, + minUtilization, + upperLimit, + lowerLimit, + sources.size(), + targets.size(), + totalOverUtilizedBytes, + totalUnderUtilizedBytes, + totalOverUtilizedBytes, + imbalance, + topHostnames(sources), + topHostnames(targets)); + } + + /** + * Same rules as {@code ContainerBalancer.shouldExcludeDatanode}. + */ + public static boolean shouldExcludeDatanode( + DatanodeDetails datanode, + Set<String> excludeNodes, + Set<String> includeNodes) { + if (excludeNodes.contains(datanode.getHostName()) || + excludeNodes.contains(datanode.getIpAddress())) { + return true; + } + if (!includeNodes.isEmpty()) { + return !includeNodes.contains(datanode.getHostName()) && + !includeNodes.contains(datanode.getIpAddress()); + } + return false; + } + + static double calculateNodeUtilization(DatanodeUsageInfoProto node) { + long capacity = node.getCapacity(); + if (capacity == 0) { + return 0; + } + return (capacity - node.getRemaining()) / (double) capacity; + } + + private static List<DatanodeUsageInfoProto> filterEligibleNodes( + List<DatanodeUsageInfoProto> nodes, + Set<String> includeNodes, + Set<String> excludeNodes) { + List<DatanodeUsageInfoProto> eligible = new ArrayList<>(); + for (DatanodeUsageInfoProto node : nodes) { + if (!node.hasNode()) { + continue; + } + DatanodeDetails datanode = DatanodeDetails.getFromProtoBuf(node.getNode()); + if (!shouldExcludeDatanode(datanode, excludeNodes, includeNodes)) { + eligible.add(node); + } + } + return eligible; + } + + private static long ratioToBytes(long nodeCapacity, double utilizationRatio) { Review Comment: same here we should extract this aswell -- 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]
