SLIDER-302: add -live and --history flags to the list action

Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/3e46cb9c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/3e46cb9c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/3e46cb9c

Branch: refs/heads/develop
Commit: 3e46cb9cf4ce05ef57a98d462b2af0a6ec73c7e1
Parents: 07c4da8
Author: Steve Loughran <ste...@apache.org>
Authored: Mon Aug 11 11:06:52 2014 +0100
Committer: Steve Loughran <ste...@apache.org>
Committed: Fri Oct 24 21:51:10 2014 +0100

----------------------------------------------------------------------
 .../org/apache/slider/client/SliderClient.java  | 54 ++++++++++++++++----
 .../slider/common/params/ActionListArgs.java    |  9 ++++
 .../apache/slider/common/params/Arguments.java  |  1 +
 .../apache/slider/common/tools/SliderUtils.java | 49 ++++++++++++++++++
 4 files changed, 102 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/3e46cb9c/slider-core/src/main/java/org/apache/slider/client/SliderClient.java
----------------------------------------------------------------------
diff --git 
a/slider-core/src/main/java/org/apache/slider/client/SliderClient.java 
b/slider-core/src/main/java/org/apache/slider/client/SliderClient.java
index 92a017d..2805ee3 100644
--- a/slider-core/src/main/java/org/apache/slider/client/SliderClient.java
+++ b/slider-core/src/main/java/org/apache/slider/client/SliderClient.java
@@ -77,6 +77,7 @@ import org.apache.slider.common.params.ActionEchoArgs;
 import org.apache.slider.common.params.ActionFlexArgs;
 import org.apache.slider.common.params.ActionFreezeArgs;
 import org.apache.slider.common.params.ActionKillContainerArgs;
+import org.apache.slider.common.params.ActionListArgs;
 import org.apache.slider.common.params.ActionRegistryArgs;
 import org.apache.slider.common.params.ActionResolveArgs;
 import org.apache.slider.common.params.ActionStatusArgs;
@@ -304,7 +305,7 @@ public class SliderClient extends 
AbstractSliderLaunchedService implements RunSe
             serviceArgs.getActionAMSuicideArgs());
         break;
       case ACTION_LIST:
-        exitCode = actionList(clusterName);
+        exitCode = actionList(clusterName, serviceArgs.getActionListArgs());
         break;
       case ACTION_REGISTRY:
         exitCode = actionRegistry(
@@ -1676,28 +1677,50 @@ public class SliderClient extends 
AbstractSliderLaunchedService implements RunSe
   }
 
   @Override
+  /**
+   * Implement the list action: list all nodes
+   *
+   * live: List out only live instances
+   * history: List out only history instances
+   *
+   * If arguments are not given then list out both finished and
+   * running instances
+   *
+   * @param clustername List out specific cluster
+   * @param args Action list arguments
+   * @return exit code of 0 if a list was created
+   */
   @VisibleForTesting
