YARN-7724. yarn application status should support application name. Contributed 
by Jian He


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/53f27689
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/53f27689
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/53f27689

Branch: refs/heads/YARN-7402
Commit: 53f2768926700d2a27ce6223f1ccbfd3be49fc29
Parents: e404650
Author: Billie Rinaldi <bil...@apache.org>
Authored: Fri Jan 12 15:33:46 2018 -0800
Committer: Billie Rinaldi <bil...@apache.org>
Committed: Fri Jan 12 15:33:46 2018 -0800

----------------------------------------------------------------------
 .../yarn/service/client/ServiceClient.java      | 19 ++++++--
 .../hadoop/yarn/client/api/AppAdminClient.java  |  6 +--
 .../hadoop/yarn/client/cli/ApplicationCLI.java  | 51 ++++++++++++--------
 .../hadoop/yarn/client/cli/TestYarnCLI.java     | 13 ++++-
 .../src/site/markdown/YarnCommands.md           |  2 +-
 5 files changed, 61 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/53f27689/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/main/java/org/apache/hadoop/yarn/service/client/ServiceClient.java
----------------------------------------------------------------------
diff --git 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/main/java/org/apache/hadoop/yarn/service/client/ServiceClient.java
 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/main/java/org/apache/hadoop/yarn/service/client/ServiceClient.java
index d1ccc4f..bf46d15 100644
--- 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/main/java/org/apache/hadoop/yarn/service/client/ServiceClient.java
+++ 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/main/java/org/apache/hadoop/yarn/service/client/ServiceClient.java
@@ -898,7 +898,21 @@ public class ServiceClient extends AppAdminClient 
implements SliderExitCodes,
     }
   }
 
