-
Notifications
You must be signed in to change notification settings - Fork 644
HDDS-16174. Add container balancer assessment command to report cluster imbalance #11256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sravani-revuri
wants to merge
4
commits into
apache:master
Choose a base branch
from
sravani-revuri:HDDS-16174
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
321 changes: 321 additions & 0 deletions
321
...n/src/main/java/org/apache/hadoop/hdds/scm/cli/ContainerBalancerAssessmentSubcommand.java
|
sravani-revuri marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,321 @@ | ||
| /* | ||
| * 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.cli; | ||
|
|
||
| import static org.apache.hadoop.util.StringUtils.byteDesc; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.hadoop.hdds.cli.HddsVersionProvider; | ||
| import org.apache.hadoop.hdds.protocol.proto.HddsProtos.DatanodeUsageInfoProto; | ||
| import org.apache.hadoop.hdds.scm.client.ScmClient; | ||
| import org.apache.hadoop.hdds.scm.container.balancer.ContainerBalancerClusterAnalyzer; | ||
| import org.apache.hadoop.hdds.scm.container.balancer.ContainerBalancerClusterSnapshot; | ||
| import org.apache.hadoop.hdds.scm.container.balancer.ContainerBalancerConfiguration; | ||
| import org.apache.hadoop.hdds.server.JsonUtils; | ||
| import picocli.CommandLine.Command; | ||
| import picocli.CommandLine.Option; | ||
|
|
||
| /** Reports cluster imbalance without starting the container balancer. */ | ||
| @Command( | ||
| name = "assessment", | ||
| description = "Report cluster imbalance before starting ContainerBalancer", | ||
| mixinStandardHelpOptions = true, | ||
| versionProvider = HddsVersionProvider.class) | ||
| public class ContainerBalancerAssessmentSubcommand extends ScmSubcommand { | ||
|
|
||
| @Option(names = {"-t", "--threshold"}, | ||
| description = "Percentage deviation from average utilization of " + | ||
| "the cluster after which a datanode is considered over- or under-utilized. " + | ||
| "The value should be in the range [0.0, 100.0), with a default of 10 " + | ||
| "(specify '10' for 10%%).") | ||
| private Optional<Double> threshold; | ||
|
|
||
| @Option(names = {"-n", "--limit"}, | ||
| description = "Maximum number of over- and under-utilized datanodes to list in the report. Default: 5.") | ||
| private int nodeLimit = 5; | ||
|
|
||
| @Option(names = {"--include-datanodes"}, | ||
| description = "A list of Datanode hostnames or ip addresses separated by commas. " + | ||
| "Only the Datanodes specified in this list are included in the assessment.") | ||
| private Optional<String> includeNodes; | ||
|
|
||
| @Option(names = {"--exclude-datanodes"}, | ||
| description = "A list of Datanode hostnames or ip addresses separated by commas. " + | ||
| "The Datanodes specified in this list are excluded from the assessment.") | ||
| private Optional<String> excludeNodes; | ||
|
|
||
| @Option(names = {"--json"}, | ||
| defaultValue = "false", | ||
| description = "Format output as JSON") | ||
| private boolean json; | ||
|
|
||
| @Override | ||
| public void execute(ScmClient scmClient) throws IOException { | ||
| ContainerBalancerConfiguration config = | ||
| getOzoneConf().getObject(ContainerBalancerConfiguration.class); | ||
|
|
||
| if (nodeLimit < 1) { | ||
| throw new IllegalArgumentException("limit must be at least 1."); | ||
| } | ||
|
|
||
| double thresholdRatio = threshold | ||
| .map(t -> { | ||
| if (t < 0d || t >= 100d) { | ||
| throw new IllegalArgumentException( | ||
| "Threshold must be a percentage in the range [0.0, 100.0)."); | ||
| } | ||
| return t / 100.0; | ||
| }) | ||
| .orElseGet(config::getThresholdAsRatio); | ||
|
|
||
| Set<String> include = includeNodes | ||
| .map(ContainerBalancerAssessmentSubcommand::parseNodeList) | ||
| .orElseGet(config::getIncludeNodes); | ||
| Set<String> exclude = excludeNodes | ||
| .map(ContainerBalancerAssessmentSubcommand::parseNodeList) | ||
| .orElseGet(config::getExcludeNodes); | ||
|
|
||
| List<DatanodeUsageInfoProto> nodes = | ||
| scmClient.getDatanodeUsageInfo(true, Integer.MAX_VALUE); | ||
|
|
||
| ContainerBalancerClusterSnapshot snapshot = | ||
| ContainerBalancerClusterAnalyzer.analyze(nodes, thresholdRatio, include, exclude, nodeLimit); | ||
|
|
||
| if (json) { | ||
| printJsonReport(snapshot, thresholdRatio); | ||
| } else { | ||
| printReport(snapshot, thresholdRatio, nodeLimit); | ||
| } | ||
| } | ||
|
|
||
| private static void printReport(ContainerBalancerClusterSnapshot snapshot, double thresholdRatio, int nodeLimit) { | ||
| System.out.println("CLUSTER BALANCE ASSESSMENT"); | ||
|
|
||
| if (snapshot.getTotalEligibleDatanodes() == 0) { | ||
| System.out.println(getSummaryPrettyString(snapshot, thresholdRatio)); | ||
| System.out.println(); | ||
| System.out.println("No eligible datanodes found for assessment."); | ||
| return; | ||
| } | ||
|
|
||
| System.out.println(getSummaryPrettyString(snapshot, thresholdRatio)); | ||
| System.out.println(); | ||
| System.out.println(getSourceNodesPrettyString(snapshot, nodeLimit)); | ||
| System.out.println(); | ||
| System.out.println(getTargetNodesPrettyString(snapshot, nodeLimit)); | ||
| System.out.println(); | ||
| System.out.println(getMovementSummaryPrettyString(snapshot)); | ||
| } | ||
|
|
||
| private static String getSummaryPrettyString(ContainerBalancerClusterSnapshot snapshot, | ||
| double thresholdRatio) { | ||
| if (snapshot.getTotalEligibleDatanodes() == 0) { | ||
| return String.format("%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n", "Key", "Value", | ||
| "Threshold", formatPercent(thresholdRatio), | ||
| "Upper limit", formatPercent(snapshot.getUpperLimit()), | ||
| "Lower limit", formatPercent(snapshot.getLowerLimit()), | ||
| "Eligible datanodes", "0 datanodes"); | ||
| } | ||
| return String.format("%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n", "Key", "Value", | ||
| "Threshold", formatPercent(thresholdRatio), | ||
| "Upper limit", formatPercent(snapshot.getUpperLimit()), | ||
| "Lower limit", formatPercent(snapshot.getLowerLimit()), | ||
| "Drift", String.format(Locale.US, | ||
| "%.1f%% (max utilization %.1f%% - min utilization %.1f%%)", | ||
| snapshot.getImbalance() * 100, | ||
| snapshot.getMaxUtilization() * 100, | ||
| snapshot.getMinUtilization() * 100), | ||
| "Mean Utilization", formatPercent(snapshot.getClusterAvgUtilization()), | ||
| "Eligible datanodes", snapshot.getTotalEligibleDatanodes() + " datanode" | ||
| + (snapshot.getTotalEligibleDatanodes() == 1 ? "" : "s"), | ||
| "Category", formatCategory(snapshot)); | ||
| } | ||
|
|
||
| private static String getSourceNodesPrettyString(ContainerBalancerClusterSnapshot snapshot, int nodeLimit) { | ||
| StringBuilder builder = new StringBuilder(); | ||
| builder.append("Source Nodes (over-utilized):").append(System.lineSeparator()) | ||
| .append(String.format("%-50s %s%n", "Datanodes above threshold", snapshot.getSourceCount())); | ||
| appendNodeList(builder, snapshot.getTopSourceNodes(), snapshot.getClusterAvgUtilization(), true, nodeLimit); | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| private static String getTargetNodesPrettyString(ContainerBalancerClusterSnapshot snapshot, int nodeLimit) { | ||
| StringBuilder builder = new StringBuilder(); | ||
| builder.append("Target Nodes (under-utilized):").append(System.lineSeparator()) | ||
| .append(String.format("%-50s %s%n", "Datanodes below threshold", snapshot.getTargetCount())); | ||
| appendNodeList(builder, snapshot.getBottomTargetNodes(), snapshot.getClusterAvgUtilization(), false, nodeLimit); | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| private static String getMovementSummaryPrettyString(ContainerBalancerClusterSnapshot snapshot) { | ||
| double movementRatio = snapshot.getClusterCapacityBytes() == 0 ? 0 | ||
| : (double) snapshot.getBytesToMove() / snapshot.getClusterCapacityBytes(); | ||
| return String.format("Movement Summary:%n" + | ||
| "%-50s %s%n" + | ||
| "%-50s %s%n", "Total bytes to move", byteDesc(snapshot.getBytesToMove()), | ||
| "Movement ratio", | ||
| String.format(Locale.US, "%.1f%% of cluster capacity", movementRatio * 100)); | ||
| } | ||
|
|
||
| private static void appendNodeList(StringBuilder builder, | ||
| List<ContainerBalancerClusterSnapshot.NodeUtilization> nodes, | ||
| double clusterAvgUtilization, boolean aboveMean, int nodeLimit) { | ||
| if (nodes.isEmpty()) { | ||
| builder.append(System.lineSeparator()) | ||
| .append("(none)") | ||
| .append(System.lineSeparator()); | ||
| return; | ||
|
sravani-revuri marked this conversation as resolved.
|
||
| } | ||
| int headerCount = Math.min(nodeLimit, nodes.size()); | ||
| builder.append(System.lineSeparator()) | ||
| .append(aboveMean ? "Top " + headerCount + ":" : "Bottom " + headerCount + ":") | ||
| .append(System.lineSeparator()); | ||
| for (ContainerBalancerClusterSnapshot.NodeUtilization node : nodes) { | ||
| double deltaFromMean = (node.getUtilization() - clusterAvgUtilization) * 100; | ||
| builder.append(String.format(Locale.US, "%-50s %s%n", node.getHostname(), | ||
| String.format(Locale.US, "%.1f%% (%+.1f%% %s mean)", | ||
| node.getUtilization() * 100, deltaFromMean, aboveMean ? "above" : "below"))); | ||
| } | ||
| } | ||
|
|
||
| private static String formatPercent(double ratio) { | ||
| return String.format(Locale.US, "%.1f%%", ratio * 100); | ||
| } | ||
|
|
||
| private static String formatCategory(ContainerBalancerClusterSnapshot snapshot) { | ||
| return formatClusterSize(snapshot.getTotalEligibleDatanodes()) + ", " | ||
| + formatImbalance(snapshot.getImbalance()) + ", " | ||
| + formatMovementRatio(snapshot); | ||
| } | ||
|
|
||
| private static String formatClusterSize(int eligibleDatanodes) { | ||
| if (eligibleDatanodes >= 100) { | ||
| return "Large cluster"; | ||
| } | ||
| if (eligibleDatanodes >= 20) { | ||
| return "Medium cluster"; | ||
| } | ||
| return "Small cluster"; | ||
| } | ||
|
|
||
| private static String formatImbalance(double imbalance) { | ||
| if (imbalance >= 0.20) { | ||
| return "High imbalance"; | ||
| } | ||
| if (imbalance >= 0.10) { | ||
| return "Medium imbalance"; | ||
| } | ||
| return "Low imbalance"; | ||
| } | ||
|
|
||
| private static String formatMovementRatio(ContainerBalancerClusterSnapshot snapshot) { | ||
| if (snapshot.getClusterCapacityBytes() == 0) { | ||
| return "Low movement ratio"; | ||
| } | ||
| double ratio = (double) snapshot.getBytesToMove() / snapshot.getClusterCapacityBytes(); | ||
| if (ratio >= 0.05) { | ||
| return "High movement ratio"; | ||
| } | ||
| if (ratio >= 0.01) { | ||
| return "Medium movement ratio"; | ||
| } | ||
| return "Low movement ratio"; | ||
| } | ||
|
|
||
| private static void printJsonReport(ContainerBalancerClusterSnapshot snapshot, double thresholdRatio) | ||
| throws IOException { | ||
| Map<String, Object> result = new LinkedHashMap<>(); | ||
| result.put("totalEligibleDatanodes", snapshot.getTotalEligibleDatanodes()); | ||
| result.put("thresholdPercentage", String.format(Locale.US, "%.1f%%", thresholdRatio * 100)); | ||
| result.put("clusterAvgUtilizationPercentage", | ||
| String.format(Locale.US, "%.1f%%", snapshot.getClusterAvgUtilization() * 100)); | ||
| result.put("driftPercentage", | ||
| String.format(Locale.US, "%.1f%%", snapshot.getImbalance() * 100)); | ||
| result.put("maxUtilizationPercentage", | ||
| String.format(Locale.US, "%.1f%%", snapshot.getMaxUtilization() * 100)); | ||
| result.put("minUtilizationPercentage", | ||
| String.format(Locale.US, "%.1f%%", snapshot.getMinUtilization() * 100)); | ||
| result.put("upperLimitPercentage", | ||
| String.format(Locale.US, "%.1f%%", snapshot.getUpperLimit() * 100)); | ||
| result.put("lowerLimitPercentage", | ||
| String.format(Locale.US, "%.1f%%", snapshot.getLowerLimit() * 100)); | ||
|
|
||
| Map<String, Object> sourceNodes = new LinkedHashMap<>(); | ||
| sourceNodes.put("count", snapshot.getSourceCount()); | ||
| sourceNodes.put("nodes", snapshot.getTopSourceNodes().stream() | ||
| .map(node -> { | ||
| Map<String, Object> nodeMap = new LinkedHashMap<>(); | ||
| nodeMap.put("hostname", node.getHostname()); | ||
| nodeMap.put("utilizationPercentage", | ||
| String.format(Locale.US, "%.1f%%", node.getUtilization() * 100)); | ||
| return nodeMap; | ||
| }) | ||
| .collect(Collectors.toList())); | ||
| result.put("sourceNodes", sourceNodes); | ||
|
|
||
| Map<String, Object> targetNodes = new LinkedHashMap<>(); | ||
| targetNodes.put("count", snapshot.getTargetCount()); | ||
| targetNodes.put("nodes", snapshot.getBottomTargetNodes().stream() | ||
| .map(node -> { | ||
| Map<String, Object> nodeMap = new LinkedHashMap<>(); | ||
| nodeMap.put("hostname", node.getHostname()); | ||
| nodeMap.put("utilizationPercentage", | ||
| String.format(Locale.US, "%.1f%%", node.getUtilization() * 100)); | ||
| return nodeMap; | ||
| }) | ||
| .collect(Collectors.toList())); | ||
| result.put("targetNodes", targetNodes); | ||
|
|
||
| result.put("bytesToMove", byteDesc(snapshot.getBytesToMove())); | ||
| result.put("movementRatioPercentage", String.format(Locale.US, "%.1f%%", | ||
| snapshot.getClusterCapacityBytes() == 0 ? 0 | ||
| : (double) snapshot.getBytesToMove() / snapshot.getClusterCapacityBytes() * 100)); | ||
|
|
||
| System.out.println(JsonUtils.toJsonStringWithDefaultPrettyPrinter(result)); | ||
| } | ||
|
|
||
| private static Set<String> parseNodeList(String commaSeparated) { | ||
| if (commaSeparated == null || commaSeparated.isEmpty()) { | ||
| return Collections.emptySet(); | ||
| } | ||
| return Arrays.stream(commaSeparated.split(",")) | ||
| .map(String::trim) | ||
| .filter(s -> !s.isEmpty()) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.