Просмотр исходного кода

AMBARI-18866. Stack advisor should limit call history it preserves (Madhuvanthi Radhakrishnan via smohanty)

Sumit Mohanty 9 лет назад
Родитель
Сommit
3526ac503e

+ 15 - 3
ambari-server/src/main/java/org/apache/ambari/server/api/services/stackadvisor/StackAdvisorHelper.java

@@ -41,6 +41,7 @@ public class StackAdvisorHelper {
 
   private File recommendationsDir;
   private String recommendationsArtifactsLifetime;
+  private int recommendationsArtifactsRolloverMax;
   private String stackAdvisorScript;
   private final AmbariMetaInfo metaInfo;
 
@@ -53,6 +54,7 @@ public class StackAdvisorHelper {
                             AmbariMetaInfo metaInfo) throws IOException {
     this.recommendationsDir = conf.getRecommendationsDir();
     this.recommendationsArtifactsLifetime = conf.getRecommendationsArtifactsLifetime();
+    this.recommendationsArtifactsRolloverMax = conf.getRecommendationsArtifactsRolloverMax();
     this.stackAdvisorScript = conf.getStackAdvisorScript();
     this.saRunner = saRunner;
     this.metaInfo = metaInfo;
@@ -68,7 +70,7 @@ public class StackAdvisorHelper {
    */
   public synchronized ValidationResponse validate(StackAdvisorRequest request)
       throws StackAdvisorException {
-    requestId += 1;
+      requestId = generateRequestId();
 
     StackAdvisorCommand<ValidationResponse> command = createValidationCommand(request
         .getRequestType());
@@ -103,9 +105,9 @@ public class StackAdvisorHelper {
    */
   public synchronized RecommendationResponse recommend(StackAdvisorRequest request)
       throws StackAdvisorException {
-    requestId += 1;
+      requestId = generateRequestId();
 
-    StackAdvisorCommand<RecommendationResponse> command = createRecommendationCommand(request
+      StackAdvisorCommand<RecommendationResponse> command = createRecommendationCommand(request
         .getRequestType());
 
     return command.invoke(request);
@@ -131,4 +133,14 @@ public class StackAdvisorHelper {
     return command;
   }
 
+  /**
+   * Returns an incremented requestId. Rollsover back to 0 in case the requestId >= recommendationsArtifactsrollovermax
+   * @return {int requestId}
+   */
+  private int generateRequestId(){
+      requestId += 1;
+      return requestId % recommendationsArtifactsRolloverMax;
+
+  }
+
 }

+ 11 - 0
ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java

@@ -400,6 +400,12 @@ public class Configuration {
   public static final ConfigurationProperty<String> RECOMMENDATIONS_ARTIFACTS_LIFETIME = new ConfigurationProperty<>(
       "recommendations.artifacts.lifetime", "1w");
 
+    @Markdown(
+            description = "Maximum number of recommendations artifacts at a given time",
+            examples = {"50","10","100"} )
+    public static final ConfigurationProperty<Integer> RECOMMENDATIONS_ARTIFACTS_ROLLOVER_MAX = new ConfigurationProperty<>(
+            "recommendations.artifacts.rollover.max",100);
+
   /**
    * The directory on the Ambari Server file system used for storing
    * Recommendation API artifacts.
@@ -3114,6 +3120,11 @@ public class Configuration {
     return getProperty(RECOMMENDATIONS_ARTIFACTS_LIFETIME);
   }
 
+  public int getRecommendationsArtifactsRolloverMax() {
+        int rollovermax = Integer.parseInt(getProperty(RECOMMENDATIONS_ARTIFACTS_ROLLOVER_MAX));
+        return (rollovermax == 0) ? 100 : rollovermax;
+    }
+
   public String areHostsSysPrepped(){
     return getProperty(SYS_PREPPED_HOSTS);
   }

+ 9 - 1
ambari-server/src/test/java/org/apache/ambari/server/api/services/stackadvisor/StackAdvisorHelperTest.java

@@ -44,11 +44,11 @@ import org.junit.Test;
  * StackAdvisorHelper unit tests.
  */
 public class StackAdvisorHelperTest {
-
   @Test
   @SuppressWarnings("unchecked")
   public void testValidate_returnsCommandResult() throws StackAdvisorException, IOException {
     Configuration configuration = mock(Configuration.class);
+    when(configuration.getRecommendationsArtifactsRolloverMax()).thenReturn(100);
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     StackAdvisorHelper helper = spy(new StackAdvisorHelper(configuration, saRunner, metaInfo));
@@ -61,6 +61,7 @@ public class StackAdvisorHelperTest {
 
     when(command.invoke(request)).thenReturn(expected);
     doReturn(command).when(helper).createValidationCommand(requestType);
+
     ValidationResponse response = helper.validate(request);
 
     assertEquals(expected, response);
@@ -71,6 +72,7 @@ public class StackAdvisorHelperTest {
   public void testValidate_commandThrowsException_throwsException() throws StackAdvisorException,
       IOException {
     Configuration configuration = mock(Configuration.class);
+    when(configuration.getRecommendationsArtifactsRolloverMax()).thenReturn(100);
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     StackAdvisorHelper helper = spy(new StackAdvisorHelper(configuration, saRunner, metaInfo));
@@ -91,6 +93,7 @@ public class StackAdvisorHelperTest {
   @SuppressWarnings("unchecked")
   public void testRecommend_returnsCommandResult() throws StackAdvisorException, IOException {
     Configuration configuration = mock(Configuration.class);
+    when(configuration.getRecommendationsArtifactsRolloverMax()).thenReturn(100);
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     StackAdvisorHelper helper = spy(new StackAdvisorHelper(configuration, saRunner, metaInfo));
@@ -113,6 +116,7 @@ public class StackAdvisorHelperTest {
   public void testRecommend_commandThrowsException_throwsException() throws StackAdvisorException,
       IOException {
     Configuration configuration = mock(Configuration.class);
+    when(configuration.getRecommendationsArtifactsRolloverMax()).thenReturn(100);
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     StackAdvisorHelper helper = spy(new StackAdvisorHelper(configuration, saRunner, metaInfo));
@@ -133,6 +137,7 @@ public class StackAdvisorHelperTest {
   public void testCreateRecommendationCommand_returnsComponentLayoutRecommendationCommand()
       throws IOException, StackAdvisorException {
     Configuration configuration = mock(Configuration.class);
+    when(configuration.getRecommendationsArtifactsRolloverMax()).thenReturn(100);
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     StackAdvisorHelper helper = new StackAdvisorHelper(configuration, saRunner, metaInfo);
@@ -148,6 +153,7 @@ public class StackAdvisorHelperTest {
   public void testCreateValidationCommand_returnsComponentLayoutValidationCommand()
       throws IOException, StackAdvisorException {
     Configuration configuration = mock(Configuration.class);
+    when(configuration.getRecommendationsArtifactsRolloverMax()).thenReturn(100);
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     StackAdvisorHelper helper = new StackAdvisorHelper(configuration, saRunner, metaInfo);
@@ -162,6 +168,7 @@ public class StackAdvisorHelperTest {
   public void testCreateValidationCommand_returnsConfigurationValidationCommand()
       throws IOException, StackAdvisorException {
     Configuration configuration = mock(Configuration.class);
+    when(configuration.getRecommendationsArtifactsRolloverMax()).thenReturn(100);
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     StackAdvisorHelper helper = new StackAdvisorHelper(configuration, saRunner, metaInfo);
@@ -176,6 +183,7 @@ public class StackAdvisorHelperTest {
   public void testCreateRecommendationDependencyCommand_returnsConfigurationDependencyRecommendationCommand()
     throws IOException, StackAdvisorException {
     Configuration configuration = mock(Configuration.class);
+    when(configuration.getRecommendationsArtifactsRolloverMax()).thenReturn(100);
     StackAdvisorRunner saRunner = mock(StackAdvisorRunner.class);
     AmbariMetaInfo metaInfo = mock(AmbariMetaInfo.class);
     StackAdvisorHelper helper = new StackAdvisorHelper(configuration, saRunner, metaInfo);