ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aonis...@apache.org
Subject [2/2] ambari git commit: AMBARI-16929. Automatically cleanup /var/run/ambari-server/stack-recommendations (aonishuk)
Date Fri, 27 May 2016 16:16:33 GMT
AMBARI-16929. Automatically cleanup /var/run/ambari-server/stack-recommendations (aonishuk)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/18c531fe
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/18c531fe
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/18c531fe

Branch: refs/heads/branch-2.4
Commit: 18c531fe155bb949637ecfbd9c28a7b6a85ab6f3
Parents: 2d1b09e
Author: Andrew Onishuk <aonishuk@hortonworks.com>
Authored: Fri May 27 19:16:36 2016 +0300
Committer: Andrew Onishuk <aonishuk@hortonworks.com>
Committed: Fri May 27 19:16:36 2016 +0300

----------------------------------------------------------------------
 ambari-server/conf/unix/ambari.properties       |  3 ++
 .../stackadvisor/StackAdvisorHelper.java        | 12 +++---
 .../ComponentLayoutRecommendationCommand.java   |  3 +-
 .../ComponentLayoutValidationCommand.java       |  4 +-
 ...rationDependenciesRecommendationCommand.java |  4 +-
 .../ConfigurationRecommendationCommand.java     |  4 +-
 .../ConfigurationValidationCommand.java         |  4 +-
 .../commands/StackAdvisorCommand.java           | 36 +++++++++++++++++-
 .../server/configuration/Configuration.java     |  7 ++++
 .../apache/ambari/server/utils/DateUtils.java   | 40 ++++++++++++++++++++
 .../ConfigurationRecommendationCommandTest.java |  2 +-
 .../commands/StackAdvisorCommandTest.java       | 25 +++++++-----
 12 files changed, 119 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/conf/unix/ambari.properties
----------------------------------------------------------------------
diff --git a/ambari-server/conf/unix/ambari.properties b/ambari-server/conf/unix/ambari.properties
index 9f1692e..e324d764 100644
--- a/ambari-server/conf/unix/ambari.properties
+++ b/ambari-server/conf/unix/ambari.properties
@@ -54,6 +54,9 @@ stackadvisor.script=$ROOT/var/lib/ambari-server/resources/scripts/stack_advisor.
 server.tmp.dir=$ROOT/var/lib/ambari-server/data/tmp
 ambari.python.wrap=ambari-python-wrap
 
