You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ga...@apache.org on 2015/11/02 16:31:18 UTC

[37/50] [abbrv] stratos git commit: Revert "Merge with tenant-isolation branch"

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/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 7dd33c5..912a2f6 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
@@ -30,6 +30,7 @@ import org.apache.stratos.autoscaler.event.publisher.ClusterStatusEventPublisher
 import org.apache.stratos.autoscaler.monitor.Monitor;
 import org.apache.stratos.autoscaler.monitor.component.ApplicationMonitor;
 import org.apache.stratos.autoscaler.monitor.component.GroupMonitor;
+import org.apache.stratos.autoscaler.registry.RegistryManager;
 import org.apache.stratos.messaging.domain.application.*;
 import org.apache.stratos.messaging.domain.instance.ApplicationInstance;
 import org.apache.stratos.messaging.domain.instance.ClusterInstance;
@@ -79,10 +80,10 @@ public class ApplicationBuilder {
                 appClusterContexts);
     }
 
-    public static ApplicationInstance handleApplicationInstanceCreatedEvent(String appUuid,
-                                                                            String networkPartitionUuid) {
+    public static ApplicationInstance handleApplicationInstanceCreatedEvent(String appId,
+                                                                            String networkPartitionId) {
         if (log.isDebugEnabled()) {
-            log.debug("Handling application instance creation event: [application-id] " + appUuid);
+            log.debug("Handling application instance creation event: [application-id] " + appId);
         }
         ApplicationInstance applicationInstance = null;
         //acquiring write lock to add the required instances
@@ -90,26 +91,26 @@ public class ApplicationBuilder {
         try {
 
             Applications applications = ApplicationHolder.getApplications();
-            Application application = applications.getApplication(appUuid);
+            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",
-                        appUuid));
+                        appId));
                 return null;
             }
