diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java index 50e5825..0f227ae 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java @@ -18,12 +18,17 @@ package org.apache.hadoop.yarn.client.cli; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.PrintWriter; import java.util.Arrays; import java.util.Collection; +import java.util.Date; +import java.util.List; import java.util.Map; import com.google.common.collect.ImmutableMap; +import org.apache.commons.lang.time.DateFormatUtils; import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.conf.Configuration; @@ -33,6 +38,8 @@ import org.apache.hadoop.ipc.RemoteException; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.util.ToolRunner; +import org.apache.hadoop.yarn.api.records.NodeReport; +import org.apache.hadoop.yarn.client.api.YarnClient; import org.apache.hadoop.yarn.client.ClientRMProxy; import org.apache.hadoop.yarn.client.RMHAServiceTarget; import org.apache.hadoop.yarn.conf.HAUtil; @@ -41,13 +48,13 @@ import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import org.apache.hadoop.yarn.factories.RecordFactory; import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider; -import org.apache.hadoop.yarn.server.api.ResourceManagerAdministrationProtocol; import org.apache.hadoop.yarn.server.api.protocolrecords.RefreshAdminAclsRequest; import org.apache.hadoop.yarn.server.api.protocolrecords.RefreshNodesRequest; import org.apache.hadoop.yarn.server.api.protocolrecords.RefreshQueuesRequest; import org.apache.hadoop.yarn.server.api.protocolrecords.RefreshServiceAclsRequest; import org.apache.hadoop.yarn.server.api.protocolrecords.RefreshSuperUserGroupsConfigurationRequest; import org.apache.hadoop.yarn.server.api.protocolrecords.RefreshUserToGroupsMappingsRequest; +import org.apache.hadoop.yarn.server.api.ResourceManagerAdministrationProtocol; @Private @Unstable @@ -55,6 +62,7 @@ private final RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null); + protected YarnClient client; protected final static Map ADMIN_USAGE = ImmutableMap.builder() @@ -75,17 +83,24 @@ "ResoureceManager will reload the authorization policy file.")) .put("-getGroups", new UsageInfo("[username]", "Get the groups which given user belongs to.")) + .put("-report", new UsageInfo("", "Prints the status report of all nodes")) .put("-help", new UsageInfo("[cmd]", "Displays help for the given command or all commands if none " + "is specified.")) .build(); public RMAdminCLI() { - super(); + super(new Configuration()); + client = YarnClient.createYarnClient(); + client.init(getConf()); + client.start(); } public RMAdminCLI(Configuration conf) { super(conf); + client = YarnClient.createYarnClient(); + client.init(getConf()); + client.start(); } private static void appendHAUsage(final StringBuilder usageBuilder) { @@ -161,6 +176,7 @@ private static void printHelp(String cmd, boolean isHAEnabled) { " [-refreshAdminAcls]" + " [-refreshServiceAcl]" + " [-getGroup [username]]" + + " [-report" + " [-help [cmd]]"); if (isHAEnabled) { appendHAUsage(summary); @@ -285,6 +301,49 @@ private int getGroups(String[] usernames) throws IOException { return 0; } + private int report() throws IOException, YarnException { + List nodesReport = client.getNodeReports(); + // Use PrintWriter.println, which uses correct platform line ending. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintWriter nodeReportStr = new PrintWriter(baos); + + nodeReportStr.println("Nodes Report : "); + for (NodeReport nodeReport : nodesReport) { + nodeReportStr.print("\tNode-Id : "); + nodeReportStr.println(nodeReport.getNodeId()); + nodeReportStr.print("\tRack : "); + nodeReportStr.println(nodeReport.getRackName()); + nodeReportStr.print("\tNode-State : "); + nodeReportStr.println(nodeReport.getNodeState()); + nodeReportStr.print("\tNode-Http-Address : "); + nodeReportStr.println(nodeReport.getHttpAddress()); + nodeReportStr.print("\tLast-Health-Update : "); + nodeReportStr.println(DateFormatUtils.format( + new Date(nodeReport.getLastHealthReportTime()), + "E dd/MMM/yy hh:mm:ss:SSzz")); + nodeReportStr.print("\tHealth-Report : "); + nodeReportStr + .println(nodeReport.getHealthReport()); + nodeReportStr.print("\tContainers : "); + nodeReportStr.println(nodeReport.getNumContainers()); + nodeReportStr.print("\tMemory-Used : "); + nodeReportStr.println((nodeReport.getUsed() == null) ? "0MB" + : (nodeReport.getUsed().getMemory() + "MB")); + nodeReportStr.print("\tMemory-Capacity : "); + nodeReportStr.println(nodeReport.getCapability().getMemory() + "MB"); + nodeReportStr.print("\tCPU-Used : "); + nodeReportStr.println((nodeReport.getUsed() == null) ? "0 vcores" + : (nodeReport.getUsed().getVirtualCores() + " vcores")); + nodeReportStr.print("\tCPU-Capacity : "); + nodeReportStr.println(nodeReport.getCapability().getVirtualCores() + " vcores"); + nodeReportStr.println(); + } + + nodeReportStr.close(); + System.out.println(baos.toString("UTF-8")); + return 0; + } + @Override public int run(String[] args) throws Exception { YarnConfiguration yarnConf = @@ -328,13 +387,15 @@ public int run(String[] args) throws Exception { if ("-refreshAdminAcls".equals(cmd) || "-refreshQueues".equals(cmd) || "-refreshNodes".equals(cmd) || "-refreshServiceAcl".equals(cmd) || "-refreshUserToGroupsMappings".equals(cmd) || - "-refreshSuperUserGroupsConfiguration".equals(cmd)) { + "-refreshSuperUserGroupsConfiguration".equals(cmd) || + "-report".equals(cmd)) { if (args.length != 1) { printUsage(cmd, isHAEnabled); return exitCode; } } - + + List nodesReport = client.getNodeReports(); try { if ("-refreshQueues".equals(cmd)) { exitCode = refreshQueues(); @@ -351,6 +412,8 @@ public int run(String[] args) throws Exception { } else if ("-getGroups".equals(cmd)) { String[] usernames = Arrays.copyOfRange(args, i, args.length); exitCode = getGroups(usernames); + } else if ("-report".equals(cmd)) { + exitCode = report(); } else { exitCode = -1; System.err.println(cmd.substring(1) + ": Unknown command");