You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by im...@apache.org on 2014/12/12 10:37:13 UTC

stratos git commit: Stopping statistics updater tasks for patterns where mode=continue|stop

Repository: stratos
Updated Branches:
  refs/heads/4.1.0-test f1876e726 -> 7d3b46b58


Stopping statistics updater tasks for patterns where mode=continue|stop


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/7d3b46b5
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/7d3b46b5
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/7d3b46b5

Branch: refs/heads/4.1.0-test
Commit: 7d3b46b587460e678701767ecb96ebce07499421
Parents: f1876e7
Author: Imesh Gunaratne <im...@apache.org>
Authored: Fri Dec 12 15:07:03 2014 +0530
Committer: Imesh Gunaratne <im...@apache.org>
Committed: Fri Dec 12 15:07:03 2014 +0530

----------------------------------------------------------------------
 .../MockHealthStatisticsGenerator.java          | 48 ++++++++++++--------
 .../generator/MockHealthStatisticsPattern.java  |  7 ++-
 .../generator/MockHealthStatisticsUpdater.java  | 19 ++++++--
 3 files changed, 51 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/7d3b46b5/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsGenerator.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsGenerator.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsGenerator.java
index 9d43071..be3e474 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsGenerator.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsGenerator.java
@@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.cloud.controller.iaases.mock.config.MockIaasConfig;
 import org.apache.stratos.common.threading.StratosThreadPool;
 
