Make the Group monitor as a schedular executor service
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/eaa92e9f Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/eaa92e9f Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/eaa92e9f Branch: refs/heads/master Commit: eaa92e9fe5b8c53a7157655a3fc59f5825718c30 Parents: caa24ff Author: gayan <[email protected]> Authored: Tue Dec 16 13:08:43 2014 +0530 Committer: Udara Liyanage <[email protected]> Committed: Thu Dec 18 12:06:57 2014 +0530 ---------------------------------------------------------------------- .../autoscaler/monitor/MonitorFactory.java | 1 + .../monitor/component/GroupMonitor.java | 74 +++++++++++++------- 2 files changed, 48 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/eaa92e9f/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java index 3291ea9..12e0794 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java @@ -125,6 +125,7 @@ public class MonitorFactory { } else { groupMonitor.setHasStartupDependents(false); } + groupMonitor.startScheduler(); } } finally { ApplicationHolder.releaseReadLock(); http://git-wip-us.apache.org/repos/asf/stratos/blob/eaa92e9f/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java index f69a05a..43a385b 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java @@ -48,10 +48,10 @@ import org.apache.stratos.messaging.domain.instance.Instance; import org.apache.stratos.messaging.domain.topology.ClusterStatus; import org.apache.stratos.messaging.domain.topology.lifecycle.LifeCycleState; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * This is GroupMonitor to monitor the group which consists of @@ -66,11 +66,16 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { private boolean groupScalingEnabled; //Network partition contexts private Map<String, GroupLevelNetworkPartitionContext> networkPartitionCtxts; + + private Map<String, ScalingEvent> mapScalingEvent; + //Indicates whether the monitor is destroyed or not private boolean isDestroyed; //Monitoring interval of the monitor private int monitoringIntervalMilliseconds = 60000; //TODO get this from config file + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + /** * Constructor of GroupMonitor * @@ -85,38 +90,50 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { this.appId = appId; networkPartitionCtxts = new HashMap<String, GroupLevelNetworkPartitionContext>(); this.hasScalingDependents = hasScalingDependents; - } + } @Override public void run() { - while (!isDestroyed) { - try { + try { - if (log.isDebugEnabled()) { - log.debug("Group monitor is running : " + this.toString()); - } - monitor(); - } catch (Exception e) { - log.error("Group monitor failed : " + this.toString(), e); - } - try { - Thread.sleep(monitoringIntervalMilliseconds); - } catch (InterruptedException ignore) { - } - } + if (log.isDebugEnabled()) { + log.debug("Group monitor is running : " + this.toString()); + } + monitor(); + } catch (Exception e) { + log.error("Group monitor failed : " + this.toString(), e); + } } - public void monitor() { + public void startScheduler() { + scheduler.scheduleAtFixedRate(this, 0, monitoringIntervalMilliseconds, TimeUnit.MILLISECONDS); + } - Runnable monitoringRunnable = new Runnable() { - @Override - public void run() { - //TODO implement group monitor - } - }; - monitoringRunnable.run(); + protected void stopScheduler() { + scheduler.shutdownNow(); } + public void monitor() { + + Runnable monitoringRunnable = new Runnable() { + @Override + public void run() { + float finalFactor = 1; + if (log.isInfoEnabled()) { + log.info("Group monitor is running====== : " + this.toString()); + } + + Collection<ScalingEvent> events = mapScalingEvent.values(); + for (ScalingEvent event : events) { + log.info("Monitor Scaling Event"+event.getId()); + } + //TODO : call the on demand group scaling + mapScalingEvent.clear(); + } + }; + monitoringRunnable.run(); + } + /** * Will set the status of the monitor based on Topology Group status/child status like scaling * @@ -309,6 +326,9 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { } } } + if (scalingEvent.getId().equals(appId)) { + mapScalingEvent.put(scalingEvent.getInstanceId(), scalingEvent); + } } @Override
