diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java index a847cd5..e142933 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java @@ -40,6 +40,9 @@ import org.apache.hadoop.yarn.api.records.ApplicationReport; import org.apache.hadoop.yarn.api.records.ApplicationResourceUsageReport; import org.apache.hadoop.yarn.api.records.ContainerReport; +import org.apache.hadoop.yarn.api.records.QueueACL; +import org.apache.hadoop.yarn.api.records.QueueInfo; +import org.apache.hadoop.yarn.api.records.QueueUserACLInfo; import org.apache.hadoop.yarn.api.records.YarnApplicationState; import org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException; import org.apache.hadoop.yarn.exceptions.YarnException; @@ -59,15 +62,18 @@ private static final String CONTAINER_PATTERN = "%30s\t%20s\t%20s\t%20s\t%20s\t%35s" + System.getProperty("line.separator"); - + private static final String APP_TYPE_CMD = "appTypes"; private static final String APP_STATE_CMD = "appStates"; private static final String ALLSTATES_OPTION = "ALL"; private static final String QUEUE_CMD = "queue"; + private static final String SHOW_ACLS_CMD = "showAcls"; + private static final String SHOW_NODE_LABELS = "showNodeLabels"; public static final String APPLICATION = "application"; public static final String APPLICATION_ATTEMPT = "applicationattempt"; public static final String CONTAINER = "container"; - + public static final String QUEUE = "queue"; + private boolean allAppStates; public static void main(String[] args) throws Exception { @@ -133,6 +139,18 @@ public int run(String[] args) throws Exception { opts.addOption(HELP_CMD, false, "Displays help for all commands."); opts.getOption(STATUS_CMD).setArgName("Container ID"); opts.getOption(LIST_CMD).setArgName("Application Attempt ID"); + } else if (args.length > 0 && args[0].equalsIgnoreCase(QUEUE)) { + title = QUEUE; + opts.addOption(LIST_ALL_CMD, false, + "List queue information about all queues."); + opts.addOption(LIST_CMD, true, + "List queue information about given queue."); + opts.addOption(SHOW_ACLS_CMD, false, + "Show Queue ACLs of current user."); + opts.addOption(SHOW_NODE_LABELS, false, + "Show Node Labels of the queue."); + opts.addOption(HELP_CMD, false, "Displays help for all commands."); + opts.getOption(LIST_CMD).setArgName("Queue Name"); } int exitCode = -1; @@ -209,6 +227,28 @@ public int run(String[] args) throws Exception { return exitCode; } listContainers(cliParser.getOptionValue(LIST_CMD)); + } else if (args[0].equalsIgnoreCase(QUEUE)) { + boolean isShowLabelsEnabled = cliParser.hasOption(SHOW_NODE_LABELS); + + if (isShowLabelsEnabled && args.length != 4) { + printUsage(title, opts); + return exitCode; + } + + /* + * If command input has show node labels option, then print the ACL of + * that queue + */ + if (isShowLabelsEnabled) { + printQueueNodeLabels(cliParser.getOptionValue(LIST_CMD)); + return 0; + } + + if (args.length != 3) { + printUsage(title, opts); + return exitCode; + } + listQueue(cliParser.getOptionValue(LIST_CMD)); } } else if (cliParser.hasOption(KILL_CMD)) { if (args.length != 3) { @@ -227,6 +267,28 @@ public int run(String[] args) throws Exception { } moveApplicationAcrossQueues(cliParser.getOptionValue(MOVE_TO_QUEUE_CMD), cliParser.getOptionValue(QUEUE_CMD)); + } else if (cliParser.hasOption(LIST_ALL_CMD)) { + /* + * If command input has ALL list option, then print complete information + * about all Queues + */ + if ((args.length != 2) || (!args[0].equalsIgnoreCase(QUEUE))) { + printUsage(title, opts); + return exitCode; + } + listQueues(); + return 0; + } else if (cliParser.hasOption(SHOW_ACLS_CMD)) { + /* + * If command input has show acl option, then print complete information + * about acls of all Queues + */ + if ((args.length != 2) || (!args[0].equalsIgnoreCase(QUEUE))) { + printUsage(title, opts); + return exitCode; + } + printQueueACLs(); + return 0; } else if (cliParser.hasOption(HELP_CMD)) { printUsage(title, opts); return 0; @@ -543,4 +605,120 @@ private void listContainers(String appAttemptId) throws YarnException, } writer.flush(); } + + /** + * Lists the Queue Information matching the given queue name + * + * @param queueName + * @throws YarnException + * @throws IOException + */ + private void listQueue(String queueName) throws YarnException, IOException { + PrintWriter writer = new PrintWriter(sysout); + + QueueInfo queueInfo = client.getQueueInfo(queueName); + writer.println("Queue Information : "); + printQueueInfo(writer, queueInfo); + writer.flush(); + } + + /** + * Lists the Queue Information for all queues + * + * @param + * @throws YarnException + * @throws IOException + */ + private void listQueues() throws YarnException, IOException { + PrintWriter writer = new PrintWriter(sysout); + + List queuesInfo = client.getAllQueues(); + writer.println("Total number of queues " + ": " + queuesInfo.size()); + writer.println("Queue Information : "); + for (QueueInfo queueInfo : queuesInfo) { + printQueueInfo(writer, queueInfo); + } + writer.flush(); + } + + private void printQueueInfo(PrintWriter writer, QueueInfo queueInfo) { + writer.print("Queue Name : "); + writer.println(queueInfo.getQueueName()); + + Set nodeLabels = queueInfo.getAccessibleNodeLabels(); + StringBuilder labelList = new StringBuilder(); + writer.print("\tNode Labels : "); + for (String nodeLabel : nodeLabels) { + if (labelList.length() > 0) { + labelList.append(','); + } + labelList.append(nodeLabel); + } + writer.println(labelList.toString()); + writer.print("\tState : "); + writer.println(queueInfo.getQueueState()); + DecimalFormat df = new DecimalFormat("#.0"); + writer.print("\tCapacity : "); + writer.println(df.format(queueInfo.getCapacity() * 100) + "%"); + writer.print("\tCurrent Capacity : "); + writer.println(df.format(queueInfo.getCurrentCapacity() * 100) + "%"); + } + + /** + * Lists the Queue ACL Information for current user + * + * @param + * @throws YarnException + * @throws IOException + */ + private void printQueueACLs() throws YarnException, IOException { + PrintWriter writer = new PrintWriter(sysout); + + List queueAcls = client.getQueueAclsInfo(); + writer.println("Queue ACL Information : "); + + for (QueueUserACLInfo queueAcl : queueAcls) { + writer.print("Queue Name : "); + writer.println(queueAcl.getQueueName()); + StringBuilder aclOpsList = new StringBuilder(); + for (QueueACL qAcl : queueAcl.getUserAcls()) { + if (aclOpsList.length() > 0) { + aclOpsList.append(','); + } + aclOpsList.append(qAcl.toString()); + } + writer.print("\tQueue Operations : "); + writer.println(aclOpsList.toString()); + } + writer.flush(); + } + + /** + * Lists the Node Label Information about given queue + * + * @param queueName + * @throws YarnException + * @throws IOException + */ + private void printQueueNodeLabels(String queueName) throws YarnException, + IOException { + PrintWriter writer = new PrintWriter(sysout); + + QueueInfo queueInfo = client.getQueueInfo(queueName); + writer.println("Queue Information : "); + writer.print("Queue Name : "); + writer.println(queueInfo.getQueueName()); + + Set nodeLabels = queueInfo.getAccessibleNodeLabels(); + StringBuilder labelList = new StringBuilder(); + writer.print("\tNode Labels : "); + for (String nodeLabel : nodeLabels) { + if (labelList.length() > 0) { + labelList.append(','); + } + labelList.append(nodeLabel); + } + writer.println(labelList.toString()); + writer.flush(); + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/YarnCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/YarnCLI.java index 26349fa..c0d8eff 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/YarnCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/YarnCLI.java @@ -32,6 +32,7 @@ public static final String STATUS_CMD = "status"; public static final String LIST_CMD = "list"; + public static final String LIST_ALL_CMD = "listall"; public static final String KILL_CMD = "kill"; public static final String MOVE_TO_QUEUE_CMD = "movetoqueue"; public static final String HELP_CMD = "help"; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java index 980517f..16f307b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java @@ -40,7 +40,6 @@ import java.util.Set; import org.junit.Assert; - import org.apache.commons.lang.time.DateFormatUtils; import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; import org.apache.hadoop.yarn.api.records.ApplicationAttemptReport; @@ -55,6 +54,10 @@ import org.apache.hadoop.yarn.api.records.NodeReport; import org.apache.hadoop.yarn.api.records.NodeState; import org.apache.hadoop.yarn.api.records.Priority; +import org.apache.hadoop.yarn.api.records.QueueACL; +import org.apache.hadoop.yarn.api.records.QueueInfo; +import org.apache.hadoop.yarn.api.records.QueueState; +import org.apache.hadoop.yarn.api.records.QueueUserACLInfo; import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.YarnApplicationAttemptState; import org.apache.hadoop.yarn.api.records.YarnApplicationState; @@ -64,7 +67,6 @@ import org.junit.Before; import org.junit.Test; import org.mortbay.log.Log; - import org.apache.commons.cli.Options; public class TestYarnCLI { @@ -649,7 +651,126 @@ public void testGetApplications() throws Exception { Assert.assertEquals(appsReportStr, sysOutStream.toString()); verify(sysOut, times(6)).write(any(byte[].class), anyInt(), anyInt()); } + + @Test + public void testGetQueueNodeLabels() throws Exception { + ApplicationCLI cli = createAndGetAppCLI(); + Set nodeLabels = new HashSet(); + nodeLabels.add("GPU"); + nodeLabels.add("JDK_7"); + QueueInfo queueInfo = QueueInfo.newInstance("queueA", 0.4f, 0.8f, 0.5f, + null, null, QueueState.RUNNING, nodeLabels, null); + when(client.getQueueInfo(any(String.class))).thenReturn(queueInfo); + int result = cli.run(new String[] { "queue", "-list", "queueA", + "-showNodeLabels" }); + assertEquals(0, result); + verify(client).getQueueInfo("queueA"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(baos); + pw.println("Queue Information : "); + pw.println("Queue Name : " + "queueA"); + pw.println("\tNode Labels : " + "JDK_7,GPU"); + pw.close(); + String queueInfoStr = baos.toString("UTF-8"); + Assert.assertEquals(queueInfoStr, sysOutStream.toString()); + } + + @Test + public void testGetQueueACLs() throws Exception { + ApplicationCLI cli = createAndGetAppCLI(); + List queueAcls = new ArrayList(); + List acls = new ArrayList(); + acls.add(QueueACL.SUBMIT_APPLICATIONS); + acls.add(QueueACL.ADMINISTER_QUEUE); + QueueUserACLInfo queueAcl = QueueUserACLInfo.newInstance("queueA", acls); + queueAcls.add(queueAcl); + + when(client.getQueueAclsInfo()).thenReturn(queueAcls); + int result = cli.run(new String[] { "queue", "-showAcls" }); + assertEquals(0, result); + verify(client).getQueueAclsInfo(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(baos); + pw.println("Queue ACL Information : "); + pw.println("Queue Name : " + "queueA"); + pw.println("\tQueue Operations : " + + "SUBMIT_APPLICATIONS,ADMINISTER_QUEUE"); + pw.close(); + String queueInfoStr = baos.toString("UTF-8"); + Assert.assertEquals(queueInfoStr, sysOutStream.toString()); + } + + @Test + public void testGetQueueInfo() throws Exception { + ApplicationCLI cli = createAndGetAppCLI(); + Set nodeLabels = new HashSet(); + nodeLabels.add("GPU"); + nodeLabels.add("JDK_7"); + QueueInfo queueInfo = QueueInfo.newInstance("queueA", 0.4f, 0.8f, 0.5f, + null, null, QueueState.RUNNING, nodeLabels, null); + when(client.getQueueInfo(any(String.class))).thenReturn(queueInfo); + int result = cli.run(new String[] { "queue", "-list", "queueA" }); + assertEquals(0, result); + verify(client).getQueueInfo("queueA"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(baos); + pw.println("Queue Information : "); + pw.println("Queue Name : " + "queueA"); + pw.println("\tNode Labels : " + "JDK_7,GPU"); + pw.println("\tState : " + "RUNNING"); + pw.println("\tCapacity : " + "40.0%"); + pw.println("\tCurrent Capacity : " + "50.0%"); + pw.close(); + String queueInfoStr = baos.toString("UTF-8"); + Assert.assertEquals(queueInfoStr, sysOutStream.toString()); + } + @Test + public void testGetAllQueues() throws Exception { + ApplicationCLI cli = createAndGetAppCLI(); + Set nodeLabels = new HashSet(); + nodeLabels.add("GPU"); + nodeLabels.add("JDK_7"); + QueueInfo queueInfoB = QueueInfo.newInstance("queueB", 0.4f, 0.6f, 0.5f, + null, null, QueueState.RUNNING, nodeLabels, null); + QueueInfo queueInfoC = QueueInfo.newInstance("queueC", 0.6f, 0.6f, 0.5f, + null, null, QueueState.RUNNING, nodeLabels, null); + List childQueues = new ArrayList(); + childQueues.add(queueInfoB); + childQueues.add(queueInfoC); + QueueInfo queueInfoA = QueueInfo.newInstance("queueA", 1.0f, 1.0f, 1.0f, + childQueues, null, QueueState.RUNNING, nodeLabels, null); + List allQueues = new ArrayList(); + allQueues.add(queueInfoA); + allQueues.addAll(childQueues); + when(client.getAllQueues()).thenReturn(allQueues); + int result = cli.run(new String[] { "queue", "-listall" }); + assertEquals(0, result); + verify(client).getAllQueues(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(baos); + pw.println("Total number of queues : 3"); + pw.println("Queue Information : "); + pw.println("Queue Name : " + "queueA"); + pw.println("\tNode Labels : " + "JDK_7,GPU"); + pw.println("\tState : " + "RUNNING"); + pw.println("\tCapacity : " + "100.0%"); + pw.println("\tCurrent Capacity : " + "100.0%"); + pw.println("Queue Name : " + "queueB"); + pw.println("\tNode Labels : " + "JDK_7,GPU"); + pw.println("\tState : " + "RUNNING"); + pw.println("\tCapacity : " + "40.0%"); + pw.println("\tCurrent Capacity : " + "50.0%"); + pw.println("Queue Name : " + "queueC"); + pw.println("\tNode Labels : " + "JDK_7,GPU"); + pw.println("\tState : " + "RUNNING"); + pw.println("\tCapacity : " + "60.0%"); + pw.println("\tCurrent Capacity : " + "50.0%"); + pw.close(); + String queueInfoStr = baos.toString("UTF-8"); + Assert.assertEquals(queueInfoStr, sysOutStream.toString()); + } + private List getApplicationReports( List applicationReports, Set appTypes, EnumSet appStates, -- 1.9.4.msysgit.1