hadoop-yarn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From w...@apache.org
Subject svn commit: r1524865 [2/8] - in /hadoop/common/branches/HDFS-4949/hadoop-yarn-project: ./ hadoop-yarn/bin/ hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/ hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/protoc...
Date Thu, 19 Sep 2013 23:42:29 GMT
Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java Thu Sep 19 23:42:10 2013
@@ -21,11 +21,15 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
+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.time.DateFormatUtils;
 import org.apache.hadoop.classification.InterfaceAudience.Private;
@@ -40,9 +44,12 @@ import org.apache.hadoop.yarn.util.Conve
 @Private
 @Unstable
 public class NodeCLI extends YarnCLI {
-  private static final String NODES_PATTERN = "%16s\t%10s\t%17s\t%18s" +
+  private static final String NODES_PATTERN = "%16s\t%15s\t%17s\t%28s" +
     System.getProperty("line.separator");
 
+  private static final String NODE_STATE_CMD = "states";
+  private static final String NODE_ALL = "all";
+
   public static void main(String[] args) throws Exception {
     NodeCLI cli = new NodeCLI();
     cli.setSysOutPrintStream(System.out);
@@ -57,10 +64,30 @@ public class NodeCLI extends YarnCLI {
 
     Options opts = new Options();
     opts.addOption(STATUS_CMD, true, "Prints the status report of the node.");
-    opts.addOption(LIST_CMD, false, "Lists all the nodes in the RUNNING state.");
-    CommandLine cliParser = new GnuParser().parse(opts, args);
+    opts.addOption(LIST_CMD, false, "List all running nodes. " +
+        "Supports optional use of -states to filter nodes " +
+        "based on node state, all -all to list all nodes.");
+    Option nodeStateOpt = new Option(NODE_STATE_CMD, true,
+        "Works with -list to filter nodes based on input comma-separated list of node states.");
+    nodeStateOpt.setValueSeparator(',');
+    nodeStateOpt.setArgs(Option.UNLIMITED_VALUES);
+    nodeStateOpt.setArgName("States");
+    opts.addOption(nodeStateOpt);
+    Option allOpt = new Option(NODE_ALL, false,
+        "Works with -list to list all nodes.");
+    opts.addOption(allOpt);
+    opts.getOption(STATUS_CMD).setArgName("NodeId");
 
     int exitCode = -1;
+    CommandLine cliParser = null;
+    try {
+      cliParser = new GnuParser().parse(opts, args);
+    } catch (MissingArgumentException ex) {
+      sysout.println("Missing argument for options");
+      printUsage(opts);
+      return exitCode;
+    }
+
     if (cliParser.hasOption("status")) {
       if (args.length != 2) {
         printUsage(opts);
@@ -68,7 +95,24 @@ public class NodeCLI extends YarnCLI {
       }
       printNodeStatus(cliParser.getOptionValue("status"));
     } else if (cliParser.hasOption("list")) {
-      listClusterNodes();
+      Set<NodeState> nodeStates = new HashSet<NodeState>();
+      if (cliParser.hasOption(NODE_ALL)) {
+        for (NodeState state : NodeState.values()) {
+          nodeStates.add(state);
+        }
+      } else if (cliParser.hasOption(NODE_STATE_CMD)) {
+        String[] types = cliParser.getOptionValues(NODE_STATE_CMD);
+        if (types != null) {
+          for (String type : types) {
+            if (!type.trim().isEmpty()) {
+              nodeStates.add(NodeState.valueOf(type.trim().toUpperCase()));
+            }
+          }
+        }
+      } else {
+        nodeStates.add(NodeState.RUNNING);
+      }
+      listClusterNodes(nodeStates);
     } else {
       syserr.println("Invalid Command Usage : ");
       printUsage(opts);
@@ -86,17 +130,20 @@ public class NodeCLI extends YarnCLI {
   }
 
   /**
-   * Lists all the nodes present in the cluster
+   * Lists the nodes matching the given node states
    * 
+   * @param nodeStates
    * @throws YarnException
    * @throws IOException
    */
-  private void listClusterNodes() throws YarnException, IOException {
+  private void listClusterNodes(Set<NodeState> nodeStates) 
+            throws YarnException, IOException {
     PrintWriter writer = new PrintWriter(sysout);
-    List<NodeReport> nodesReport = client.getNodeReports(NodeState.RUNNING);
+    List<NodeReport> nodesReport = client.getNodeReports(
+                                       nodeStates.toArray(new NodeState[0]));
     writer.println("Total Nodes:" + nodesReport.size());
     writer.printf(NODES_PATTERN, "Node-Id", "Node-State", "Node-Http-Address",
-        "Running-Containers");
+        "Number-of-Running-Containers");
     for (NodeReport nodeReport : nodesReport) {
       writer.printf(NODES_PATTERN, nodeReport.getNodeId(), nodeReport
           .getNodeState(), nodeReport.getHttpAddress(), nodeReport

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java Thu Sep 19 23:42:10 2013
@@ -18,13 +18,16 @@
 
 package org.apache.hadoop.yarn.client.api.impl;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -97,6 +100,7 @@ public class TestAMRMClient {
   static String rack;
   static String[] nodes;
   static String[] racks;
+  private final static int DEFAULT_ITERATION = 3;
   
   @BeforeClass
   public static void setup() throws Exception {
@@ -476,6 +480,144 @@ public class TestAMRMClient {
       }
     }
   }
+  
+  @Test (timeout=60000)
+  public void testAllocationWithBlacklist() throws YarnException, IOException {
+    AMRMClientImpl<ContainerRequest> amClient = null;
+    try {
+      // start am rm client
+      amClient =
+          (AMRMClientImpl<ContainerRequest>) AMRMClient
+            .<ContainerRequest> createAMRMClient();
+      amClient.init(conf);
+      amClient.start();
+      amClient.registerApplicationMaster("Host", 10000, "");
+      
+      assertTrue(amClient.ask.size() == 0);
+      assertTrue(amClient.release.size() == 0);
+      
+      ContainerRequest storedContainer1 = 
+          new ContainerRequest(capability, nodes, racks, priority);
+      amClient.addContainerRequest(storedContainer1);
+      assertTrue(amClient.ask.size() == 3);
+      assertTrue(amClient.release.size() == 0);
+      
+      List<String> localNodeBlacklist = new ArrayList<String>();
+      localNodeBlacklist.add(node);
+      
+      // put node in black list, so no container assignment
+      amClient.updateBlacklist(localNodeBlacklist, null);
+
+      int allocatedContainerCount = getAllocatedContainersNumber(amClient,
+        DEFAULT_ITERATION);
+      // the only node is in blacklist, so no allocation
+      assertTrue(allocatedContainerCount == 0);
+
+      // Remove node from blacklist, so get assigned with 2
+      amClient.updateBlacklist(null, localNodeBlacklist);
+      ContainerRequest storedContainer2 = 
+              new ContainerRequest(capability, nodes, racks, priority);
+      amClient.addContainerRequest(storedContainer2);
+      allocatedContainerCount = getAllocatedContainersNumber(amClient,
+          DEFAULT_ITERATION);
+      assertEquals(allocatedContainerCount, 2);
+      
+      // Test in case exception in allocate(), blacklist is kept
+      assertTrue(amClient.blacklistAdditions.isEmpty());
+      assertTrue(amClient.blacklistRemovals.isEmpty());
+      
+      // create a invalid ContainerRequest - memory value is minus
+      ContainerRequest invalidContainerRequest = 
+          new ContainerRequest(Resource.newInstance(-1024, 1),
+              nodes, racks, priority);
+      amClient.addContainerRequest(invalidContainerRequest);
+      amClient.updateBlacklist(localNodeBlacklist, null);
+      try {
+        // allocate() should complain as ContainerRequest is invalid.
+        amClient.allocate(0.1f);
+        fail("there should be an exception here.");
+      } catch (Exception e) {
+        assertEquals(amClient.blacklistAdditions.size(), 1);
+      }
+    } finally {
+      if (amClient != null && amClient.getServiceState() == STATE.STARTED) {
+        amClient.stop();
+      }
+    }
+  }
+  
+  @Test (timeout=60000)
+  public void testAMRMClientWithBlacklist() throws YarnException, IOException {
+    AMRMClientImpl<ContainerRequest> amClient = null;
+    try {
+      // start am rm client
+      amClient =
+          (AMRMClientImpl<ContainerRequest>) AMRMClient
+            .<ContainerRequest> createAMRMClient();
+      amClient.init(conf);
+      amClient.start();
+      amClient.registerApplicationMaster("Host", 10000, "");
+      String[] nodes = {"node1", "node2", "node3"};
+      
+      // Add nodes[0] and nodes[1]
+      List<String> nodeList01 = new ArrayList<String>();
+      nodeList01.add(nodes[0]);
+      nodeList01.add(nodes[1]);
+      amClient.updateBlacklist(nodeList01, null);
+      assertEquals(amClient.blacklistAdditions.size(),2);
+      assertEquals(amClient.blacklistRemovals.size(),0);
+      
+      // Add nodes[0] again, verify it is not added duplicated.
+      List<String> nodeList02 = new ArrayList<String>();
+      nodeList02.add(nodes[0]);
+      nodeList02.add(nodes[2]);
+      amClient.updateBlacklist(nodeList02, null);
+      assertEquals(amClient.blacklistAdditions.size(),3);
+      assertEquals(amClient.blacklistRemovals.size(),0);
+      
+      // Add nodes[1] and nodes[2] to removal list, 
+      // Verify addition list remove these two nodes.
+      List<String> nodeList12 = new ArrayList<String>();
+      nodeList12.add(nodes[1]);
+      nodeList12.add(nodes[2]);
+      amClient.updateBlacklist(null, nodeList12);
+      assertEquals(amClient.blacklistAdditions.size(),1);
+      assertEquals(amClient.blacklistRemovals.size(),2);
+      
+      // Add nodes[1] again to addition list, 
+      // Verify removal list will remove this node.
+      List<String> nodeList1 = new ArrayList<String>();
+      nodeList1.add(nodes[1]);
+      amClient.updateBlacklist(nodeList1, null);
+      assertEquals(amClient.blacklistAdditions.size(),2);
+      assertEquals(amClient.blacklistRemovals.size(),1);
+    } finally {
+      if (amClient != null && amClient.getServiceState() == STATE.STARTED) {
+        amClient.stop();
+      }
+    }
+  }
+
+  private int getAllocatedContainersNumber(
+      AMRMClientImpl<ContainerRequest> amClient, int iterationsLeft)
+      throws YarnException, IOException {
+    int allocatedContainerCount = 0;
+    while (iterationsLeft-- > 0) {
+      Log.info(" == alloc " + allocatedContainerCount + " it left " + iterationsLeft);
+      AllocateResponse allocResponse = amClient.allocate(0.1f);
+      assertTrue(amClient.ask.size() == 0);
+      assertTrue(amClient.release.size() == 0);
+        
+      assertTrue(nodeCount == amClient.getClusterNodeCount());
+      allocatedContainerCount += allocResponse.getAllocatedContainers().size();
+        
+      if(allocatedContainerCount == 0) {
+        // sleep to let NM's heartbeat to RM and trigger allocations
+        sleep(100);
+      }
+    }
+    return allocatedContainerCount;
+  }
 
   @Test (timeout=60000)
   public void testAMRMClient() throws YarnException, IOException {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java Thu Sep 19 23:42:10 2013
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.when;
 import java.io.IOException;
 import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
+import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -55,10 +56,12 @@ import org.apache.hadoop.yarn.client.api
 import org.apache.hadoop.yarn.client.api.YarnClientApplication;
 import org.apache.hadoop.yarn.conf.YarnConfiguration;
 import org.apache.hadoop.yarn.exceptions.YarnException;
+import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
 import org.apache.hadoop.yarn.server.MiniYARNCluster;
 import org.apache.hadoop.yarn.server.resourcemanager.MockRM;
 import org.apache.hadoop.yarn.server.resourcemanager.ResourceManager;
 import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp;
+import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppState;
 import org.apache.hadoop.yarn.util.Records;
 import org.apache.log4j.Level;
 import org.apache.log4j.LogManager;
@@ -83,6 +86,7 @@ public class TestYarnClient {
     client.init(conf);
     client.start();
     client.stop();
+    rm.stop();
   }
 
   @Test (timeout = 30000)
@@ -163,11 +167,15 @@ public class TestYarnClient {
 
     List<ApplicationReport> expectedReports = ((MockYarnClient)client).getReports();
 
+    List<ApplicationReport>  reports = client.getApplications();
+    Assert.assertEquals(reports, expectedReports);
+
     Set<String> appTypes = new HashSet<String>();
     appTypes.add("YARN");
     appTypes.add("NON-YARN");
 
-    List<ApplicationReport> reports = client.getApplications(appTypes);
+    reports =
+        client.getApplications(appTypes, null);
     Assert.assertEquals(reports.size(), 2);
     Assert
         .assertTrue((reports.get(0).getApplicationType().equals("YARN") && reports
@@ -178,8 +186,28 @@ public class TestYarnClient {
       Assert.assertTrue(expectedReports.contains(report));
     }
 
-    reports = client.getApplications();
-    Assert.assertEquals(reports, expectedReports);
+    EnumSet<YarnApplicationState> appStates =
+        EnumSet.noneOf(YarnApplicationState.class);
+    appStates.add(YarnApplicationState.FINISHED);
+    appStates.add(YarnApplicationState.FAILED);
+    reports = client.getApplications(null, appStates);
+    Assert.assertEquals(reports.size(), 2);
+    Assert
+    .assertTrue((reports.get(0).getApplicationType().equals("NON-YARN") && reports
+        .get(1).getApplicationType().equals("NON-MAPREDUCE"))
+        || (reports.get(1).getApplicationType().equals("NON-YARN") && reports
+            .get(0).getApplicationType().equals("NON-MAPREDUCE")));
+    for (ApplicationReport report : reports) {
+      Assert.assertTrue(expectedReports.contains(report));
+    }
+
+    reports = client.getApplications(appTypes, appStates);
+    Assert.assertEquals(reports.size(), 1);
+    Assert
+    .assertTrue((reports.get(0).getApplicationType().equals("NON-YARN")));
+    for (ApplicationReport report : reports) {
+      Assert.assertTrue(expectedReports.contains(report));
+    }
 
     client.stop();
   }
@@ -187,6 +215,8 @@ public class TestYarnClient {
   private static class MockYarnClient extends YarnClientImpl {
     private ApplicationReport mockReport;
     private List<ApplicationReport> reports;
+    GetApplicationsResponse mockAppResponse =
+        mock(GetApplicationsResponse.class);
 
     public MockYarnClient() {
       super();
@@ -202,6 +232,8 @@ public class TestYarnClient {
       try{
         when(rmClient.getApplicationReport(any(
             GetApplicationReportRequest.class))).thenReturn(mockResponse);
+        when(rmClient.getApplications(any(GetApplicationsRequest.class)))
+            .thenReturn(mockAppResponse);
       } catch (YarnException e) {
         Assert.fail("Exception is not expected.");
       } catch (IOException e) {
@@ -212,16 +244,11 @@ public class TestYarnClient {
 
     @Override
     public List<ApplicationReport> getApplications(
-        Set<String> applicationTypes) throws YarnException, IOException {
-      GetApplicationsRequest request =
-          applicationTypes == null ? GetApplicationsRequest.newInstance()
-              : GetApplicationsRequest.newInstance(applicationTypes);
-      when(rmClient.getApplications(request))
-          .thenReturn(
-              getApplicationReports(reports,
-                  request));
-      GetApplicationsResponse response = rmClient.getApplications(request);
-      return response.getApplicationList();
+        Set<String> applicationTypes, EnumSet<YarnApplicationState> applicationStates)
+        throws YarnException, IOException {
+      when(mockAppResponse.getApplicationList()).thenReturn(
+          getApplicationReports(reports, applicationTypes, applicationStates));
+      return super.getApplications(applicationTypes, applicationStates);
     }
 
     @Override
@@ -243,7 +270,7 @@ public class TestYarnClient {
       ApplicationReport newApplicationReport = ApplicationReport.newInstance(
           applicationId, ApplicationAttemptId.newInstance(applicationId, 1),
           "user", "queue", "appname", "host", 124, null,
-          YarnApplicationState.FINISHED, "diagnostics", "url", 0, 0,
+          YarnApplicationState.RUNNING, "diagnostics", "url", 0, 0,
           FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.53789f, "YARN", null);
       List<ApplicationReport> applicationReports =
           new ArrayList<ApplicationReport>();
@@ -262,31 +289,44 @@ public class TestYarnClient {
       ApplicationReport newApplicationReport3 = ApplicationReport.newInstance(
           applicationId3, ApplicationAttemptId.newInstance(applicationId3, 3),
           "user3", "queue3", "appname3", "host3", 126, null,
-          YarnApplicationState.FINISHED, "diagnostics3", "url3", 3, 3,
-          FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.73789f, "MAPREDUCE", 
+          YarnApplicationState.RUNNING, "diagnostics3", "url3", 3, 3,
+          FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.73789f, "MAPREDUCE",
         null);
       applicationReports.add(newApplicationReport3);
+
+      ApplicationId applicationId4 = ApplicationId.newInstance(1234, 8);
+      ApplicationReport newApplicationReport4 =
+          ApplicationReport.newInstance(
+              applicationId4,
+              ApplicationAttemptId.newInstance(applicationId4, 4),
+              "user4", "queue4", "appname4", "host4", 127, null,
+              YarnApplicationState.FAILED, "diagnostics4", "url4", 4, 4,
+              FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.83789f,
+              "NON-MAPREDUCE", null);
+      applicationReports.add(newApplicationReport4);
       return applicationReports;
     }
 
-    private GetApplicationsResponse getApplicationReports(
+    private List<ApplicationReport> getApplicationReports(
         List<ApplicationReport> applicationReports,
-        GetApplicationsRequest request) {
+        Set<String> applicationTypes, EnumSet<YarnApplicationState> applicationStates) {
 
       List<ApplicationReport> appReports = new ArrayList<ApplicationReport>();
-      Set<String> appTypes = request.getApplicationTypes();
-      boolean bypassFilter = appTypes.isEmpty();
-
       for (ApplicationReport appReport : applicationReports) {
-        if (!(bypassFilter || appTypes.contains(
-            appReport.getApplicationType()))) {
-          continue;
+        if (applicationTypes != null && !applicationTypes.isEmpty()) {
+          if (!applicationTypes.contains(appReport.getApplicationType())) {
+            continue;
+          }
+        }
+
+        if (applicationStates != null && !applicationStates.isEmpty()) {
+          if (!applicationStates.contains(appReport.getYarnApplicationState())) {
+            continue;
+          }
         }
         appReports.add(appReport);
       }
-      GetApplicationsResponse response =
-          GetApplicationsResponse.newInstance(appReports);
-      return response;
+      return appReports;
     }
   }
 

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java Thu Sep 19 23:42:10 2013
@@ -26,12 +26,15 @@ import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.doThrow;
 
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -70,6 +73,7 @@ public class TestYarnCLI {
     sysOut = spy(new PrintStream(sysOutStream));
     sysErrStream = new ByteArrayOutputStream();
     sysErr = spy(new PrintStream(sysErrStream));
+    System.setOut(sysOut);
   }
   
   @Test
@@ -133,7 +137,7 @@ public class TestYarnCLI {
     ApplicationReport newApplicationReport = ApplicationReport.newInstance(
         applicationId, ApplicationAttemptId.newInstance(applicationId, 1),
         "user", "queue", "appname", "host", 124, null,
-        YarnApplicationState.FINISHED, "diagnostics", "url", 0, 0,
+        YarnApplicationState.RUNNING, "diagnostics", "url", 0, 0,
         FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.53789f, "YARN", null);
     List<ApplicationReport> applicationReports = new ArrayList<ApplicationReport>();
     applicationReports.add(newApplicationReport);
@@ -151,23 +155,59 @@ public class TestYarnCLI {
     ApplicationReport newApplicationReport3 = ApplicationReport.newInstance(
         applicationId3, ApplicationAttemptId.newInstance(applicationId3, 3),
         "user3", "queue3", "appname3", "host3", 126, null,
-        YarnApplicationState.FINISHED, "diagnostics3", "url3", 3, 3,
+        YarnApplicationState.RUNNING, "diagnostics3", "url3", 3, 3,
         FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.73789f, "MAPREDUCE", 
         null);
     applicationReports.add(newApplicationReport3);
 
-    Set<String> appType1 = new HashSet<String>();
-    appType1.add("YARN");
+    ApplicationId applicationId4 = ApplicationId.newInstance(1234, 8);
+    ApplicationReport newApplicationReport4 = ApplicationReport.newInstance(
+        applicationId4, ApplicationAttemptId.newInstance(applicationId4, 4),
+        "user4", "queue4", "appname4", "host4", 127, null,
+        YarnApplicationState.FAILED, "diagnostics4", "url4", 4, 4,
+        FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.83789f, "NON-MAPREDUCE",
+        null);
+    applicationReports.add(newApplicationReport4);
+
+    ApplicationId applicationId5 = ApplicationId.newInstance(1234, 9);
+    ApplicationReport newApplicationReport5 = ApplicationReport.newInstance(
+        applicationId5, ApplicationAttemptId.newInstance(applicationId5, 5),
+        "user5", "queue5", "appname5", "host5", 128, null,
+        YarnApplicationState.ACCEPTED, "diagnostics5", "url5", 5, 5,
+        FinalApplicationStatus.KILLED, null, "N/A", 0.93789f, "HIVE",
+        null);
+    applicationReports.add(newApplicationReport5);
+
+    ApplicationId applicationId6 = ApplicationId.newInstance(1234, 10);
+    ApplicationReport newApplicationReport6 = ApplicationReport.newInstance(
+        applicationId6, ApplicationAttemptId.newInstance(applicationId6, 6),
+        "user6", "queue6", "appname6", "host6", 129, null,
+        YarnApplicationState.SUBMITTED, "diagnostics6", "url6", 6, 6,
+        FinalApplicationStatus.KILLED, null, "N/A", 0.99789f, "PIG",
+        null);
+    applicationReports.add(newApplicationReport6);
 
-    when(client.getApplications(appType1)).thenReturn(
-        getApplicationReports(applicationReports, appType1));
-    int result = cli.run(new String[] { "-list", "-appTypes", "YARN" });
+    // Test command yarn application -list
+    // if the set appStates is empty, RUNNING state will be automatically added
+    // to the appStates list
+    // the output of yarn application -list should be the same as
+    // equals to yarn application -list --appStates RUNNING,ACCEPTED,SUBMITTED
+    Set<String> appType1 = new HashSet<String>();
+    EnumSet<YarnApplicationState> appState1 =
+        EnumSet.noneOf(YarnApplicationState.class);
+    appState1.add(YarnApplicationState.RUNNING);
+    appState1.add(YarnApplicationState.ACCEPTED);
+    appState1.add(YarnApplicationState.SUBMITTED);
+    when(client.getApplications(appType1, appState1)).thenReturn(
+        getApplicationReports(applicationReports, appType1, appState1, false));
+    int result = cli.run(new String[] { "-list" });
     assertEquals(0, result);
-    verify(client).getApplications(appType1);
+    verify(client).getApplications(appType1, appState1);
 
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     PrintWriter pw = new PrintWriter(baos);
-    pw.println("Total Applications:1");
+    pw.println("Total number of applications (application-types: " + appType1
+        + " and states: " + appState1 + ")" + ":" + 4);
     pw.print("                Application-Id\t    Application-Name");
     pw.print("\t    Application-Type");
     pw.print("\t      User\t     Queue\t             State\t       ");
@@ -175,27 +215,54 @@ public class TestYarnCLI {
     pw.println("\t                       Tracking-URL");
     pw.print("         application_1234_0005\t             ");
     pw.print("appname\t                YARN\t      user\t     ");
-    pw.print("queue\t          FINISHED\t         ");
+    pw.print("queue\t           RUNNING\t         ");
     pw.print("SUCCEEDED\t         53.79%");
     pw.println("\t                                N/A");
+    pw.print("         application_1234_0007\t            ");
+    pw.print("appname3\t           MAPREDUCE\t     user3\t    ");
+    pw.print("queue3\t           RUNNING\t         ");
+    pw.print("SUCCEEDED\t         73.79%");
+    pw.println("\t                                N/A");
+    pw.print("         application_1234_0009\t            ");
+    pw.print("appname5\t                HIVE\t     user5\t    ");
+    pw.print("queue5\t          ACCEPTED\t            ");
+    pw.print("KILLED\t         93.79%");
+    pw.println("\t                                N/A");
+    pw.print("         application_1234_0010\t            ");
+    pw.print("appname6\t                 PIG\t     user6\t    ");
+    pw.print("queue6\t         SUBMITTED\t            ");
+    pw.print("KILLED\t         99.79%");
+    pw.println("\t                                N/A");
     pw.close();
     String appsReportStr = baos.toString("UTF-8");
     Assert.assertEquals(appsReportStr, sysOutStream.toString());
     verify(sysOut, times(1)).write(any(byte[].class), anyInt(), anyInt());
 
+    //Test command yarn application -list --appTypes apptype1,apptype2
+    //the output should be the same as
+    // yarn application -list --appTypes apptyp1, apptype2 --appStates
+    // RUNNING,ACCEPTED,SUBMITTED
     sysOutStream.reset();
     Set<String> appType2 = new HashSet<String>();
     appType2.add("YARN");
-    appType2.add("FOO-YARN");
-    when(client.getApplications(appType2)).thenReturn(
-        getApplicationReports(applicationReports, appType2));
-    cli.run(new String[] { "-list", "-appTypes", "YARN , ,, ,FOO-YARN",
-        ",,,,, YARN,," });
+    appType2.add("NON-YARN");
+
+    EnumSet<YarnApplicationState> appState2 =
+        EnumSet.noneOf(YarnApplicationState.class);
+    appState2.add(YarnApplicationState.RUNNING);
+    appState2.add(YarnApplicationState.ACCEPTED);
+    appState2.add(YarnApplicationState.SUBMITTED);
+    when(client.getApplications(appType2, appState2)).thenReturn(
+        getApplicationReports(applicationReports, appType2, appState2, false));
+    result =
+        cli.run(new String[] { "-list", "-appTypes", "YARN, ,,  NON-YARN",
+            "   ,, ,," });
     assertEquals(0, result);
-    verify(client).getApplications(appType2);
+    verify(client).getApplications(appType2, appState2);
     baos = new ByteArrayOutputStream();
     pw = new PrintWriter(baos);
-    pw.println("Total Applications:1");
+    pw.println("Total number of applications (application-types: " + appType2
+        + " and states: " + appState2 + ")" + ":" + 1);
     pw.print("                Application-Id\t    Application-Name");
     pw.print("\t    Application-Type");
     pw.print("\t      User\t     Queue\t             State\t       ");
@@ -203,7 +270,7 @@ public class TestYarnCLI {
     pw.println("\t                       Tracking-URL");
     pw.print("         application_1234_0005\t             ");
     pw.print("appname\t                YARN\t      user\t     ");
-    pw.print("queue\t          FINISHED\t         ");
+    pw.print("queue\t           RUNNING\t         ");
     pw.print("SUCCEEDED\t         53.79%");
     pw.println("\t                                N/A");
     pw.close();
@@ -211,50 +278,122 @@ public class TestYarnCLI {
     Assert.assertEquals(appsReportStr, sysOutStream.toString());
     verify(sysOut, times(2)).write(any(byte[].class), anyInt(), anyInt());
 
+    //Test command yarn application -list --appStates appState1,appState2
     sysOutStream.reset();
     Set<String> appType3 = new HashSet<String>();
-    appType3.add("YARN");
-    appType3.add("NON-YARN");
-    when(client.getApplications(appType3)).thenReturn(
-        getApplicationReports(applicationReports, appType3));
 
-    result = cli.run(new String[] { "-list", "-appTypes", "YARN,NON-YARN" });
+    EnumSet<YarnApplicationState> appState3 =
+        EnumSet.noneOf(YarnApplicationState.class);
+    appState3.add(YarnApplicationState.FINISHED);
+    appState3.add(YarnApplicationState.FAILED);
+
+    when(client.getApplications(appType3, appState3)).thenReturn(
+        getApplicationReports(applicationReports, appType3, appState3, false));
+    result =
+        cli.run(new String[] { "-list", "--appStates", "FINISHED ,, , FAILED",
+            ",,FINISHED" });
     assertEquals(0, result);
-    verify(client).getApplications(appType3);
+    verify(client).getApplications(appType3, appState3);
     baos = new ByteArrayOutputStream();
     pw = new PrintWriter(baos);
-    pw.println("Total Applications:2");
+    pw.println("Total number of applications (application-types: " + appType3
+        + " and states: " + appState3 + ")" + ":" + 2);
     pw.print("                Application-Id\t    Application-Name");
     pw.print("\t    Application-Type");
     pw.print("\t      User\t     Queue\t             State\t       ");
     pw.print("Final-State\t       Progress");
     pw.println("\t                       Tracking-URL");
-    pw.print("         application_1234_0005\t             ");
-    pw.print("appname\t                YARN\t      user\t     ");
-    pw.print("queue\t          FINISHED\t         ");
-    pw.print("SUCCEEDED\t         53.79%");
-    pw.println("\t                                N/A");
     pw.print("         application_1234_0006\t            ");
     pw.print("appname2\t            NON-YARN\t     user2\t    ");
     pw.print("queue2\t          FINISHED\t         ");
     pw.print("SUCCEEDED\t         63.79%");
     pw.println("\t                                N/A");
+    pw.print("         application_1234_0008\t            ");
+    pw.print("appname4\t       NON-MAPREDUCE\t     user4\t    ");
+    pw.print("queue4\t            FAILED\t         ");
+    pw.print("SUCCEEDED\t         83.79%");
+    pw.println("\t                                N/A");
     pw.close();
     appsReportStr = baos.toString("UTF-8");
     Assert.assertEquals(appsReportStr, sysOutStream.toString());
     verify(sysOut, times(3)).write(any(byte[].class), anyInt(), anyInt());
 
+    // Test command yarn application -list --appTypes apptype1,apptype2
+    // --appStates appstate1,appstate2
     sysOutStream.reset();
     Set<String> appType4 = new HashSet<String>();
-    when(client.getApplications(appType4)).thenReturn(
-        getApplicationReports(applicationReports, appType4));
-    result = cli.run(new String[] { "-list" });
+    appType4.add("YARN");
+    appType4.add("NON-YARN");
+
+    EnumSet<YarnApplicationState> appState4 =
+        EnumSet.noneOf(YarnApplicationState.class);
+    appState4.add(YarnApplicationState.FINISHED);
+    appState4.add(YarnApplicationState.FAILED);
+
+    when(client.getApplications(appType4, appState4)).thenReturn(
+        getApplicationReports(applicationReports, appType4, appState4, false));
+    result =
+        cli.run(new String[] { "-list", "--appTypes", "YARN,NON-YARN",
+        "--appStates", "FINISHED ,, , FAILED" });
     assertEquals(0, result);
-    verify(client).getApplications(appType4);
+    verify(client).getApplications(appType2, appState2);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total number of applications (application-types: " + appType4
+        + " and states: " + appState4 + ")" + ":" + 1);
+    pw.print("                Application-Id\t    Application-Name");
+    pw.print("\t    Application-Type");
+    pw.print("\t      User\t     Queue\t             State\t       ");
+    pw.print("Final-State\t       Progress");
+    pw.println("\t                       Tracking-URL");
+    pw.print("         application_1234_0006\t            ");
+    pw.print("appname2\t            NON-YARN\t     user2\t    ");
+    pw.print("queue2\t          FINISHED\t         ");
+    pw.print("SUCCEEDED\t         63.79%");
+    pw.println("\t                                N/A");
+    pw.close();
+    appsReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(appsReportStr, sysOutStream.toString());
+    verify(sysOut, times(4)).write(any(byte[].class), anyInt(), anyInt());
 
+    //Test command yarn application -list --appStates with invalid appStates
+    sysOutStream.reset();
+    result =
+        cli.run(new String[] { "-list", "--appStates", "FINISHED ,, , INVALID" });
+    assertEquals(-1, result);
     baos = new ByteArrayOutputStream();
     pw = new PrintWriter(baos);
-    pw.println("Total Applications:3");
+    pw.println("The application state  INVALID is invalid.");
+    pw.print("The valid application state can be one of the following: ");
+    StringBuilder sb = new StringBuilder();
+    sb.append("ALL,");
+    for(YarnApplicationState state : YarnApplicationState.values()) {
+      sb.append(state+",");
+    }
+    String output = sb.toString();
+    pw.println(output.substring(0, output.length()-1));
+    pw.close();
+    appsReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(appsReportStr, sysOutStream.toString());
+    verify(sysOut, times(4)).write(any(byte[].class), anyInt(), anyInt());
+
+    //Test command yarn application -list --appStates all
+    sysOutStream.reset();
+    Set<String> appType5 = new HashSet<String>();
+
+    EnumSet<YarnApplicationState> appState5 =
+        EnumSet.noneOf(YarnApplicationState.class);
+    appState5.add(YarnApplicationState.FINISHED);
+    when(client.getApplications(appType5, appState5)).thenReturn(
+        getApplicationReports(applicationReports, appType5, appState5, true));
+    result =
+        cli.run(new String[] { "-list", "--appStates", "FINISHED ,, , ALL" });
+    assertEquals(0, result);
+    verify(client).getApplications(appType5, appState5);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total number of applications (application-types: " + appType5
+        + " and states: " + appState5 + ")" + ":" + 6);
     pw.print("                Application-Id\t    Application-Name");
     pw.print("\t    Application-Type");
     pw.print("\t      User\t     Queue\t             State\t       ");
@@ -262,7 +401,7 @@ public class TestYarnCLI {
     pw.println("\t                       Tracking-URL");
     pw.print("         application_1234_0005\t             ");
     pw.print("appname\t                YARN\t      user\t     ");
-    pw.print("queue\t          FINISHED\t         ");
+    pw.print("queue\t           RUNNING\t         ");
     pw.print("SUCCEEDED\t         53.79%");
     pw.println("\t                                N/A");
     pw.print("         application_1234_0006\t            ");
@@ -272,92 +411,411 @@ public class TestYarnCLI {
     pw.println("\t                                N/A");
     pw.print("         application_1234_0007\t            ");
     pw.print("appname3\t           MAPREDUCE\t     user3\t    ");
-    pw.print("queue3\t          FINISHED\t         ");
+    pw.print("queue3\t           RUNNING\t         ");
     pw.print("SUCCEEDED\t         73.79%");
     pw.println("\t                                N/A");
+    pw.print("         application_1234_0008\t            ");
+    pw.print("appname4\t       NON-MAPREDUCE\t     user4\t    ");
+    pw.print("queue4\t            FAILED\t         ");
+    pw.print("SUCCEEDED\t         83.79%");
+    pw.println("\t                                N/A");
+    pw.print("         application_1234_0009\t            ");
+    pw.print("appname5\t                HIVE\t     user5\t    ");
+    pw.print("queue5\t          ACCEPTED\t            ");
+    pw.print("KILLED\t         93.79%");
+    pw.println("\t                                N/A");
+    pw.print("         application_1234_0010\t            ");
+    pw.print("appname6\t                 PIG\t     user6\t    ");
+    pw.print("queue6\t         SUBMITTED\t            ");
+    pw.print("KILLED\t         99.79%");
+    pw.println("\t                                N/A");
     pw.close();
     appsReportStr = baos.toString("UTF-8");
     Assert.assertEquals(appsReportStr, sysOutStream.toString());
-    verify(sysOut, times(4)).write(any(byte[].class), anyInt(), anyInt());
+    verify(sysOut, times(5)).write(any(byte[].class), anyInt(), anyInt());
+
+    // Test command yarn application user case insensitive
+    sysOutStream.reset();
+    Set<String> appType6 = new HashSet<String>();
+    appType6.add("YARN");
+    appType6.add("NON-YARN");
+
+    EnumSet<YarnApplicationState> appState6 =
+        EnumSet.noneOf(YarnApplicationState.class);
+    appState6.add(YarnApplicationState.FINISHED);
+    when(client.getApplications(appType6, appState6)).thenReturn(
+        getApplicationReports(applicationReports, appType6, appState6, false));
+    result =
+        cli.run(new String[] { "-list", "-appTypes", "YARN, ,,  NON-YARN",
+            "--appStates", "finished" });
+    assertEquals(0, result);
+    verify(client).getApplications(appType6, appState6);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total number of applications (application-types: " + appType6
+        + " and states: " + appState6 + ")" + ":" + 1);
+    pw.print("                Application-Id\t    Application-Name");
+    pw.print("\t    Application-Type");
+    pw.print("\t      User\t     Queue\t             State\t       ");
+    pw.print("Final-State\t       Progress");
+    pw.println("\t                       Tracking-URL");
+    pw.print("         application_1234_0006\t            ");
+    pw.print("appname2\t            NON-YARN\t     user2\t    ");
+    pw.print("queue2\t          FINISHED\t         ");
+    pw.print("SUCCEEDED\t         63.79%");
+    pw.println("\t                                N/A");
+    pw.close();
+    appsReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(appsReportStr, sysOutStream.toString());
+    verify(sysOut, times(6)).write(any(byte[].class), anyInt(), anyInt());
   }
 
   private List<ApplicationReport> getApplicationReports(
       List<ApplicationReport> applicationReports,
-      Set<String> appTypes) {
+      Set<String> appTypes, EnumSet<YarnApplicationState> appStates,
+      boolean allStates) {
 
     List<ApplicationReport> appReports = new ArrayList<ApplicationReport>();
-    boolean bypassFilter = appTypes.isEmpty();
 
+    if (allStates) {
+      for(YarnApplicationState state : YarnApplicationState.values()) {
+        appStates.add(state);
+      }
+    }
     for (ApplicationReport appReport : applicationReports) {
-      if (!(bypassFilter || appTypes.contains(
-          appReport.getApplicationType()))) {
-        continue;
+      if (appTypes != null && !appTypes.isEmpty()) {
+        if (!appTypes.contains(appReport.getApplicationType())) {
+          continue;
+        }
+      }
+
+      if (appStates != null && !appStates.isEmpty()) {
+        if (!appStates.contains(appReport.getYarnApplicationState())) {
+          continue;
+        }
       }
+
       appReports.add(appReport);
     }
     return appReports;
   }
 
   @Test (timeout = 10000)
-  public void testHelpCommand() throws Exception {
+  public void testAppsHelpCommand() throws Exception {
     ApplicationCLI cli = createAndGetAppCLI();
     ApplicationCLI spyCli = spy(cli);
     int result = spyCli.run(new String[] { "-help" });
     Assert.assertTrue(result == 0);
     verify(spyCli).printUsage(any(Options.class));
+    Assert.assertEquals(createApplicationCLIHelpMessage(),
+        sysOutStream.toString());
 
+    sysOutStream.reset();
     ApplicationId applicationId = ApplicationId.newInstance(1234, 5);
     result =
         cli.run(new String[] { "-kill", applicationId.toString(), "args" });
     verify(spyCli).printUsage(any(Options.class));
+    Assert.assertEquals(createApplicationCLIHelpMessage(),
+        sysOutStream.toString());
 
+    sysOutStream.reset();
     NodeId nodeId = NodeId.newInstance("host0", 0);
     result = cli.run(new String[] { "-status", nodeId.toString(), "args" });
     verify(spyCli).printUsage(any(Options.class));
+    Assert.assertEquals(createApplicationCLIHelpMessage(),
+        sysOutStream.toString());
+  }
+
+  @Test (timeout = 5000)
+  public void testNodesHelpCommand() throws Exception {
+    NodeCLI nodeCLI = new NodeCLI();
+    nodeCLI.setClient(client);
+    nodeCLI.setSysOutPrintStream(sysOut);
+    nodeCLI.setSysErrPrintStream(sysErr);
+    nodeCLI.run(new String[] {});
+    Assert.assertEquals(createNodeCLIHelpMessage(),
+        sysOutStream.toString());
   }
 
   @Test
   public void testKillApplication() throws Exception {
     ApplicationCLI cli = createAndGetAppCLI();
     ApplicationId applicationId = ApplicationId.newInstance(1234, 5);
+
+    ApplicationReport newApplicationReport2 = ApplicationReport.newInstance(
+        applicationId, ApplicationAttemptId.newInstance(applicationId, 1),
+        "user", "queue", "appname", "host", 124, null,
+        YarnApplicationState.FINISHED, "diagnostics", "url", 0, 0,
+        FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.53789f, "YARN", null);
+    when(client.getApplicationReport(any(ApplicationId.class))).thenReturn(
+        newApplicationReport2);
     int result = cli.run(new String[] { "-kill", applicationId.toString() });
     assertEquals(0, result);
+    verify(client, times(0)).killApplication(any(ApplicationId.class));
+    verify(sysOut).println(
+        "Application " + applicationId + " has already finished ");
+
+    ApplicationReport newApplicationReport = ApplicationReport.newInstance(
+        applicationId, ApplicationAttemptId.newInstance(applicationId, 1),
+        "user", "queue", "appname", "host", 124, null,
+        YarnApplicationState.RUNNING, "diagnostics", "url", 0, 0,
+        FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.53789f, "YARN", null);
+    when(client.getApplicationReport(any(ApplicationId.class))).thenReturn(
+        newApplicationReport);
+    result = cli.run(new String[] { "-kill", applicationId.toString() });
+    assertEquals(0, result);
     verify(client).killApplication(any(ApplicationId.class));
     verify(sysOut).println("Killing application application_1234_0005");
+
+    doThrow(new ApplicationNotFoundException("Application with id '"
+        + applicationId + "' doesn't exist in RM.")).when(client)
+        .getApplicationReport(applicationId);
+    cli = createAndGetAppCLI();
+    try {
+      cli.run(new String[] { "-kill", applicationId.toString() });
+      Assert.fail();
+    } catch (Exception ex) {
+      Assert.assertTrue(ex instanceof ApplicationNotFoundException);
+      Assert.assertEquals("Application with id '" + applicationId +
+          "' doesn't exist in RM.", ex.getMessage());
+    }
   }
 
   @Test
   public void testListClusterNodes() throws Exception {
+    List<NodeReport> nodeReports = new ArrayList<NodeReport>();
+    nodeReports.addAll(getNodeReports(1, NodeState.NEW));
+    nodeReports.addAll(getNodeReports(2, NodeState.RUNNING));
+    nodeReports.addAll(getNodeReports(1, NodeState.UNHEALTHY));
+    nodeReports.addAll(getNodeReports(1, NodeState.DECOMMISSIONED));
+    nodeReports.addAll(getNodeReports(1, NodeState.REBOOTED));
+    nodeReports.addAll(getNodeReports(1, NodeState.LOST));
+
     NodeCLI cli = new NodeCLI();
-    when(client.getNodeReports(NodeState.RUNNING)).thenReturn(
-        getNodeReports(3));
     cli.setClient(client);
     cli.setSysOutPrintStream(sysOut);
-    int result = cli.run(new String[] { "-list" });
+
+    Set<NodeState> nodeStates = new HashSet<NodeState>();
+    nodeStates.add(NodeState.NEW);
+    NodeState[] states = nodeStates.toArray(new NodeState[0]);
+    when(client.getNodeReports(states))
+        .thenReturn(getNodeReports(nodeReports, nodeStates));
+    int result = cli.run(new String[] { "-list", "--states", "NEW" });
     assertEquals(0, result);
-    verify(client).getNodeReports(NodeState.RUNNING);
+    verify(client).getNodeReports(states);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     PrintWriter pw = new PrintWriter(baos);
-    pw.println("Total Nodes:3");
-    pw.print("         Node-Id\tNode-State\tNode-Http-Address\t");
-    pw.println("Running-Containers");
-    pw.print("         host0:0\t   RUNNING\t       host1:8888");
-    pw.println("\t                 0");
-    pw.print("         host1:0\t   RUNNING\t       host1:8888");
-    pw.println("\t                 0");
-    pw.print("         host2:0\t   RUNNING\t       host1:8888");
-    pw.println("\t                 0");
+    pw.println("Total Nodes:1");
+    pw.print("         Node-Id\t     Node-State\tNode-Http-Address\t");
+    pw.println("Number-of-Running-Containers");
+    pw.print("         host0:0\t            NEW\t       host1:8888\t");
+    pw.println("                           0");
     pw.close();
     String nodesReportStr = baos.toString("UTF-8");
     Assert.assertEquals(nodesReportStr, sysOutStream.toString());
     verify(sysOut, times(1)).write(any(byte[].class), anyInt(), anyInt());
+
+    sysOutStream.reset();
+    nodeStates.clear();
+    nodeStates.add(NodeState.RUNNING);
+    states = nodeStates.toArray(new NodeState[0]);
+    when(client.getNodeReports(states))
+        .thenReturn(getNodeReports(nodeReports, nodeStates));
+    result = cli.run(new String[] { "-list", "--states", "RUNNING" });
+    assertEquals(0, result);
+    verify(client).getNodeReports(states);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total Nodes:2");
+    pw.print("         Node-Id\t     Node-State\tNode-Http-Address\t");
+    pw.println("Number-of-Running-Containers");
+    pw.print("         host0:0\t        RUNNING\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host1:0\t        RUNNING\t       host1:8888\t");
+    pw.println("                           0");
+    pw.close();
+    nodesReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(nodesReportStr, sysOutStream.toString());
+    verify(sysOut, times(2)).write(any(byte[].class), anyInt(), anyInt());
+
+    sysOutStream.reset();
+    result = cli.run(new String[] { "-list" });
+    assertEquals(0, result);
+    Assert.assertEquals(nodesReportStr, sysOutStream.toString());
+    verify(sysOut, times(3)).write(any(byte[].class), anyInt(), anyInt());
+
+    sysOutStream.reset();
+    nodeStates.clear();
+    nodeStates.add(NodeState.UNHEALTHY);
+    states = nodeStates.toArray(new NodeState[0]);
+    when(client.getNodeReports(states))
+        .thenReturn(getNodeReports(nodeReports, nodeStates));
+    result = cli.run(new String[] { "-list", "--states", "UNHEALTHY" });
+    assertEquals(0, result);
+    verify(client).getNodeReports(states);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total Nodes:1");
+    pw.print("         Node-Id\t     Node-State\tNode-Http-Address\t");
+    pw.println("Number-of-Running-Containers");
+    pw.print("         host0:0\t      UNHEALTHY\t       host1:8888\t");
+    pw.println("                           0");
+    pw.close();
+    nodesReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(nodesReportStr, sysOutStream.toString());
+    verify(sysOut, times(4)).write(any(byte[].class), anyInt(), anyInt());
+
+    sysOutStream.reset();
+    nodeStates.clear();
+    nodeStates.add(NodeState.DECOMMISSIONED);
+    states = nodeStates.toArray(new NodeState[0]);
+    when(client.getNodeReports(states))
+        .thenReturn(getNodeReports(nodeReports, nodeStates));
+    result = cli.run(new String[] { "-list", "--states", "DECOMMISSIONED" });
+    assertEquals(0, result);
+    verify(client).getNodeReports(states);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total Nodes:1");
+    pw.print("         Node-Id\t     Node-State\tNode-Http-Address\t");
+    pw.println("Number-of-Running-Containers");
+    pw.print("         host0:0\t DECOMMISSIONED\t       host1:8888\t");
+    pw.println("                           0");
+    pw.close();
+    nodesReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(nodesReportStr, sysOutStream.toString());
+    verify(sysOut, times(5)).write(any(byte[].class), anyInt(), anyInt());
+
+    sysOutStream.reset();
+    nodeStates.clear();
+    nodeStates.add(NodeState.REBOOTED);
+    states = nodeStates.toArray(new NodeState[0]);
+    when(client.getNodeReports(states))
+        .thenReturn(getNodeReports(nodeReports, nodeStates));
+    result = cli.run(new String[] { "-list", "--states", "REBOOTED" });
+    assertEquals(0, result);
+    verify(client).getNodeReports(states);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total Nodes:1");
+    pw.print("         Node-Id\t     Node-State\tNode-Http-Address\t");
+    pw.println("Number-of-Running-Containers");
+    pw.print("         host0:0\t       REBOOTED\t       host1:8888\t");
+    pw.println("                           0");
+    pw.close();
+    nodesReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(nodesReportStr, sysOutStream.toString());
+    verify(sysOut, times(6)).write(any(byte[].class), anyInt(), anyInt());
+
+    sysOutStream.reset();
+    nodeStates.clear();
+    nodeStates.add(NodeState.LOST);
+    states = nodeStates.toArray(new NodeState[0]);
+    when(client.getNodeReports(states))
+        .thenReturn(getNodeReports(nodeReports, nodeStates));
+    result = cli.run(new String[] { "-list", "--states", "LOST" });
+    assertEquals(0, result);
+    verify(client).getNodeReports(states);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total Nodes:1");
+    pw.print("         Node-Id\t     Node-State\tNode-Http-Address\t");
+    pw.println("Number-of-Running-Containers");
+    pw.print("         host0:0\t           LOST\t       host1:8888\t");
+    pw.println("                           0");
+    pw.close();
+    nodesReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(nodesReportStr, sysOutStream.toString());
+    verify(sysOut, times(7)).write(any(byte[].class), anyInt(), anyInt());
+
+    sysOutStream.reset();
+    nodeStates.clear();
+    nodeStates.add(NodeState.NEW);
+    nodeStates.add(NodeState.RUNNING);
+    nodeStates.add(NodeState.LOST);
+    nodeStates.add(NodeState.REBOOTED);
+    states = nodeStates.toArray(new NodeState[0]);
+    when(client.getNodeReports(states))
+        .thenReturn(getNodeReports(nodeReports, nodeStates));
+    result = cli.run(new String[] { "-list", "--states", 
+                                        "NEW,RUNNING,LOST,REBOOTED" });
+    assertEquals(0, result);
+    verify(client).getNodeReports(states);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total Nodes:5");
+    pw.print("         Node-Id\t     Node-State\tNode-Http-Address\t");
+    pw.println("Number-of-Running-Containers");
+    pw.print("         host0:0\t            NEW\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host0:0\t        RUNNING\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host1:0\t        RUNNING\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host0:0\t       REBOOTED\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host0:0\t           LOST\t       host1:8888\t");
+    pw.println("                           0");
+    pw.close();
+    nodesReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(nodesReportStr, sysOutStream.toString());
+    verify(sysOut, times(8)).write(any(byte[].class), anyInt(), anyInt());
+
+    sysOutStream.reset();
+    nodeStates.clear();
+    for (NodeState s : NodeState.values()) {
+      nodeStates.add(s);
+    }
+    states = nodeStates.toArray(new NodeState[0]);
+    when(client.getNodeReports(states))
+        .thenReturn(getNodeReports(nodeReports, nodeStates));
+    result = cli.run(new String[] { "-list", "--all" });
+    assertEquals(0, result);
+    verify(client).getNodeReports(states);
+    baos = new ByteArrayOutputStream();
+    pw = new PrintWriter(baos);
+    pw.println("Total Nodes:7");
+    pw.print("         Node-Id\t     Node-State\tNode-Http-Address\t");
+    pw.println("Number-of-Running-Containers");
+    pw.print("         host0:0\t            NEW\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host0:0\t        RUNNING\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host1:0\t        RUNNING\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host0:0\t      UNHEALTHY\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host0:0\t DECOMMISSIONED\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host0:0\t       REBOOTED\t       host1:8888\t");
+    pw.println("                           0");
+    pw.print("         host0:0\t           LOST\t       host1:8888\t");
+    pw.println("                           0");
+    pw.close();
+    nodesReportStr = baos.toString("UTF-8");
+    Assert.assertEquals(nodesReportStr, sysOutStream.toString());
+    verify(sysOut, times(9)).write(any(byte[].class), anyInt(), anyInt());
+  }
+
+  private List<NodeReport> getNodeReports(
+      List<NodeReport> nodeReports,
+      Set<NodeState> nodeStates) {
+    List<NodeReport> reports = new ArrayList<NodeReport>();
+
+    for (NodeReport nodeReport : nodeReports) {
+      if (nodeStates.contains(nodeReport.getNodeState())) {
+        reports.add(nodeReport);
+      }
+    }
+    return reports;
   }
 
   @Test
   public void testNodeStatus() throws Exception {
     NodeId nodeId = NodeId.newInstance("host0", 0);
     NodeCLI cli = new NodeCLI();
-    when(client.getNodeReports()).thenReturn(getNodeReports(3));
+    when(client.getNodeReports()).thenReturn(
+                    getNodeReports(3, NodeState.RUNNING));
     cli.setClient(client);
     cli.setSysOutPrintStream(sysOut);
     cli.setSysErrPrintStream(sysErr);
@@ -389,7 +847,8 @@ public class TestYarnCLI {
   public void testAbsentNodeStatus() throws Exception {
     NodeId nodeId = NodeId.newInstance("Absenthost0", 0);
     NodeCLI cli = new NodeCLI();
-    when(client.getNodeReports()).thenReturn(getNodeReports(0));
+    when(client.getNodeReports()).thenReturn(
+                getNodeReports(0, NodeState.RUNNING));
     cli.setClient(client);
     cli.setSysOutPrintStream(sysOut);
     cli.setSysErrPrintStream(sysErr);
@@ -411,18 +870,37 @@ public class TestYarnCLI {
     verifyUsageInfo(new NodeCLI());
   }
 
+  @Test
+  public void testMissingArguments() throws Exception {
+    ApplicationCLI cli = createAndGetAppCLI();
+    int result = cli.run(new String[] { "-status" });
+    Assert.assertEquals(result, -1);
+    Assert.assertEquals("Missing argument for options\n"
+        + createApplicationCLIHelpMessage(), sysOutStream.toString());
+
+    sysOutStream.reset();
+    NodeCLI nodeCLI = new NodeCLI();
+    nodeCLI.setClient(client);
+    nodeCLI.setSysOutPrintStream(sysOut);
+    nodeCLI.setSysErrPrintStream(sysErr);
+    result = nodeCLI.run(new String[] { "-status" });
+    Assert.assertEquals(result, -1);
+    Assert.assertEquals("Missing argument for options\n"
+        + createNodeCLIHelpMessage(), sysOutStream.toString());
+  }
+
   private void verifyUsageInfo(YarnCLI cli) throws Exception {
     cli.setSysErrPrintStream(sysErr);
     cli.run(new String[0]);
     verify(sysErr).println("Invalid Command Usage : ");
   }
 
-  private List<NodeReport> getNodeReports(int noOfNodes) {
+  private List<NodeReport> getNodeReports(int noOfNodes, NodeState state) {
     List<NodeReport> nodeReports = new ArrayList<NodeReport>();
 
     for (int i = 0; i < noOfNodes; i++) {
       NodeReport nodeReport = NodeReport.newInstance(NodeId
-        .newInstance("host" + i, 0), NodeState.RUNNING, "host" + 1 + ":8888",
+        .newInstance("host" + i, 0), state, "host" + 1 + ":8888",
           "rack1", Records.newRecord(Resource.class), Records
               .newRecord(Resource.class), 0, "", 0);
       nodeReports.add(nodeReport);
@@ -437,4 +915,45 @@ public class TestYarnCLI {
     return cli;
   }
 
+  private String createApplicationCLIHelpMessage() throws IOException {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    PrintWriter pw = new PrintWriter(baos);
+    pw.println("usage: application");
+    pw.println(" -appStates <States>        Works with -list to filter applications based");
+    pw.println("                            on input comma-separated list of application");
+    pw.println("                            states. The valid application state can be one");
+    pw.println("                            of the following:");
+    pw.println("                            ALL,NEW,NEW_SAVING,SUBMITTED,ACCEPTED,RUNNING,");
+    pw.println("                            FINISHED,FAILED,KILLED");
+    pw.println(" -appTypes <Types>          Works with -list to filter applications based");
+    pw.println("                            on input comma-separated list of application");
+    pw.println("                            types.");
+    pw.println(" -help                      Displays help for all commands.");
+    pw.println(" -kill <Application ID>     Kills the application.");
+    pw.println(" -list                      List applications from the RM. Supports");
+    pw.println("                            optional use of -appTypes to filter");
+    pw.println("                            applications based on application type, and");
+    pw.println("                            -appStates to filter applications based on");
+    pw.println("                            application state");
+    pw.println(" -status <Application ID>   Prints the status of the application.");
+    pw.close();
+    String appsHelpStr = baos.toString("UTF-8");
+    return appsHelpStr;
+  }
+
+  private String createNodeCLIHelpMessage() throws IOException {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    PrintWriter pw = new PrintWriter(baos);
+    pw.println("usage: node");
+    pw.println(" -all               Works with -list to list all nodes.");
+    pw.println(" -list              List all running nodes. Supports optional use of");
+    pw.println("                    -states to filter nodes based on node state, all -all");
+    pw.println("                    to list all nodes.");
+    pw.println(" -states <States>   Works with -list to filter nodes based on input");
+    pw.println("                    comma-separated list of node states.");
+    pw.println(" -status <NodeId>   Prints the status report of the node.");
+    pw.close();
+    String nodesHelpStr = baos.toString("UTF-8");
+    return nodesHelpStr;
+  }
 }

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateRequestPBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateRequestPBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateRequestPBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateRequestPBImpl.java Thu Sep 19 23:42:10 2013
@@ -38,6 +38,8 @@ import org.apache.hadoop.yarn.proto.Yarn
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.AllocateRequestProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.AllocateRequestProtoOrBuilder;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class AllocateRequestPBImpl extends AllocateRequest {
@@ -83,7 +85,7 @@ public class AllocateRequestPBImpl exten
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private void mergeLocalToBuilder() {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateResponsePBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateResponsePBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateResponsePBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateResponsePBImpl.java Thu Sep 19 23:42:10 2013
@@ -49,6 +49,8 @@ import org.apache.hadoop.yarn.proto.Yarn
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.AllocateResponseProtoOrBuilder;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.NMTokenProto;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class AllocateResponsePBImpl extends AllocateResponse {
@@ -99,7 +101,7 @@ public class AllocateResponsePBImpl exte
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private synchronized void mergeLocalToBuilder() {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/CancelDelegationTokenRequestPBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/CancelDelegationTokenRequestPBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/CancelDelegationTokenRequestPBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/CancelDelegationTokenRequestPBImpl.java Thu Sep 19 23:42:10 2013
@@ -26,6 +26,8 @@ import org.apache.hadoop.yarn.api.protoc
 import org.apache.hadoop.yarn.api.records.Token;
 import org.apache.hadoop.yarn.api.records.impl.pb.TokenPBImpl;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class CancelDelegationTokenRequestPBImpl extends
@@ -90,7 +92,7 @@ public class CancelDelegationTokenReques
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private void mergeLocalToBuilder() {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/CancelDelegationTokenResponsePBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/CancelDelegationTokenResponsePBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/CancelDelegationTokenResponsePBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/CancelDelegationTokenResponsePBImpl.java Thu Sep 19 23:42:10 2013
@@ -22,6 +22,8 @@ import org.apache.hadoop.classification.
 import org.apache.hadoop.security.proto.SecurityProtos.CancelDelegationTokenResponseProto;
 import org.apache.hadoop.yarn.api.protocolrecords.CancelDelegationTokenResponse;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class CancelDelegationTokenResponsePBImpl extends CancelDelegationTokenResponse {
@@ -58,6 +60,6 @@ public class CancelDelegationTokenRespon
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 }

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/FinishApplicationMasterRequestPBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/FinishApplicationMasterRequestPBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/FinishApplicationMasterRequestPBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/FinishApplicationMasterRequestPBImpl.java Thu Sep 19 23:42:10 2013
@@ -28,6 +28,8 @@ import org.apache.hadoop.yarn.proto.Yarn
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.FinishApplicationMasterRequestProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.FinishApplicationMasterRequestProtoOrBuilder;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class FinishApplicationMasterRequestPBImpl extends FinishApplicationMasterRequest {
@@ -68,7 +70,7 @@ public class FinishApplicationMasterRequ
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private void mergeLocalToBuilder() {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/FinishApplicationMasterResponsePBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/FinishApplicationMasterResponsePBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/FinishApplicationMasterResponsePBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/FinishApplicationMasterResponsePBImpl.java Thu Sep 19 23:42:10 2013
@@ -24,6 +24,8 @@ import org.apache.hadoop.classification.
 import org.apache.hadoop.yarn.api.protocolrecords.FinishApplicationMasterResponse;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.FinishApplicationMasterResponseProto;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class FinishApplicationMasterResponsePBImpl extends FinishApplicationMasterResponse {
@@ -63,6 +65,6 @@ public class FinishApplicationMasterResp
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 }  

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationReportRequestPBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationReportRequestPBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationReportRequestPBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationReportRequestPBImpl.java Thu Sep 19 23:42:10 2013
@@ -28,6 +28,8 @@ import org.apache.hadoop.yarn.proto.Yarn
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationReportRequestProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationReportRequestProtoOrBuilder;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class GetApplicationReportRequestPBImpl extends GetApplicationReportRequest {
@@ -71,7 +73,7 @@ public class GetApplicationReportRequest
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private void mergeLocalToBuilder() {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationReportResponsePBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationReportResponsePBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationReportResponsePBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationReportResponsePBImpl.java Thu Sep 19 23:42:10 2013
@@ -28,6 +28,8 @@ import org.apache.hadoop.yarn.proto.Yarn
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationReportResponseProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationReportResponseProtoOrBuilder;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class GetApplicationReportResponsePBImpl extends GetApplicationReportResponse {
@@ -71,7 +73,7 @@ public class GetApplicationReportRespons
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private void mergeLocalToBuilder() {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationsRequestPBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationsRequestPBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationsRequestPBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationsRequestPBImpl.java Thu Sep 19 23:42:10 2013
@@ -18,16 +18,23 @@
 
 package org.apache.hadoop.yarn.api.protocolrecords.impl.pb;
 
+import java.util.EnumSet;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
 import org.apache.hadoop.classification.InterfaceAudience.Private;
 import org.apache.hadoop.classification.InterfaceStability.Unstable;
 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest;
+import org.apache.hadoop.yarn.api.records.YarnApplicationState;
+import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils;
+import org.apache.hadoop.yarn.proto.YarnProtos.YarnApplicationStateProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationsRequestProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationsRequestProtoOrBuilder;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class GetApplicationsRequestPBImpl extends GetApplicationsRequest {
@@ -36,6 +43,7 @@ public class GetApplicationsRequestPBImp
   boolean viaProto = false;
 
   Set<String> applicationTypes = null;
+  EnumSet<YarnApplicationState> applicationStates = null;
 
   public GetApplicationsRequestPBImpl() {
     builder = GetApplicationsRequestProto.newBuilder();
@@ -65,6 +73,40 @@ public class GetApplicationsRequestPBImp
     if (this.applicationTypes != null) {
       addLocalApplicationTypesToProto();
     }
+    if (this.applicationStates != null) {
+      maybeInitBuilder();
+      builder.clearApplicationStates();
+      Iterable<YarnApplicationStateProto> iterable =
+          new Iterable<YarnApplicationStateProto>() {
+
+            @Override
+            public Iterator<YarnApplicationStateProto> iterator() {
+              return new Iterator<YarnApplicationStateProto>() {
+
+                Iterator<YarnApplicationState> iter = applicationStates
+                    .iterator();
+
+                @Override
+                public boolean hasNext() {
+                  return iter.hasNext();
+                }
+
+                @Override
+                public YarnApplicationStateProto next() {
+                  return ProtoUtils.convertToProtoFormat(iter.next());
+                }
+
+                @Override
+                public void remove() {
+                  throw new UnsupportedOperationException();
+
+                }
+              };
+
+            }
+          };
+      builder.addAllApplicationStates(iterable);
+    }
   }
 
   private void addLocalApplicationTypesToProto() {
@@ -92,6 +134,20 @@ public class GetApplicationsRequestPBImp
     this.applicationTypes.addAll(appTypeList);
   }
 
+  private void initApplicationStates() {
+    if (this.applicationStates != null) {
+      return;
+    }
+    GetApplicationsRequestProtoOrBuilder p = viaProto ? proto : builder;
+    List<YarnApplicationStateProto> appStatesList =
+        p.getApplicationStatesList();
+    this.applicationStates = EnumSet.noneOf(YarnApplicationState.class);
+
+    for (YarnApplicationStateProto c : appStatesList) {
+      this.applicationStates.add(ProtoUtils.convertFromProtoFormat(c));
+    }
+  }
+
   @Override
   public Set<String> getApplicationTypes() {
     initApplicationTypes();
@@ -107,6 +163,21 @@ public class GetApplicationsRequestPBImp
   }
 
   @Override
+  public EnumSet<YarnApplicationState> getApplicationStates() {
+    initApplicationStates();
+    return this.applicationStates;
+  }
+
+  @Override
+  public void setApplicationStates(EnumSet<YarnApplicationState> applicationStates) {
+    maybeInitBuilder();
+    if (applicationStates == null) {
+      builder.clearApplicationStates();
+    }
+    this.applicationStates = applicationStates;
+  }
+
+  @Override
   public int hashCode() {
     return getProto().hashCode();
   }
@@ -123,6 +194,6 @@ public class GetApplicationsRequestPBImp
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 }

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationsResponsePBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationsResponsePBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationsResponsePBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetApplicationsResponsePBImpl.java Thu Sep 19 23:42:10 2013
@@ -31,6 +31,8 @@ import org.apache.hadoop.yarn.proto.Yarn
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationsResponseProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetApplicationsResponseProtoOrBuilder;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class GetApplicationsResponsePBImpl
@@ -90,7 +92,7 @@ extends GetApplicationsResponse {
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private void mergeLocalToBuilder() {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterMetricsRequestPBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterMetricsRequestPBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterMetricsRequestPBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterMetricsRequestPBImpl.java Thu Sep 19 23:42:10 2013
@@ -24,6 +24,8 @@ import org.apache.hadoop.classification.
 import org.apache.hadoop.yarn.api.protocolrecords.GetClusterMetricsRequest;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetClusterMetricsRequestProto;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class GetClusterMetricsRequestPBImpl extends GetClusterMetricsRequest {
@@ -63,6 +65,6 @@ public class GetClusterMetricsRequestPBI
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 }  

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterMetricsResponsePBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterMetricsResponsePBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterMetricsResponsePBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterMetricsResponsePBImpl.java Thu Sep 19 23:42:10 2013
@@ -28,6 +28,8 @@ import org.apache.hadoop.yarn.proto.Yarn
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetClusterMetricsResponseProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetClusterMetricsResponseProtoOrBuilder;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class GetClusterMetricsResponsePBImpl extends GetClusterMetricsResponse {
@@ -71,7 +73,7 @@ public class GetClusterMetricsResponsePB
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private void mergeLocalToBuilder() {

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterNodesRequestPBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterNodesRequestPBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterNodesRequestPBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterNodesRequestPBImpl.java Thu Sep 19 23:42:10 2013
@@ -18,17 +18,20 @@
 
 package org.apache.hadoop.yarn.api.protocolrecords.impl.pb;
 
-import org.apache.hadoop.classification.InterfaceAudience.Private;
-import org.apache.hadoop.classification.InterfaceStability.Unstable;
 import java.util.EnumSet;
 import java.util.Iterator;
 import java.util.List;
+
+import org.apache.hadoop.classification.InterfaceAudience.Private;
+import org.apache.hadoop.classification.InterfaceStability.Unstable;
 import org.apache.hadoop.yarn.api.protocolrecords.GetClusterNodesRequest;
 import org.apache.hadoop.yarn.api.records.NodeState;
+import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils;
 import org.apache.hadoop.yarn.proto.YarnProtos.NodeStateProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetClusterNodesRequestProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetClusterNodesRequestProtoOrBuilder;
-import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils;
+
+import com.google.protobuf.TextFormat;
 
 @Private
 @Unstable
@@ -152,6 +155,6 @@ public class GetClusterNodesRequestPBImp
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 }

Modified: hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterNodesResponsePBImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterNodesResponsePBImpl.java?rev=1524865&r1=1524864&r2=1524865&view=diff
==============================================================================
--- hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterNodesResponsePBImpl.java (original)
+++ hadoop/common/branches/HDFS-4949/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetClusterNodesResponsePBImpl.java Thu Sep 19 23:42:10 2013
@@ -31,6 +31,8 @@ import org.apache.hadoop.yarn.proto.Yarn
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetClusterNodesResponseProto;
 import org.apache.hadoop.yarn.proto.YarnServiceProtos.GetClusterNodesResponseProtoOrBuilder;
 
+import com.google.protobuf.TextFormat;
+
 @Private
 @Unstable
 public class GetClusterNodesResponsePBImpl extends GetClusterNodesResponse {
@@ -89,7 +91,7 @@ public class GetClusterNodesResponsePBIm
 
   @Override
   public String toString() {
-    return getProto().toString().replaceAll("\\n", ", ").replaceAll("\\s+", " ");
+    return TextFormat.shortDebugString(getProto());
   }
 
   private void mergeLocalToBuilder() {



Mime
View raw message