-            String instanceId = application.getNextInstanceId(appUuid);
+            String instanceId = application.getNextInstanceId(appId);
 
             if (!application.containsInstanceContext(instanceId)) {
                 //setting the status, persist and publish
-                applicationInstance = new ApplicationInstance(appUuid, instanceId);
-                applicationInstance.setNetworkPartitionUuid(networkPartitionUuid);
+                applicationInstance = new ApplicationInstance(appId, instanceId);
+                applicationInstance.setNetworkPartitionId(networkPartitionId);
                 application.addInstance(instanceId, applicationInstance);
                 //updateApplicationMonitor(appId, status);
                 ApplicationHolder.persistApplication(application);
-                ApplicationsEventPublisher.sendApplicationInstanceCreatedEvent(appUuid, applicationInstance);
+                ApplicationsEventPublisher.sendApplicationInstanceCreatedEvent(appId, applicationInstance);
             } else {
                 log.warn(String.format("Application Instance Context already exists" +
-                        " [appId] %s [ApplicationInstanceId] %s", appUuid, instanceId));
+                        " [appId] %s [ApplicationInstanceId] %s", appId, instanceId));
             }
         } finally {
             ApplicationHolder.releaseWriteLock();
@@ -137,7 +138,7 @@ public class ApplicationBuilder {
         if (applicationInstance.isStateTransitionValid(status)) {
             //setting the status, persist and publish
             application.setStatus(status, instanceId);
-            updateApplicationMonitor(appId, status, applicationInstance.getNetworkPartitionUuid(),
+            updateApplicationMonitor(appId, status, applicationInstance.getNetworkPartitionId(),
                     instanceId);
             ApplicationHolder.persistApplication(application);
             ApplicationsEventPublisher.sendApplicationInstanceActivatedEvent(appId, instanceId);
@@ -168,7 +169,7 @@ public class ApplicationBuilder {
         if (applicationInstance.isStateTransitionValid(status)) {
             //setting the status, persist and publish
             application.setStatus(status, instanceId);
-            updateApplicationMonitor(appId, status, applicationInstance.getNetworkPartitionUuid(),
+            updateApplicationMonitor(appId, status, applicationInstance.getNetworkPartitionId(),
                     instanceId);
             ApplicationHolder.persistApplication(application);
             ApplicationsEventPublisher.sendApplicationInstanceInactivatedEvent(appId, instanceId);
@@ -198,7 +199,7 @@ public class ApplicationBuilder {
         if (applicationInstance.isStateTransitionValid(status)) {
             // setting the status, persist and publish
             application.setStatus(status, instanceId);
-            updateApplicationMonitor(appId, status, applicationInstance.getNetworkPartitionUuid(), instanceId);
+            updateApplicationMonitor(appId, status, applicationInstance.getNetworkPartitionId(), instanceId);
             ApplicationHolder.persistApplication(application);
             ApplicationsEventPublisher.sendApplicationInstanceTerminatingEvent(appId, instanceId);
         } else {
@@ -240,11 +241,12 @@ public class ApplicationBuilder {
             Set<ClusterDataHolder> appClusterData = application.getClusterDataRecursively();
             for (ClusterDataHolder currClusterData : appClusterData) {
                 ClusterDataHolder newClusterData = new ClusterDataHolder(currClusterData.getServiceType(),
-                        currClusterData.getClusterId(), currClusterData.getServiceUuid());
+                        currClusterData.getClusterId());
                 appClusterDataToSend.add(newClusterData);
             }
 
             AutoscalerContext.getInstance().removeApplicationContext(appId);
+            RegistryManager.getInstance().removeApplicationContext(appId);
             ApplicationHolder.removeApplication(appId);
 
         } finally {
@@ -274,14 +276,16 @@ public class ApplicationBuilder {
                 //setting the status, persist and publish
                 applicationInstance.setStatus(status);
                 updateApplicationMonitor(applicationId, status,
-                        applicationInstance.getNetworkPartitionUuid(),
+                        applicationInstance.getNetworkPartitionId(),
                         instanceId);
                 ApplicationMonitor applicationMonitor = AutoscalerContext.getInstance().
                         getAppMonitor(applicationId);
                 NetworkPartitionContext networkPartitionContext = applicationMonitor.
                         getNetworkPartitionContext(applicationInstance.
-                                getNetworkPartitionUuid());
+                                getNetworkPartitionId());
                 networkPartitionContext.removeInstanceContext(instanceId);
+                applicationMonitor.removeNetworkPartitionContext(applicationInstance.
+                        getNetworkPartitionId());
                 applicationMonitor.removeInstance(instanceId);
                 application.removeInstance(instanceId);
                 ApplicationHolder.persistApplication(application);
@@ -302,10 +306,12 @@ public class ApplicationBuilder {
                     // stopping application thread
                     applicationMonitor.destroy();
                     AutoscalerContext.getInstance().removeAppMonitor(applicationId);
-
+                    // Remove network partition algorithm context
+                    AutoscalerContext.getInstance().removeNetworkPartitionAlgorithmContext(applicationId);
                     // update application status in application context
                     applicationContext.setStatus(ApplicationContext.STATUS_CREATED);
                     AutoscalerContext.getInstance().updateApplicationContext(applicationContext);
+                    RegistryManager.getInstance().persistApplicationContext(applicationContext);
 
                     log.info("Application un-deployed successfully: [application-id] " + applicationId);
                 }
@@ -360,7 +366,7 @@ public class ApplicationBuilder {
                                     cluster.getInstanceIdToInstanceContextMap().values()) {
                                 ClusterStatusEventPublisher.
                                         sendClusterStatusClusterTerminatingEvent(applicationId,
-                                                aClusterData.getServiceUuid(),
+                                                aClusterData.getServiceType(),
                                                 aClusterData.getClusterId(),
                                                 instance.getInstanceId());
                             }
@@ -423,7 +429,7 @@ public class ApplicationBuilder {
                     }
                     org.apache.stratos.autoscaler.context.partition.network.NetworkPartitionContext networkPartitionContext =
                             (org.apache.stratos.autoscaler.context.partition.network.NetworkPartitionContext) monitor.
-                                    getNetworkPartitionContext(groupInstance.getNetworkPartitionUuid());
+                                    getNetworkPartitionContext(groupInstance.getNetworkPartitionId());
                     networkPartitionContext.removeInstanceContext(instanceId);
                     if (groupInstance.getPartitionId() != null) {
                         networkPartitionContext.getPartitionCtxt(groupInstance.getPartitionId()).
@@ -476,7 +482,7 @@ public class ApplicationBuilder {
             if (groupInstance.isStateTransitionValid(status)) {
                 //setting the status, persist and publish
                 groupInstance.setStatus(status);
-                updateGroupMonitor(appId, groupId, status, groupInstance.getNetworkPartitionUuid(),
+                updateGroupMonitor(appId, groupId, status, groupInstance.getNetworkPartitionId(),
                         instanceId, groupInstance.getParentId());
                 ApplicationHolder.persistApplication(application);
                 ApplicationsEventPublisher.sendGroupInstanceActivatedEvent(appId, groupId, instanceId);
@@ -534,7 +540,7 @@ public class ApplicationBuilder {
                 groupInstance = new GroupInstance(groupId, instanceId);
                 groupInstance.setParentId(parentId);
                 groupInstance.setPartitionId(partitionId);
-                groupInstance.setNetworkPartitionUuid(networkPartitionId);
+                groupInstance.setNetworkPartitionId(networkPartitionId);
                 groupInstance.setStatus(status);
                 group.addInstance(instanceId, groupInstance);
                 //updateGroupMonitor(appId, groupId, status);
@@ -580,7 +586,7 @@ public class ApplicationBuilder {
             if (groupInstance.isStateTransitionValid(status)) {
                 //setting the status, persist and publish
                 groupInstance.setStatus(status);
-                updateGroupMonitor(appId, groupId, status, groupInstance.getNetworkPartitionUuid(),
+                updateGroupMonitor(appId, groupId, status, groupInstance.getNetworkPartitionId(),
                         instanceId, groupInstance.getParentId());
                 ApplicationHolder.persistApplication(application);
                 ApplicationsEventPublisher.sendGroupInstanceInactivateEvent(appId, groupId, instanceId);
@@ -626,7 +632,7 @@ public class ApplicationBuilder {
                 if (groupInstance.isStateTransitionValid(status)) {
                     //setting the status, persist and publish
                     groupInstance.setStatus(status);
-                    updateGroupMonitor(appId, groupId, status, groupInstance.getNetworkPartitionUuid(),
+                    updateGroupMonitor(appId, groupId, status, groupInstance.getNetworkPartitionId(),
                             instanceId, groupInstance.getParentId());
                     ApplicationHolder.persistApplication(application);
                     ApplicationsEventPublisher.sendGroupInstanceTerminatingEvent(appId,

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/client/AutoscalerCloudControllerClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/client/AutoscalerCloudControllerClient.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/client/AutoscalerCloudControllerClient.java
index 5a9aab2..0124206 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/client/AutoscalerCloudControllerClient.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/client/AutoscalerCloudControllerClient.java
@@ -52,18 +52,6 @@ public class AutoscalerCloudControllerClient {
 
     private static CloudControllerServiceStub stub;
 
-    /* An instance of a CloudControllerClient is created when the class is loaded. 
-     * Since the class is loaded only once, it is guaranteed that an object of 
-     * CloudControllerClient is created only once. Hence it is singleton.
-     */
-    private static class InstanceHolder {
-        private static final AutoscalerCloudControllerClient INSTANCE = new AutoscalerCloudControllerClient();
-    }
-
-    public static AutoscalerCloudControllerClient getInstance() {
-        return InstanceHolder.INSTANCE;
-    }
-
     private AutoscalerCloudControllerClient() {
         try {
             XMLConfiguration conf = ConfUtil.getInstance(null).getConfiguration();
@@ -81,16 +69,19 @@ public class AutoscalerCloudControllerClient {
         }
     }
 
+    public static AutoscalerCloudControllerClient getInstance() {
+        return InstanceHolder.INSTANCE;
+    }
+
     public synchronized MemberContext startInstance(PartitionRef partition,
                                                     String clusterId, String clusterInstanceId,
-                                                    String networkPartitionId, boolean isPrimary,
-                                                    int minMemberCount, String autoscalingReason,
-                                                    long scalingTime) throws SpawningException {
+                                                    String networkPartitionId,
+                                                    int minMemberCount) throws SpawningException {
         try {
             if (log.isInfoEnabled()) {
                 log.info(String.format("Trying to spawn an instance via cloud controller: " +
                                 "[cluster] %s [partition] %s [network-partition-id] %s",
-                        clusterId, partition.getUuid(), networkPartitionId));
+                        clusterId, partition.getId(), networkPartitionId));
             }
 
             XMLConfiguration conf = ConfUtil.getInstance(null).getConfiguration();
@@ -108,26 +99,11 @@ public class AutoscalerCloudControllerClient {
             instanceContext.setNetworkPartitionId(networkPartitionId);
 
             Properties memberContextProps = new Properties();
-            Property isPrimaryProp = new Property();
-            isPrimaryProp.setName("PRIMARY");
-            isPrimaryProp.setValue(String.valueOf(isPrimary));
-
             Property minCountProp = new Property();
             minCountProp.setName(StratosConstants.MIN_COUNT);
             minCountProp.setValue(String.valueOf(minMemberCount));
 
-            Property autoscalingReasonProp = new Property();
-            autoscalingReasonProp.setName(StratosConstants.SCALING_REASON);
-            autoscalingReasonProp.setValue(autoscalingReason);
-
-            Property scalingTimeProp = new Property();
-            scalingTimeProp.setName(StratosConstants.SCALING_TIME);
-            scalingTimeProp.setValue(String.valueOf(scalingTime));
-
-            memberContextProps.addProperty(isPrimaryProp);
             memberContextProps.addProperty(minCountProp);
-            memberContextProps.addProperty(autoscalingReasonProp);
-            memberContextProps.addProperty(scalingTimeProp);
             instanceContext.setProperties(AutoscalerUtil.toStubProperties(memberContextProps));
 
             long startTime = System.currentTimeMillis();
@@ -156,7 +132,7 @@ public class AutoscalerCloudControllerClient {
         }
     }
 
-    public synchronized void createApplicationClusters(String appUuid,
+    public synchronized void createApplicationClusters(String appId,
                                                        ApplicationClusterContext[] applicationClusterContexts) {
         List<org.apache.stratos.cloud.controller.stub.domain.ApplicationClusterContext> contextDTOs =
                 new ArrayList<org.apache.stratos.cloud.controller.stub.domain.ApplicationClusterContext>();
@@ -168,7 +144,7 @@ public class AutoscalerCloudControllerClient {
                     dto.setClusterId(applicationClusterContext.getClusterId());
                     dto.setAutoscalePolicyName(applicationClusterContext.getAutoscalePolicyName());
                     dto.setDeploymentPolicyName(applicationClusterContext.getDeploymentPolicyName());
-                    dto.setCartridgeUuid(applicationClusterContext.getCartridgeUuid());
+                    dto.setCartridgeType(applicationClusterContext.getCartridgeType());
                     dto.setHostName(applicationClusterContext.getHostName());
                     dto.setTenantRange(applicationClusterContext.getTenantRange());
                     dto.setTextPayload(applicationClusterContext.getTextPayload());
@@ -188,7 +164,7 @@ public class AutoscalerCloudControllerClient {
                 new org.apache.stratos.cloud.controller.stub.domain.ApplicationClusterContext[contextDTOs.size()];
         contextDTOs.toArray(applicationClusterContextDTOs);
         try {
-            stub.createApplicationClusters(appUuid, applicationClusterContextDTOs);
+            stub.createApplicationClusters(appId, applicationClusterContextDTOs);
         } catch (RemoteException e) {
             String msg = e.getMessage();
             log.error(msg, e);
@@ -198,7 +174,6 @@ public class AutoscalerCloudControllerClient {
         }
     }
 
-
     private Volume[] convertVolumesToStubVolumes(VolumeContext[] volumeContexts) {
 
         ArrayList<Volume> volumes = new ArrayList<Volume>();
@@ -239,8 +214,7 @@ public class AutoscalerCloudControllerClient {
     public void terminateAllInstances(String clusterId) throws RemoteException,
             CloudControllerServiceInvalidClusterExceptionException {
         if (log.isInfoEnabled()) {
-            log.info(String.format("Terminating all instances of cluster via cloud controller: " +
-                    "[cluster] %s", clusterId));
+            log.info(String.format("Terminating all instances of cluster via cloud controller: [cluster] %s", clusterId));
         }
         long startTime = System.currentTimeMillis();
         stub.terminateInstances(clusterId);
@@ -251,4 +225,11 @@ public class AutoscalerCloudControllerClient {
         }
     }
 
- }
+    /* An instance of a CloudControllerClient is created when the class is loaded.
+     * Since the class is loaded only once, it is guaranteed that an object of
+     * CloudControllerClient is created only once. Hence it is singleton.
+     */
+    private static class InstanceHolder {
+        private static final AutoscalerCloudControllerClient INSTANCE = new AutoscalerCloudControllerClient();
+    }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/AutoscalerContext.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/AutoscalerContext.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/AutoscalerContext.java
index 8cbf6c1..0af9b56 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/AutoscalerContext.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/AutoscalerContext.java
@@ -153,12 +153,10 @@ public class AutoscalerContext {
     }
 
     public void addApplicationContext(ApplicationContext applicationContext) {
-        applicationContextMap.put(applicationContext.getApplicationUuid(), applicationContext);
-        RegistryManager.getInstance().persistApplicationContext(applicationContext);
+        applicationContextMap.put(applicationContext.getApplicationId(), applicationContext);
     }
 
     public ApplicationContext removeApplicationContext(String applicationId) {
-        RegistryManager.getInstance().removeApplicationContext(applicationId);
         return applicationContextMap.remove(applicationId);
     }
 
@@ -166,23 +164,12 @@ public class AutoscalerContext {
         return applicationContextMap.get(applicationId);
     }
 
-    public ApplicationContext getApplicationContextByTenant(String applicationId, int tenantId) {
-        ApplicationContext applicationContext = null;
-        for (ApplicationContext applicationContext1 : getApplicationContexts()) {
-            if (applicationContext1.getTenantId() == tenantId && applicationContext1.getApplicationId().equals
-                    (applicationId)) {
-                applicationContext = applicationContext1;
-            }
-        }
-        return applicationContext;
-    }
-
     public Collection<ApplicationContext> getApplicationContexts() {
         return applicationContextMap.values();
     }
 
     public void updateApplicationContext(ApplicationContext applicationContext) {
-        applicationContextMap.put(applicationContext.getApplicationUuid(), applicationContext);
+        applicationContextMap.put(applicationContext.getApplicationId(), applicationContext);
         RegistryManager.getInstance().persistApplicationContext(applicationContext);
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContext.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContext.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContext.java
index b1e3bd4..235b7b7 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContext.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContext.java
@@ -120,16 +120,16 @@ public class ClusterContext extends AbstractClusterContext {
         DeploymentPolicy deploymentPolicy = PolicyManager.getInstance().
                 getDeploymentPolicy(deploymentPolicyName);
 
-        if (networkPartitionCtxts.containsKey(clusterInstance.getNetworkPartitionUuid())) {
+        if (networkPartitionCtxts.containsKey(clusterInstance.getNetworkPartitionId())) {
             networkPartitionContext = this.networkPartitionCtxts.get(
-                    clusterInstance.getNetworkPartitionUuid());
+                    clusterInstance.getNetworkPartitionId());
         } else {
 
             NetworkPartitionRef[] networkPartitions = deploymentPolicy.getNetworkPartitionRefs();
             NetworkPartitionRef networkPartition = null;
             if (networkPartitions != null && networkPartitions.length != 0) {
                 for (NetworkPartitionRef i : networkPartitions) {
-                    if (i.getId().equals(clusterInstance.getNetworkPartitionUuid())) {
+                    if (i.getId().equals(clusterInstance.getNetworkPartitionId())) {
                         networkPartition = i;
                     }
                 }
@@ -138,7 +138,7 @@ public class ClusterContext extends AbstractClusterContext {
             if (networkPartition == null) {
                 //Parent should have the partition specified
                 networkPartitionContext = new NetworkPartitionContext(
-                        clusterInstance.getNetworkPartitionUuid());
+                        clusterInstance.getNetworkPartitionId());
             } else {
                 networkPartitionContext = new NetworkPartitionContext(networkPartition.getId(),
                         networkPartition.getPartitionAlgo());
@@ -153,15 +153,15 @@ public class ClusterContext extends AbstractClusterContext {
             networkPartitionContext = parseDeploymentPolicy(clusterInstance, cluster,
                     networkPartitionContext, hasScalingDependents, groupScalingEnabledSubtree);
         }
-        if (!networkPartitionCtxts.containsKey(clusterInstance.getNetworkPartitionUuid())) {
-            this.networkPartitionCtxts.put(clusterInstance.getNetworkPartitionUuid(),
+        if (!networkPartitionCtxts.containsKey(clusterInstance.getNetworkPartitionId())) {
+            this.networkPartitionCtxts.put(clusterInstance.getNetworkPartitionId(),
                     networkPartitionContext);
             if (log.isInfoEnabled()) {
                 log.info(String.format("Cluster instance context has been added to network partition," +
                                 " [application] %s [cluster] %s  [cluster-instance] %s " +
                                 "[network partition] %s", cluster.getAppId(), cluster.getClusterId(),
                         clusterInstance.getInstanceId(),
-                        clusterInstance.getNetworkPartitionUuid()));
+                        clusterInstance.getNetworkPartitionId()));
             }
         }
 
@@ -185,7 +185,7 @@ public class ClusterContext extends AbstractClusterContext {
         PartitionRef[] partitions = null;
         if (networkPartitions != null && networkPartitions.length != 0) {
             for (NetworkPartitionRef networkPartitionRef : networkPartitions) {
-                if (networkPartitionRef.getUuid().equals(
+                if (networkPartitionRef.getId().equals(
                         clusterLevelNetworkPartitionContext.getId())) {
                     partitions = networkPartitionRef.getPartitionRefs();
                 }
@@ -204,8 +204,8 @@ public class ClusterContext extends AbstractClusterContext {
         NetworkPartitionRef networkPartitionRef = null;
         if (networkPartitions != null && networkPartitions.length != 0) {
             for (NetworkPartitionRef networkPartition2 : networkPartitions) {
-                if (networkPartition2.getUuid().equals(
-                        clusterInstance.getNetworkPartitionUuid())) {
+                if (networkPartition2.getId().equals(
+                        clusterInstance.getNetworkPartitionId())) {
                     networkPartitionRef = networkPartition2;
                 }
             }
@@ -246,7 +246,7 @@ public class ClusterContext extends AbstractClusterContext {
         if (clusterInstance.getPartitionId() == null && partition == null) {
             String msg = "[Partition] " + clusterInstance.getPartitionId() + " for [application] " +
                     cluster.getAppId() + " [networkPartition] " +
-                    clusterInstance.getNetworkPartitionUuid() + "is null " +
+                    clusterInstance.getNetworkPartitionId() + "is null " +
                     "in deployment policy: [cluster-alias]: " + clusterInstance.getAlias();
             log.error(msg);
             throw new PolicyValidationException(msg);
@@ -278,7 +278,7 @@ public class ClusterContext extends AbstractClusterContext {
         String partitionId;
         if (partition != null) {
             //use it own defined partition
-            partitionId = partition.getUuid();
+            partitionId = partition.getId();
             maxInstances = partition.getPartitionMax();
         } else {
             //handling the partition given by the parent
@@ -302,7 +302,7 @@ public class ClusterContext extends AbstractClusterContext {
         PartitionRef partition3 = null;
         if (partitions != null && partitions.length != 0) {
             for (PartitionRef partition2 : partitions) {
-                if (partition2.getUuid().equals(partitionId)) {
+                if (partition2.getId().equals(partitionId)) {
                     partition3 = partition2;
                 }
             }
@@ -311,7 +311,7 @@ public class ClusterContext extends AbstractClusterContext {
         //Creating cluster level partition context
         ClusterLevelPartitionContext clusterLevelPartitionContext = new ClusterLevelPartitionContext(
                 partition3,
-                clusterInstance.getNetworkPartitionUuid(), this.deploymentPolicyId);
+                clusterInstance.getNetworkPartitionId(), this.deploymentPolicyId);
         clusterLevelPartitionContext.setServiceName(cluster.getServiceName());
         clusterLevelPartitionContext.setProperties(cluster.getProperties());
 
@@ -346,7 +346,7 @@ public class ClusterContext extends AbstractClusterContext {
                                         String ClusterInstanceId) {
         for (Member member : cluster.getMembers()) {
             String memberId = member.getMemberId();
-            if (member.getPartitionId().equalsIgnoreCase(partition.getUuid()) &&
+            if (member.getPartitionId().equalsIgnoreCase(partition.getId()) &&
                     member.getClusterInstanceId().equals(ClusterInstanceId)) {
                 MemberContext memberContext = new MemberContext();
                 memberContext.setClusterId(member.getClusterId());

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContextFactory.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContextFactory.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContextFactory.java
index 5637cd3..a8fa9ad 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContextFactory.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/ClusterContextFactory.java
@@ -23,6 +23,8 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.autoscaler.exception.partition.PartitionValidationException;
 import org.apache.stratos.autoscaler.exception.policy.PolicyValidationException;
+import org.apache.stratos.autoscaler.pojo.policy.PolicyManager;
+import org.apache.stratos.autoscaler.pojo.policy.autoscale.AutoscalePolicy;
 import org.apache.stratos.messaging.domain.topology.Cluster;
 
 public class ClusterContextFactory {

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/partition/PartitionContext.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/partition/PartitionContext.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/partition/PartitionContext.java
index c28ea0f..d1f6fae 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/partition/PartitionContext.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/partition/PartitionContext.java
@@ -51,7 +51,7 @@ public abstract class PartitionContext implements Serializable {
     public PartitionContext(PartitionRef partition, String networkPartitionId, String deploymentPolicyId) {
         this.partition = partition;
         this.deploymentPolicyId = deploymentPolicyId;
-        this.partitionId = partition.getUuid();
+        this.partitionId = partition.getId();
         this.networkPartitionId = networkPartitionId;
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/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 56a567e..2573991 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
@@ -132,47 +132,38 @@ public class AutoscalerTopologyEventReceiver {
                     ApplicationClustersCreatedEvent applicationClustersCreatedEvent =
                             (ApplicationClustersCreatedEvent) event;
                     String appId = applicationClustersCreatedEvent.getAppId();
-                    boolean appMonitorCreationTriggered = false;
-                    int retries = 5;
-                    while (!appMonitorCreationTriggered && retries > 0) {
-                        try {
-                            //acquire read lock
-                            ApplicationHolder.acquireReadLock();
-                            //start the application monitor
-                            ApplicationContext applicationContext = AutoscalerContext.getInstance().
-                                    getApplicationContext(appId);
-                            if (applicationContext != null &&
-                                    applicationContext.getStatus().
-                                            equals(ApplicationContext.STATUS_DEPLOYED)) {
-                                if (!AutoscalerContext.getInstance().
-                                        containsApplicationPendingMonitor(appId)) {
-                                    appMonitorCreationTriggered = true;
-                                    AutoscalerUtil.getInstance().startApplicationMonitor(appId);
-                                    break;
-                                }
+                    try {
+                        //acquire read lock
+                        ApplicationHolder.acquireReadLock();
+                        //start the application monitor
+                        ApplicationContext applicationContext = AutoscalerContext.getInstance().
+                                getApplicationContext(appId);
+                        if (applicationContext != null &&
+                                applicationContext.getStatus().
+                                        equals(ApplicationContext.STATUS_DEPLOYED)) {
+                            if (!AutoscalerContext.getInstance().
+                                    containsApplicationPendingMonitor(appId)) {
+                                AutoscalerUtil.getInstance().startApplicationMonitor(appId);
                             }
-                        } catch (Exception e) {
-                            String msg = "Error processing event " + e.getLocalizedMessage();
-                            log.error(msg, e);
-                        } finally {
-                            //release read lock
-                            ApplicationHolder.releaseReadLock();
-                        }
-
-                        try {
-                            retries--;
-                            Thread.sleep(1000);
-                        } catch (InterruptedException e) {
+                        } else {
+                            String status;
+                            if(applicationContext == null) {
+                                status = null;
+                            } else {
+                                status = applicationContext.getStatus();
+                            }
+                            log.error("Error while creating the application monitor due to " +
+                                    "in-consistent persistence of [application] " +
+                                    applicationClustersCreatedEvent.getAppId() + ", " +
+                                    "the [application-context] " + applicationContext +
+                            " status of [application-context] " + status);
                         }
-                    }
-
-                    // Reason is to re-try 5 time is because application status might not become "deployed" yet, refer deployApplication API for more information.
-                    // Reason why not throwing error after 5 times is because this not the only place we trigger app-monitor creation.
-                    if (!appMonitorCreationTriggered) {
-                        String msg = String.format("Application monitor creation is not triggered on application "
-                                + "clusters created event even after 5 retries [application-id] %s. "
-                                + "Possible cause is either application context is null or application status didn't become %s yet.", appId, ApplicationContext.STATUS_DEPLOYED);
-                        log.warn(msg);
+                    } catch (Exception e) {
+                        String msg = "Error processing event " + e.getLocalizedMessage();
+                        log.error(msg, e);
+                    } finally {
+                        //release read lock
+                        ApplicationHolder.releaseReadLock();
                     }
                 } catch (ClassCastException e) {
                     String msg = "Error while casting the event " + e.getLocalizedMessage();
@@ -287,13 +278,13 @@ public class AutoscalerTopologyEventReceiver {
                             sendInstanceCleanupEventForCluster(clusterId, clusterInstanceId);
                     //Terminating the pending members
                     monitor.terminatePendingMembers(clusterInstanceId,
-                            clusterInstance.getNetworkPartitionUuid());
+                            clusterInstance.getNetworkPartitionId());
                     //Move all members to terminating pending list
                     monitor.moveMembersToTerminatingPending(clusterInstanceId,
-                            clusterInstance.getNetworkPartitionUuid());
+                            clusterInstance.getNetworkPartitionId());
                 } else {
                     monitor.notifyParentMonitor(ClusterStatus.Terminating, clusterInstanceId);
-                    monitor.terminateAllMembers(clusterInstanceId, clusterInstance.getNetworkPartitionUuid());
+                    monitor.terminateAllMembers(clusterInstanceId, clusterInstance.getNetworkPartitionId());
                 }
                 ServiceReferenceHolder.getInstance().getClusterStatusProcessorChain().
                         process("", clusterId, clusterInstanceId);
@@ -333,7 +324,7 @@ public class AutoscalerTopologyEventReceiver {
                 //Removing the instance and instanceContext
                 ClusterInstance instance = (ClusterInstance) monitor.getInstance(instanceId);
                 monitor.getClusterContext().
-                        getNetworkPartitionCtxt(instance.getNetworkPartitionUuid()).
+                        getNetworkPartitionCtxt(instance.getNetworkPartitionId()).
                         removeInstanceContext(instanceId);
                 monitor.removeInstance(instanceId);
                 if (!monitor.hasInstance() && appMonitor.isTerminating()) {
@@ -464,12 +455,12 @@ public class AutoscalerTopologyEventReceiver {
                                                        String instanceId = clusterInstance.getInstanceId();
                                                        //FIXME to take lock when clusterMonitor is running
                                                        if (clusterMonitor != null) {
-                                                           TopologyManager.acquireReadLockForCluster(clusterInstanceCreatedEvent.getServiceUuid(),
+                                                           TopologyManager.acquireReadLockForCluster(clusterInstanceCreatedEvent.getServiceName(),
                                                                    clusterInstanceCreatedEvent.getClusterId());
 
                                                            try {
                                                                Service service = TopologyManager.getTopology().
-                                                                       getService(clusterInstanceCreatedEvent.getServiceUuid());
+                                                                       getService(clusterInstanceCreatedEvent.getServiceName());
 
                                                                if (service != null) {
                                                                    Cluster cluster = service.getCluster(clusterInstanceCreatedEvent.getClusterId());
@@ -509,13 +500,13 @@ public class AutoscalerTopologyEventReceiver {
                                                                    }
 
                                                                } else {
-                                                                   log.error("Service " + clusterInstanceCreatedEvent.getServiceUuid() +
+                                                                   log.error("Service " + clusterInstanceCreatedEvent.getServiceName() +
                                                                            " not found, no cluster instance added to ClusterMonitor " +
                                                                            clusterInstanceCreatedEvent.getClusterId());
                                                                }
 
                                                            } finally {
-                                                               TopologyManager.releaseReadLockForCluster(clusterInstanceCreatedEvent.getServiceUuid(),
+                                                               TopologyManager.releaseReadLockForCluster(clusterInstanceCreatedEvent.getServiceName(),
                                                                        clusterInstanceCreatedEvent.getClusterId());
                                                            }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/internal/AutoscalerServiceComponent.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/internal/AutoscalerServiceComponent.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/internal/AutoscalerServiceComponent.java
index 030a8b5..48ee481 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/internal/AutoscalerServiceComponent.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/internal/AutoscalerServiceComponent.java
@@ -40,6 +40,7 @@ import org.apache.stratos.autoscaler.registry.RegistryManager;
 import org.apache.stratos.autoscaler.status.processor.cluster.ClusterStatusProcessorChain;
 import org.apache.stratos.autoscaler.status.processor.group.GroupStatusProcessorChain;
 import org.apache.stratos.autoscaler.util.AutoscalerConstants;
+import org.apache.stratos.autoscaler.util.AutoscalerUtil;
 import org.apache.stratos.autoscaler.util.ConfUtil;
 import org.apache.stratos.autoscaler.util.ServiceReferenceHolder;
 import org.apache.stratos.common.Component;
@@ -76,11 +77,8 @@ import java.util.concurrent.TimeUnit;
  */
 
 public class AutoscalerServiceComponent {
-
     private static final Log log = LogFactory.getLog(AutoscalerServiceComponent.class);
-
     private static final String AUTOSCALER_COORDINATOR_LOCK = "AUTOSCALER_COORDINATOR_LOCK";
-
     private AutoscalerTopologyEventReceiver asTopologyReceiver;
     private AutoscalerHealthStatEventReceiver autoscalerHealthStatEventReceiver;
     private ExecutorService executorService;
@@ -138,7 +136,6 @@ public class AutoscalerServiceComponent {
                         } else {
                             executeCoordinatorTasks();
                         }
-
                         componentStartUpSynchronizer.waitForAxisServiceActivation(Component.Autoscaler,
                                 "AutoscalerService");
                         componentStartUpSynchronizer.setComponentStatus(Component.Autoscaler, true);
@@ -213,6 +210,9 @@ public class AutoscalerServiceComponent {
             AutoscalerContext.getInstance().addNetworkPartitionAlgorithmContext(algorithmContext);
         }
 
+        //Adding application context from registry
+        AutoscalerUtil.readApplicationContextsFromRegistry();
+
         //starting the processor chain
         ClusterStatusProcessorChain clusterStatusProcessorChain = new ClusterStatusProcessorChain();
         ServiceReferenceHolder.getInstance().setClusterStatusProcessorChain(clusterStatusProcessorChain);

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java
index 0e31883..cf7e06e 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorFactory.java
@@ -42,6 +42,8 @@ import org.apache.stratos.common.partition.PartitionRef;
 import org.apache.stratos.messaging.domain.application.Application;
 import org.apache.stratos.messaging.domain.application.Group;
 import org.apache.stratos.messaging.domain.application.ScalingDependentList;
+import org.apache.stratos.messaging.domain.instance.ClusterInstance;
+import org.apache.stratos.messaging.domain.instance.GroupInstance;
 import org.apache.stratos.messaging.domain.instance.Instance;
 import org.apache.stratos.messaging.domain.topology.Cluster;
 import org.apache.stratos.messaging.domain.topology.Service;
@@ -218,22 +220,22 @@ public class MonitorFactory {
 
         //Retrieving the Cluster from Topology
         String clusterId = context.getId();
-        String serviceUuid = context.getServiceUuid();
+        String serviceName = context.getServiceName();
         Cluster cluster;
         //acquire read lock for the service and cluster
-        TopologyManager.acquireReadLockForCluster(serviceUuid, clusterId);
+        TopologyManager.acquireReadLockForCluster(serviceName, clusterId);
         try {
             Topology topology = TopologyManager.getTopology();
-            Service service = topology.getService(serviceUuid);
+            Service service = topology.getService(serviceName);
             if (service == null) {
-                String msg = String.format("Service not found in topology: [service] %s", serviceUuid);
+                String msg = String.format("Service not found in topology: [service] %s", serviceName);
                 throw new RuntimeException(msg);
             }
 
             cluster = service.getCluster(clusterId);
             if (cluster == null) {
                 String msg = String.format("Cluster not found in topology: [service] %s [cluster] %s",
-                        serviceUuid, clusterId);
+                        serviceName, clusterId);
                 throw new RuntimeException(msg);
             }
             //Find whether any other instances exists in group
@@ -266,12 +268,12 @@ public class MonitorFactory {
                     try {
 
                         CloudControllerServiceClient.getInstance().
-                                validateNetworkPartitionOfDeploymentPolicy(serviceUuid,
-                                        networkPartitionRef.getUuid());
+                                validateNetworkPartitionOfDeploymentPolicy(serviceName,
+                                        networkPartitionRef.getId());
                     } catch (Exception e) {
                         String msg = String.format("Error while validating deployment policy " +
                                         "from cloud controller [network-partition-id] %s",
-                                networkPartitionRef.getUuid());
+                                networkPartitionRef.getId());
                         log.error(msg, e);
                         throw new RuntimeException(msg, e);
                     }
@@ -318,7 +320,7 @@ public class MonitorFactory {
 
             return clusterMonitor;
         } finally {
-            TopologyManager.releaseReadLockForCluster(serviceUuid, clusterId);
+            TopologyManager.releaseReadLockForCluster(serviceName, clusterId);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/ClusterMonitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/ClusterMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/ClusterMonitor.java
index 952be10..43493bd 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/ClusterMonitor.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/ClusterMonitor.java
@@ -84,26 +84,21 @@ import java.util.concurrent.atomic.AtomicBoolean;
  */
 public class ClusterMonitor extends Monitor {
 
+    private static final Log log = LogFactory.getLog(ClusterMonitor.class);
     private final ScheduledExecutorService scheduler;
     private final ExecutorService executorService;
-
     protected boolean hasFaultyMember = false;
     protected ClusterContext clusterContext;
     protected String serviceType;
     protected String clusterId;
     // future to cancel it when destroying monitors
     private ScheduledFuture<?> schedulerFuture;
-    protected String serviceUuid;
     private AtomicBoolean monitoringStarted;
-
     private Cluster cluster;
     private int monitoringIntervalMilliseconds;
     //has scaling dependents
     private boolean hasScalingDependents;
     private boolean groupScalingEnabledSubtree;
-
-    private static final Log log = LogFactory.getLog(ClusterMonitor.class);
-    private boolean hasPrimary;
     private String deploymentPolicyId;
 
 
@@ -118,12 +113,33 @@ public class ClusterMonitor extends Monitor {
         readConfigurations();
         this.groupScalingEnabledSubtree = groupScalingEnabledSubtree;
         this.setCluster(new Cluster(cluster));
-        this.serviceUuid = cluster.getServiceName();
+        this.serviceType = cluster.getServiceName();
         this.monitoringStarted = new AtomicBoolean(false);
         this.hasScalingDependents = hasScalingDependents;
         this.deploymentPolicyId = deploymentPolicyId;
     }
 
+    private static void createClusterInstance(String serviceType,
+                                              String clusterId, String alias, String instanceId,
+                                              String partitionId, String networkPartitionId) {
+
+        try {
+            CloudControllerServiceClient.getInstance().createClusterInstance(
+                    serviceType, clusterId, alias, instanceId, partitionId,
+                    networkPartitionId);
+        } catch (RemoteException e) {
+            String msg = " Exception occurred in creating cluster instance with cluster-id [" + clusterId
+                    + "] instance-id [" + instanceId + "] service-type [" + serviceType + "]"
+                    + "] alias [" + alias + "] partition-id [" + partitionId + "]"
+                    + "] network-parition-id [" + networkPartitionId + "]"
+                    + " .Reason [" + e.getMessage() + "]";
+            log.error(msg);
+            throw new RuntimeException(msg, e);
+        }
+
+
+    }
+
     @Override
     public MonitorType getMonitorType() {
         return MonitorType.Cluster;
@@ -211,7 +227,7 @@ public class ClusterMonitor extends Monitor {
     }
 
     public String getServiceId() {
-        return serviceUuid;
+        return serviceType;
     }
 
     protected int getRoundedInstanceCount(float requiredInstances, float fraction) {
@@ -249,27 +265,6 @@ public class ClusterMonitor extends Monitor {
         return groupScalingEnabledSubtree;
     }
 
-    private static void createClusterInstance(String serviceUuid,
-                                              String clusterId, String alias, String instanceId,
-                                              String partitionId, String networkPartitionId) {
-
-        try {
-            CloudControllerServiceClient.getInstance().createClusterInstance(
-                    serviceUuid, clusterId, alias, instanceId, partitionId,
-                    networkPartitionId);
-        } catch (RemoteException e) {
-            String msg = " Exception occurred in creating cluster instance with cluster-id [" + clusterId
-                    + "] instance-id [" + instanceId + "] service-type [" + serviceUuid + "]"
-                    + "] alias [" + alias + "] partition-id [" + partitionId + "]"
-                    + "] network-parition-id [" + networkPartitionId + "]"
-                    + " .Reason [" + e.getMessage() + "]";
-            log.error(msg);
-            throw new RuntimeException(msg, e);
-        }
-
-
-    }
-
     public void handleAverageLoadAverageEvent(
             AverageLoadAverageEvent averageLoadAverageEvent) {
 
@@ -311,25 +306,6 @@ public class ClusterMonitor extends Monitor {
         }
     }
 
-    private boolean isPrimaryMember(MemberContext memberContext) {
-        Properties props = AutoscalerObjectConverter.convertCCPropertiesToProperties(memberContext.getProperties());
-        if (log.isDebugEnabled()) {
-            log.debug(" Properties [" + props + "] ");
-        }
-        if (props != null && props.getProperties() != null) {
-            for (Property prop : props.getProperties()) {
-                if (prop.getName().equals("PRIMARY")) {
-                    if (Boolean.parseBoolean(prop.getValue())) {
-                        log.debug("Adding member id [" + memberContext.getMemberId() + "] " +
-                                "member instance id [" + memberContext.getInstanceId() + "] as a primary member");
-                        return true;
-                    }
-                }
-            }
-        }
-        return false;
-    }
-
     public synchronized void monitor() {
 
         try {
@@ -355,31 +331,8 @@ public class ClusterMonitor extends Monitor {
                                     log.debug(String.format("Cluster monitor is running: [application-id] %s [cluster-id]: " +
                                             "%s", getAppId(), getClusterId()));
                                 }
-                                // store primary members in the cluster instance context
-                                List<String> primaryMemberListInClusterInstance = new ArrayList<String>();
-
-                                for (ClusterLevelPartitionContext partitionContext :
-                                        instanceContext.getPartitionCtxts()) {
-
-                                    // get active primary members in this cluster instance context
-                                    for (MemberContext memberContext : partitionContext.getActiveMembers()) {
-                                        if (isPrimaryMember(memberContext)) {
-                                            primaryMemberListInClusterInstance.add(memberContext.getMemberId());
-                                        }
-                                    }
-
-                                    // get pending primary members in this cluster instance context
-                                    for (MemberContext memberContext : partitionContext.getPendingMembers()) {
-                                        if (isPrimaryMember(memberContext)) {
-                                            primaryMemberListInClusterInstance.add(memberContext.getMemberId());
-                                        }
-                                    }
-                                }
 
-                                instanceContext.getMinCheckKnowledgeSession().setGlobal("primaryMemberCount",
-                                        primaryMemberListInClusterInstance.size());
                                 instanceContext.getMinCheckKnowledgeSession().setGlobal("clusterId", getClusterId());
-                                instanceContext.getMinCheckKnowledgeSession().setGlobal("isPrimary", hasPrimary);
                                 //FIXME when parent chosen the partition
                                 String paritionAlgo = instanceContext.getPartitionAlgorithm();
 
@@ -394,14 +347,7 @@ public class ClusterMonitor extends Monitor {
                                 instanceContext.setMinCheckFactHandle(evaluate(instanceContext.
                                                 getMinCheckKnowledgeSession(),
                                         instanceContext.getMinCheckFactHandle(), instanceContext));
-
-
-                                instanceContext.getMaxCheckKnowledgeSession().setGlobal("primaryMemberCount",
-                                        primaryMemberListInClusterInstance.size());
                                 instanceContext.getMaxCheckKnowledgeSession().setGlobal("clusterId", getClusterId());
-                                instanceContext.getMaxCheckKnowledgeSession().setGlobal("isPrimary", hasPrimary);
-                                instanceContext.getMaxCheckKnowledgeSession().setGlobal("primaryMembers",
-                                        primaryMemberListInClusterInstance);
                                 if (log.isDebugEnabled()) {
                                     log.debug(String.format("Running max check for cluster instance %s ",
                                             instanceContext.getId() + " for the cluster: " + clusterId));
@@ -435,20 +381,11 @@ public class ClusterMonitor extends Monitor {
                                     instanceContext.getScaleCheckKnowledgeSession().setGlobal("rifReset", rifReset);
                                     instanceContext.getScaleCheckKnowledgeSession().setGlobal("mcReset", memoryConsumptionReset);
                                     instanceContext.getScaleCheckKnowledgeSession().setGlobal("laReset", loadAverageReset);
-                                    instanceContext.getScaleCheckKnowledgeSession().setGlobal("isPrimary", hasPrimary);
                                     instanceContext.getScaleCheckKnowledgeSession().setGlobal("algorithmName", paritionAlgo);
                                     instanceContext.getScaleCheckKnowledgeSession().setGlobal("autoscalePolicy",
                                             clusterContext.getAutoscalePolicy());
                                     instanceContext.getScaleCheckKnowledgeSession().setGlobal("arspiReset",
                                             averageRequestServedPerInstanceReset);
-                                    instanceContext.getScaleCheckKnowledgeSession().setGlobal("primaryMembers",
-                                            primaryMemberListInClusterInstance);
-
-                                    if (log.isDebugEnabled()) {
-                                        log.debug(String.format("Running scale check for [cluster instance context] %s ",
-                                                instanceContext.getId()));
-                                        log.debug(" Primary members : " + primaryMemberListInClusterInstance);
-                                    }
 
                                     instanceContext.setScaleCheckFactHandle(evaluate(
                                             instanceContext.getScaleCheckKnowledgeSession()
@@ -468,6 +405,25 @@ public class ClusterMonitor extends Monitor {
                         executorService.execute(monitoringRunnable);
                     }
 
+                    if (instance.getStatus() == ClusterStatus.Terminating) {
+                        //Move active members to Termination pending
+                        if (instanceContext.getActiveMembers() > 0) {
+                            //Sending cluster cleanup event
+                            InstanceNotificationPublisher.getInstance().
+                                    sendInstanceCleanupEventForCluster(clusterId,
+                                            instance.getInstanceId());
+                            moveMembersToTerminatingPending(networkPartitionContext.getId(),
+                                    instance.getInstanceId());
+                        }
+
+                        if (instanceContext.getPendingMemberCount() > 0) {
+                            //Terminating the pending members
+                            terminatePendingMembers(networkPartitionContext.getId(),
+                                    instance.getInstanceId());
+                        }
+
+                    }
+
                     for (final ClusterLevelPartitionContext partitionContext : instanceContext.getPartitionCtxts()) {
                         Runnable monitoringRunnable = new Runnable() {
                             @Override
@@ -481,7 +437,7 @@ public class ClusterMonitor extends Monitor {
                                         && partitionContext.getTerminationPendingMembers().size() == 0
                                         && partitionContext.getObsoletedMembers().size() == 0) {
 
-                                    instanceContext.removePartitionCtxt(partitionContext.getPartition().getUuid());
+                                    instanceContext.removePartitionCtxt(partitionContext.getPartition().getId());
                                 }
                             }
                         };
@@ -522,7 +478,9 @@ public class ClusterMonitor extends Monitor {
     @Override
     public void destroy() {
         //shutting down the scheduler
-        schedulerFuture.cancel(true);
+        if (schedulerFuture != null) {
+            schedulerFuture.cancel(true);
+        }
 
         if (log.isDebugEnabled()) {
             log.debug("ClusterMonitor task has been stopped " + this.toString());
@@ -530,32 +488,31 @@ public class ClusterMonitor extends Monitor {
     }
 
     @Override
-    public String toString() {
-        return "ClusterMonitor [clusterId=" + getClusterId() +
-                ", hasPrimary=" + hasPrimary + " ]";
-    }
-
-    public void setHasPrimary(boolean hasPrimary) {
-        this.hasPrimary = hasPrimary;
-    }
-
-    @Override
     public void onChildStatusEvent(MonitorStatusEvent statusEvent) {
 
     }
 
     @Override
-    public void onParentStatusEvent(MonitorStatusEvent statusEvent) {
-        String instanceId = statusEvent.getInstanceId();
-        // send the ClusterTerminating event
-        if (statusEvent.getStatus() == GroupStatus.Terminating || statusEvent.getStatus() ==
-                ApplicationStatus.Terminating) {
-            if (log.isInfoEnabled()) {
-                log.info("Publishing Cluster terminating event for [application] " + appId +
-                        " [cluster] " + this.getClusterId() + " [instance] " + instanceId);
+    public void onParentStatusEvent(final MonitorStatusEvent statusEvent) {
+        Runnable monitoringRunnable = new Runnable() {
+            @Override
+            public void run() {
+                String instanceId = statusEvent.getInstanceId();
+                // send the ClusterTerminating event
+                if (statusEvent.getStatus() == GroupStatus.Terminating || statusEvent.getStatus() ==
+                        ApplicationStatus.Terminating) {
+                    if (log.isInfoEnabled()) {
+                        log.info("Publishing Cluster terminating event for [application] " + appId +
+                                " [cluster] " + getClusterId() + " [instance] " + instanceId);
+                    }
+                    ClusterStatusEventPublisher.sendClusterStatusClusterTerminatingEvent(getAppId(),
+                            getServiceId(), getClusterId(), instanceId);
+                }
             }
-            ClusterStatusEventPublisher.sendClusterStatusClusterTerminatingEvent(getAppId(), getServiceId(), getClusterId(), instanceId);
-        }
+
+        };
+        executorService.execute(monitoringRunnable);
+
     }
 
     @Override
@@ -584,28 +541,6 @@ public class ClusterMonitor extends Monitor {
         ClusterInstanceContext clusterInstanceContext =
                 getClusterInstanceContext(scalingEvent.getNetworkPartitionId(), instanceId);
 
-
-        // store primary members in the cluster instance context
-        List<String> primaryMemberListInClusterInstance = new ArrayList<String>();
-
-        for (ClusterLevelPartitionContext partitionContext : clusterInstanceContext.getPartitionCtxts()) {
-
-            // get active primary members in this cluster instance context
-            for (MemberContext memberContext : partitionContext.getActiveMembers()) {
-                if (isPrimaryMember(memberContext)) {
-                    primaryMemberListInClusterInstance.add(memberContext.getMemberId());
-                }
-            }
-
-            // get pending primary members in this cluster instance context
-            for (MemberContext memberContext : partitionContext.getPendingMembers()) {
-                if (isPrimaryMember(memberContext)) {
-                    primaryMemberListInClusterInstance.add(memberContext.getMemberId());
-                }
-            }
-        }
-
-
         //TODO get min instance count from instance context
         float requiredInstanceCount = clusterInstanceContext.getMinInstanceCount() * scalingFactorBasedOnDependencies;
         int roundedRequiredInstanceCount = getRoundedInstanceCount(requiredInstanceCount,
@@ -615,8 +550,6 @@ public class ClusterMonitor extends Monitor {
         clusterInstanceContext.getDependentScaleCheckKnowledgeSession().setGlobal("clusterId", getClusterId());
         clusterInstanceContext.getDependentScaleCheckKnowledgeSession().setGlobal("roundedRequiredInstanceCount", roundedRequiredInstanceCount);
         clusterInstanceContext.getDependentScaleCheckKnowledgeSession().setGlobal("algorithmName", clusterInstanceContext.getPartitionAlgorithm());
-        clusterInstanceContext.getDependentScaleCheckKnowledgeSession().setGlobal("isPrimary", hasPrimary);
-        clusterInstanceContext.getDependentScaleCheckKnowledgeSession().setGlobal("primaryMembers", primaryMemberListInClusterInstance);
         clusterInstanceContext.setDependentScaleCheckFactHandle(evaluate(
                 clusterInstanceContext.getDependentScaleCheckKnowledgeSession()
                 , clusterInstanceContext.getDependentScaleCheckFactHandle(), clusterInstanceContext));
@@ -811,9 +744,11 @@ public class ClusterMonitor extends Monitor {
                         float averageRequestsInFlight = value * clusterInstanceContext.getActiveMemberCount() /
                                 totalActiveMemberCount;
                         clusterInstanceContext.setAverageRequestsInFlight(averageRequestsInFlight);
-                        log.debug(String.format("Calculated average RIF: [cluster] %s [cluster-instance] %s " +
-                                        "[network-partition] %s [average-rif] %s", clusterId,
-                                clusterInstanceContext.getId(), networkPartitionId, averageRequestsInFlight));
+                        if (log.isDebugEnabled()) {
+                            log.debug(String.format("Calculated average RIF: [cluster] %s [cluster-instance] %s " +
+                                            "[network-partition] %s [average-rif] %s", clusterId,
+                                    clusterInstanceContext.getId(), networkPartitionId, averageRequestsInFlight));
+                        }
                     }
                 }
             } else {
@@ -1558,7 +1493,7 @@ public class ClusterMonitor extends Monitor {
                 }
             } else {
                 createClusterInstance(cluster.getServiceName(), cluster.getClusterId(), null, parentInstanceId, partitionId,
-                        parentMonitorInstance.getNetworkPartitionUuid());
+                        parentMonitorInstance.getNetworkPartitionId());
                 if (log.isDebugEnabled()) {
                     log.debug(String.format("Cluster instance created: [application-id] %s [service-name] %s " +
                             "[cluster-id] %s", appId, cluster.getServiceName(), cluster.getClusterId()));

http://git-wip-us.apache.org/repos/asf/stratos/blob/4b864473/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ApplicationMonitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ApplicationMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ApplicationMonitor.java
index 787458c..70ee9d4 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ApplicationMonitor.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ApplicationMonitor.java
@@ -78,7 +78,7 @@ public class ApplicationMonitor extends ParentComponentMonitor {
             TopologyInConsistentException {
         super(application);
 
-        int threadPoolSize = Integer.getInteger(AutoscalerConstants.MONITOR_THREAD_POOL_ID, 100);
+        int threadPoolSize = Integer.getInteger(AutoscalerConstants.MONITOR_THREAD_POOL_SIZE, 100);
         this.executorService = StratosThreadPool.getExecutorService(
                 AutoscalerConstants.MONITOR_THREAD_POOL_ID, threadPoolSize);
 
@@ -147,12 +147,27 @@ public class ApplicationMonitor extends ParentComponentMonitor {
                                 new ConcurrentHashMap<String, ScalingUpBeyondMaxEvent>());
                     }
                 }
+                //When the application is getting un-deployed, need to avoid
+                // checking the minimum count sanctification
+                if (!isTerminating()) {
+                    Application application = ApplicationHolder.getApplications().getApplication(appId);
+                    if (application != null) {
+                        List<String> defaultNetworkPartitions = getDefaultNetworkPartitions(application);
+                        //Checking for whether minimum application instances are there.
+                        if (defaultNetworkPartitions != null) {
+                            checkForMinimumApplicationInstances(application, defaultNetworkPartitions);
+                        }
+
+                    /*//Checking for whether any application instances need to be terminated.
+                    checkForApplicationInstanceTermination(application, defaultNetworkPartitions);*/
+                    }
+                }
             }
         };
         executorService.execute(monitoringRunnable);
     }
 
-    private void handleScalingMaxOut(InstanceContext instanceContext,
+    private void handleScalingMaxOut(ParentInstanceContext instanceContext,
                                      NetworkPartitionContext networkPartitionContext) {
         if (((NetworkPartitionContext) networkPartitionContext).getPendingInstancesCount() == 0) {
             //handling the application bursting only when there are no pending instances found
@@ -251,6 +266,95 @@ public class ApplicationMonitor extends ParentComponentMonitor {
         }
     }
 
+    public List<String> getDefaultNetworkPartitions(Application application) {
+        //Minimum check, Need to get the network partition
+        NetworkPartitionAlgorithmContext algorithmContext = AutoscalerContext.getInstance().
+                getNetworkPartitionAlgorithmContext(appId);
+        ApplicationPolicy applicationPolicy = PolicyManager.getInstance().
+                getApplicationPolicy(application.getApplicationPolicyId());
+        List<String> defaultNetworkPartitions = null;
+
+        if (applicationPolicy != null) {
+            String networkPartitionAlgorithmName = applicationPolicy.getAlgorithm();
+            if (log.isDebugEnabled()) {
+                String msg = String.format("Network partition algorithm is %s [application-id] %s",
+                        networkPartitionAlgorithmName, appId);
+                log.debug(msg);
+            }
+
+            NetworkPartitionAlgorithm algorithm = getNetworkPartitionAlgorithm(
+                    networkPartitionAlgorithmName);
+            if (algorithm == null) {
+                String msg = String.format("Couldn't create network partition algorithm " +
+                        "[application-id] %s", appId);
+                log.error(msg);
+                throw new RuntimeException(msg);
+            }
+
+
+            // Check whether the network-partition of the application
+            // instance belongs to default set of network-partitions.
+            // If it is default set, then application instance cannot be terminated.
+            defaultNetworkPartitions = algorithm.
+                    getDefaultNetworkPartitions(algorithmContext);
+        }
+
+        return defaultNetworkPartitions;
+    }
+
+    private void checkForMinimumApplicationInstances(Application application,
+                                                     List<String> defaultNetworkPartitions) {
+        List<String> instanceIds = new ArrayList<String>();
+        for (String networkPartitionId : defaultNetworkPartitions) {
+            if (!networkPartitionContextsMap.containsKey(networkPartitionId)) {
+                String instanceId;
+                log.info("Detected a newly updated [network-partition] " + networkPartitionId +
+                        " for [application] " + appId + ". Hence new application instance " +
+                        "creation is going to start now!");
+                NetworkPartitionContext context =
+                        new NetworkPartitionContext(networkPartitionId);
+                //If application instances found in the ApplicationsTopology,
+                // then have to add them first before creating new one
+                ApplicationInstance appInstance = (ApplicationInstance) application.
+                        getInstanceByNetworkPartitionId(context.getId());
+                if (appInstance != null) {
+                    log.warn("The [application] " + appId + " already has the " +
+                            "[application-instance] " + appInstance.getInstanceId() + " for the " +
+                            "[network-partition] " + networkPartitionId);
+                    return;
+                }
+                instanceId = handleApplicationInstanceCreation(application, context, null);
+                instanceIds.add(instanceId);
+
+            }
+        }
+        //Starting the dependencies
+        if (!instanceIds.isEmpty()) {
+            startDependency(application, instanceIds);
+        }
+
+    }
+
+    private void checkForApplicationInstanceTermination(Application application,
+                                                        List<String> defaultNetworkPartitions) {
+
+        for (NetworkPartitionContext networkPartitionContext : networkPartitionContextsMap.values()) {
+            String nPartitionId = networkPartitionContext.getId();
+            if (!defaultNetworkPartitions.contains(nPartitionId)) {
+                log.info("The [application] " + appId + " runtime cannot be in [network-partition] "
+                        + nPartitionId + " as it is removed from the [application-policy]...!");
+                for (InstanceContext instanceContext : networkPartitionContext.
+                        getInstanceIdToInstanceContextMap().values()) {
+                    //Handling application instance termination
+                    ApplicationBuilder.handleApplicationInstanceTerminatingEvent(this.appId,
+                            instanceContext.getId());
+                }
+
+            }
+        }
+
+    }
+
 
     /**
      * Find the group monitor by traversing recursively in the hierarchical monitors.
@@ -385,20 +489,6 @@ public class ApplicationMonitor extends ParentComponentMonitor {
     }
 
     /**
-     * This will start the minimum dependencies
-     *
-     * @param application the application which used to create monitors
-     * @return whether monitor created or not
-     * @throws TopologyInConsistentException
-     * @throws PolicyValidationException
-     */
-    public boolean startMinimumDependencies(Application application)
-            throws TopologyInConsistentException, PolicyValidationException {
-
-        return createInstanceAndStartDependency(application);
-    }
-
-    /**
      * Utility to create application instance by parsing the deployment policy for a monitor
      *
      * @param application the application
@@ -418,7 +508,7 @@ public class ApplicationMonitor extends ParentComponentMonitor {
                     getApplicationPolicy(application.getApplicationPolicyId());
             if (applicationPolicy == null) {
                 String msg = String.format("Application policy not found in registry or " +
-                        "in-memory [application-id] %s [application-policy-id] %s", appId,application.getApplicationPolicyId());
+                        "in-memory [application-id] %s", appId);
                 log.error(msg);
                 throw new RuntimeException(msg);
             }
@@ -456,9 +546,9 @@ public class ApplicationMonitor extends ParentComponentMonitor {
                 return false;
             }
 
-            for (String networkPartitionUuids : nextNetworkPartitions) {
+            for (String networkPartitionIds : nextNetworkPartitions) {
                 NetworkPartitionContext context =
-                        new NetworkPartitionContext(networkPartitionUuids);
+                        new NetworkPartitionContext(networkPartitionIds);
                 //If application instances found in the ApplicationsTopology,
                 // then have to add them first before creating new one
                 ApplicationInstance appInstance = (ApplicationInstance) application.
@@ -477,7 +567,7 @@ public class ApplicationMonitor extends ParentComponentMonitor {
                 }
                 instanceIds.add(instanceId);
                 log.info("Application instance has been added for the [network partition] " +
-                        networkPartitionUuids + " [appInstanceId] " + instanceId);
+                        networkPartitionIds + " [appInstanceId] " + instanceId);
             }
 
             //Find whether any other instances exists in cluster
@@ -486,7 +576,7 @@ public class ApplicationMonitor extends ParentComponentMonitor {
             for (ApplicationInstance instance : instanceMap.values()) {
                 if (!instanceIds.contains(instance.getInstanceId())) {
                     NetworkPartitionContext context =
-                            new NetworkPartitionContext(instance.getNetworkPartitionUuid());
+                            new NetworkPartitionContext(instance.getNetworkPartitionId());
                     //If application instances found in the ApplicationsTopology,
                     // then have to add them first before creating new one
                     ApplicationInstance appInstance = (ApplicationInstance) application.
@@ -495,12 +585,13 @@ public class ApplicationMonitor extends ParentComponentMonitor {
                     handleApplicationInstanceCreation(application, context, appInstance);
                     instanceIds.add(instance.getInstanceId());
                     log.info("Burst Application instance has been added in the restart for " +
-                            "the [network partition] " + instance.getNetworkPartitionUuid() +
+                            "the [network partition] " + instance.getNetworkPartitionId() +
                             " [appInstanceId] " + instance.getInstanceId());
                 }
             }
-
-            startDependency(application, instanceIds);
+            if (!instanceIds.isEmpty()) {
+                startDependency(application, instanceIds);
+            }
 
         } catch (Exception e) {
             log.error(String.format("Application instance creation failed [applcaition-id] %s", appId), e);
@@ -571,7 +662,7 @@ public class ApplicationMonitor extends ParentComponentMonitor {
                 getApplicationPolicy(application.getApplicationPolicyId());
         if (applicationPolicy == null) {
             String msg = String.format("Application policy not found in registry or in-memory " +
-                    "[application-id] %s [application-policy-id] %s ", appId, application.getApplicationPolicyId());
+                    "[application-id] %s", appId);
             log.error(msg);
             throw new RuntimeException(msg);
         }