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/11/21 12:06:42 UTC

[1/3] stratos git commit: adding contains api for instanceContexts

Repository: stratos
Updated Branches:
  refs/heads/master ee57e3174 -> be729245e


adding contains api for instanceContexts


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

Branch: refs/heads/master
Commit: be729245ee11eaa75c817fb83a86017ab8a019ca
Parents: 6f5e650
Author: reka <rt...@gmail.com>
Authored: Fri Nov 21 16:36:19 2014 +0530
Committer: reka <rt...@gmail.com>
Committed: Fri Nov 21 16:36:29 2014 +0530

----------------------------------------------------------------------
 .../messaging/domain/applications/ParentComponent.java    | 10 ++++++++++
 1 file changed, 10 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/be729245/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/applications/ParentComponent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/applications/ParentComponent.java b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/applications/ParentComponent.java
index 45e1366..c3831c2 100644
--- a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/applications/ParentComponent.java
+++ b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/domain/applications/ParentComponent.java
@@ -225,6 +225,16 @@ public abstract class ParentComponent<T extends InstanceContext> implements Seri
     }
 
     /**
+     * Adds InstanceContext of a child to the instanceIdToInstanceContextMap.
+     *
+     * @param instanceId instance id of child
+     */
+    public boolean containsInstanceContext (String instanceId) {
+        return instanceIdToInstanceContextMap.containsKey(instanceId);
+    }
+
+
+    /**
      * Retrieves InstanceContext obj. for the given instance id
      *
      * @param instanceId instance id


[2/3] stratos git commit: adding instance id to the events and monitors

Posted by re...@apache.org.
adding instance id to the events and monitors


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

Branch: refs/heads/master
Commit: 6f5e650d205762549154b9fb0a635cffd5e1d44d
Parents: 081116d
Author: reka <rt...@gmail.com>
Authored: Fri Nov 21 16:35:50 2014 +0530
Committer: reka <rt...@gmail.com>
Committed: Fri Nov 21 16:36:29 2014 +0530

----------------------------------------------------------------------
 .../applications/topic/ApplicationBuilder.java  | 49 +++++++++++---------
 .../publisher/ClusterStatusEventPublisher.java  |  2 +-
 .../AutoscalerTopologyEventReceiver.java        |  2 +-
 .../autoscaler/monitor/EventHandler.java        |  1 -
 .../monitor/ParentComponentMonitor.java         | 31 ++++++-------
 .../monitor/application/ApplicationMonitor.java |  5 +-
 .../monitor/cluster/AbstractClusterMonitor.java |  1 -
 .../cluster/VMServiceClusterMonitor.java        |  3 +-
 .../autoscaler/monitor/events/MonitorEvent.java |  3 ++
 .../autoscaler/monitor/group/GroupMonitor.java  | 16 +++----
 .../status/checker/StatusChecker.java           | 12 ++---
 11 files changed, 66 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java
index d45a62f..707ed23 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java
@@ -28,6 +28,8 @@ import org.apache.stratos.autoscaler.event.publisher.ClusterStatusEventPublisher
 import org.apache.stratos.autoscaler.monitor.application.ApplicationMonitor;
 import org.apache.stratos.autoscaler.monitor.group.GroupMonitor;
 import org.apache.stratos.messaging.domain.applications.*;
+import org.apache.stratos.messaging.domain.applications.scaling.instance.context.ApplicationInstanceContext;
+import org.apache.stratos.messaging.domain.applications.scaling.instance.context.GroupInstanceContext;
 
 import java.util.Collection;
 import java.util.Set;
@@ -88,18 +90,20 @@ public class ApplicationBuilder {
             return;
         }
 
-        ApplicationStatus status = ApplicationStatus.Active;
+        ApplicationStatus status = ApplicationStatus.Created;
+
 
-        //TODO create new instanceConext***************
-        if (application.isStateTransitionValid(status)) {
+        if (!application.containsInstanceContext(instanceId)) {
             //setting the status, persist and publish
-            application.setStatus(status);
-            updateApplicationMonitor(appId, status);
+            ApplicationInstanceContext context = new ApplicationInstanceContext(appId, instanceId);
+            context.setStatus(status);
+            application.addInstanceContext(instanceId, context);
+            //updateApplicationMonitor(appId, status);
             ApplicationHolder.persistApplication(application);
-            ApplicationsEventPublisher.sendApplicationActivatedEvent(appId);
+            //ApplicationsEventPublisher.sendApplicationActivatedEvent(appId);
         } else {
-            log.warn(String.format("Application state transition is not valid: [application-id] %s " +
-                    " [current-status] %s [status-requested] %s", appId, application.getStatus(), status));
+            log.warn(String.format("Application Instance Context already exists" +
+                    " [appId] %s [ApplicationInstanceId] %s", appId, instanceId));
         }
     }
 
@@ -170,7 +174,8 @@ public class ApplicationBuilder {
         for (ClusterDataHolder aClusterData : clusterData){
             if (AutoscalerContext.getInstance().getClusterMonitor(aClusterData.getClusterId()) == null) {
                 ClusterStatusEventPublisher.sendClusterTerminatingEvent(appId, aClusterData.getServiceType(),
-                        aClusterData.getClusterId());
+                        aClusterData.getClusterId(), null);
+                                                        //TODO add instanceID******
             }
         }
     }
@@ -240,7 +245,7 @@ public class ApplicationBuilder {
         }
     }
 
-    public static void handleGroupTerminatedEvent(String appId, String groupId) {
+    public static void handleGroupTerminatedEvent(String appId, String groupId, String instanceId) {
         if (log.isDebugEnabled()) {
             log.debug("Handling group terminated event: [group-id] " + groupId +
                     " [application-id] " + appId);
@@ -276,7 +281,7 @@ public class ApplicationBuilder {
 
     }
 
-    public static void handleGroupActivatedEvent(String appId, String groupId) {
+    public static void handleGroupActivatedEvent(String appId, String groupId, String instanceId) {
         if (log.isDebugEnabled()) {
             log.debug("Handling group activation for the [group-id]: " + groupId +
                     " in the [application-id] " + appId);
@@ -311,7 +316,7 @@ public class ApplicationBuilder {
         }
     }
 
-    public static void handleGroupCreatedEvent(String appId, String groupId) {
+    public static void handleGroupCreatedEvent(String appId, String groupId, String instanceId) {
         if (log.isDebugEnabled()) {
             log.debug("Handling Group creation for the [group]: " + groupId +
                     " in the [application] " + appId);
@@ -369,21 +374,23 @@ public class ApplicationBuilder {
         }
 
         GroupStatus status = GroupStatus.Created;
-        group.getInstanceContexts(instanceId);
-        if (group.isStateTransitionValid(status)) {
+
+        if (!group.containsInstanceContext(instanceId)) {
             //setting the status, persist and publish
-            group.setStatus(status);
-            updateGroupMonitor(appId, groupId, status);
+            GroupInstanceContext context = new GroupInstanceContext(groupId, instanceId);
+            context.setStatus(status);
+            group.addInstanceContext(instanceId, context);
+            //updateGroupMonitor(appId, groupId, status);
             ApplicationHolder.persistApplication(application);
-            ApplicationsEventPublisher.sendGroupCreatedEvent(appId, groupId);
+            //ApplicationsEventPublisher.sendGroupCreatedEvent(appId, groupId);
         } else {
-            log.warn("Group state transition is not valid: [group-id] " + groupId + " [current-state] " + group.getStatus()
-                    + "[requested-state] " + status);
+            log.warn("Group Instance Context already exists: [group-id] " + groupId +
+                                    " [Group-Instance-Id] " + instanceId);
         }
     }
 
 
-    public static void handleGroupInActivateEvent(String appId, String groupId) {
+    public static void handleGroupInActivateEvent(String appId, String groupId, String instanceId) {
         if (log.isDebugEnabled()) {
             log.debug("Handling group in-active event: [group]: " + groupId +
                     " [application-id] " + appId);
@@ -418,7 +425,7 @@ public class ApplicationBuilder {
         }
     }
 
-    public static void handleGroupTerminatingEvent(String appId, String groupId) {
+    public static void handleGroupTerminatingEvent(String appId, String groupId, String instanceId) {
         if (log.isDebugEnabled()) {
             log.debug("Handling group terminating: [group-id] " + groupId +
                     " [application-id] " + appId);

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/publisher/ClusterStatusEventPublisher.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/publisher/ClusterStatusEventPublisher.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/publisher/ClusterStatusEventPublisher.java
index f230cd4..23312f5 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/publisher/ClusterStatusEventPublisher.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/publisher/ClusterStatusEventPublisher.java
@@ -126,7 +126,7 @@ public class ClusterStatusEventPublisher {
         }
     }
 
-    public static void sendClusterTerminatingEvent(String appId, String serviceName, String clusterId) {
+    public static void sendClusterTerminatingEvent(String appId, String serviceName, String clusterId, String instanceId) {
 
         try {
             TopologyManager.acquireReadLockForCluster(serviceName, clusterId);

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java
index bd36dd9..a8306c2 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java
@@ -309,7 +309,7 @@ public class AutoscalerTopologyEventReceiver implements Runnable {
                     // if the cluster monitor is null, assume that its termianted
                     ApplicationMonitor appMonitor = AutoscalerContext.getInstance().getAppMonitor(clusterTerminatedEvent.getAppId());
                     if (appMonitor != null)  {
-                        appMonitor.onChildStatusEvent(new ClusterStatusEvent(ClusterStatus.Terminated, clusterId));
+                        appMonitor.onChildStatusEvent(new ClusterStatusEvent(ClusterStatus.Terminated, clusterId, null));
                     }
                     return;
                 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/EventHandler.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/EventHandler.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/EventHandler.java
index 13fd6b0..5fcbb2c 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/EventHandler.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/EventHandler.java
@@ -20,7 +20,6 @@ package org.apache.stratos.autoscaler.monitor;
 
 import org.apache.stratos.autoscaler.monitor.events.MonitorScalingEvent;
 import org.apache.stratos.autoscaler.monitor.events.MonitorStatusEvent;
-import org.apache.stratos.autoscaler.monitor.events.MonitorTerminateAllEvent;
 
 /**
  * Event Handler to notify the observer/to receive notification

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/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 93415bf..7e16560 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
@@ -34,7 +34,6 @@ import org.apache.stratos.autoscaler.monitor.application.ApplicationMonitorFacto
 import org.apache.stratos.autoscaler.monitor.cluster.AbstractClusterMonitor;
 import org.apache.stratos.autoscaler.monitor.group.GroupMonitor;
 import org.apache.stratos.autoscaler.status.checker.StatusChecker;
-import org.apache.stratos.messaging.domain.applications.GroupStatus;
 import org.apache.stratos.messaging.domain.applications.ParentComponent;
 import org.apache.stratos.messaging.domain.applications.scaling.instance.context.InstanceContext;
 import org.apache.stratos.messaging.domain.topology.ClusterStatus;
@@ -107,10 +106,10 @@ public abstract class ParentComponentMonitor extends Monitor {
      *
      * @param id alias/clusterId of which receive the activated event
      */