+# removes artifacts from recommendations.dir which are older than specified (h,d,w,m,y) during
the next recommendation call.
+recommendations.artifacts.lifetime=1w
+
 server.connection.max.idle.millis=900000
 server.fqdn.service.url=http://169.254.169.254/latest/meta-data/public-hostname
 server.stages.parallel=true

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/StackAdvisorHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/StackAdvisorHelper.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/StackAdvisorHelper.java
index a925d7d..e175c69 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/StackAdvisorHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/StackAdvisorHelper.java
@@ -40,6 +40,7 @@ import com.google.inject.Singleton;
 public class StackAdvisorHelper {
 
   private File recommendationsDir;
+  private String recommendationsArtifactsLifetime;
   private String stackAdvisorScript;
   private final AmbariMetaInfo metaInfo;
 
@@ -51,6 +52,7 @@ public class StackAdvisorHelper {
   public StackAdvisorHelper(Configuration conf, StackAdvisorRunner saRunner,
                             AmbariMetaInfo metaInfo) throws IOException {
     this.recommendationsDir = conf.getRecommendationsDir();
+    this.recommendationsArtifactsLifetime = conf.getRecommendationsArtifactsLifetime();
     this.stackAdvisorScript = conf.getStackAdvisorScript();
     this.saRunner = saRunner;
     this.metaInfo = metaInfo;
@@ -78,10 +80,10 @@ public class StackAdvisorHelper {
       StackAdvisorRequestType requestType) throws StackAdvisorException {
     StackAdvisorCommand<ValidationResponse> command;
     if (requestType == StackAdvisorRequestType.HOST_GROUPS) {
-      command = new ComponentLayoutValidationCommand(recommendationsDir, stackAdvisorScript,
+      command = new ComponentLayoutValidationCommand(recommendationsDir, recommendationsArtifactsLifetime,
stackAdvisorScript,
           requestId, saRunner, metaInfo);
     } else if (requestType == StackAdvisorRequestType.CONFIGURATIONS) {
-      command = new ConfigurationValidationCommand(recommendationsDir, stackAdvisorScript,
+      command = new ConfigurationValidationCommand(recommendationsDir, recommendationsArtifactsLifetime,
stackAdvisorScript,
           requestId, saRunner, metaInfo);
     } else {
       throw new StackAdvisorRequestException(String.format("Unsupported request type, type=%s",
@@ -113,13 +115,13 @@ public class StackAdvisorHelper {
       StackAdvisorRequestType requestType) throws StackAdvisorException {
     StackAdvisorCommand<RecommendationResponse> command;
     if (requestType == StackAdvisorRequestType.HOST_GROUPS) {
-      command = new ComponentLayoutRecommendationCommand(recommendationsDir, stackAdvisorScript,
+      command = new ComponentLayoutRecommendationCommand(recommendationsDir, recommendationsArtifactsLifetime,
stackAdvisorScript,
           requestId, saRunner, metaInfo);
     } else if (requestType == StackAdvisorRequestType.CONFIGURATIONS) {
-      command = new ConfigurationRecommendationCommand(recommendationsDir, stackAdvisorScript,
+      command = new ConfigurationRecommendationCommand(recommendationsDir, recommendationsArtifactsLifetime,
stackAdvisorScript,
           requestId, saRunner, metaInfo);
     } else if (requestType == StackAdvisorRequestType.CONFIGURATION_DEPENDENCIES) {
-      command = new ConfigurationDependenciesRecommendationCommand(recommendationsDir, stackAdvisorScript,
+      command = new ConfigurationDependenciesRecommendationCommand(recommendationsDir, recommendationsArtifactsLifetime,
stackAdvisorScript,
           requestId, saRunner, metaInfo);
     } else {
       throw new StackAdvisorRequestException(String.format("Unsupported request type, type=%s",

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutRecommendationCommand.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutRecommendationCommand.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutRecommendationCommand.java
index 0dff92b..87725dc 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutRecommendationCommand.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutRecommendationCommand.java
@@ -34,11 +34,12 @@ public class ComponentLayoutRecommendationCommand extends
     StackAdvisorCommand<RecommendationResponse> {
 
   public ComponentLayoutRecommendationCommand(File recommendationsDir,
+                                              String recommendationsArtifactsLifetime,
                                               String stackAdvisorScript,
                                               int requestId,
                                               StackAdvisorRunner saRunner,
                                               AmbariMetaInfo metaInfo) {
-    super(recommendationsDir, stackAdvisorScript, requestId, saRunner, metaInfo);
+    super(recommendationsDir, recommendationsArtifactsLifetime, stackAdvisorScript, requestId,
saRunner, metaInfo);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutValidationCommand.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutValidationCommand.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutValidationCommand.java
index 757ebee..8ca53fe 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutValidationCommand.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ComponentLayoutValidationCommand.java
@@ -31,9 +31,9 @@ import org.apache.ambari.server.api.services.stackadvisor.validations.Validation
  */
 public class ComponentLayoutValidationCommand extends StackAdvisorCommand<ValidationResponse>
{
 
-  public ComponentLayoutValidationCommand(File recommendationsDir, String stackAdvisorScript,
+  public ComponentLayoutValidationCommand(File recommendationsDir, String recommendationsArtifactsLifetime,
String stackAdvisorScript,
                                           int requestId, StackAdvisorRunner saRunner, AmbariMetaInfo
metaInfo) {
-    super(recommendationsDir, stackAdvisorScript, requestId, saRunner, metaInfo);
+    super(recommendationsDir, recommendationsArtifactsLifetime, stackAdvisorScript, requestId,
saRunner, metaInfo);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationDependenciesRecommendationCommand.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationDependenciesRecommendationCommand.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationDependenciesRecommendationCommand.java
index ae86548..dd2a7d2 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationDependenciesRecommendationCommand.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationDependenciesRecommendationCommand.java
@@ -40,9 +40,9 @@ import static org.apache.ambari.server.api.services.stackadvisor.recommendations
 public class ConfigurationDependenciesRecommendationCommand extends
     StackAdvisorCommand<RecommendationResponse> {
 
-  public ConfigurationDependenciesRecommendationCommand(File recommendationsDir, String stackAdvisorScript,
int requestId,
+  public ConfigurationDependenciesRecommendationCommand(File recommendationsDir, String recommendationsArtifactsLifetime,
String stackAdvisorScript, int requestId,
                                                         StackAdvisorRunner saRunner, AmbariMetaInfo
metaInfo) {
-    super(recommendationsDir, stackAdvisorScript, requestId, saRunner, metaInfo);
+    super(recommendationsDir, recommendationsArtifactsLifetime, stackAdvisorScript, requestId,
saRunner, metaInfo);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommand.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommand.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommand.java
index ad01b40..e6279c4 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommand.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommand.java
@@ -38,9 +38,9 @@ import java.util.Set;
 public class ConfigurationRecommendationCommand extends
     StackAdvisorCommand<RecommendationResponse> {
 
-  public ConfigurationRecommendationCommand(File recommendationsDir, String stackAdvisorScript,
int requestId,
+  public ConfigurationRecommendationCommand(File recommendationsDir, String recommendationsArtifactsLifetime,
String stackAdvisorScript, int requestId,
                                             StackAdvisorRunner saRunner, AmbariMetaInfo metaInfo)
{
-    super(recommendationsDir, stackAdvisorScript, requestId, saRunner, metaInfo);
+    super(recommendationsDir, recommendationsArtifactsLifetime, stackAdvisorScript, requestId,
saRunner, metaInfo);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationValidationCommand.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationValidationCommand.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationValidationCommand.java
index c234947..60132a1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationValidationCommand.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationValidationCommand.java
@@ -31,9 +31,9 @@ import org.apache.ambari.server.api.services.stackadvisor.validations.Validation
  */
 public class ConfigurationValidationCommand extends StackAdvisorCommand<ValidationResponse>
{
 
-  public ConfigurationValidationCommand(File recommendationsDir, String stackAdvisorScript,
+  public ConfigurationValidationCommand(File recommendationsDir, String recommendationsArtifactsLifetime,
String stackAdvisorScript,
                                         int requestId, StackAdvisorRunner saRunner, AmbariMetaInfo
metaInfo) {
-    super(recommendationsDir, stackAdvisorScript, requestId, saRunner, metaInfo);
+    super(recommendationsDir, recommendationsArtifactsLifetime, stackAdvisorScript, requestId,
saRunner, metaInfo);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommand.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommand.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommand.java
index 3e20a09..6f612af 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommand.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommand.java
@@ -19,11 +19,13 @@
 package org.apache.ambari.server.api.services.stackadvisor.commands;
 
 import java.io.File;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.lang.reflect.ParameterizedType;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -45,11 +47,16 @@ import org.apache.ambari.server.api.services.stackadvisor.StackAdvisorResponse;
 import org.apache.ambari.server.api.services.stackadvisor.StackAdvisorRunner;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.state.ServiceInfo;
+import org.apache.ambari.server.utils.DateUtils;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.AgeFileFilter;
+import org.apache.commons.io.filefilter.FalseFileFilter;
+import org.apache.commons.io.filefilter.TrueFileFilter;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.derby.iapi.util.StringUtil;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
 import org.codehaus.jackson.map.SerializationConfig;
@@ -93,6 +100,7 @@ public abstract class StackAdvisorCommand<T extends StackAdvisorResponse>
extend
   private static final String AMBARI_SERVER_CONFIGURATIONS_PROPERTY = "ambari-server-properties";
 
   private File recommendationsDir;
+  private String recommendationsArtifactsLifetime;
   private String stackAdvisorScript;
 
   private int requestId;
@@ -104,7 +112,7 @@ public abstract class StackAdvisorCommand<T extends StackAdvisorResponse>
extend
   private final AmbariMetaInfo metaInfo;
 
   @SuppressWarnings("unchecked")
-  public StackAdvisorCommand(File recommendationsDir, String stackAdvisorScript, int requestId,
+  public StackAdvisorCommand(File recommendationsDir, String recommendationsArtifactsLifetime,
String stackAdvisorScript, int requestId,
       StackAdvisorRunner saRunner, AmbariMetaInfo metaInfo) {
     this.type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
         .getActualTypeArguments()[0];
@@ -113,6 +121,7 @@ public abstract class StackAdvisorCommand<T extends StackAdvisorResponse>
extend
     this.mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
 
     this.recommendationsDir = recommendationsDir;
+    this.recommendationsArtifactsLifetime = recommendationsArtifactsLifetime;
     this.stackAdvisorScript = stackAdvisorScript;
     this.requestId = requestId;
     this.saRunner = saRunner;
@@ -314,6 +323,8 @@ public abstract class StackAdvisorCommand<T extends StackAdvisorResponse>
extend
       }
     }
 
+    cleanupRequestDirectory();
+
     requestDirectory = new File(recommendationsDir, Integer.toString(requestId));
 
     if (requestDirectory.exists()) {
@@ -324,6 +335,29 @@ public abstract class StackAdvisorCommand<T extends StackAdvisorResponse>
extend
     }
   }
 
+  /**
+   * Deletes folders older than (now - recommendationsArtifactsLifetime)
+   */
+  private void cleanupRequestDirectory() throws IOException {
+    final Date cutoffDate = DateUtils.getDateSpecifiedTimeAgo(recommendationsArtifactsLifetime);
// subdirectories older than this date will be deleted
+
+    String[] oldDirectories = recommendationsDir.list(new FilenameFilter() {
+      @Override
+      public boolean accept(File current, String name) {
+        File file = new File(current, name);
+        return file.isDirectory() && !FileUtils.isFileNewer(file, cutoffDate);
+      }
+    });
+    
+    if(oldDirectories.length > 0) {
+      LOG.info(String.format("Deleting old directories %s from %s", StringUtils.join(oldDirectories,
", "), recommendationsDir));
+    }
+    
+    for(String oldDirectory:oldDirectories) {
+      FileUtils.deleteDirectory(new File(recommendationsDir, oldDirectory));
+    }
+  }
+
   String getHostsInformation(StackAdvisorRequest request) throws StackAdvisorException {
     String hostsURI = String.format(GET_HOSTS_INFO_URI, request.getHostsCommaSeparated());
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
index 7cfaf61..0c2fbba 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
@@ -98,6 +98,8 @@ public class Configuration {
   public static final String BOOTSTRAP_SETUP_AGENT_SCRIPT = "bootstrap.setup_agent.script";
   public static final String BOOTSTRAP_SETUP_AGENT_PASSWORD = "bootstrap.setup_agent.password";
   public static final String BOOTSTRAP_MASTER_HOSTNAME = "bootstrap.master_host_name";
+  public static final String RECOMMENDATIONS_ARTIFACTS_LIFETIME = "recommendations.artifacts.lifetime";
+  public static final String RECOMMENDATIONS_ARTIFACTS_LIFETIME_DEFAULT = "1w";
   public static final String RECOMMENDATIONS_DIR = "recommendations.dir";
   public static final String RECOMMENDATIONS_DIR_DEFAULT = AmbariPath.getPath("/var/run/ambari-server/stack-recommendations");
   public static final String STACK_ADVISOR_SCRIPT = "stackadvisor.script";
@@ -1225,6 +1227,11 @@ public class Configuration {
     return new File(fileName);
   }
 
+  public String getRecommendationsArtifactsLifetime() {
+    String lifetime = properties.getProperty(RECOMMENDATIONS_ARTIFACTS_LIFETIME, RECOMMENDATIONS_ARTIFACTS_LIFETIME_DEFAULT);
+    return lifetime;
+  }
+
   public String areHostsSysPrepped(){
     return properties.getProperty(SYS_PREPPED_HOSTS_KEY, SYS_PREPPED_HOSTS_DEFAULT);
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/main/java/org/apache/ambari/server/utils/DateUtils.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/utils/DateUtils.java b/ambari-server/src/main/java/org/apache/ambari/server/utils/DateUtils.java
index 785f4fd..d2429ad 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/utils/DateUtils.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/utils/DateUtils.java
@@ -19,7 +19,12 @@ package org.apache.ambari.server.utils;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * Static Helper methods for datetime conversions
@@ -94,4 +99,39 @@ public class DateUtils {
     Date now = new Date();
     return time.after(now);
   }
+
+  /**
+   * Returns a date given period before now
+   *
+   * @param periodString is a string indicating a time period. Example '1y2m3w4d5y'
+   * means 1 year, 2 months, 3 weeks, 4 days, 5 hours.
+   * @return
+   */
+  public static Date getDateSpecifiedTimeAgo(String periodString) {
+    String pattern = "((\\d+)([hdwmy]))";
+    Pattern findPattern = Pattern.compile(pattern);
+    Pattern matchPattern = Pattern.compile(pattern+"+");
+
+    Map<String, Integer> qualifierToConstant = new HashMap<String, Integer>()
{{
+        put("h",   Calendar.HOUR);
+        put("d",    Calendar.DATE);
+        put("w",   Calendar.WEEK_OF_YEAR);
+        put("m",  Calendar.MONTH);
+        put("y",   Calendar.YEAR);
+    }};
+
+    if(!matchPattern.matcher(periodString).matches()) {
+      throw new IllegalArgumentException(String.format("Invalid string for indicating period
%s", periodString));
+    }
+
+    Calendar calendar = Calendar.getInstance();
+    Matcher m = findPattern.matcher(periodString);
+
+    while (m.find()) {
+      int amount = Integer.parseInt(m.group(2));
+      int unit = qualifierToConstant.get(m.group(3));
+      calendar.add(unit, -amount);
+    }
+    return calendar.getTime();
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommandTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommandTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommandTest.java
index bc9cf77..172fc6c 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommandTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/ConfigurationRecommendationCommandTest.java
@@ -43,7 +43,7 @@ public class ConfigurationRecommendationCommandTest {
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     File file = mock(File.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
-    ConfigurationRecommendationCommand command = new ConfigurationRecommendationCommand(file,
"script", 1, saRunner, metaInfo);
+    ConfigurationRecommendationCommand command = new ConfigurationRecommendationCommand(file,
"1w", "script", 1, saRunner, metaInfo);
 
     StackAdvisorRequest request = mock(StackAdvisorRequest.class);
     Map<String, Set<String>> componentHostGroupMap = new HashMap<String, Set<String>>();

http://git-wip-us.apache.org/repos/asf/ambari/blob/18c531fe/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommandTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommandTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommandTest.java
index 263bbe1..25d634b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommandTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/commands/StackAdvisorCommandTest.java
@@ -78,12 +78,13 @@ public class StackAdvisorCommandTest {
   @Test(expected = StackAdvisorException.class)
   public void testInvoke_invalidRequest_throwsException() throws StackAdvisorException {
     File recommendationsDir = temp.newFolder("recommendationDir");
+    String recommendationsArtifactsLifetime = "1w";
     String stackAdvisorScript = "echo";
     int requestId = 0;
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     doReturn(Collections.emptyList()).when(metaInfo).getStackParentVersions(anyString(),
anyString());
-    StackAdvisorCommand<TestResource> command = spy(new TestStackAdvisorCommand(recommendationsDir,
+    StackAdvisorCommand<TestResource> command = spy(new TestStackAdvisorCommand(recommendationsDir,
recommendationsArtifactsLifetime, 
         stackAdvisorScript, requestId, saRunner, metaInfo));
 
     StackAdvisorRequest request = StackAdvisorRequestBuilder.forStack("stackName", "stackVersion")
@@ -99,11 +100,12 @@ public class StackAdvisorCommandTest {
   public void testInvoke_saRunnerNotSucceed_throwsException() throws StackAdvisorException
{
     File recommendationsDir = temp.newFolder("recommendationDir");
     String stackAdvisorScript = "echo";
+    String recommendationsArtifactsLifetime = "1w";
     int requestId = 0;
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     doReturn(Collections.emptyList()).when(metaInfo).getStackParentVersions(anyString(),
anyString());
-    StackAdvisorCommand<TestResource> command = spy(new TestStackAdvisorCommand(recommendationsDir,
+    StackAdvisorCommand<TestResource> command = spy(new TestStackAdvisorCommand(recommendationsDir,
recommendationsArtifactsLifetime,
         stackAdvisorScript, requestId, saRunner, metaInfo));
 
     StackAdvisorRequest request = StackAdvisorRequestBuilder.forStack("stackName", "stackVersion")
@@ -127,11 +129,12 @@ public class StackAdvisorCommandTest {
   public void testInvoke_adjustThrowsException_throwsException() throws StackAdvisorException
{
     File recommendationsDir = temp.newFolder("recommendationDir");
     String stackAdvisorScript = "echo";
+    String recommendationsArtifactsLifetime = "1w";
     int requestId = 0;
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     doReturn(Collections.emptyList()).when(metaInfo).getStackParentVersions(anyString(),
anyString());
-    StackAdvisorCommand<TestResource> command = spy(new TestStackAdvisorCommand(recommendationsDir,
+    StackAdvisorCommand<TestResource> command = spy(new TestStackAdvisorCommand(recommendationsDir,
recommendationsArtifactsLifetime,
         stackAdvisorScript, requestId, saRunner, metaInfo));
 
     StackAdvisorRequest request = StackAdvisorRequestBuilder.forStack("stackName", "stackVersion")
@@ -154,12 +157,13 @@ public class StackAdvisorCommandTest {
     final String testResourceString = String.format("{\"type\": \"%s\"}", expected);
     final File recommendationsDir = temp.newFolder("recommendationDir");
     String stackAdvisorScript = "echo";
+    String recommendationsArtifactsLifetime = "1w";
     final int requestId = 2;
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     doReturn(Collections.emptyList()).when(metaInfo).getStackParentVersions(anyString(),
anyString());
     final StackAdvisorCommand<TestResource> command = spy(new TestStackAdvisorCommand(
-        recommendationsDir, stackAdvisorScript, requestId, saRunner, metaInfo));
+        recommendationsDir, recommendationsArtifactsLifetime, stackAdvisorScript, requestId,
saRunner, metaInfo));
 
     StackAdvisorRequest request = StackAdvisorRequestBuilder.forStack("stackName", "stackVersion")
         .build();
@@ -189,9 +193,10 @@ public class StackAdvisorCommandTest {
   @Test
   public void testPopulateStackHierarchy() throws Exception {
     File file = mock(File.class);
+    String recommendationsArtifactsLifetime = "1w";
     StackAdvisorRunner stackAdvisorRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo ambariMetaInfo = mock(AmbariMetaInfo.class);
-    StackAdvisorCommand<TestResource> cmd = new TestStackAdvisorCommand(file, "test",
1,
+    StackAdvisorCommand<TestResource> cmd = new TestStackAdvisorCommand(file, recommendationsArtifactsLifetime,
"test", 1,
         stackAdvisorRunner, ambariMetaInfo);
     ObjectNode objectNode = (ObjectNode) cmd.mapper.readTree("{\"Versions\": " +
         "{\"stack_name\": \"stack\", \"stack_version\":\"1.0.0\"}}");
@@ -216,9 +221,10 @@ public class StackAdvisorCommandTest {
   @Test
   public void testPopulateAmbariServerProperties() throws Exception {
     File file = mock(File.class);
+    String recommendationsArtifactsLifetime = "1w";
     StackAdvisorRunner stackAdvisorRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo ambariMetaInfo = mock(AmbariMetaInfo.class);
-    StackAdvisorCommand<TestResource> cmd = new TestStackAdvisorCommand(file, "test",
1,
+    StackAdvisorCommand<TestResource> cmd = new TestStackAdvisorCommand(file, recommendationsArtifactsLifetime,
"test", 1,
       stackAdvisorRunner, ambariMetaInfo);
     ObjectNode objectNode = (ObjectNode) cmd.mapper.readTree("{\"Versions\": " +
       "{\"stack_name\": \"stack\", \"stack_version\":\"1.0.0\"}}");
@@ -237,9 +243,10 @@ public class StackAdvisorCommandTest {
   @Test
   public void testPopulateStackHierarchy_noParents() throws Exception {
     File file = mock(File.class);
+    String recommendationsArtifactsLifetime = "1w";
     StackAdvisorRunner stackAdvisorRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo ambariMetaInfo = mock(AmbariMetaInfo.class);
-    StackAdvisorCommand<TestResource> cmd = new TestStackAdvisorCommand(file, "test",
1,
+    StackAdvisorCommand<TestResource> cmd = new TestStackAdvisorCommand(file, recommendationsArtifactsLifetime,
"test", 1,
         stackAdvisorRunner, ambariMetaInfo);
     ObjectNode objectNode = (ObjectNode) cmd.mapper.readTree("{\"Versions\": " +
         "{\"stack_name\": \"stack\", \"stack_version\":\"1.0.0\"}}");
@@ -259,9 +266,9 @@ public class StackAdvisorCommandTest {
   }
 
   class TestStackAdvisorCommand extends StackAdvisorCommand<TestResource> {
-    public TestStackAdvisorCommand(File recommendationsDir, String stackAdvisorScript,
+    public TestStackAdvisorCommand(File recommendationsDir, String recommendationsArtifactsLifetime,
String stackAdvisorScript,
         int requestId, StackAdvisorRunner saRunner, AmbariMetaInfo metaInfo) {
-      super(recommendationsDir, stackAdvisorScript, requestId, saRunner, metaInfo);
+      super(recommendationsDir, recommendationsArtifactsLifetime, stackAdvisorScript, requestId,
saRunner, metaInfo);
     }
 
     @Override


Mime
View raw message