diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ClusterCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ClusterCLI.java index b930014..d6e1223 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ClusterCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ClusterCLI.java @@ -26,16 +26,20 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Set; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.MissingArgumentException; +import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.lang.StringUtils; import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.util.ToolRunner; +import org.apache.hadoop.yarn.api.records.NodeId; import org.apache.hadoop.yarn.api.records.NodeLabel; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.exceptions.YarnException; @@ -48,10 +52,13 @@ */ @Private public class ClusterCLI extends YarnCLI { + private static final String LABELS_PATTERN = "%32s\t%32s" + + System.getProperty("line.separator"); private static final String TITLE = "yarn cluster"; public static final String LIST_LABELS_CMD = "list-node-labels"; public static final String DIRECTLY_ACCESS_NODE_LABEL_STORE = "directly-access-node-label-store"; + public static final String NODES = "nodes"; public static final String CMD = "cluster"; private boolean accessLocal = false; static CommonNodeLabelsManager localNodeLabelsManager = null; @@ -70,7 +77,8 @@ public int run(String[] args) throws Exception { Options opts = new Options(); opts.addOption("lnl", LIST_LABELS_CMD, false, - "List cluster node-label collection"); + "List cluster node-label collection. " + + "Supports optional use of -nodes to print all nodes partitioned by labels."); opts.addOption("h", HELP_CMD, false, "Displays help for all commands."); opts.addOption("dnl", DIRECTLY_ACCESS_NODE_LABEL_STORE, false, "This is DEPRECATED, will be removed in future releases. Directly access node label store, " @@ -85,7 +93,9 @@ public int run(String[] args) throws Exception { + " when the command run on the machine where RM is running." + " Also, this option is UNSTABLE, could be removed in future" + " releases."); - + Option nodesOpt = new Option(NODES, false, + "Work with -lnl/list-node-labels to print all nodes partitioned by labels."); + opts.addOption(nodesOpt); int exitCode = -1; CommandLine parsedCli = null; try { @@ -101,7 +111,11 @@ public int run(String[] args) throws Exception { } if (parsedCli.hasOption(LIST_LABELS_CMD)) { - printClusterNodeLabels(); + if(parsedCli.hasOption(NODES)) { + printClusterNodeLabelsMap(); + } else { + printClusterNodeLabels(); + } } else if (parsedCli.hasOption(HELP_CMD)) { printUsage(opts); return 0; @@ -124,6 +138,21 @@ void printClusterNodeLabels() throws YarnException, IOException { StringUtils.join(nodeLabels.iterator(), ","))); } + void printClusterNodeLabelsMap()throws YarnException, IOException { + Map> labelsMap= null; + if (accessLocal) { + labelsMap = getNodeLabelManagerInstance(getConf()).getLabelsToNodes(); + } else { + labelsMap = client.getLabelsToNodes(); + } + sysout.println("Node Labels Num: " + labelsMap.size()); + sysout.printf(LABELS_PATTERN, "Labels", "Nodes"); + for(String label : labelsMap.keySet()) { + sysout.printf(LABELS_PATTERN, NodeLabel.newInstance(label).toString(), + StringUtils.join(labelsMap.get(label).iterator(), ", ")); + } + } + @VisibleForTesting static synchronized CommonNodeLabelsManager getNodeLabelManagerInstance(Configuration conf) {