-  public String getStatusString(String appId)
+  @Override
+  public String getStatusString(String appIdOrName)
+      throws IOException, YarnException {
+    try {
+      // try parsing appIdOrName, if it succeeds, it means it's appId
+      ApplicationId.fromString(appIdOrName);
+      return getStatusByAppId(appIdOrName);
+    } catch (IllegalArgumentException e) {
+      // not appId format, it could be appName.
+      Service status = getStatus(appIdOrName);
+      return status.toString();
+    }
+  }
+
+  private String getStatusByAppId(String appId)
       throws IOException, YarnException {
     ApplicationReport appReport =
         yarnClient.getApplicationReport(ApplicationId.fromString(appId));
@@ -909,8 +923,7 @@ public class ServiceClient extends AppAdminClient 
implements SliderExitCodes,
     if (StringUtils.isEmpty(appReport.getHost())) {
       return "";
     }
-    ClientAMProtocol amProxy =
-        createAMProxy(appReport.getName(), appReport);
+    ClientAMProtocol amProxy = createAMProxy(appReport.getName(), appReport);
     GetStatusResponseProto response =
         amProxy.getStatus(GetStatusRequestProto.newBuilder().build());
     return response.getStatus();

http://git-wip-us.apache.org/repos/asf/hadoop/blob/53f27689/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/AppAdminClient.java
----------------------------------------------------------------------
diff --git 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/AppAdminClient.java
 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/AppAdminClient.java
index 6310178..6aba91a 100644
--- 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/AppAdminClient.java
+++ 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/AppAdminClient.java
@@ -207,16 +207,16 @@ public abstract class AppAdminClient extends 
CompositeService {
 
   /**
    * <p>
-   * Get detailed status string for a YARN application.
+   * Get detailed app specific status string for a YARN application.
    * </p>
    *
-   * @param applicationId application id
+   * @param appIdOrName appId or appName
    * @return status string
    * @throws IOException IOException
    * @throws YarnException exception in client or server
    */
   @Public
   @Unstable
-  public abstract String getStatusString(String applicationId) throws
+  public abstract String getStatusString(String appIdOrName) throws
       IOException, YarnException;
 }

http://git-wip-us.apache.org/repos/asf/hadoop/blob/53f27689/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java
----------------------------------------------------------------------
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 fb08fcd..c751f79 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
@@ -138,7 +138,12 @@ public class ApplicationCLI extends YarnCLI {
         .equalsIgnoreCase(APP))) {
       title = APPLICATION;
       opts.addOption(STATUS_CMD, true,
-          "Prints the status of the application.");
+          "Prints the status of the application. If app ID is"
+              + " provided, it prints the generic YARN application status."
+              + " If name is provided, it prints the application specific"
+              + " status based on app's own implementation, and -appTypes"
+              + " option must be specified unless it is the default"
+              + " yarn-service type.");
       opts.addOption(LIST_CMD, false, "List applications. "
           + "Supports optional use of -appTypes to filter applications "
           + "based on application type, -appStates to filter applications "
@@ -190,7 +195,7 @@ public class ApplicationCLI extends YarnCLI {
       opts.addOption(killOpt);
       opts.getOption(MOVE_TO_QUEUE_CMD).setArgName("Application ID");
       opts.getOption(QUEUE_CMD).setArgName("Queue Name");
-      opts.getOption(STATUS_CMD).setArgName("Application ID");
+      opts.getOption(STATUS_CMD).setArgName("Application Name or ID");
       opts.getOption(APP_ID).setArgName("Application ID");
       opts.getOption(UPDATE_PRIORITY).setArgName("Priority");
       opts.getOption(UPDATE_LIFETIME).setArgName("Timeout");
@@ -289,27 +294,31 @@ public class ApplicationCLI extends YarnCLI {
     }
 
     if (cliParser.hasOption(STATUS_CMD)) {
-      if (hasAnyOtherCLIOptions(cliParser, opts, STATUS_CMD)) {
+      if (hasAnyOtherCLIOptions(cliParser, opts, STATUS_CMD, APP_TYPE_CMD)) {
         printUsage(title, opts);
         return exitCode;
       }
       if (title.equalsIgnoreCase(APPLICATION) ||
           title.equalsIgnoreCase(APP)) {
-        ApplicationReport report = printApplicationReport(cliParser
-            .getOptionValue(STATUS_CMD));
-        if (report == null) {
-          exitCode = -1;
-        } else {
-          exitCode = 0;
-          String appType = report.getApplicationType();
+        String appIdOrName = cliParser.getOptionValue(STATUS_CMD);
+        try {
+          // try parsing appIdOrName, if it succeeds, it means it's appId
+          ApplicationId.fromString(appIdOrName);
+          exitCode = printApplicationReport(appIdOrName);
+        } catch (IllegalArgumentException e) {
+          // not appId format, it could be appName.
+          // Print app specific report, if app-type is not provided,
+          // assume it is yarn-service type.
+          AppAdminClient client = AppAdminClient
+              .createAppAdminClient(getSingleAppTypeFromCLI(cliParser),
+                  getConf());
           try {
-            AppAdminClient client = 
AppAdminClient.createAppAdminClient(appType,
-                getConf());
-            sysout.println("Detailed Application Status :");
-            sysout.println(client.getStatusString(cliParser.getOptionValue(
-                STATUS_CMD)));
-          } catch (IllegalArgumentException e) {
-            // app type does not have app admin client implementation
+            sysout.println(client.getStatusString(appIdOrName));
+            exitCode = 0;
+          } catch (ApplicationNotFoundException exception) {
+            System.err.println("Application with name '" + appIdOrName
+                + "' doesn't exist in RM or Timeline Server.");
+            return -1;
           }
         }
       } else if (title.equalsIgnoreCase(APPLICATION_ATTEMPT)) {
@@ -891,7 +900,7 @@ public class ApplicationCLI extends YarnCLI {
    * @return ApplicationReport
    * @throws YarnException
    */
-  private ApplicationReport printApplicationReport(String applicationId)
+  private int printApplicationReport(String applicationId)
       throws YarnException, IOException {
     ApplicationReport appReport = null;
     try {
@@ -900,7 +909,7 @@ public class ApplicationCLI extends YarnCLI {
     } catch (ApplicationNotFoundException e) {
       sysout.println("Application with id '" + applicationId
           + "' doesn't exist in RM or Timeline Server.");
-      return null;
+      return -1;
     }
     // Use PrintWriter.println, which uses correct platform line ending.
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -964,11 +973,11 @@ public class ApplicationCLI extends YarnCLI {
           + "' doesn't exist in RM.");
       appReportStr.close();
       sysout.println(baos.toString("UTF-8"));
-      return null;
+      return -1;
     }
     appReportStr.close();
     sysout.println(baos.toString("UTF-8"));
-    return appReport;
+    return 0;
   }
 
   private void printResourceUsage(PrintWriter appReportStr,

http://git-wip-us.apache.org/repos/asf/hadoop/blob/53f27689/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java
----------------------------------------------------------------------
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 54bd71e..25b426d 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
@@ -2122,8 +2122,17 @@ public class TestYarnCLI {
     pw.println("                                          application. 
Supports -appTypes");
     pw.println("                                          option to specify 
which client");
     pw.println("                                          implementation to 
use.");
-    pw.println(" -status <Application ID>                 Prints the status of 
the");
-    pw.println("                                          application.");
+    pw.println(" -status <Application Name or ID>         Prints the status of 
the");
+    pw.println("                                          application. If app 
ID is");
+    pw.println("                                          provided, it prints 
the generic");
+    pw.println("                                          YARN application 
status. If name");
+    pw.println("                                          is provided, it 
prints the");
+    pw.println("                                          application specific 
status");
+    pw.println("                                          based on app's own");
+    pw.println("                                          implementation, and 
-appTypes");
+    pw.println("                                          option must be 
specified unless");
+    pw.println("                                          it is the default 
yarn-service");
+    pw.println("                                          type.");
     pw.println(" -stop <Application Name or ID>           Stops application 
gracefully");
     pw.println("                                          (may be started 
again later). If");
     pw.println("                                          name is provided, 
appType must");

http://git-wip-us.apache.org/repos/asf/hadoop/blob/53f27689/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/YarnCommands.md
----------------------------------------------------------------------
diff --git 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/YarnCommands.md
 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/YarnCommands.md
index 018aa0b..67163c4 100644
--- 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/YarnCommands.md
+++ 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/YarnCommands.md
@@ -61,7 +61,7 @@ Usage: `yarn app [options] `
 | -queue \<Queue Name\> | Works with the movetoqueue command to specify which 
queue to move an application to. |
 | -save \<Application Name\> \<File Name\> | Saves specification file for an 
application. Options -updateLifetime and -changeQueue can be specified to alter 
the values provided in the file. Supports -appTypes option to specify which 
client implementation to use. |
 | -start \<Application Name\> | Starts a previously saved application. 
Supports -appTypes option to specify which client implementation to use. |
-| -status \<ApplicationId\> | Prints the status of the application. |
+| -status \<ApplicationId or ApplicationName\> | Prints the status of the 
application. If app ID is provided, it prints the generic YARN application 
status. If name is provided, it prints the application specific status based on 
app's own implementation, and -appTypes option must be specified unless it is 
the default `yarn-service` type.|
 | -stop \<Application Name or ID\> | Stops application gracefully (may be 
started again later). If name is provided, appType must be provided unless it 
is the default yarn-service. If ID is provided, the appType will be looked up. 
Supports -appTypes option to specify which client implementation to use. |
 | -updateLifetime \<Timeout\> | Update timeout of an application from NOW. 
ApplicationId can be passed using 'appId' option. Timeout value is in seconds. |
 | -updatePriority \<Priority\> | Update priority of an application. 
ApplicationId can be passed using 'appId' option. |


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-commits-h...@hadoop.apache.org

Reply via email to