-  public int actionList(String clustername) throws IOException, YarnException {
+  public int actionList(String clustername, ActionListArgs args)
+      throws IOException, YarnException {
     verifyBindingsDefined();
 
     String user = UserGroupInformation.getCurrentUser().getUserName();
     List<ApplicationReport> instances = listSliderInstances(user);
-
+    SliderUtils.sortApplicationReport(instances);
     if (isUnset(clustername)) {
       log.info("Instances for {}: {}",
-               (user != null ? user : "all users"),
-               instances.size());
+          (user != null ? user : "all users"),
+          instances.size());
       for (ApplicationReport report : instances) {
-        logAppReport(report);
+        logAppReport(report, args.live, args.history);
       }
       return EXIT_SUCCESS;
     } else {
       SliderUtils.validateClusterName(clustername);
       log.debug("Listing cluster named {}", clustername);
-      ApplicationReport report =
-        findClusterInInstanceList(instances, clustername);
+      if (args.history) {
+        for (ApplicationReport report : instances) {
+          if (report.getName().equals(clustername)) {
+            logAppReport(report, args.live, args.history);
+          }
+        }
+        return EXIT_SUCCESS;
+      }
+      ApplicationReport report = findClusterInInstanceList(instances,
+          clustername);
       if (report != null) {
-        logAppReport(report);
+        logAppReport(report, true, false);
         return EXIT_SUCCESS;
       } else {
         throw unknownClusterException(clustername);
@@ -1709,8 +1732,17 @@ public class SliderClient extends 
AbstractSliderLaunchedService implements RunSe
    * Log the application report at INFO
    * @param report report to log
    */
-  public void logAppReport(ApplicationReport report) {
-    log.info(SliderUtils.appReportToString(report, "\n"));
+  public void logAppReport(ApplicationReport report, boolean live, boolean 
history) {
+    boolean active = report.getYarnApplicationState() == 
YarnApplicationState.RUNNING
+                || report.getYarnApplicationState() ==
+                   YarnApplicationState.ACCEPTED;
+    if (active && live && !history) {
+      log.info(SliderUtils.appReportToString(report, "\n"));
+    } else if (!active && history && !live) {
+      log.info(SliderUtils.appReportToString(report, "\n"));
+    } else {
+      log.info(SliderUtils.appReportToString(report, "\n"));
+    }
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/3e46cb9c/slider-core/src/main/java/org/apache/slider/common/params/ActionListArgs.java
----------------------------------------------------------------------
diff --git 
a/slider-core/src/main/java/org/apache/slider/common/params/ActionListArgs.java 
b/slider-core/src/main/java/org/apache/slider/common/params/ActionListArgs.java
index 8f42b69..586f0a1 100644
--- 
a/slider-core/src/main/java/org/apache/slider/common/params/ActionListArgs.java
+++ 
b/slider-core/src/main/java/org/apache/slider/common/params/ActionListArgs.java
@@ -18,6 +18,7 @@
 
 package org.apache.slider.common.params;
 
+import com.beust.jcommander.Parameter;
 import com.beust.jcommander.Parameters;
 
 @Parameters(commandNames = {SliderActions.ACTION_LIST},
@@ -29,6 +30,14 @@ public class ActionListArgs extends AbstractActionArgs {
     return SliderActions.ACTION_LIST;
   }
 
+  @Parameter(names = {ARG_LIVE},
+          description = "List only live application instances")
+  public boolean live;
+
+  @Parameter(names = {ARG_HISTORY},
+          description = "List only historical application instances")
+  public boolean history;
+
   /**
    * Get the min #of params expected
    * @return the min number of params in the {@link #parameters} field

http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/3e46cb9c/slider-core/src/main/java/org/apache/slider/common/params/Arguments.java
----------------------------------------------------------------------
diff --git 
a/slider-core/src/main/java/org/apache/slider/common/params/Arguments.java 
b/slider-core/src/main/java/org/apache/slider/common/params/Arguments.java
index 06d9dfb..693ad2f 100644
--- a/slider-core/src/main/java/org/apache/slider/common/params/Arguments.java
+++ b/slider-core/src/main/java/org/apache/slider/common/params/Arguments.java
@@ -55,6 +55,7 @@ public interface Arguments {
   String ARG_GETEXP = "--getexp";
   String ARG_GETFILES = "--getfiles";
   String ARG_HELP = "--help";
+  String ARG_HISTORY = "--history";
   String ARG_ID = "--id";
   String ARG_IMAGE = "--image";
   String ARG_INTERNAL = "--internal";

http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/3e46cb9c/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java
----------------------------------------------------------------------
diff --git 
a/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java 
b/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java
index a6d8cd5..e4d54e8 100644
--- a/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java
+++ b/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java
@@ -85,6 +85,8 @@ import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -618,6 +620,53 @@ public final class SliderUtils {
   }
 
   /**
+   * Sorts the given list of application reports
+   * Finished instances are ordered by finished time and running/accepted 
instances are
+   * ordered by start time
+   * Finally Instance are order by finished instances and running instances
+   *
+   * @param instances
+   */
+  public static void sortApplicationReport(List<ApplicationReport> instances) {
+    if (instances.size() <= 1) {
+      return;
+    }
+    List<ApplicationReport> nonLiveInstance = new 
ArrayList<>(instances.size());
+    List<ApplicationReport> liveInstance = new ArrayList<>(instances.size());
+
+    for (ApplicationReport report : instances) {
+      if (report.getYarnApplicationState() == YarnApplicationState.RUNNING
+          || report.getYarnApplicationState() == 
YarnApplicationState.ACCEPTED) {
+        liveInstance.add(report);
+      } else {
+        nonLiveInstance.add(report);
+      }
+    }
+
+    if (liveInstance.size() > 1) {
+      Comparator<ApplicationReport> liveInstanceComparator = new 
Comparator<ApplicationReport>() {
+        @Override
+        public int compare(ApplicationReport r1, ApplicationReport r2) {
+          return Long.compare(r1.getStartTime(), r2.getStartTime());
+        }
+      };
+      Collections.sort(liveInstance, liveInstanceComparator);
+    }
+    if (nonLiveInstance.size() > 1) {
+      Comparator<ApplicationReport> nonLiveInstanceComparator = new 
Comparator<ApplicationReport>() {
+        @Override
+        public int compare(ApplicationReport r1, ApplicationReport r2) {
+          return Long.compare(r1.getFinishTime(), r2.getFinishTime());
+        }
+      };
+      Collections.sort(nonLiveInstance, nonLiveInstanceComparator);
+    }
+    instances.clear();
+    instances.addAll(nonLiveInstance);
+    instances.addAll(liveInstance);
+  }
+
+  /**
    * Merge in one map to another -all entries in the second map are
    * merged into the first -overwriting any duplicate keys.
    * @param first first map -the updated one.

Reply via email to