removing unwanted event ApplicationUndeployed from the flow
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/87d77a2d Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/87d77a2d Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/87d77a2d Branch: refs/heads/4.0.0-grouping Commit: 87d77a2dfa8d9ac9ca0f53f119fcabdedffb4159 Parents: 6f7baa8 Author: Isuru Haththotuwa <[email protected]> Authored: Fri Oct 24 17:42:37 2014 +0530 Committer: Isuru Haththotuwa <[email protected]> Committed: Sun Oct 26 21:54:59 2014 +0530 ---------------------------------------------------------------------- .../AutoscalerTopologyEventReceiver.java | 85 ++++++++++++++++--- .../controller/topology/TopologyBuilder.java | 88 ++++++++++++++++---- .../topology/ApplicationTerminatingEvent.java | 13 ++- 3 files changed, 159 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/87d77a2d/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java index 63f9079..2f7cca4 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java @@ -38,6 +38,11 @@ import org.apache.stratos.autoscaler.status.checker.StatusChecker; import org.apache.stratos.messaging.domain.topology.*; import org.apache.stratos.messaging.event.Event; import org.apache.stratos.messaging.event.topology.*; +import org.apache.stratos.messaging.event.topology.ApplicationActivatedEvent; +import org.apache.stratos.messaging.event.topology.ApplicationCreatedEvent; +import org.apache.stratos.messaging.event.topology.ClusterActivatedEvent; +import org.apache.stratos.messaging.event.topology.ClusterInActivateEvent; +import org.apache.stratos.messaging.event.topology.GroupActivatedEvent; import org.apache.stratos.messaging.listener.topology.*; import org.apache.stratos.messaging.message.receiver.topology.TopologyEventReceiver; import org.apache.stratos.messaging.message.receiver.topology.TopologyManager; @@ -323,17 +328,78 @@ public class AutoscalerTopologyEventReceiver implements Runnable { } }); - topologyEventReceiver.addEventListener(new ApplicationUndeployedEventListener() { +// topologyEventReceiver.addEventListener(new ApplicationUndeployedEventListener() { +// @Override +// protected void onEvent(Event event) { +// +// log.info("[ApplicationUndeployedEvent] Received: " + event.getClass()); +// +// ApplicationUndeployedEvent applicationUndeployedEvent = (ApplicationUndeployedEvent) event; +// +// // acquire reead locks for application and relevant clusters +// TopologyManager.acquireReadLockForApplication(applicationUndeployedEvent.getApplicationId()); +// Set<ClusterDataHolder> clusterDataHolders = applicationUndeployedEvent.getClusterData(); +// if (clusterDataHolders != null) { +// for (ClusterDataHolder clusterData : clusterDataHolders) { +// TopologyManager.acquireReadLockForCluster(clusterData.getServiceType(), +// clusterData.getClusterId()); +// } +// } +// +// try { +// ApplicationMonitor appMonitor = AutoscalerContext.getInstance(). +// getAppMonitor(applicationUndeployedEvent.getApplicationId()); +// +// if (appMonitor != null) { +// // update the status as Terminating +// appMonitor.setStatus(ApplicationStatus.Terminating); +// +// List<String> clusters = appMonitor. +// findClustersOfApplication(applicationUndeployedEvent.getApplicationId()); +// +// for (String clusterId : clusters) { +// //stopping the cluster monitor and remove it from the AS +// ClusterMonitor clusterMonitor = +// ((ClusterMonitor) AutoscalerContext.getInstance().getMonitor(clusterId)); +// if (clusterMonitor != null) { +// clusterMonitor.setDestroyed(true); +// clusterMonitor.terminateAllMembers(); +// clusterMonitor.setStatus(ClusterStatus.Terminating); +// } else { +// log.warn("No Cluster Monitor found for cluster id " + clusterId); +// } +// } +// +// } else { +// log.warn("Application Monitor cannot be found for the undeployed [application] " +// + applicationUndeployedEvent.getApplicationId()); +// } +// +// } finally { +// if (clusterDataHolders != null) { +// for (ClusterDataHolder clusterData : clusterDataHolders) { +// TopologyManager.releaseReadLockForCluster(clusterData.getServiceType(), +// clusterData.getClusterId()); +// } +// } +// TopologyManager. +// releaseReadLockForApplication(applicationUndeployedEvent.getApplicationId()); +// } +// } +// }); + + + topologyEventReceiver.addEventListener(new ApplicationTerminatingEventListener() { @Override protected void onEvent(Event event) { - log.info("[ApplicationUndeployedEvent] Received: " + event.getClass()); + log.info("[ApplicationTerminatingEvent] Received: " + event.getClass()); - ApplicationUndeployedEvent applicationUndeployedEvent = (ApplicationUndeployedEvent) event; + ApplicationTerminatingEvent appTerminatingEvent = (ApplicationTerminatingEvent) event; // acquire reead locks for application and relevant clusters - TopologyManager.acquireReadLockForApplication(applicationUndeployedEvent.getApplicationId()); - Set<ClusterDataHolder> clusterDataHolders = applicationUndeployedEvent.getClusterData(); + TopologyManager.acquireReadLockForApplication(appTerminatingEvent.getAppId()); + Set<ClusterDataHolder> clusterDataHolders = appTerminatingEvent.getClusterData(); if (clusterDataHolders != null) { for (ClusterDataHolder clusterData : clusterDataHolders) { TopologyManager.acquireReadLockForCluster(clusterData.getServiceType(), @@ -343,14 +409,14 @@ public class AutoscalerTopologyEventReceiver implements Runnable { try { ApplicationMonitor appMonitor = AutoscalerContext.getInstance(). - getAppMonitor(applicationUndeployedEvent.getApplicationId()); + getAppMonitor(appTerminatingEvent.getAppId()); if (appMonitor != null) { // update the status as Terminating appMonitor.setStatus(ApplicationStatus.Terminating); List<String> clusters = appMonitor. - findClustersOfApplication(applicationUndeployedEvent.getApplicationId()); + findClustersOfApplication(appTerminatingEvent.getAppId()); for (String clusterId : clusters) { //stopping the cluster monitor and remove it from the AS @@ -367,7 +433,7 @@ public class AutoscalerTopologyEventReceiver implements Runnable { } else { log.warn("Application Monitor cannot be found for the undeployed [application] " - + applicationUndeployedEvent.getApplicationId()); + + appTerminatingEvent.getAppId()); } } finally { @@ -378,12 +444,11 @@ public class AutoscalerTopologyEventReceiver implements Runnable { } } TopologyManager. - releaseReadLockForApplication(applicationUndeployedEvent.getApplicationId()); + releaseReadLockForApplication(appTerminatingEvent.getAppId()); } } }); - topologyEventReceiver.addEventListener(new ApplicationRemovedEventListener() { @Override protected void onEvent(Event event) { http://git-wip-us.apache.org/repos/asf/stratos/blob/87d77a2d/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyBuilder.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyBuilder.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyBuilder.java index 80315ea..d245a6f 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyBuilder.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyBuilder.java @@ -933,29 +933,85 @@ public class TopologyBuilder { } public static void handleApplicationTerminatingEvent(ApplicationTerminatingEvent event) { - Topology topology = TopologyManager.getTopology(); - Application application = topology.getApplication(event.getAppId()); - //update the status of the Group - if (application == null) { - log.warn(String.format("Application %s does not exist", - event.getAppId())); - return; - } - org.apache.stratos.messaging.event.topology.ApplicationTerminatingEvent applicationTerminatingEvent = - new org.apache.stratos.messaging.event.topology.ApplicationTerminatingEvent( - event.getAppId()); + Set<ClusterDataHolder> clusterData; + String applicationId = event.getAppId(); + + // update the Application and Cluster Statuses as 'Terminating' + TopologyManager.acquireWriteLock(); + try { - TopologyManager.acquireWriteLock(); + + Topology topology = TopologyManager.getTopology(); + + if (!topology.applicationExists(applicationId)) { + log.warn("Application with id [ " + applicationId + " ] doesn't exist in Topology"); + return; + } + + Application application = topology.getApplication(applicationId); + // check and update application status to 'Terminating' + if (!application.isStateTransitionValid(ApplicationStatus.Terminating)) { + log.error("Invalid state transfer from " + application.getStatus() + " to " + ApplicationStatus.Terminating); + } + // for now anyway update the status forcefully application.setStatus(ApplicationStatus.Terminating); - log.info("Application terminating adding status started for Topology"); + log.info("Application " + applicationId + "'s status updated to " + ApplicationStatus.Terminating); + + // update all the Clusters' statuses to 'Terminating' + clusterData = application.getClusterDataRecursively(); + for (ClusterDataHolder clusterDataHolder : clusterData) { + Service service = topology.getService(clusterDataHolder.getServiceType()); + if (service != null) { + Cluster aCluster = service.getCluster(clusterDataHolder.getClusterId()); + if (aCluster != null) { + // validate state transition + if (!aCluster.isStateTransitionValid(ClusterStatus.Terminating)) { + log.error("Invalid state transfer from " + aCluster.getStatus() + " to " + + ClusterStatus.Terminating); + } + // for now anyway update the status forcefully + aCluster.setStatus(ClusterStatus.Terminating); + + } else { + log.warn("Unable to find Cluster with cluster id " + clusterDataHolder.getClusterId() + + " in Topology"); + } + + } else { + log.warn("Unable to update cluster with cluster id: " + clusterDataHolder.getClusterId() + " from Topology, " + + " associated Service [ " + clusterDataHolder.getServiceType() + " ] not found"); + } + } + + // update all Group's statuses to 'Terminating' + if (application.getGroups() != null) { + updateGroupStatusesRecursively(GroupStatus.Terminating, application.getGroups()); + } - TopologyManager.updateTopology(topology); } finally { TopologyManager.releaseWriteLock(); } - //publishing data - TopologyEventPublisher.sendApplicationTerminatingEvent(applicationTerminatingEvent); + + TopologyEventPublisher.sendApplicationTerminatingEvent( + new org.apache.stratos.messaging.event.topology.ApplicationTerminatingEvent( + applicationId, clusterData)); + } + + private static void updateGroupStatusesRecursively (GroupStatus groupStatus, Collection<Group> groups) { + + for (Group group : groups) { + if (!group.isStateTransitionValid(groupStatus)) { + log.error("Invalid state transfer from " + group.getStatus() + " to " + groupStatus); + } + // force update for now + group.setStatus(groupStatus); + + // go recursively and update + if (group.getGroups() != null) { + updateGroupStatusesRecursively(groupStatus, group.getGroups()); + } + } } public static void handleApplicationTerminatedEvent(ApplicationTerminatedEvent event) { http://git-wip-us.apache.org/repos/asf/stratos/blob/87d77a2d/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/topology/ApplicationTerminatingEvent.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/topology/ApplicationTerminatingEvent.java b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/topology/ApplicationTerminatingEvent.java index e1fbc02..c718f08 100644 --- a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/topology/ApplicationTerminatingEvent.java +++ b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/topology/ApplicationTerminatingEvent.java @@ -18,17 +18,28 @@ */ package org.apache.stratos.messaging.event.topology; +import org.apache.stratos.messaging.domain.topology.ClusterDataHolder; + +import java.util.Set; + /** * This event will be sent to Topology upon terminating of application */ public class ApplicationTerminatingEvent extends TopologyEvent { + private final String appId; + private Set<ClusterDataHolder> clusterData; - public ApplicationTerminatingEvent(String appId) { + public ApplicationTerminatingEvent(String appId, Set<ClusterDataHolder> clusterData) { this.appId = appId; + this.clusterData = clusterData; } public String getAppId() { return appId; } + + public Set<ClusterDataHolder> getClusterData() { + return clusterData; + } } \ No newline at end of file