-import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -46,7 +45,7 @@ public class MockHealthStatisticsGenerator {
 
     private boolean scheduled;
     // Map<ServiceName, List<ScheduledFuture>>
-    private Map<String, List<ScheduledFuture>> serviceNameToTaskListMap;
+    private Map<String, Map<String, ScheduledFuture>> serviceNameToTaskListMap;
 
     public static MockHealthStatisticsGenerator getInstance() {
         if (instance == null) {
@@ -60,7 +59,7 @@ public class MockHealthStatisticsGenerator {
     }
 
     private MockHealthStatisticsGenerator() {
-        serviceNameToTaskListMap = new ConcurrentHashMap<String, List<ScheduledFuture>>();
+        serviceNameToTaskListMap = new ConcurrentHashMap<String, Map<String, ScheduledFuture>>();
     }
 
     /**
@@ -74,9 +73,9 @@ public class MockHealthStatisticsGenerator {
                 List<MockHealthStatisticsPattern> statisticsPatterns = MockIaasConfig.getInstance().
                         getMockHealthStatisticsConfig().getStatisticsPatterns();
 
-                List taskList = serviceNameToTaskListMap.get(serviceName);
+                Map<String, ScheduledFuture> taskList = serviceNameToTaskListMap.get(serviceName);
                 if (taskList == null) {
-                    taskList = new ArrayList<ScheduledFuture>();
+                    taskList = new ConcurrentHashMap<String, ScheduledFuture>();
                     serviceNameToTaskListMap.put(serviceName, taskList);
                 }
 
@@ -86,7 +85,7 @@ public class MockHealthStatisticsGenerator {
                         MockHealthStatisticsUpdater runnable = new MockHealthStatisticsUpdater(statisticsPattern);
                         ScheduledFuture<?> task = scheduledExecutorService.scheduleAtFixedRate(runnable, 0,
                                 statisticsPattern.getSampleDuration(), TimeUnit.SECONDS);
-                        taskList.add(task);
+                        taskList.put(statisticsPattern.getFactor().toString(), task);
                     }
                 }
 
@@ -104,20 +103,33 @@ public class MockHealthStatisticsGenerator {
      */
     public void stopStatisticsUpdaterTasks(String serviceName) {
         synchronized (MockHealthStatisticsGenerator.class) {
-            List<ScheduledFuture> taskList = serviceNameToTaskListMap.get(serviceName);
-            if ((taskList != null) && (taskList.size() > 0)) {
-                Iterator<ScheduledFuture> iterator = taskList.iterator();
-                while(iterator.hasNext()) {
-                    // Cancel task
-                    ScheduledFuture task = iterator.next();
-                    task.cancel(true);
-
-                    // Remove from task list
-                    iterator.remove();
+            Map<String, ScheduledFuture> taskMap = serviceNameToTaskListMap.get(serviceName);
+            if ((taskMap != null) && (taskMap.size() > 0)) {
+                Iterator<String> factorIterator = taskMap.keySet().iterator();
+                while(factorIterator.hasNext()) {
+                    String factor = factorIterator.next();
+                    stopStatisticsUpdaterTask(serviceName, factor);
                 }
+            }
+        }
+    }
+
+    /**
+     * Stop statistics updater task of a service/cartridge type, factor.
+     * @param serviceName
+     * @param factor
+     */
+    public void stopStatisticsUpdaterTask(String serviceName, String factor) {
+        Map<String, ScheduledFuture> factorToTaskMap = serviceNameToTaskListMap.get(serviceName);
+        if(factorToTaskMap != null) {
+            ScheduledFuture task = factorToTaskMap.get(factor);
+            if(task != null) {
+                task.cancel(true);
+                factorToTaskMap.remove(factor);
 
                 if (log.isInfoEnabled()) {
-                    log.info(String.format("Mock statistics updaters stopped: [service-name] %s", serviceName));
+                    log.info(String.format("Mock statistics updater task stopped: [service-name] %s" +
+                            " [factor] %s", serviceName, factor));
                 }
             }
         }
@@ -130,7 +142,7 @@ public class MockHealthStatisticsGenerator {
      * @return
      */
     public boolean statisticsUpdaterTasksScheduled(String serviceName) {
-        List<ScheduledFuture> tasks = serviceNameToTaskListMap.get(serviceName);
+        Map<String, ScheduledFuture> tasks = serviceNameToTaskListMap.get(serviceName);
         return ((tasks != null) && (tasks.size() > 0));
     }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/7d3b46b5/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsPattern.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsPattern.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsPattern.java
index 4385a51..f59df2d 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsPattern.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsPattern.java
@@ -20,6 +20,7 @@
 package org.apache.stratos.cloud.controller.iaases.mock.statistics.generator;
 
 import org.apache.stratos.cloud.controller.iaases.mock.MockAutoscalingFactor;
+import org.apache.stratos.cloud.controller.iaases.mock.exceptions.ContinueLastSampleValueException;
 import org.apache.stratos.cloud.controller.iaases.mock.exceptions.NoSampleValuesFoundException;
 import org.apache.stratos.cloud.controller.iaases.mock.exceptions.StopStatisticsPublishingException;
 import org.apache.stratos.cloud.controller.iaases.mock.statistics.StatisticsPatternMode;
@@ -73,7 +74,8 @@ public class MockHealthStatisticsPattern {
      * Returns next sample value
      * @return
      */
-    public int getNextSample() throws NoSampleValuesFoundException, StopStatisticsPublishingException {
+    public int getNextSample() throws NoSampleValuesFoundException, StopStatisticsPublishingException,
+            ContinueLastSampleValueException {
         if((sampleValues == null) || (sampleValues.size() < 1)) {
             throw new NoSampleValuesFoundException();
         }
@@ -86,7 +88,8 @@ public class MockHealthStatisticsPattern {
                 return Integer.parseInt(sampleValuesIterator.next().toString());
             } else if(getMode() == StatisticsPatternMode.Continue) {
                 // Continue: return the last value
-                return Integer.parseInt(sampleValues.get(sampleValues.size() - 1).toString());
+                int lastSampleValue = Integer.parseInt(sampleValues.get(sampleValues.size() - 1).toString());
+                throw new ContinueLastSampleValueException(lastSampleValue);
             } else if(getMode() == StatisticsPatternMode.Stop) {
                 throw new StopStatisticsPublishingException();
             } else {

http://git-wip-us.apache.org/repos/asf/stratos/blob/7d3b46b5/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsUpdater.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsUpdater.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsUpdater.java
index eed649f..6e55725 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsUpdater.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/iaases/mock/statistics/generator/MockHealthStatisticsUpdater.java
@@ -21,8 +21,8 @@ package org.apache.stratos.cloud.controller.iaases.mock.statistics.generator;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.iaases.mock.exceptions.ContinueLastSampleValueException;
 import org.apache.stratos.cloud.controller.iaases.mock.exceptions.NoSampleValuesFoundException;
-import org.apache.stratos.cloud.controller.iaases.mock.exceptions.NoStatisticsFoundException;
 import org.apache.stratos.cloud.controller.iaases.mock.exceptions.StopStatisticsPublishingException;
 import org.apache.stratos.cloud.controller.iaases.mock.statistics.MockHealthStatistics;
 
@@ -52,17 +52,30 @@ public class MockHealthStatisticsUpdater implements Runnable {
                         statisticsPattern.getCartridgeType(), statisticsPattern.getFactor().toString(), nextSample));
             }
         } catch (NoSampleValuesFoundException ignore) {
-            if(log.isDebugEnabled()) {
+            if (log.isDebugEnabled()) {
                 log.debug(String.format("No sample values found for: [cartridge-type] %s [factor] %s",
                         statisticsPattern.getCartridgeType(), statisticsPattern.getFactor().toString()));
             }
+        } catch (ContinueLastSampleValueException e) {
+            if (log.isInfoEnabled()) {
+                log.info(String.format("Continuing last sample value: [cartridge-type] %s [factor] %s [value] %d",
+                        statisticsPattern.getCartridgeType(), statisticsPattern.getFactor().toString(),
+                        e.getLastSampleValue()));
+            }
+            // Stop statistics updater task
+            MockHealthStatisticsGenerator.getInstance().stopStatisticsUpdaterTask(statisticsPattern.getCartridgeType(),
+                    statisticsPattern.getFactor().toString());
         } catch (StopStatisticsPublishingException action) {
+            // Remove statistics
             MockHealthStatistics.getInstance().removeStatistics(statisticsPattern.getCartridgeType(),
                     statisticsPattern.getFactor());
-            if(log.isDebugEnabled()) {
+            if (log.isDebugEnabled()) {
                 log.debug(String.format("Statistics removed: [cartridge-type] %s [factor] %s",
                         statisticsPattern.getCartridgeType(), statisticsPattern.getFactor().toString()));
             }
+            // Stop statistics updater task
+            MockHealthStatisticsGenerator.getInstance().stopStatisticsUpdaterTask(statisticsPattern.getCartridgeType(),
+                    statisticsPattern.getFactor().toString());
         } catch (Exception e) {
             log.error("Could not update mock statistics", e);
         }