-    public boolean startDependency(String id) throws TopologyInConsistentException {
+    public boolean startDependency(String id, String instanceId) throws TopologyInConsistentException {
         List<ApplicationChildContext> applicationContexts = this.startupDependencyTree
                 .getStarAbleDependencies(id);
-        return startDependency(applicationContexts, null);
+        return startDependency(applicationContexts, instanceId);
     }
 
     /**
@@ -173,7 +172,7 @@ public abstract class ParentComponentMonitor extends Monitor {
      *
      * @param eventId parent id of the event which received
      */
-    protected void onChildActivatedEvent(String eventId) {
+    protected void onChildActivatedEvent(String eventId, String instanceId) {
         try {
             //if the activated monitor is in in_active map move it to active map
             if (this.inactiveMonitorsList.contains(eventId)) {
@@ -184,13 +183,13 @@ public abstract class ParentComponentMonitor extends Monitor {
                 this.terminatingMonitorsList.remove(eventId);
             }
 
-            boolean startDep = startDependency(eventId);
+            boolean startDep = startDependency(eventId, instanceId);
             if (log.isDebugEnabled()) {
                 log.debug("started a child: " + startDep + " by the group/cluster: " + eventId);
 
             }
             if (!startDep) {
-                StatusChecker.getInstance().onChildStatusChange(eventId, this.id, this.appId);
+                StatusChecker.getInstance().onChildStatusChange(eventId, this.id, this.appId, instanceId);
             }
         } catch (TopologyInConsistentException e) {
             //TODO revert the siblings and notify parent, change a flag for reverting/un-subscription
@@ -202,13 +201,13 @@ public abstract class ParentComponentMonitor extends Monitor {
     /**
      * @param eventId
      */
-    protected void onChildInactiveEvent(String eventId) {
+    protected void onChildInactiveEvent(String eventId, String instanceId) {
         List<ApplicationChildContext> terminationList;
         Monitor monitor;
         terminationList = this.startupDependencyTree.getTerminationDependencies(eventId);
         //Need to notify the parent about the status  change from Active-->InActive
         if (this.parent != null) {
-            StatusChecker.getInstance().onChildStatusChange(eventId, this.id, this.appId);
+            StatusChecker.getInstance().onChildStatusChange(eventId, this.id, this.appId, instanceId);
         }
         //TODO checking whether terminating them in reverse order,
         // TODO if so can handle it in the parent event.
@@ -221,20 +220,20 @@ public abstract class ParentComponentMonitor extends Monitor {
             //handling the killall scenario
             if (this.parent != null) {
                 //send terminating to the parent. So that it will push terminating to its children
-                ApplicationBuilder.handleGroupTerminatingEvent(this.appId, eventId);
+                ApplicationBuilder.handleGroupTerminatingEvent(this.appId, eventId, instanceId);
             } else {
                 //if it is an application, send terminating event individually for children
-                sendTerminatingEventOnNotification(terminationList, eventId, true);
+                sendTerminatingEventOnNotification(terminationList, eventId, true ,instanceId);
             }
             log.info("The group" + eventId + " has been marked as terminating " +
                     "due to all the children are to be terminated");
         } else {
-            sendTerminatingEventOnNotification(terminationList, eventId, false);
+            sendTerminatingEventOnNotification(terminationList, eventId, false, instanceId);
         }
     }
 
     private void sendTerminatingEventOnNotification(List<ApplicationChildContext> terminationList,
-                                                    String notifier, boolean terminateAll) {
+                                                    String notifier, boolean terminateAll, String instanceId) {
         Monitor monitor;
         //Checking the termination dependents status
         for (ApplicationChildContext terminationContext : terminationList) {
@@ -248,7 +247,7 @@ public abstract class ParentComponentMonitor extends Monitor {
                     //it will be handled by the terminating case of its children
                     if (terminateAll || !notifier.equals(monitor.getId())) {
                         ApplicationBuilder.handleGroupTerminatingEvent(this.appId,
-                                terminationContext.getId());
+                                terminationContext.getId(), instanceId);
                     }
                 } else {
                     if (log.isInfoEnabled()) {
@@ -257,7 +256,7 @@ public abstract class ParentComponentMonitor extends Monitor {
                     }
                     ClusterStatusEventPublisher.sendClusterTerminatingEvent(this.appId,
                             ((AbstractClusterMonitor) monitor).getServiceId(),
-                            terminationContext.getId());
+                            terminationContext.getId(), instanceId);
                 }
             } else {
                 log.warn("The relevant [monitor] " + terminationContext.getId() +
@@ -273,7 +272,7 @@ public abstract class ParentComponentMonitor extends Monitor {
      * @param eventId id of the notifier
      */
 
-    protected void onChildTerminatedEvent(String eventId) {
+    protected void onChildTerminatedEvent(String eventId, String instanceId) {
         List<ApplicationChildContext> terminationList;
         boolean allDependentTerminated = false;
 
@@ -306,7 +305,7 @@ public abstract class ParentComponentMonitor extends Monitor {
                 log.error("Error while starting the monitor upon termination" + e);
             }
         } else {
-            StatusChecker.getInstance().onChildStatusChange(eventId, this.id, this.appId);
+            StatusChecker.getInstance().onChildStatusChange(eventId, this.id, this.appId, instanceId);
             log.info("Checking the status of group/application as no dependent found...");
         }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/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 1ce5cdd..44ae280 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
@@ -109,14 +109,15 @@ public class ApplicationMonitor extends ParentComponentMonitor {
     @Override
     public void onChildStatusEvent(MonitorStatusEvent statusEvent) {
         String id = statusEvent.getId();
+        String instanceId = statusEvent.getInstanceId();
         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(id);
+            onChildActivatedEvent(id, instanceId);
 
         } else if (status1 == ClusterStatus.Inactive || status1 == GroupStatus.Inactive) {
             this.markMonitorAsInactive(id);
-            onChildInactiveEvent(id);
+            onChildInactiveEvent(id, instanceId);
 
         } else if (status1 == ClusterStatus.Terminating || status1 == GroupStatus.Terminating) {
             //mark the child monitor as inActive in the map

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java
index 6101082..eaba67c 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java
@@ -26,7 +26,6 @@ import org.apache.stratos.autoscaler.monitor.Monitor;
 import org.apache.stratos.autoscaler.monitor.MonitorStatusEventBuilder;
 import org.apache.stratos.autoscaler.monitor.events.MonitorScalingEvent;
 import org.apache.stratos.autoscaler.monitor.events.MonitorStatusEvent;
-import org.apache.stratos.autoscaler.monitor.events.MonitorTerminateAllEvent;
 import org.apache.stratos.autoscaler.rule.AutoscalerRuleEvaluator;
 import org.apache.stratos.cloud.controller.stub.pojo.Properties;
 import org.apache.stratos.messaging.domain.topology.ClusterStatus;

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/VMServiceClusterMonitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/VMServiceClusterMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/VMServiceClusterMonitor.java
index f0b0db9..031f716 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/VMServiceClusterMonitor.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/VMServiceClusterMonitor.java
@@ -246,6 +246,7 @@ public class VMServiceClusterMonitor extends VMClusterMonitor {
 
     @Override
     public void onParentStatusEvent(MonitorStatusEvent statusEvent) {
+        String instanceId = statusEvent.getInstanceId();
         // send the ClusterTerminating event
         if (statusEvent.getStatus() == GroupStatus.Terminating || statusEvent.getStatus() ==
                 ApplicationStatus.Terminating) {
@@ -253,7 +254,7 @@ public class VMServiceClusterMonitor extends VMClusterMonitor {
                 log.info("Publishing Cluster terminating event for [application]: " + appId +
                         " [cluster]: " + this.getClusterId());
             }
-            ClusterStatusEventPublisher.sendClusterTerminatingEvent(getAppId(), getServiceId(), getClusterId());
+            ClusterStatusEventPublisher.sendClusterTerminatingEvent(getAppId(), getServiceId(), getClusterId(), instanceId);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java
index c4aff32..65c891a 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java
@@ -32,4 +32,7 @@ public abstract class MonitorEvent {
         return id;
     }
 
+    public String getInstanceId() {
+        return instanceId;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/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 c5a5d4d..121c4dc 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
@@ -32,17 +32,12 @@ import org.apache.stratos.autoscaler.monitor.MonitorStatusEventBuilder;
 import org.apache.stratos.autoscaler.monitor.ParentComponentMonitor;
 import org.apache.stratos.autoscaler.monitor.application.ApplicationMonitor;
 import org.apache.stratos.autoscaler.monitor.events.*;
-import org.apache.stratos.autoscaler.status.checker.StatusChecker;
 import org.apache.stratos.messaging.domain.applications.ApplicationStatus;
 import org.apache.stratos.messaging.domain.applications.Group;
 import org.apache.stratos.messaging.domain.applications.GroupStatus;
-import org.apache.stratos.messaging.domain.applications.ParentComponent;
-import org.apache.stratos.messaging.domain.applications.scaling.instance.context.InstanceContext;
 import org.apache.stratos.messaging.domain.topology.ClusterStatus;
 import org.apache.stratos.messaging.domain.topology.lifecycle.LifeCycleState;
 
-import java.util.Map;
-
 /**
  * This is GroupMonitor to monitor the group which consists of
  * groups and clusters
@@ -101,14 +96,15 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
     @Override
     public void onChildStatusEvent(MonitorStatusEvent statusEvent) {
         String id = statusEvent.getId();
+        String instanceId = statusEvent.getInstanceId();
         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(id);
+            onChildActivatedEvent(id, instanceId);
 
         } else if (status1 == ClusterStatus.Inactive || status1 == GroupStatus.Inactive) {
             this.markMonitorAsInactive(id);
-            onChildInactiveEvent(id);
+            onChildInactiveEvent(id, instanceId);
 
         } else if (status1 == ClusterStatus.Created || status1 == GroupStatus.Created) {
             if (this.terminatingMonitorsList.contains(id)) {
@@ -148,13 +144,15 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
 
     @Override
     public void onParentStatusEvent(MonitorStatusEvent statusEvent) {
+        String instanceId = statusEvent.getInstanceId();
         // send the ClusterTerminating event
         if (statusEvent.getStatus() == GroupStatus.Terminating ||
                 statusEvent.getStatus() == ApplicationStatus.Terminating) {
-            ApplicationBuilder.handleGroupTerminatingEvent(appId, id);
+            ApplicationBuilder.handleGroupTerminatingEvent(appId, id, instanceId);
         } if(statusEvent.getStatus() == ClusterStatus.Created ||
                                             statusEvent.getStatus() == GroupStatus.Created) {
-            ApplicationBuilder.handleGroupInstanceCreatedEvent(this.appId, this.id,null );
+            ApplicationBuilder.handleGroupInstanceCreatedEvent(this.appId, this.id, instanceId);
+            //TODO start dependencies*******
         }
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/6f5e650d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
index 73f9b27..97dfed8 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
@@ -278,7 +278,7 @@ public class StatusChecker {
      * @param idOfComponent id of the component to which calculate the status
      * @param idOfChild     children of the component as groups
      */
-    public void onChildStatusChange(String idOfChild, String idOfComponent, String appId) {
+    public void onChildStatusChange(String idOfChild, String idOfComponent, String appId, String instanceId) {
         ParentComponent component;
         Map<String, Group> groups;
         Map<String, ClusterDataHolder> clusterData;
@@ -317,7 +317,7 @@ public class StatusChecker {
                     //send activation to the parent
                     if (((Group) component).getStatus(null) != GroupStatus.Active) {
                         log.info("sending group activate: " + component.getUniqueIdentifier());
-                        ApplicationBuilder.handleGroupActivatedEvent(appId, component.getUniqueIdentifier());
+                        ApplicationBuilder.handleGroupActivatedEvent(appId, component.getUniqueIdentifier(), instanceId);
                     }
                 }
             } else if (groups.isEmpty() && getAllClusterInSameState(clusterData, ClusterStatus.Terminated) ||
@@ -332,7 +332,7 @@ public class StatusChecker {
                     //send activation to the parent
                     if (((Group) component).getStatus(null) != GroupStatus.Terminated) {
                         log.info("sending group terminated : " + component.getUniqueIdentifier());
-                        ApplicationBuilder.handleGroupTerminatedEvent(appId, component.getUniqueIdentifier());
+                        ApplicationBuilder.handleGroupTerminatedEvent(appId, component.getUniqueIdentifier(), instanceId);
                     }
                 }
             } else if (groups.isEmpty() && getAllClusterInSameState(clusterData, ClusterStatus.Created) ||
@@ -346,7 +346,7 @@ public class StatusChecker {
                     //send activation to the parent
                     if (((Group) component).getStatus(null) != GroupStatus.Created) {
                         log.info("sending group created : " + component.getUniqueIdentifier());
-                        ApplicationBuilder.handleGroupCreatedEvent(appId, component.getUniqueIdentifier());
+                        ApplicationBuilder.handleGroupCreatedEvent(appId, component.getUniqueIdentifier(), instanceId);
                     }
                 }
             } else if (groups.isEmpty() && getAllClusterInactive(clusterData) ||
@@ -361,7 +361,7 @@ public class StatusChecker {
                     //send activation to the parent
                     if (((Group) component).getStatus(null) != GroupStatus.Inactive) {
                         log.info("sending group in-active: " + component.getUniqueIdentifier());
-                        ApplicationBuilder.handleGroupInActivateEvent(appId, component.getUniqueIdentifier());
+                        ApplicationBuilder.handleGroupInActivateEvent(appId, component.getUniqueIdentifier(), instanceId);
                     }
                 }
             } else {
@@ -373,7 +373,7 @@ public class StatusChecker {
                     //send activation to the parent
                     if (((Group) component).getStatus(null) != GroupStatus.Inactive) {
                         log.info("sending group in-active: " + component.getUniqueIdentifier());
-                        ApplicationBuilder.handleGroupInActivateEvent(appId, component.getUniqueIdentifier());
+                        ApplicationBuilder.handleGroupInActivateEvent(appId, component.getUniqueIdentifier(), "test*****");
                     }
                 }
             }


[3/3] stratos git commit: adding instance id generation and isolating the dynamic data from monitors

Posted by re...@apache.org.
adding instance id generation and isolating the dynamic data from monitors


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

Branch: refs/heads/master
Commit: 081116de0ada9a88e537a427c2dfb48322b9d04c
Parents: ee57e31
Author: reka <rt...@gmail.com>
Authored: Fri Nov 21 16:00:57 2014 +0530
Committer: reka <rt...@gmail.com>
Committed: Fri Nov 21 16:36:29 2014 +0530

----------------------------------------------------------------------
 .../applications/topic/ApplicationBuilder.java  | 74 ++++++++++++++++++--
 .../AutoscalerTopologyEventReceiver.java        |  4 +-
 .../stratos/autoscaler/monitor/Monitor.java     | 10 +--
 .../monitor/MonitorStatusEventBuilder.java      |  8 +--
 .../monitor/ParentComponentMonitor.java         | 68 ++++++++++++++----
 .../monitor/application/ApplicationMonitor.java | 73 +++++--------------
 .../application/ApplicationMonitorFactory.java  | 38 ++++++----
 .../monitor/events/ApplicationStatusEvent.java  |  4 +-
 .../monitor/events/ClusterStatusEvent.java      |  4 +-
 .../monitor/events/GroupStatusEvent.java        |  4 +-
 .../monitor/events/GroupTerminateAllEvent.java  | 29 --------
 .../autoscaler/monitor/events/MonitorEvent.java |  5 +-
 .../monitor/events/MonitorScalingEvent.java     |  4 +-
 .../monitor/events/MonitorStartAllEvent.java    | 28 --------
 .../monitor/events/MonitorStatusEvent.java      |  4 +-
 .../events/MonitorTerminateAllEvent.java        | 29 --------
 .../autoscaler/monitor/group/GroupMonitor.java  | 55 ++++++++++-----
 17 files changed, 228 insertions(+), 213 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java
index de45f62..d45a62f 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/applications/topic/ApplicationBuilder.java
@@ -74,6 +74,35 @@ public class ApplicationBuilder {
         ApplicationsEventPublisher.sendApplicationCreatedEvent(application);
     }
 
+    public static void handleApplicationInstanceCreatedEvent(String appId, String instanceId) {
+        if (log.isDebugEnabled()) {
+            log.debug("Handling application activation event: [application-id] " + appId);
+        }
+
+        Applications applications = ApplicationHolder.getApplications();
+        Application application = applications.getApplication(appId);
+        //update the status of the Group
+        if (application == null) {
+            log.warn(String.format("Application does not exist: [application-id] %s",
+                    appId));
+            return;
+        }
+
+        ApplicationStatus status = ApplicationStatus.Active;
+
+        //TODO create new instanceConext***************
+        if (application.isStateTransitionValid(status)) {
+            //setting the status, persist and publish
+            application.setStatus(status);
+            updateApplicationMonitor(appId, status);
+            ApplicationHolder.persistApplication(application);
+            ApplicationsEventPublisher.sendApplicationActivatedEvent(appId);
+        } else {
+            log.warn(String.format("Application state transition is not valid: [application-id] %s " +
+                    " [current-status] %s [status-requested] %s", appId, application.getStatus(), status));
+        }
+    }
+
     public static void handleApplicationActivatedEvent(String appId) {
         if (log.isDebugEnabled()) {
             log.debug("Handling application activation event: [application-id] " + appId);
@@ -317,6 +346,43 @@ public class ApplicationBuilder {
         }
     }
 
+    public static void handleGroupInstanceCreatedEvent(String appId, String groupId, String instanceId) {
+        if (log.isDebugEnabled()) {
+            log.debug("Handling Group creation for the [group]: " + groupId +
+                    " in the [application] " + appId);
+        }
+
+        Applications applications = ApplicationHolder.getApplications();
+        Application application = applications.getApplication(appId);
+        //update the status of the Group
+        if (application == null) {
+            log.warn(String.format("Application %s does not exist",
+                    appId));
+            return;
+        }
+
+        Group group = application.getGroupRecursively(groupId);
+        if (group == null) {
+            log.warn(String.format("Group %s does not exist",
+                    groupId));
+            return;
+        }
+
+        GroupStatus status = GroupStatus.Created;
+        group.getInstanceContexts(instanceId);
+        if (group.isStateTransitionValid(status)) {
+            //setting the status, persist and publish
+            group.setStatus(status);
+            updateGroupMonitor(appId, groupId, status);
+            ApplicationHolder.persistApplication(application);
+            ApplicationsEventPublisher.sendGroupCreatedEvent(appId, groupId);
+        } else {
+            log.warn("Group state transition is not valid: [group-id] " + groupId + " [current-state] " + group.getStatus()
+                    + "[requested-state] " + status);
+        }
+    }
+
+
     public static void handleGroupInActivateEvent(String appId, String groupId) {
         if (log.isDebugEnabled()) {
             log.debug("Handling group in-active event: [group]: " + groupId +
@@ -412,9 +478,7 @@ public class ApplicationBuilder {
         //Updating the Application Monitor
         ApplicationMonitor applicationMonitor = AutoscalerContext.getInstance().getAppMonitor(appId);
         if (applicationMonitor != null) {
-            if(applicationMonitor.getStatus() != status) {
-                applicationMonitor.setStatus(status);
-            }
+            applicationMonitor.setStatus(status, null);
         } else {
             log.warn("Application monitor cannot be found: [application-id] " + appId);
         }
@@ -427,9 +491,7 @@ public class ApplicationBuilder {
         if (applicationMonitor != null) {
             GroupMonitor monitor = (GroupMonitor) applicationMonitor.findGroupMonitorWithId(groupId);
             if (monitor != null ) {
-                if(monitor.getStatus() != status) {
-                    monitor.setStatus(status);
-                }
+                monitor.setStatus(status, null);
             } else {
                 log.warn("Group monitor cannot be found: [group-id] " + groupId +
                         " [application-id] " + appId);

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java
index 5296d75..bd36dd9 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/event/receiver/topology/AutoscalerTopologyEventReceiver.java
@@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.autoscaler.AutoscalerContext;
 import org.apache.stratos.autoscaler.applications.ApplicationHolder;
+import org.apache.stratos.autoscaler.applications.topic.ApplicationBuilder;
 import org.apache.stratos.autoscaler.event.publisher.ClusterStatusEventPublisher;
 import org.apache.stratos.autoscaler.event.publisher.InstanceNotificationPublisher;
 import org.apache.stratos.autoscaler.exception.DependencyBuilderException;
@@ -204,7 +205,7 @@ public class AutoscalerTopologyEventReceiver implements Runnable {
                     return;
                 }
                 //changing the status in the monitor, will notify its parent monitor
-                monitor.setStatus(ClusterStatus.Active);
+
             }
         });
 
@@ -472,7 +473,6 @@ public class AutoscalerTopologyEventReceiver implements Runnable {
                                 appId);
                     }
                     applicationMonitor = ApplicationMonitorFactory.getApplicationMonitor(appId);
-
                     long end = System.currentTimeMillis();
                     log.info("Time taken to start app monitor: " + (end - start) / 1000);
                     success = true;

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
index 30b015d..31276c1 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
@@ -31,7 +31,7 @@ public abstract class Monitor implements EventHandler {
     //has startup dependents
     protected boolean hasStartupDependents;
     //has scaling dependents
-    protected boolean isGroupScalingEnabled;
+    protected boolean hasGroupScalingDependent;
 
     /**
      * Return the id of the monitor
@@ -102,8 +102,8 @@ public abstract class Monitor implements EventHandler {
      *
      * @return startup dependencies exist or not
      */
-    public boolean isGroupScalingEnabled() {
-        return isGroupScalingEnabled;
+    public boolean isHasGroupScalingDependent() {
+        return hasGroupScalingDependent;
     }
 
     /**
@@ -120,7 +120,7 @@ public abstract class Monitor implements EventHandler {
      *
      * @param hasDependent
      */
-    public void setGroupScalingEnabled(boolean hasDependent) {
-        this.isGroupScalingEnabled = hasDependent;
+    public void setHasGroupScalingDependent(boolean hasDependent) {
+        this.hasGroupScalingDependent = hasDependent;
     }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
index 5ee4204..08e15a7 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
@@ -32,24 +32,24 @@ public class MonitorStatusEventBuilder {
     private static final Log log = LogFactory.getLog(MonitorStatusEventBuilder.class);
 
     public static void handleClusterStatusEvent(ParentComponentMonitor parent, ClusterStatus status, String clusterId) {
-        ClusterStatusEvent clusterStatusEvent = new ClusterStatusEvent(status, clusterId);
+        ClusterStatusEvent clusterStatusEvent = new ClusterStatusEvent(status, clusterId, null);
         notifyParent(parent, clusterStatusEvent);
     }
 
     public static void handleGroupStatusEvent(ParentComponentMonitor parent, GroupStatus status, String groupId) {
-        GroupStatusEvent groupStatusEvent = new GroupStatusEvent(status, groupId);
+        GroupStatusEvent groupStatusEvent = new GroupStatusEvent(status, groupId, null);
         notifyParent(parent, groupStatusEvent);
     }
 
     public static void handleApplicationStatusEvent(ParentComponentMonitor parent, ApplicationStatus status, String appId) {
-        ApplicationStatusEvent applicationStatusEvent = new ApplicationStatusEvent(status, appId);
+        ApplicationStatusEvent applicationStatusEvent = new ApplicationStatusEvent(status, appId, null);
         notifyParent(parent, applicationStatusEvent);
     }
 
     public static void handleClusterScalingEvent(ParentComponentMonitor parent, String networkPartitionId, float factor, String appId) {
 
         //Send notifications to parent of the cluster monitor
-        MonitorScalingEvent monitorScalingEvent = new MonitorScalingEvent(appId, networkPartitionId, factor) ;
+        MonitorScalingEvent monitorScalingEvent = new MonitorScalingEvent(appId, networkPartitionId, null,factor) ;
         notifyParent(parent, monitorScalingEvent);
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/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 bc33c55..93415bf 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
@@ -36,6 +36,7 @@ import org.apache.stratos.autoscaler.monitor.group.GroupMonitor;
 import org.apache.stratos.autoscaler.status.checker.StatusChecker;
 import org.apache.stratos.messaging.domain.applications.GroupStatus;
 import org.apache.stratos.messaging.domain.applications.ParentComponent;
+import org.apache.stratos.messaging.domain.applications.scaling.instance.context.InstanceContext;
 import org.apache.stratos.messaging.domain.topology.ClusterStatus;
 import org.apache.stratos.messaging.message.receiver.topology.TopologyManager;
 
@@ -57,6 +58,8 @@ public abstract class ParentComponentMonitor extends Monitor {
     protected DependencyTree scalingDependencyTree;
     //monitors map, key=GroupAlias/clusterId and value=GroupMonitor/AbstractClusterMonitor
     protected Map<String, Monitor> aliasToActiveMonitorsMap;
+    //instances, key=instance id and value=instanceContext
+    protected Map<String, InstanceContext> instanceIdToInstanceContextMap;
     //monitors map, stopped monitors
     protected List<String> inactiveMonitorsList;
     //terminating monitors list
@@ -65,7 +68,8 @@ public abstract class ParentComponentMonitor extends Monitor {
     public ParentComponentMonitor(ParentComponent component) throws DependencyBuilderException {
         aliasToActiveMonitorsMap = new HashMap<String, Monitor>();
         inactiveMonitorsList = new ArrayList<String>();
-        setTerminatingMonitorsList(new ArrayList<String>());
+        terminatingMonitorsList = new ArrayList<String>();
+        setInstanceIdToInstanceContextMap(new HashMap<String, InstanceContext>());
         //clusterIdToClusterMonitorsMap = new HashMap<String, AbstractClusterMonitor>();
         this.id = component.getUniqueIdentifier();
         //Building the startup dependencies for this monitor within the immediate children
@@ -78,11 +82,23 @@ public abstract class ParentComponentMonitor extends Monitor {
      * This will start the parallel dependencies at once from the top level.
      * it will get invoked when the monitor starts up only.
      */
-    public void startDependency() throws TopologyInConsistentException {
+    public void startDependency(ParentComponent component, String instanceId) throws TopologyInConsistentException {
         //start the first dependency
         List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
                 getStarAbleDependencies();
-        startDependency(applicationContexts);
+        startDependency(applicationContexts, instanceId);
+
+    }
+
+    /**
+     * This will start the parallel dependencies at once from the top level.
+     * it will get invoked when the monitor starts up only.
+     */
+    public void startDependency(ParentComponent component) throws TopologyInConsistentException {
+        //start the first dependency
+        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
+                getStarAbleDependencies();
+        startDependency(applicationContexts, null);
 
     }
 
@@ -94,7 +110,7 @@ public abstract class ParentComponentMonitor extends Monitor {
     public boolean startDependency(String id) throws TopologyInConsistentException {
         List<ApplicationChildContext> applicationContexts = this.startupDependencyTree
                 .getStarAbleDependencies(id);
-        return startDependency(applicationContexts);
+        return startDependency(applicationContexts, null);
     }
 
     /**
@@ -106,7 +122,7 @@ public abstract class ParentComponentMonitor extends Monitor {
         //start the first dependency which went to terminated
         List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
                 getStarAbleDependenciesByTermination();
-        startDependency(applicationContexts);
+        startDependency(applicationContexts, null);
 
     }
 
@@ -116,7 +132,7 @@ public abstract class ParentComponentMonitor extends Monitor {
      *
      * @param applicationContexts the found applicationContexts to be started
      */
-    private boolean startDependency(List<ApplicationChildContext> applicationContexts)
+    private boolean startDependency(List<ApplicationChildContext> applicationContexts, String instanceId)
             throws TopologyInConsistentException {
         if (applicationContexts != null && applicationContexts.isEmpty()) {
             //all the groups/clusters have been started and waiting for activation
@@ -131,7 +147,14 @@ public abstract class ParentComponentMonitor extends Monitor {
             if (!this.aliasToActiveMonitorsMap.containsKey(context.getId())) {
                 //to avoid if it is already started
                 context.setTerminated(false);
-                startMonitor(this, context);
+                startMonitor(this, context, instanceId);
+            } else {
+                if(!this.instanceIdToInstanceContextMap.containsKey(instanceId)) {
+                    Monitor monitor = this.aliasToActiveMonitorsMap.get(context.getId());
+                    //monitor.onParentStatusEvent();
+                    //ask to start the next snapshot for the monitor
+                    //TODO notify the relevant children
+                }
             }
         }
 
@@ -139,6 +162,12 @@ public abstract class ParentComponentMonitor extends Monitor {
 
     }
 
+    protected String generateInstanceId(ParentComponent component) {
+        String instanceId = component.getUniqueIdentifier() + "_" +
+                (component.getInstanceContextCount() + 1);
+        return instanceId;
+    }
+
     /**
      * This will start the child monitors based on the active of siblings according to start up order
      *
@@ -341,10 +370,11 @@ public abstract class ParentComponentMonitor extends Monitor {
                     GroupMonitor monitor1 = (GroupMonitor) monitor;
                     ApplicationHolder.acquireReadLock();
                     try {
-                        if (monitor1.getStatus() == GroupStatus.Active) {
+                        //TODO***********
+                        /*if (monitor1.getStatus() == GroupStatus.Active) {
                             parentsActive = true;
 
-                        }
+                        }*/
                     } finally {
                         ApplicationHolder.releaseReadLock();
                     }
@@ -385,11 +415,11 @@ public abstract class ParentComponentMonitor extends Monitor {
         }
     }
 
-    protected synchronized void startMonitor(ParentComponentMonitor parent, ApplicationChildContext context) {
+    protected synchronized void startMonitor(ParentComponentMonitor parent, ApplicationChildContext context, String instanceId) {
         Thread th = null;
         if (!this.aliasToActiveMonitorsMap.containsKey(context.getId())) {
             th = new Thread(
-                    new MonitorAdder(parent, context, this.appId));
+                    new MonitorAdder(parent, context, this.appId, instanceId));
             if (log.isDebugEnabled()) {
                 log.debug(String
                         .format("Monitor Adder has been added: [cluster] %s ",
@@ -404,15 +434,26 @@ public abstract class ParentComponentMonitor extends Monitor {
         }
     }
 
+    public Map<String, InstanceContext> getInstanceIdToInstanceContextMap() {
+        return instanceIdToInstanceContextMap;
+    }
+
+    public void setInstanceIdToInstanceContextMap(Map<String, InstanceContext> instanceIdToInstanceContextMap) {
+        this.instanceIdToInstanceContextMap = instanceIdToInstanceContextMap;
+    }
+
     private class MonitorAdder implements Runnable {
         private ApplicationChildContext context;
         private ParentComponentMonitor parent;
         private String appId;
+        private String instanceId;
 
-        public MonitorAdder(ParentComponentMonitor parent, ApplicationChildContext context, String appId) {
+        public MonitorAdder(ParentComponentMonitor parent, ApplicationChildContext context,
+                                                            String instanceId, String appId) {
             this.parent = parent;
             this.context = context;
             this.appId = appId;
+            this.instanceId = instanceId;
         }
 
         public void run() {
@@ -430,7 +471,7 @@ public abstract class ParentComponentMonitor extends Monitor {
                             + context.getId());
                 }
                 try {
-                    monitor = ApplicationMonitorFactory.getMonitor(parent, context, appId);
+                    monitor = ApplicationMonitorFactory.getMonitor(parent, context, appId, instanceId);
                 } catch (DependencyBuilderException e) {
                     String msg = "Monitor creation failed for: " + context.getId();
                     log.warn(msg, e);
@@ -462,6 +503,7 @@ public abstract class ParentComponentMonitor extends Monitor {
                 throw new RuntimeException(msg);
             }
             aliasToActiveMonitorsMap.put(context.getId(), monitor);
+            // ApplicationBuilder.
             if (log.isInfoEnabled()) {
                 log.info(String.format("Monitor has been added successfully for: %s",
                         context.getId()));

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/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 a90bc12..1ce5cdd 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
@@ -20,6 +20,7 @@ package org.apache.stratos.autoscaler.monitor.application;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.autoscaler.applications.topic.ApplicationBuilder;
 import org.apache.stratos.autoscaler.exception.DependencyBuilderException;
 import org.apache.stratos.autoscaler.exception.TopologyInConsistentException;
 import org.apache.stratos.autoscaler.monitor.Monitor;
@@ -31,6 +32,7 @@ import org.apache.stratos.autoscaler.status.checker.StatusChecker;
 import org.apache.stratos.messaging.domain.applications.Application;
 import org.apache.stratos.messaging.domain.applications.ApplicationStatus;
 import org.apache.stratos.messaging.domain.applications.GroupStatus;
+import org.apache.stratos.messaging.domain.applications.ParentComponent;
 import org.apache.stratos.messaging.domain.topology.ClusterStatus;
 import org.apache.stratos.messaging.domain.topology.lifecycle.LifeCycleState;
 
@@ -41,61 +43,23 @@ import java.util.Collection;
  */
 public class ApplicationMonitor extends ParentComponentMonitor {
     private static final Log log = LogFactory.getLog(ApplicationMonitor.class);
-    //status of the monitor whether it is running/in_maintainable/terminated
-    private ApplicationStatus status;
 
     public ApplicationMonitor(Application application) throws DependencyBuilderException,
             TopologyInConsistentException {
         super(application);
         //setting the appId for the application
         this.appId = application.getUniqueIdentifier();
-        this.status = application.getStatus(null);
         //starting the first set of dependencies from its children
-        startDependency();
-
-    }
-
-    /**
-     * To find all the clusters of an application
-     *
-     * @param appId the application which contains the clusters
-     * @return all the clusters of the application
-     */
-//    public List<String> findClustersOfApplication(String appId) {
-//        List<String> clusters = new ArrayList<String>();
-//        Set<ClusterDataHolder> clusterData;
-//
-//        TopologyManager.acquireReadLockForApplication(appId);
-//        try {
-//            clusterData = TopologyManager.getTopology().getApplication(appId).getClusterDataRecursively();
-//
-//        } finally {
-//            TopologyManager.releaseReadLockForApplication(appId);
-//        }
-//
-//        if (clusterData != null) {
-//            for (ClusterDataHolder clusterDataHolder : clusterData) {
-//                clusters.add(clusterDataHolder.getClusterId());
-//            }
-//        }
-//
-//        return clusters;
-//    }
-
-    /**
-     * Find the cluster monitor by traversing recursively in the hierarchical monitors.
-     *
-     * @param clusterId cluster id of the monitor to be searched
-     * @return the found cluster monitor
-     */
-    public AbstractClusterMonitor findClusterMonitorWithId(String clusterId) {
-        /*return findClusterMonitor(clusterId, clusterIdToClusterMonitorsMap.values(),
-                aliasToActiveMonitorsMap.values());*/
-        return null;
+        if(application.getInstanceContextCount() > 0) {
+            startDependency(application);
+        } else {
+            String instanceId = this.generateInstanceId(application);
+            startDependency(application, instanceId);
+            ApplicationBuilder.handleApplicationInstanceCreatedEvent(appId, instanceId);
 
+        }
     }
 
-
     /**
      * Find the group monitor by traversing recursively in the hierarchical monitors.
      *
@@ -137,10 +101,9 @@ public class ApplicationMonitor extends ParentComponentMonitor {
      *
      * @param status the status
      */
-    public void setStatus(ApplicationStatus status) {
-        this.status = status;
+    public void setStatus(ApplicationStatus status, String instanceId) {
         //notify the children about the state change
-        MonitorStatusEventBuilder.notifyChildren(this, new ApplicationStatusEvent(status, appId));
+        MonitorStatusEventBuilder.notifyChildren(this, new ApplicationStatusEvent(status, appId, null));
     }
 
     @Override
@@ -164,11 +127,12 @@ public class ApplicationMonitor extends ParentComponentMonitor {
                 this.terminatingMonitorsList.remove(id);
                 this.aliasToActiveMonitorsMap.remove(id);
             }
-            if (this.status == ApplicationStatus.Terminating) {
+            //TODO
+            /*if (this.status == ApplicationStatus.Terminating) {
                 StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
             } else {
                 onChildTerminatedEvent(id);
-            }
+            }*/
 
         } else if (status1 == ClusterStatus.Terminated || status1 == GroupStatus.Terminated) {
             //Check whether all dependent goes Terminated and then start them in parallel.
@@ -178,10 +142,11 @@ public class ApplicationMonitor extends ParentComponentMonitor {
             } else {
                 log.warn("[monitor] " + id + " cannot be found in the inActive monitors list");
             }
-            if (this.status == ApplicationStatus.Terminating || this.status == ApplicationStatus.Terminated) {
+            //TODO
+            /*if (this.status == ApplicationStatus.Terminating || this.status == ApplicationStatus.Terminated) {
                 StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
                 log.info("Executing the un-subscription request for the [monitor] " + id);
-            }
+            }*/
         }
     }
 
@@ -204,8 +169,4 @@ public class ApplicationMonitor extends ParentComponentMonitor {
     public void onEvent(MonitorScalingEvent scalingEvent) {
 
     }
-
-    public ApplicationStatus getStatus() {
-        return status;
-    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitorFactory.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitorFactory.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitorFactory.java
index cb8a4fa..45a6a36 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitorFactory.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitorFactory.java
@@ -57,15 +57,16 @@ public class ApplicationMonitorFactory {
      * @throws PolicyValidationException     throws while validating the policy associated with cluster
      * @throws PartitionValidationException  throws while validating the partition used in a cluster
      */
-    public static Monitor getMonitor(ParentComponentMonitor parentMonitor, ApplicationChildContext context, String appId)
+    public static Monitor getMonitor(ParentComponentMonitor parentMonitor,
+                                     ApplicationChildContext context, String appId, String instanceId)
             throws TopologyInConsistentException,
             DependencyBuilderException, PolicyValidationException, PartitionValidationException {
     	
         Monitor monitor;
         if (context instanceof GroupChildContext) {
-            monitor = getGroupMonitor(parentMonitor, context, appId);
+            monitor = getGroupMonitor(parentMonitor, context, appId, instanceId);
         } else if (context instanceof ClusterChildContext) {
-            monitor = getClusterMonitor(parentMonitor, (ClusterChildContext) context, appId);
+            monitor = getClusterMonitor(parentMonitor, (ClusterChildContext) context, appId, instanceId);
             if (monitor != null) {
             	((AbstractClusterMonitor)monitor).startScheduler();
             	AutoscalerContext.getInstance().addClusterMonitor((AbstractClusterMonitor)monitor);
@@ -86,7 +87,8 @@ public class ApplicationMonitorFactory {
      * @throws DependencyBuilderException    throws while building dependency for app monitor
      * @throws TopologyInConsistentException throws while traversing thr topology
      */
-    public static Monitor getGroupMonitor(ParentComponentMonitor parentMonitor, ApplicationChildContext context, String appId)
+    public static Monitor getGroupMonitor(ParentComponentMonitor parentMonitor,
+                                          ApplicationChildContext context, String appId, String instanceId)
             throws DependencyBuilderException,
             TopologyInConsistentException {
         GroupMonitor groupMonitor;
@@ -95,7 +97,7 @@ public class ApplicationMonitorFactory {
         try {
             Group group = ApplicationHolder.getApplications().
                     getApplication(appId).getGroupRecursively(context.getId());
-            groupMonitor = new GroupMonitor(group, appId);
+            groupMonitor = new GroupMonitor(group, appId, instanceId);
             groupMonitor.setAppId(appId);
             if (parentMonitor != null) {
                 groupMonitor.setParent(parentMonitor);
@@ -105,13 +107,22 @@ public class ApplicationMonitorFactory {
                 } else {
                     groupMonitor.setHasStartupDependents(false);
                 }
-                //TODO make sure when it is async
 
-                if (group.getStatus(null) != groupMonitor.getStatus()) {
+                if(group.isGroupScalingEnabled()) {
+                    groupMonitor.setGroupScalingEnabled(true);
+                } else if(parentMonitor instanceof GroupMonitor) {
+                    if(((GroupMonitor)parentMonitor).isGroupScalingEnabled() ||
+                            parentMonitor.isHasGroupScalingDependent()) {
+                        groupMonitor.setHasGroupScalingDependent(true);
+                    }
+                }
+                //TODO*********** make it sync with the topology
+
+                /*if (group.getStatus() != groupMonitor.getStatus()) {
                     //updating the status, if the group is not in created state when creating group Monitor
                     //so that groupMonitor will notify the parent (useful when restarting stratos)
-                    groupMonitor.setStatus(group.getStatus(null));
-                }
+                    groupMonitor.setStatus(group.getStatus());
+                }*/
             }
 
         } finally {
@@ -165,7 +176,8 @@ public class ApplicationMonitorFactory {
      * @throws org.apache.stratos.autoscaler.exception.PartitionValidationException
      */
     public static AbstractClusterMonitor getClusterMonitor(ParentComponentMonitor parentMonitor,
-                                                     ClusterChildContext context, String appId)
+                                                            ClusterChildContext context,
+                                                            String appId, String instanceId)
             throws PolicyValidationException,
             PartitionValidationException,
             TopologyInConsistentException {
@@ -204,10 +216,10 @@ public class ApplicationMonitorFactory {
             }
 
             //setting the scaling dependent behaviour of the cluster monitor
-            if(parentMonitor.isGroupScalingEnabled() || (context.isGroupScalingEnabled())) {
-                clusterMonitor.setGroupScalingEnabled(true);
+            if(parentMonitor.isHasGroupScalingDependent() || (context.isGroupScalingEnabled())) {
+                clusterMonitor.setHasGroupScalingDependent(true);
             } else {
-                clusterMonitor.setGroupScalingEnabled(false);
+                clusterMonitor.setHasGroupScalingDependent(false);
             }
             //setting the status of the cluster, if it doesn't match with Topology cluster status.
             if (cluster.getStatus() != clusterMonitor.getStatus()) {

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ApplicationStatusEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ApplicationStatusEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ApplicationStatusEvent.java
index 83c764d..566c9c2 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ApplicationStatusEvent.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ApplicationStatusEvent.java
@@ -27,8 +27,8 @@ public class ApplicationStatusEvent extends MonitorStatusEvent {
 
     private ApplicationStatus status;
 
-    public ApplicationStatusEvent(ApplicationStatus status, String id) {
-        super(id);
+    public ApplicationStatusEvent(ApplicationStatus status, String id, String instanceId) {
+        super(id, instanceId);
         this.status = status;
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ClusterStatusEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ClusterStatusEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ClusterStatusEvent.java
index 58c59ba..1eca48a 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ClusterStatusEvent.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/ClusterStatusEvent.java
@@ -27,8 +27,8 @@ public class ClusterStatusEvent extends MonitorStatusEvent {
 
     private ClusterStatus status;
 
-    public ClusterStatusEvent(ClusterStatus status, String id) {
-        super(id);
+    public ClusterStatusEvent(ClusterStatus status, String id, String instanceId) {
+        super(id , instanceId);
         this.status = status;
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupStatusEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupStatusEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupStatusEvent.java
index 7f21761..50c297b 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupStatusEvent.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupStatusEvent.java
@@ -26,8 +26,8 @@ import org.apache.stratos.messaging.domain.applications.GroupStatus;
 public class GroupStatusEvent extends MonitorStatusEvent {
     private GroupStatus status;
 
-    public GroupStatusEvent(GroupStatus status, String id) {
-        super(id);
+    public GroupStatusEvent(GroupStatus status, String id, String instanceId) {
+        super(id, instanceId);
         this.status = status;
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupTerminateAllEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupTerminateAllEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupTerminateAllEvent.java
deleted file mode 100644
index 0fdf9e3..0000000
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/GroupTerminateAllEvent.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.stratos.autoscaler.monitor.events;
-
-/**
- * This will handle the termination of all groups/clusters of a group.
- */
-public class GroupTerminateAllEvent extends MonitorTerminateAllEvent {
-
-    public GroupTerminateAllEvent(String id) {
-        super(id);
-    }
-}

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java
index 47229a1..c4aff32 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorEvent.java
@@ -21,8 +21,11 @@ package org.apache.stratos.autoscaler.monitor.events;
 public abstract class MonitorEvent {
     protected String id;
 
-    public MonitorEvent(String id) {
+    protected String instanceId;
+
+    public MonitorEvent(String id , String instanceId) {
         this.id = id;
+        this.instanceId = instanceId;
     }
 
     public String getId(){

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorScalingEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorScalingEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorScalingEvent.java
index 53b076b..504d7c2 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorScalingEvent.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorScalingEvent.java
@@ -26,8 +26,8 @@ public class MonitorScalingEvent extends MonitorEvent {
     private float factor;
     private String networkPartitionId;
 
-    public  MonitorScalingEvent(String id, String networkPartitionId, float factor) {
-        super(id);
+    public  MonitorScalingEvent(String id, String networkPartitionId, String instanceId, float factor) {
+        super(id, instanceId);
         this.factor = factor;
         this.networkPartitionId = networkPartitionId;
     }

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStartAllEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStartAllEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStartAllEvent.java
deleted file mode 100644
index 21f83ec..0000000
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStartAllEvent.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.stratos.autoscaler.monitor.events;
-
-/**
- * This will use to start the child monitors
- */
-public class MonitorStartAllEvent extends MonitorEvent {
-    public MonitorStartAllEvent(String id) {
-        super(id);
-    }
-}

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStatusEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStatusEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStatusEvent.java
index 7d2f480..f4ba558 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStatusEvent.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorStatusEvent.java
@@ -25,8 +25,8 @@ import org.apache.stratos.messaging.domain.topology.lifecycle.LifeCycleState;
  */
 public abstract class MonitorStatusEvent extends MonitorEvent {
 
-    public MonitorStatusEvent(String id) {
-        super(id);
+    public MonitorStatusEvent(String id, String instanceId) {
+        super(id , instanceId);
     }
 
     public String getId() {

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorTerminateAllEvent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorTerminateAllEvent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorTerminateAllEvent.java
deleted file mode 100644
index 86efc7e..0000000
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/events/MonitorTerminateAllEvent.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.stratos.autoscaler.monitor.events;
-
-/**
- * This will be used to terminate all the instances of the cluster/group.
- */
-public class MonitorTerminateAllEvent extends MonitorEvent {
-
-    public MonitorTerminateAllEvent(String id) {
-        super(id);
-    }
-}

http://git-wip-us.apache.org/repos/asf/stratos/blob/081116de/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 d6d293e..c5a5d4d 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
@@ -36,17 +36,21 @@ import org.apache.stratos.autoscaler.status.checker.StatusChecker;
 import org.apache.stratos.messaging.domain.applications.ApplicationStatus;
 import org.apache.stratos.messaging.domain.applications.Group;
 import org.apache.stratos.messaging.domain.applications.GroupStatus;
+import org.apache.stratos.messaging.domain.applications.ParentComponent;
+import org.apache.stratos.messaging.domain.applications.scaling.instance.context.InstanceContext;
 import org.apache.stratos.messaging.domain.topology.ClusterStatus;
 import org.apache.stratos.messaging.domain.topology.lifecycle.LifeCycleState;
 
+import java.util.Map;
+
 /**
  * This is GroupMonitor to monitor the group which consists of
  * groups and clusters
  */
 public class GroupMonitor extends ParentComponentMonitor implements EventHandler {
     private static final Log log = LogFactory.getLog(GroupMonitor.class);
-    //status of the monitor whether it is running/in_maintainable/terminated
-    private GroupStatus status;
+    //whether groupScaling enabled or not
+    private boolean groupScalingEnabled;
 
     /**
      * Constructor of GroupMonitor
@@ -55,12 +59,21 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
      * @throws DependencyBuilderException    throws when couldn't build the Topology
      * @throws TopologyInConsistentException throws when topology is inconsistent
      */
-    public GroupMonitor(Group group, String appId) throws DependencyBuilderException,
+    public GroupMonitor(Group group, String appId, String instanceId) throws DependencyBuilderException,
             TopologyInConsistentException {
         super(group);
         this.appId = appId;
-        this.status = group.getStatus(null);
-        startDependency();
+        if(group.getInstanceContextCount() > 0) {
+            this.startDependency(group);
+        } else {
+            String instanceId1 = instanceId;
+            if (group.isGroupScalingEnabled()) {
+                instanceId1 = this.generateInstanceId(group);
+            }
+            this.startDependency(group, instanceId1);
+            ApplicationBuilder.handleGroupInstanceCreatedEvent(appId, group.getUniqueIdentifier(), instanceId1);
+
+        }
     }
 
     /**
@@ -68,10 +81,10 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
      *
      * @param status status of the group
      */
-    public void setStatus(GroupStatus status) {
+    public void setStatus(GroupStatus status, String instanceId) {
 
         //if(this.status != status) {
-        this.status = status;
+        //this.status = status;
         //notifying the parent
         if (status == GroupStatus.Inactive && !this.hasStartupDependents) {
             log.info("[Group] " + this.id + "is not notifying the parent, " +
@@ -79,10 +92,10 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
         } else {
             // notify parent
             log.info("[Group] " + this.id + "is notifying the [parent] " + this.parent.getId());
-            MonitorStatusEventBuilder.handleGroupStatusEvent(this.parent, this.status, this.id);
+            MonitorStatusEventBuilder.handleGroupStatusEvent(this.parent, status, this.id);
         }
         //notify the children about the state change
-        MonitorStatusEventBuilder.notifyChildren(this, new GroupStatusEvent(status, getId()));
+        MonitorStatusEventBuilder.notifyChildren(this, new GroupStatusEvent(status, getId(), null));
     }
 
     @Override
@@ -106,11 +119,12 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
                 }
             }
             //If cluster monitor, need to terminate the existing one
-            if (this.status == GroupStatus.Terminating) {
+            //TODO block
+            /*if (this.status == GroupStatus.Terminating) {
                 StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
             } else {
                 onChildTerminatedEvent(id);
-            }
+            }*/
 
         } else if (status1 == ClusterStatus.Terminating || status1 == GroupStatus.Terminating) {
             //mark the child monitor as inActive in the map
@@ -124,19 +138,23 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
             } else {
                 log.warn("[monitor] " + id + " cannot be found in the inActive monitors list");
             }
-            if (this.status == GroupStatus.Terminating || this.status == GroupStatus.Terminated) {
+            //TODO block
+            /*if (this.status == GroupStatus.Terminating || this.status == GroupStatus.Terminated) {
                 StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId);
                 log.info("Executing the un-subscription request for the [monitor] " + id);
-            }
+            }*/
         }
     }
 
     @Override
     public void onParentStatusEvent(MonitorStatusEvent statusEvent) {
         // send the ClusterTerminating event
-        if (statusEvent.getStatus() == GroupStatus.Terminating || statusEvent.getStatus() ==
-                ApplicationStatus.Terminating) {
+        if (statusEvent.getStatus() == GroupStatus.Terminating ||
+                statusEvent.getStatus() == ApplicationStatus.Terminating) {
             ApplicationBuilder.handleGroupTerminatingEvent(appId, id);
+        } if(statusEvent.getStatus() == ClusterStatus.Created ||
+                                            statusEvent.getStatus() == GroupStatus.Created) {
+            ApplicationBuilder.handleGroupInstanceCreatedEvent(this.appId, this.id,null );
         }
     }
 
@@ -173,8 +191,11 @@ public class GroupMonitor extends ParentComponentMonitor implements EventHandler
 
     }
 
-    public GroupStatus getStatus() {
-        return status;
+    public boolean isGroupScalingEnabled() {
+        return groupScalingEnabled;
     }
 
+    public void setGroupScalingEnabled(boolean groupScalingEnabled) {
+        this.groupScalingEnabled = groupScalingEnabled;
+    }
 }