You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by re...@apache.org on 2014/10/22 08:18:06 UTC

git commit: adding support to start instances on top of termination

Repository: stratos
Updated Branches:
  refs/heads/4.0.0-grouping c91ef96cb -> 4904878df


adding support to start instances on top of termination


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

Branch: refs/heads/4.0.0-grouping
Commit: 4904878df788dee015775caf53f6564ddf985df6
Parents: c91ef96
Author: reka <rt...@gmail.com>
Authored: Wed Oct 22 11:47:52 2014 +0530
Committer: reka <rt...@gmail.com>
Committed: Wed Oct 22 11:47:52 2014 +0530

----------------------------------------------------------------------
 .../grouping/dependency/DependencyTree.java     |  22 ++++
 .../dependency/context/ApplicationContext.java  |   9 +-
 .../monitor/ParentComponentMonitor.java         | 117 +++++++++++--------
 .../monitor/application/ApplicationMonitor.java |  14 ++-
 .../autoscaler/monitor/group/GroupMonitor.java  |  22 ++--
 5 files changed, 119 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/4904878d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
index 61648b3..48ba7b7 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
@@ -21,6 +21,7 @@ package org.apache.stratos.autoscaler.grouping.dependency;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.autoscaler.grouping.dependency.context.ApplicationContext;
+import org.apache.stratos.messaging.domain.topology.Application;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -159,6 +160,27 @@ public class DependencyTree {
         return this.applicationContextList;
     }
 
