fixing onactive action based on instances
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/a8ed191f Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/a8ed191f Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/a8ed191f Branch: refs/heads/master Commit: a8ed191f91b198319cd2baa90f3e8919ce52607c Parents: ea72151 Author: reka <[email protected]> Authored: Tue Dec 2 22:01:39 2014 +0530 Committer: reka <[email protected]> Committed: Tue Dec 2 23:28:17 2014 +0530 ---------------------------------------------------------------------- .../context/cluster/AbstractClusterContext.java | 22 +++++++ .../context/cluster/VMClusterContext.java | 1 + .../monitor/cluster/AbstractClusterMonitor.java | 7 ++- .../monitor/component/ApplicationMonitor.java | 38 +++++++++++-- .../monitor/component/GroupMonitor.java | 60 ++++++++++++++------ .../component/ParentComponentMonitor.java | 41 +++++++++---- 6 files changed, 135 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/a8ed191f/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/AbstractClusterContext.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/AbstractClusterContext.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/AbstractClusterContext.java index a2e0472..69f87a2 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/AbstractClusterContext.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/AbstractClusterContext.java @@ -23,8 +23,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.stratos.cloud.controller.stub.domain.MemberContext; import org.apache.stratos.common.constants.StratosConstants; +import org.apache.stratos.messaging.domain.instance.ClusterInstance; import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; /* * It holds the runtime data of a service cluster @@ -37,13 +40,32 @@ public class AbstractClusterContext implements Serializable { // cluster id protected String clusterId; private String serviceId; + protected Map<String, ClusterInstance> clusterInstanceMap; + public AbstractClusterContext(String clusterId, String serviceId){ this.clusterId = clusterId; this.serviceId = serviceId; + clusterInstanceMap = new HashMap<String, ClusterInstance>(); } public String getServiceId() { return serviceId; } + + public Map<String, ClusterInstance> getClusterInstanceMap() { + return clusterInstanceMap; + } + + public void setClusterInstanceMap(Map<String, ClusterInstance> clusterInstanceMap) { + this.clusterInstanceMap = clusterInstanceMap; + } + + public void addClusterInstance(ClusterInstance instance) { + this.clusterInstanceMap.put(instance.getInstanceId(), instance); + } + + public ClusterInstance getClusterInstance(String instanceId) { + return this.clusterInstanceMap.get(instanceId); + } } http://git-wip-us.apache.org/repos/asf/stratos/blob/a8ed191f/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/VMClusterContext.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/VMClusterContext.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/VMClusterContext.java index 3de6306..065902e 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/VMClusterContext.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/context/cluster/VMClusterContext.java @@ -54,6 +54,7 @@ public class VMClusterContext extends AbstractClusterContext { // Map<NetworkpartitionId, Network Partition Context> protected Map<String, ClusterLevelNetworkPartitionContext> networkPartitionCtxts; + protected DeploymentPolicy deploymentPolicy; protected AutoscalePolicy autoscalePolicy; http://git-wip-us.apache.org/repos/asf/stratos/blob/a8ed191f/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java index 756079e..3afda6f 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/cluster/AbstractClusterMonitor.java @@ -34,6 +34,7 @@ import org.apache.stratos.messaging.domain.applications.Application; import org.apache.stratos.messaging.domain.applications.ApplicationStatus; import org.apache.stratos.messaging.domain.applications.Group; import org.apache.stratos.messaging.domain.applications.GroupStatus; +import org.apache.stratos.messaging.domain.instance.ClusterInstance; import org.apache.stratos.messaging.domain.topology.ClusterStatus; import org.apache.stratos.messaging.event.health.stat.*; import org.apache.stratos.messaging.event.topology.*; @@ -212,7 +213,9 @@ public abstract class AbstractClusterMonitor extends Monitor implements Runnable return status; } - public void setStatus(ClusterStatus status) { + public void setStatus(ClusterStatus status, String instanceId) { + + this.clusterContext.getClusterInstance(instanceId).setStatus(status); /** * notifying the parent monitor about the state change * If the cluster in_active and if it is a in_dependent cluster, @@ -415,4 +418,6 @@ public abstract class AbstractClusterMonitor extends Monitor implements Runnable public void setClusterContext(AbstractClusterContext clusterContext) { this.clusterContext = clusterContext; } + + } http://git-wip-us.apache.org/repos/asf/stratos/blob/a8ed191f/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 0e5f751..990d482 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 @@ -39,6 +39,7 @@ import org.apache.stratos.autoscaler.pojo.policy.deployment.partition.network.Ap import org.apache.stratos.messaging.domain.applications.Application; import org.apache.stratos.messaging.domain.applications.ApplicationStatus; import org.apache.stratos.messaging.domain.applications.GroupStatus; +import org.apache.stratos.messaging.domain.instance.ApplicationInstance; import org.apache.stratos.messaging.domain.topology.ClusterStatus; import org.apache.stratos.messaging.domain.topology.lifecycle.LifeCycleState; @@ -52,6 +53,9 @@ public class ApplicationMonitor extends ParentComponentMonitor { //network partition contexts private Map<String, ApplicationLevelNetworkPartitionContext> networkPartitionCtxts; + //application instance id map + private Map<String, ApplicationInstance> applicationInstanceIdMap; + public ApplicationMonitor(Application application) throws DependencyBuilderException, TopologyInConsistentException { @@ -59,10 +63,7 @@ public class ApplicationMonitor extends ParentComponentMonitor { //setting the appId for the application this.appId = application.getUniqueIdentifier(); networkPartitionCtxts = new HashMap<String, ApplicationLevelNetworkPartitionContext>(); - - //starting the first set of dependencies from its children - //TODO startMinimumDependencies(application); - + setApplicationInstanceIdMap(new HashMap<String, ApplicationInstance>()); } /** @@ -72,7 +73,6 @@ public class ApplicationMonitor extends ParentComponentMonitor { * @return the found GroupMonitor */ public Monitor findGroupMonitorWithId(String groupId) { - Monitor monitor; //searching within active monitors return findGroupMonitor(groupId, aliasToActiveMonitorsMap.values()); } @@ -107,6 +107,8 @@ public class ApplicationMonitor extends ParentComponentMonitor { * @param status the status */ public void setStatus(ApplicationStatus status, String instanceId) { + this.applicationInstanceIdMap.get(instanceId).setStatus(status); + //notify the children about the state change try { MonitorStatusEventBuilder.notifyChildren(this, new ApplicationStatusEvent(status, appId, instanceId)); @@ -216,6 +218,10 @@ public class ApplicationMonitor extends ParentComponentMonitor { ApplicationInstanceContext instanceContext = new ApplicationInstanceContext(instanceId); context.addInstanceContext(instanceContext); + ApplicationInstance instance = new ApplicationInstance(appId, instanceId); + instance.setStatus(ApplicationStatus.Created); + this.applicationInstanceIdMap.put(instanceId, instance); + this.networkPartitionCtxts.put(context.getId(), context); instanceIds.add(instanceId); @@ -257,6 +263,11 @@ public class ApplicationMonitor extends ParentComponentMonitor { ApplicationInstanceContext instanceContext = new ApplicationInstanceContext(instanceId); context.addInstanceContext(instanceContext); this.networkPartitionCtxts.put(context.getId(), context); + + ApplicationInstance instance = new ApplicationInstance(appId, instanceId); + instance.setStatus(ApplicationStatus.Created); + this.applicationInstanceIdMap.put(instanceId, instance); + burstNPFound = true; } } @@ -306,4 +317,21 @@ public class ApplicationMonitor extends ParentComponentMonitor { public void addApplicationLevelNetworkPartitionContext(ApplicationLevelNetworkPartitionContext applicationLevelNetworkPartitionContext) { this.networkPartitionCtxts.put(applicationLevelNetworkPartitionContext.getId(), applicationLevelNetworkPartitionContext); } + + public Map<String, ApplicationInstance> getApplicationInstanceIdMap() { + return applicationInstanceIdMap; + } + + public void setApplicationInstanceIdMap(Map<String, ApplicationInstance> applicationInstanceIdMap) { + this.applicationInstanceIdMap = applicationInstanceIdMap; + } + + public void addApplicationInstance(ApplicationInstance instance) { + this.applicationInstanceIdMap.put(instance.getInstanceId(), instance); + + } + + public ApplicationInstance getApplicationInstance(String instanceId) { + return this.applicationInstanceIdMap.get(instanceId); + } } http://git-wip-us.apache.org/repos/asf/stratos/blob/a8ed191f/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java index 9ec04d7..b2b6697 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/GroupMonitor.java @@ -40,10 +40,12 @@ import org.apache.stratos.autoscaler.pojo.policy.PolicyManager; import org.apache.stratos.autoscaler.pojo.policy.deployment.ChildPolicy; import org.apache.stratos.autoscaler.pojo.policy.deployment.DeploymentPolicy; import org.apache.stratos.autoscaler.pojo.policy.deployment.partition.network.ChildLevelNetworkPartition; +import org.apache.stratos.autoscaler.util.ServiceReferenceHolder; import org.apache.stratos.messaging.domain.applications.Application; import org.apache.stratos.messaging.domain.applications.ApplicationStatus; import org.apache.stratos.messaging.domain.applications.Group; import org.apache.stratos.messaging.domain.applications.GroupStatus; +import org.apache.stratos.messaging.domain.instance.ApplicationInstance; import org.apache.stratos.messaging.domain.instance.GroupInstance; import org.apache.stratos.messaging.domain.instance.Instance; import org.apache.stratos.messaging.domain.topology.ClusterStatus; @@ -71,6 +73,8 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { //network partition contexts private Map<String, GroupLevelNetworkPartitionContext> networkPartitionCtxts; + private Map<String, GroupInstance> groupInstanceIdMap; + private boolean isDestroyed; /** @@ -85,9 +89,7 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { super(group); this.appId = appId; networkPartitionCtxts = new HashMap<String, GroupLevelNetworkPartitionContext>(); - - //starting the minimum start able dependencies - //startMinimumDependencies(group, parentInstanceId); + setGroupInstanceIdMap(new HashMap<String, GroupInstance>()); } @Override @@ -131,6 +133,7 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { * @param status status of the group */ public void setStatus(GroupStatus status, String instanceId) { + this.groupInstanceIdMap.get(instanceId).setStatus(status); if (status == GroupStatus.Inactive && !this.hasStartupDependents) { log.info("[Group] " + this.id + "is not notifying the parent, " + @@ -192,14 +195,6 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { AutoscalerContext.getInstance().removeClusterMonitor(id); } } - //If cluster monitor, need to terminate the existing one - //TODO block - /*if (this.status == GroupStatus.Terminating) { - StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId); - } else { - onChildTerminatedEvent(id); - }*/ - } else if (status1 == ClusterStatus.Terminating || status1 == GroupStatus.Terminating) { //mark the child monitor as inActive in the map this.markMonitorAsTerminating(id); @@ -212,11 +207,24 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { } else { log.warn("[monitor] " + id + " cannot be found in the inActive monitors list"); } - //TODO block - /*if (this.status == GroupStatus.Terminating || this.status == GroupStatus.Terminated) { - StatusChecker.getInstance().onChildStatusChange(id, this.id, this.appId); - log.info("Executing the un-subscription request for the [monitor] " + id); - }*/ + //If cluster monitor, need to terminate the existing one + ApplicationHolder.releaseReadLock(); + GroupStatus instanceStatus; + try { + Group group = ApplicationHolder.getApplications(). + getApplication(appId).getGroupRecursively(this.id); + instanceStatus = group.getInstanceContexts(instanceId).getStatus(); + + } finally { + ApplicationHolder.releaseReadLock(); + } + + if (instanceStatus == GroupStatus.Terminating) { + ServiceReferenceHolder.getInstance().getGroupStatusProcessorChain().process(this.id, + appId, instanceId); + } else { + onChildTerminatedEvent(id, instanceId); + } } } @@ -258,7 +266,8 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { //Notifying children, if this group has scaling dependencies if (currentChildContextInScalingTree.isGroupScalingEnabled()) { - for (ApplicationChildContext applicationChildContext : currentChildContextInScalingTree.getApplicationChildContextList()) { + for (ApplicationChildContext applicationChildContext : + currentChildContextInScalingTree.getApplicationChildContextList()) { //Get group monitor so that it can notify it's children Monitor monitor = aliasToActiveMonitorsMap.get(applicationChildContext.getId()); @@ -480,4 +489,21 @@ public class GroupMonitor extends ParentComponentMonitor implements Runnable { public void setDestroyed(boolean isDestroyed) { this.isDestroyed = isDestroyed; } + + public Map<String, GroupInstance> getGroupInstanceIdMap() { + return groupInstanceIdMap; + } + + public void setGroupInstanceIdMap(Map<String, GroupInstance> groupInstanceIdMap) { + this.groupInstanceIdMap = groupInstanceIdMap; + } + + public void addGroupInstance(GroupInstance instance) { + this.groupInstanceIdMap.put(instance.getInstanceId(), instance); + + } + + public GroupInstance getGroupInstance(String instanceId) { + return this.groupInstanceIdMap.get(instanceId); + } } http://git-wip-us.apache.org/repos/asf/stratos/blob/a8ed191f/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ParentComponentMonitor.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ParentComponentMonitor.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ParentComponentMonitor.java index 56e2a63..f48461f 100644 --- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ParentComponentMonitor.java +++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/component/ParentComponentMonitor.java @@ -63,9 +63,9 @@ public abstract class ParentComponentMonitor extends Monitor { protected DependencyTree scalingDependencyTree; //monitors map, key=GroupAlias/clusterId and value=GroupMonitor/AbstractClusterMonitor protected Map<String, Monitor> aliasToActiveMonitorsMap; - //monitors map, stopped monitors + //instanceIds map, stopped monitors protected List<String> inactiveMonitorsList; - //terminating monitors list + //terminating instances list protected List<String> terminatingMonitorsList; public ParentComponentMonitor(ParentComponent component) throws DependencyBuilderException { @@ -109,6 +109,18 @@ public abstract class ParentComponentMonitor extends Monitor { * This will start the parallel dependencies at once from the top level. * it will get invoked when the monitor starts up only. */ + public boolean startDependencyByInstanceCreation(String childId, String instanceId) throws + ParentMonitorNotFoundException { + //start the first dependency + List<ApplicationChildContext> applicationContexts = + this.startupDependencyTree.getStarAbleDependencies(childId); + return startDependency(applicationContexts, instanceId); + } + + /** + * This will start the parallel dependencies at once from the top level. + * it will get invoked when the monitor starts up only. + */ public void startDependency(ParentComponent component) { //start the first dependency List<ApplicationChildContext> applicationContexts = this.startupDependencyTree. @@ -132,7 +144,8 @@ public abstract class ParentComponentMonitor extends Monitor { .getStarAbleDependencies(id); List<String> instanceIds = new ArrayList<String>(); instanceIds.add(instanceId); - return startDependency(applicationContexts, instanceIds); + boolean startup = startDependency(applicationContexts, instanceIds); + return startup; } public boolean startAllChildrenDependency(ParentComponent component, String instanceId) @@ -242,8 +255,13 @@ public abstract class ParentComponentMonitor extends Monitor { if (this.terminatingMonitorsList.contains(eventId)) { this.terminatingMonitorsList.remove(eventId); } + boolean startDep; + if(!aliasToActiveMonitorsMap.containsKey(eventId)) { + startDep = startDependency(eventId, instanceId); + } else { + startDep = startDependencyByInstanceCreation(eventId, instanceId); + } - boolean startDep = startDependency(eventId, instanceId); if (log.isDebugEnabled()) { log.debug("started a child: " + startDep + " by the group/cluster: " + eventId); @@ -351,8 +369,8 @@ public abstract class ParentComponentMonitor extends Monitor { boolean allParentsActive = false; //make sure all the parent contexts got terminated or whether all of them are active if (parentContexts != null) { - parentsTerminated = allParentTerminated(parentContexts); - allParentsActive = allParentActive(parentContexts); + parentsTerminated = allParentTerminated(parentContexts, instanceId); + allParentsActive = allParentActive(parentContexts, instanceId); } if ((terminationList.isEmpty() || allDependentTerminated) && @@ -391,7 +409,8 @@ public abstract class ParentComponentMonitor extends Monitor { } - private boolean allParentTerminated(List<ApplicationChildContext> parentContexts) { + private boolean allParentTerminated(List<ApplicationChildContext> parentContexts, + String instanceId) { boolean parentsTerminated = false; for (ApplicationChildContext context1 : parentContexts) { if (this.inactiveMonitorsList.contains(context1.getId())) { @@ -413,11 +432,11 @@ public abstract class ParentComponentMonitor extends Monitor { return parentsTerminated; } - private boolean allParentActive(List<ApplicationChildContext> parentContexts) { + private boolean allParentActive(List<ApplicationChildContext> parentContexts, String instanceId) { boolean parentsActive = false; for (ApplicationChildContext context1 : parentContexts) { - if (this.inactiveMonitorsList.contains(context1.getId()) || - this.terminatingMonitorsList.contains(context1.getId())) { + if (this.inactiveMonitorsList.contains(instanceId) || + this.terminatingMonitorsList.contains(instanceId)) { parentsActive = false; log.info("Dependent [Monitor] " + context1.getId() + " is not yet active"); @@ -632,4 +651,4 @@ public abstract class ParentComponentMonitor extends Monitor { } -} \ No newline at end of file +}