+    public List<ApplicationContext> getStarAbleDependenciesByTermination() {
+        //Breadth First search over the graph to find out which level has the terminated contexts
+        return traverseGraphByLevel(this.applicationContextList);
+    }
+
+
+    private List<ApplicationContext> traverseGraphByLevel(List<ApplicationContext> contexts) {
+        for(ApplicationContext context : contexts) {
+            if(context.isTerminated()) {
+                return contexts;
+            }
+        }
+
+        for(ApplicationContext context : contexts) {
+            return traverseGraphByLevel(context.getApplicationContextList());
+        }
+        return null;
+    }
+
+
+
     /**
      * When one group/cluster terminates/in_maintenance, need to consider about other
      * dependencies

http://git-wip-us.apache.org/repos/asf/stratos/blob/4904878d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
index e496f40..45e1655 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
@@ -32,7 +32,7 @@ public abstract class ApplicationContext {
 
     private String id;
     protected boolean started;
-    protected boolean terminated;
+    private boolean terminated;
 
     private ClusterStatus status;
 
@@ -95,4 +95,11 @@ public abstract class ApplicationContext {
     }
 
 
+    public boolean isTerminated() {
+        return terminated;
+    }
+
+    public void setTerminated(boolean terminated) {
+        this.terminated = terminated;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/4904878d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java
index 74a421b..5bfb542 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java
@@ -31,10 +31,7 @@ import org.apache.stratos.autoscaler.grouping.dependency.context.ApplicationCont
 import org.apache.stratos.autoscaler.grouping.topic.StatusEventPublisher;
 import org.apache.stratos.autoscaler.monitor.events.MonitorStatusEvent;
 import org.apache.stratos.autoscaler.status.checker.StatusChecker;
-import org.apache.stratos.messaging.domain.topology.ClusterStatus;
-import org.apache.stratos.messaging.domain.topology.GroupStatus;
 import org.apache.stratos.messaging.domain.topology.ParentComponent;
-import org.apache.stratos.messaging.domain.topology.lifecycle.LifeCycleState;
 
 import java.util.HashMap;
 import java.util.List;
@@ -69,19 +66,19 @@ public abstract class ParentComponentMonitor extends Monitor {
     protected abstract void monitor(MonitorStatusEvent statusEvent);
 
 
-    protected void onChildActivatedEvent(MonitorStatusEvent statusEvent) {
+    protected void onChildActivatedEvent(String idOfEvent) {
         try {
             //if the activated monitor is in in_active map move it to active map
-            if (this.aliasToInActiveMonitorsMap.containsKey(id)) {
-                this.aliasToActiveMonitorsMap.put(id, this.aliasToInActiveMonitorsMap.remove(id));
+            if (this.aliasToInActiveMonitorsMap.containsKey(idOfEvent)) {
+                this.aliasToActiveMonitorsMap.put(id, this.aliasToInActiveMonitorsMap.remove(idOfEvent));
             }
-            boolean startDep = startDependency(statusEvent.getId());
+            boolean startDep = startDependency(idOfEvent);
             if (log.isDebugEnabled()) {
-                log.debug("started a child: " + startDep + " by the group/cluster: " + id);
+                log.debug("started a child: " + startDep + " by the group/cluster: " + idOfEvent);
 
             }
             if (!startDep) {
-                StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
+                StatusChecker.getInstance().onChildStatusChange(idOfEvent, this.id, this.appId);
             }
         } catch (TopologyInConsistentException e) {
             //TODO revert the siblings and notify parent, change a flag for reverting/un-subscription
@@ -90,13 +87,13 @@ public abstract class ParentComponentMonitor extends Monitor {
 
     }
 
-    protected void onChildTerminatingEvent() {
+    protected void onChildTerminatingEvent(String idOfEvent) {
         //Check whether hasDependent true
-        if (!this.aliasToInActiveMonitorsMap.containsKey(id)) {
-            this.aliasToInActiveMonitorsMap.put(id, this.aliasToActiveMonitorsMap.remove(id));
+        if (!this.aliasToInActiveMonitorsMap.containsKey(idOfEvent)) {
+            this.aliasToInActiveMonitorsMap.put(id, this.aliasToActiveMonitorsMap.remove(idOfEvent));
         }
 
-        Monitor monitor = this.aliasToInActiveMonitorsMap.get(id);
+        Monitor monitor = this.aliasToInActiveMonitorsMap.get(idOfEvent);
         for (Monitor monitor1 : monitor.getAliasToActiveMonitorsMap().values()) {
             if (monitor.hasMonitors()) {
                 StatusEventPublisher.sendGroupTerminatingEvent(this.appId, monitor1.getId());
@@ -107,12 +104,12 @@ public abstract class ParentComponentMonitor extends Monitor {
         }
     }
 
-    protected void onChildInActiveEvent() {
+    protected void onChildInActiveEvent(String idOfEvent) {
         List<ApplicationContext> terminationList;
         Monitor monitor;
-        terminationList = this.dependencyTree.getTerminationDependencies(id);
+        terminationList = this.dependencyTree.getTerminationDependencies(idOfEvent);
         //Temporarily move the group/cluster to inactive list
-        this.aliasToInActiveMonitorsMap.put(id, this.aliasToActiveMonitorsMap.remove(id));
+        this.aliasToInActiveMonitorsMap.put(idOfEvent, this.aliasToActiveMonitorsMap.remove(idOfEvent));
 
         if (terminationList != null) {
             //Checking the termination dependents status
@@ -131,20 +128,25 @@ public abstract class ParentComponentMonitor extends Monitor {
                 }
             }
         } else {
-            log.warn("Wrong inActive event received from [Child] " + id + "  to the [parent]"
+            log.warn("Wrong inActive event received from [Child] " + idOfEvent + "  to the [parent]"
                     + " where child is identified as a independent");
         }
     }
 
-    protected void onChildTerminatedEvent() {
+    protected void onChildTerminatedEvent(String idOfEvent) {
         List<ApplicationContext> terminationList;
         boolean allDependentTerminated = true;
-        terminationList = this.dependencyTree.getTerminationDependencies(id);
+
+        ApplicationContext context = this.dependencyTree.findApplicationContextWithId(idOfEvent);
+        context.setTerminated(true);
+
+        terminationList = this.dependencyTree.getTerminationDependencies(idOfEvent);
+        /**
+         * Make sure that all the dependents have been terminated properly to start the recovery
+         */
         if (terminationList != null) {
             for (ApplicationContext context1 : terminationList) {
                 if (this.aliasToInActiveMonitorsMap.containsKey(context1.getId())) {
-                    log.info("Waiting for the [Parent Monitor] " + context1.getId()
-                            + " to be terminated");
                     allDependentTerminated = false;
                 } else if (this.aliasToActiveMonitorsMap.containsKey(context1.getId())) {
                     log.warn("Dependent [monitor] " + context1.getId() + " not in the correct state");
@@ -153,40 +155,44 @@ public abstract class ParentComponentMonitor extends Monitor {
                     allDependentTerminated = true;
                 }
             }
+        }
 
-            if (allDependentTerminated) {
-
-            }
-        } else {
-            List<ApplicationContext> parentContexts = this.dependencyTree.findAllParentContextWithId(id);
-            boolean canStart = false;
-            if (parentContexts != null) {
-                for (ApplicationContext context1 : parentContexts) {
-                    if (this.aliasToInActiveMonitorsMap.containsKey(context1.getId())) {
-                        log.info("Waiting for the [Parent Monitor] " + context1.getId()
-                                + " to be terminated");
-                        canStart = false;
-                    } else if (this.aliasToActiveMonitorsMap.containsKey(context1.getId())) {
-                        if (canStart) {
-                            log.warn("Found the Dependent [monitor] " + context1.getId()
-                                    + " in the active list wrong state");
-                        }
-                    } else {
-                        log.info("[Parent Monitor] " + context1.getId()
-                                + " has already been terminated");
-                        canStart = true;
+        List<ApplicationContext> parentContexts = this.dependencyTree.findAllParentContextWithId(idOfEvent);
+        boolean canStart = false;
+        if (parentContexts != null) {
+            for (ApplicationContext context1 : parentContexts) {
+                if (this.aliasToInActiveMonitorsMap.containsKey(context1.getId())) {
+                    log.info("Waiting for the [Parent Monitor] " + context1.getId()
+                            + " to be terminated");
+                    canStart = false;
+                } else if (this.aliasToActiveMonitorsMap.containsKey(context1.getId())) {
+                    if (canStart) {
+                        log.warn("Found the Dependent [monitor] " + context1.getId()
+                                + " in the active list wrong state");
                     }
+                } else {
+                    log.info("[Parent Monitor] " + context1.getId()
+                            + " has already been terminated");
+                    canStart = true;
                 }
+            }
+        }
 
-                if (canStart) {
-                    //start the monitor
-                }
+        if((terminationList != null && allDependentTerminated || terminationList == null) &&
+                (parentContexts != null && canStart || parentContexts == null)) {
+            //Find the non existent monitor by traversing dependency tree
 
-            } else {
-                //Start the monitor
+            try {
+                this.startDependency();
+            } catch (TopologyInConsistentException e) {
+                e.printStackTrace();
             }
-
+        } else {
+            log.info("Waiting for the dependent of [monitor] " + idOfEvent
+                    + " to be terminated");
         }
+
+
     }
 
 
@@ -203,6 +209,19 @@ public abstract class ParentComponentMonitor extends Monitor {
     }
 
     /**
+     * This will start the parallel dependencies at once from the top level
+     * by traversing to find the terminated dependencies.
+     * it will get invoked when start a child monitor on termination of a sub tree
+     */
+    public void startDependencyOnTermination() throws TopologyInConsistentException {
+        //start the first dependency which went to terminated
+        List<ApplicationContext> applicationContexts = this.dependencyTree.
+                                                            getStarAbleDependenciesByTermination();
+        startDependency(applicationContexts);
+
+    }
+
+    /**
      * This will get invoked based on the activation event of its one of the child
      *
      * @param id alias/clusterId of which receive the activated event
@@ -229,7 +248,7 @@ public abstract class ParentComponentMonitor extends Monitor {
             if (log.isDebugEnabled()) {
                 log.debug("Dependency check for the Group " + context.getId() + " started");
             }
-            if(!this.aliasToActiveMonitorsMap.containsKey(context.getId())) {
+            if (!this.aliasToActiveMonitorsMap.containsKey(context.getId())) {
                 //to avoid if it is already started
                 startMonitor(this, context);
             }

http://git-wip-us.apache.org/repos/asf/stratos/blob/4904878d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
index c587115..b7db30b 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
@@ -160,18 +160,22 @@ public class ApplicationMonitor extends ParentComponentMonitor {
         LifeCycleState status1 = statusEvent.getStatus();
         //Events coming from parent are In_Active(in faulty detection), Scaling events, termination
         if (status1 == ClusterStatus.Active || status1 == GroupStatus.Active) {
-            onChildActivatedEvent(statusEvent);
+            onChildActivatedEvent(id);
+
         } else if (status1 == ClusterStatus.Inactive || status1 == GroupStatus.Inactive) {
-            onChildInActiveEvent();
+            onChildInActiveEvent(id);
             //TODO update the status of the Application as in_active when child becomes in_active
+
         } else if (status1 == ClusterStatus.Terminating || status1 == GroupStatus.Terminating) {
-            onChildTerminatingEvent();
+            onChildTerminatingEvent(id);
             StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
+
         } else if (status1 == ClusterStatus.Terminated || status1 == GroupStatus.Terminated) {
             //Check whether all dependent goes Terminated and then start them in parallel.
             this.aliasToInActiveMonitorsMap.remove(id);
-            if (this.status != ApplicationStatus.Terminating) {
-                onChildTerminatedEvent();
+            if (this.status != ApplicationStatus.Terminating && !this.aliasToInActiveMonitorsMap.isEmpty() &&
+                    !this.aliasToActiveMonitorsMap.isEmpty()) {
+                onChildTerminatedEvent(id);
             } else {
                 StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
                 log.info("Executing the un-subscription request for the [monitor] " + id);

http://git-wip-us.apache.org/repos/asf/stratos/blob/4904878d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
index 1b01ed0..14c53cd 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
@@ -22,9 +22,9 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.autoscaler.exception.DependencyBuilderException;
 import org.apache.stratos.autoscaler.exception.TopologyInConsistentException;
-import org.apache.stratos.autoscaler.grouping.dependency.context.ApplicationContext;
-import org.apache.stratos.autoscaler.grouping.topic.StatusEventPublisher;
-import org.apache.stratos.autoscaler.monitor.*;
+import org.apache.stratos.autoscaler.monitor.EventHandler;
+import org.apache.stratos.autoscaler.monitor.MonitorStatusEventBuilder;
+import org.apache.stratos.autoscaler.monitor.ParentComponentMonitor;
 import org.apache.stratos.autoscaler.monitor.events.MonitorScalingEvent;
 import org.apache.stratos.autoscaler.monitor.events.MonitorStatusEvent;
 import org.apache.stratos.autoscaler.monitor.events.MonitorTerminateAllEvent;
@@ -34,8 +34,6 @@ import org.apache.stratos.messaging.domain.topology.Group;
 import org.apache.stratos.messaging.domain.topology.GroupStatus;
 import org.apache.stratos.messaging.domain.topology.lifecycle.LifeCycleState;
 
-import java.util.List;
-
 /**
  * This is GroupMonitor to monitor the group which consists of
  * groups and clusters
@@ -82,19 +80,23 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
         LifeCycleState status1 = statusEvent.getStatus();
         //Events coming from parent are In_Active(in faulty detection), Scaling events, termination
         if (status1 == ClusterStatus.Active || status1 == GroupStatus.Active) {
-            onChildActivatedEvent(statusEvent);
+            onChildActivatedEvent(id);
+
         } else if (status1 == ClusterStatus.Inactive || status1 == GroupStatus.Inactive) {
-            onChildInActiveEvent();
+            onChildInActiveEvent(id);
             //To update the status of the Group
             StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
+
         } else if (status1 == ClusterStatus.Terminating || status1 == GroupStatus.Terminating) {
-            onChildTerminatingEvent();
+            onChildTerminatingEvent(id);
             StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
+
         } else if (status1 == ClusterStatus.Terminated || status1 == GroupStatus.Terminated) {
             //Check whether all dependent goes Terminated and then start them in parallel.
             this.aliasToInActiveMonitorsMap.remove(id);
-            if (this.status != GroupStatus.Terminating) {
-                onChildTerminatedEvent();
+            if (this.status != GroupStatus.Terminating && !this.aliasToInActiveMonitorsMap.isEmpty() &&
+                                !this.aliasToActiveMonitorsMap.isEmpty()) {
+                onChildTerminatedEvent(id);
             } else {
                 StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
                 log.info("Executing the un-subscription request for the [monitor] " + id);