http://git-wip-us.apache.org/repos/asf/stratos/blob/aece0c4a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java ---------------------------------------------------------------------- diff --cc products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java index 0000000,721a5c6..ad4887b mode 000000,100644..100644 --- a/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java +++ b/products/stratos/modules/integration/test-common/src/main/java/org/apache/stratos/integration/common/TopologyHandler.java @@@ -1,0 -1,686 +1,700 @@@ + /* + * Copyright 2005-2015 WSO2, Inc. (http://wso2.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + package org.apache.stratos.integration.common; + + import org.apache.commons.logging.Log; + import org.apache.commons.logging.LogFactory; + import org.apache.stratos.autoscaler.stub.pojo.ApplicationContext; + import org.apache.stratos.common.client.AutoscalerServiceClient; + import org.apache.stratos.common.threading.StratosThreadPool; + import org.apache.stratos.messaging.domain.application.*; + import org.apache.stratos.messaging.domain.instance.ClusterInstance; + import org.apache.stratos.messaging.domain.instance.GroupInstance; + import org.apache.stratos.messaging.domain.topology.Cluster; + import org.apache.stratos.messaging.domain.topology.Member; + import org.apache.stratos.messaging.domain.topology.MemberStatus; + import org.apache.stratos.messaging.domain.topology.Service; + import org.apache.stratos.messaging.event.Event; + import org.apache.stratos.messaging.event.application.*; + import org.apache.stratos.messaging.event.topology.*; + import org.apache.stratos.messaging.listener.application.*; + import org.apache.stratos.messaging.listener.topology.*; + import org.apache.stratos.messaging.message.receiver.application.ApplicationManager; + import org.apache.stratos.messaging.message.receiver.application.ApplicationsEventReceiver; + import org.apache.stratos.messaging.message.receiver.topology.TopologyEventReceiver; + import org.apache.stratos.messaging.message.receiver.topology.TopologyManager; + import org.apache.stratos.mock.iaas.client.MockIaasApiClient; + + import java.rmi.RemoteException; + import java.util.*; + import java.util.concurrent.ConcurrentHashMap; + import java.util.concurrent.ExecutorService; + + import static org.testng.AssertJUnit.*; + + /** + * To start the Topology receivers + */ + public class TopologyHandler { + private static final Log log = LogFactory.getLog(TopologyHandler.class); + + public static final int APPLICATION_ACTIVATION_TIMEOUT = 500000; + public static final int APPLICATION_UNDEPLOYMENT_TIMEOUT = 500000; + public static final int MEMBER_TERMINATION_TIMEOUT = 500000; + public static final int APPLICATION_TOPOLOGY_TIMEOUT = 120000; + public static final String APPLICATION_STATUS_CREATED = "Created"; + public static final String APPLICATION_STATUS_UNDEPLOYING = "Undeploying"; + private ApplicationsEventReceiver applicationsEventReceiver; + private TopologyEventReceiver topologyEventReceiver; + public static TopologyHandler topologyHandler; + private Map<String, Long> terminatedMembers = new ConcurrentHashMap<String, Long>(); + private Map<String, Long> terminatingMembers = new ConcurrentHashMap<String, Long>(); + private Map<String, Long> createdMembers = new ConcurrentHashMap<String, Long>(); + private Map<String, Long> inActiveMembers = new ConcurrentHashMap<String, Long>(); + private Map<String, Long> activateddMembers = new ConcurrentHashMap<String, Long>(); + + private TopologyHandler() { + initializeApplicationEventReceiver(); + initializeTopologyEventReceiver(); + assertApplicationTopologyInitialized(); + assertTopologyInitialized(); + addTopologyEventListeners(); + addApplicationEventListeners(); + } + + public static TopologyHandler getInstance() { + if (topologyHandler == null) { + synchronized (TopologyHandler.class) { + if (topologyHandler == null) { + topologyHandler = new TopologyHandler(); + } + } + } + return topologyHandler; + } + + /** + * Initialize application event receiver + */ + private void initializeApplicationEventReceiver() { + if (applicationsEventReceiver == null) { + applicationsEventReceiver = new ApplicationsEventReceiver(); + ExecutorService executorService = StratosThreadPool.getExecutorService("STRATOS_TEST_SERVER", 1); + applicationsEventReceiver.setExecutorService(executorService); + applicationsEventReceiver.execute(); + } + } + + /** + * Initialize Topology event receiver + */ + private void initializeTopologyEventReceiver() { + if (topologyEventReceiver == null) { + topologyEventReceiver = new TopologyEventReceiver(); + ExecutorService executorService = StratosThreadPool.getExecutorService("STRATOS_TEST_SERVER1", 1); + topologyEventReceiver.setExecutorService(executorService); + topologyEventReceiver.execute(); + } + } + + /** + * Assert application Topology initialization + */ + private void assertApplicationTopologyInitialized() { + long startTime = System.currentTimeMillis(); + boolean applicationTopologyInitialized = ApplicationManager.getApplications().isInitialized(); + while (!applicationTopologyInitialized) { + try { + Thread.sleep(1000); + } catch (InterruptedException ignore) { + } + applicationTopologyInitialized = ApplicationManager.getApplications().isInitialized(); + if ((System.currentTimeMillis() - startTime) > APPLICATION_TOPOLOGY_TIMEOUT) { + break; + } + } - assertEquals(String.format("Application Topology didn't get initialized "), applicationTopologyInitialized, - true); ++ assertEquals(String.format("Application Topology didn't get initialized "), applicationTopologyInitialized, true); + } + + /** + * Assert Topology initialization + */ + private void assertTopologyInitialized() { + long startTime = System.currentTimeMillis(); + boolean topologyInitialized = TopologyManager.getTopology().isInitialized(); + while (!topologyInitialized) { + try { + Thread.sleep(1000); + } catch (InterruptedException ignore) { + } + topologyInitialized = TopologyManager.getTopology().isInitialized(); + if ((System.currentTimeMillis() - startTime) > APPLICATION_TOPOLOGY_TIMEOUT) { + break; + } + } + assertEquals(String.format("Topology didn't get initialized "), topologyInitialized, true); + } + + /** + * Assert application activation + * ++ * @param tenantId + * @param applicationName + */ - public void assertApplicationStatus(String applicationName, ApplicationStatus status) { ++ public void assertApplicationStatus(String applicationName, ApplicationStatus status, int tenantId) { + long startTime = System.currentTimeMillis(); - Application application = ApplicationManager.getApplications().getApplication(applicationName); ++ Application application = ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId); + while (!((application != null) && (application.getStatus() == status))) { + try { + Thread.sleep(1000); + } catch (InterruptedException ignore) { + } + application = ApplicationManager.getApplications().getApplication(applicationName); + if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { + log.error("Application did not activate within timeout period"); + break; + } + } + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); - assertEquals(String.format("Application status did not change to %s: [application-id] %s", status.toString(), - applicationName), status, application.getStatus()); ++ assertEquals(String.format("Application status did not change to %s: [application-id] %s", ++ status.toString(), applicationName), ++ status, application.getStatus()); ++ } ++ ++ public Application getApplication(String applicationName, int tenantId) { ++ return ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId); ++ } ++ ++ public void assertApplicationForNonAvailability(String applicationName, int tenantId) { ++ ++ Application application = ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId); ++ assertNull(String.format("Application is found for other tenant : [application-id] %s", applicationName),application); + } + + /** + * Assert application activation + * + * @param applicationName ++ * @param tenantId + */ + public void assertGroupActivation(String applicationName) { + Application application = ApplicationManager.getApplications().getApplication(applicationName); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + + Collection<Group> groups = application.getAllGroupsRecursively(); + for (Group group : groups) { + assertEquals(group.getInstanceContextCount() >= group.getGroupMinInstances(), true); + } + } + + /** + * Assert application activation + * + * @param applicationName ++ * @param tenantId + */ + public void assertClusterActivation(String applicationName) { + Application application = ApplicationManager.getApplications().getApplication(applicationName); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + + Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); + for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { - String serviceName = clusterDataHolder.getServiceType(); ++ String serviceUuid = clusterDataHolder.getServiceUuid(); + String clusterId = clusterDataHolder.getClusterId(); + Service service = TopologyManager.getTopology().getService(serviceName); + assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", applicationName, + serviceName), service); + + Cluster cluster = service.getCluster(clusterId); + assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", + applicationName, serviceName, clusterId), cluster); + for (Member member : cluster.getMembers()) { + log.info(String.format("Member [member-id] %s found in cluster instance [cluster-instance] %s of " + + "cluster [cluster-id] %s", member.getMemberId(), member.getClusterInstanceId(), + member.getClusterId())); + } + boolean clusterActive = false; - int activeInstances; ++ + for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { - log.info("Checking for active members in cluster instance: " + instance.getInstanceId()); - activeInstances = 0; ++ int activeInstances = 0; + for (Member member : cluster.getMembers()) { + if (member.getClusterInstanceId().equals(instance.getInstanceId())) { + if (member.getStatus().equals(MemberStatus.Active)) { + activeInstances++; + } + } + } + clusterActive = (activeInstances >= clusterDataHolder.getMinInstances()); + assertTrue(String.format("Cluster status did not change to active: [cluster-id] %s", clusterId), + clusterActive); + } ++ assertEquals(String.format("Cluster status did not change to active: [cluster-id] %s", clusterId), ++ clusterActive, true); + } ++ + } + + /** - * Get all the members that belongs to the cluster identified by cartridge name and application name in the - * topology ++ * Assert application activation + * - * @param cartridgeName ++ * @param tenantId + * @param applicationName + */ - public Map<String, Member> getMembersForCluster(String cartridgeName, String applicationName) { - Application application = ApplicationManager.getApplications().getApplication(applicationName); - assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); ++ ++ public void terminateMemberFromCluster(String cartridgeName, String applicationName, IntegrationMockClient mockIaasApiClient, int tenantId) { ++ Application application = ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId); ++ assertNotNull(String.format("Application is not found: [application-id] %s", ++ applicationName), application); ++ + Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); - Map<String, Member> memberMap = new HashMap<String, Member>(); + for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { - String serviceName = clusterDataHolder.getServiceType(); - if (cartridgeName.equals(serviceName)) { ++ String serviceUuid = clusterDataHolder.getServiceUuid(); ++ if(cartridgeName.equals(serviceUuid)) { + String clusterId = clusterDataHolder.getClusterId(); + Service service = TopologyManager.getTopology().getService(serviceName); + assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", applicationName, + serviceName), service); + + Cluster cluster = service.getCluster(clusterId); + assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", - applicationName, serviceName, clusterId), cluster); ++ applicationName, serviceUuid, clusterId), cluster); ++ boolean memberTerminated = false; ++ + for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { + for (Member member : cluster.getMembers()) { - memberMap.put(member.getMemberId(), member); ++ if (member.getClusterInstanceId().equals(instance.getInstanceId())) { ++ if (member.getStatus().equals(MemberStatus.Active)) { ++ mockIaasApiClient.terminateInstance(member.getMemberId()); ++ memberTerminated = true; ++ break; ++ } ++ } + } - } - } - } - return memberMap; - } + + /** + * Terminate a member in mock iaas directly without involving Stratos REST API + * This is similar to manually terminating an instance in an IaaS. This could be used to simulate member failures + * + * @param memberId + * @param mockIaasApiClient + */ + public void terminateMemberInMockIaas(String memberId, MockIaasApiClient mockIaasApiClient) { + boolean memberTerminated = false; + memberTerminated = mockIaasApiClient.terminateInstance(memberId); + assertTrue(String.format("Member [member-id] %s couldn't be terminated from the mock IaaS", memberId), + memberTerminated); + } + + public void assertMemberTermination(String memberId) { + long startTime = System.currentTimeMillis(); + assertNotNull(String.format("Member id is not found: [member-id] %s", memberId)); + boolean hasMemberRemoved = false; + while (!hasMemberRemoved) { + // Wait until the member gets removed by MemberTerminatedEvent topology receiver + if (getTerminatingMembers().get(memberId) == null && + getInActiveMembers().get(memberId) == null && + getActivateddMembers().get(memberId) == null && + getCreatedMembers().get(memberId) == null) { + getTerminatedMembers().remove(memberId); + hasMemberRemoved = true; + } else { + if (getTerminatedMembers().get(memberId) - startTime > MEMBER_TERMINATION_TIMEOUT) { + log.error("Member did not get removed from the topology within timeout period"); + break; + } ++ assertTrue("Any member couldn't be terminated from the mock IaaS client", memberTerminated); + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + log.error("Could not sleep", e); + } + } - assertTrue(String.format("Member [member-id] %s did not get removed from the topology", memberId), - hasMemberRemoved); ++ + } + - public void assertClusterMinMemberCount(String applicationName, int minMembers) { ++ public void assertClusterMinMemberCount(String applicationName, int minMembers, int tenantId) { + long startTime = System.currentTimeMillis(); + + Application application = ApplicationManager.getApplications().getApplication(applicationName); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + + Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); + for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { - String serviceName = clusterDataHolder.getServiceType(); ++ String serviceName = clusterDataHolder.getServiceUuid(); + String clusterId = clusterDataHolder.getClusterId(); + Service service = TopologyManager.getTopology().getService(serviceName); + assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", applicationName, + serviceName), service); + + Cluster cluster = service.getCluster(clusterId); + assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", + applicationName, serviceName, clusterId), cluster); + boolean clusterActive = false; + + for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { + int activeInstances = 0; + for (Member member : cluster.getMembers()) { + if (member.getClusterInstanceId().equals(instance.getInstanceId())) { + if (member.getStatus().equals(MemberStatus.Active)) { + activeInstances++; + } + } + } + clusterActive = activeInstances >= minMembers; + + while (!clusterActive) { + try { + Thread.sleep(1000); + } catch (InterruptedException ignore) { + } + service = TopologyManager.getTopology().getService(serviceName); + assertNotNull( + String.format("Service is not found: [application-id] %s [service] %s", applicationName, + serviceName), service); + + cluster = service.getCluster(clusterId); + activeInstances = 0; + for (Member member : cluster.getMembers()) { + if (member.getClusterInstanceId().equals(instance.getInstanceId())) { + if (member.getStatus().equals(MemberStatus.Active)) { + activeInstances++; + } + } + } + clusterActive = activeInstances >= minMembers; - assertNotNull( - String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", - applicationName, serviceName, clusterId), cluster); ++ assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", ++ applicationName, serviceName, clusterId), cluster); + + if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { + log.error("Cluster did not activate within timeout period"); + break; + } + } + } + assertEquals(String.format("Cluster status did not change to active: [cluster-id] %s", clusterId), + clusterActive, true); + } + + } + + /** + * Assert application activation + * + * @param applicationName ++ * @param tenantId + */ - public boolean assertApplicationUndeploy(String applicationName) { ++ public boolean assertApplicationUndeploy(String applicationName, int tenantId) { + long startTime = System.currentTimeMillis(); - Application application = ApplicationManager.getApplications().getApplication(applicationName); ++ Application application = ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId); + ApplicationContext applicationContext = null; + try { + applicationContext = AutoscalerServiceClient.getInstance().getApplication(applicationName); + } catch (RemoteException e) { + log.error("Error while getting the application context for [application] " + applicationName); + } + while (((application != null) && application.getInstanceContextCount() > 0) || (applicationContext == null + || applicationContext.getStatus().equals(APPLICATION_STATUS_UNDEPLOYING))) { + try { + Thread.sleep(1000); + } catch (InterruptedException ignore) { + } + application = ApplicationManager.getApplications().getApplication(applicationName); + try { + applicationContext = AutoscalerServiceClient.getInstance().getApplication(applicationName); + } catch (RemoteException e) { + log.error("Error while getting the application context for [application] " + applicationName); + } + if ((System.currentTimeMillis() - startTime) > APPLICATION_UNDEPLOYMENT_TIMEOUT) { + log.error("Application did not undeploy within timeout period"); + break; + } + } + + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + assertNotNull(String.format("Application Context is not found: [application-id] %s", applicationName), + applicationContext); + + //Force undeployment after the graceful deployment + if (application.getInstanceContextCount() > 0 || applicationContext.getStatus() + .equals(APPLICATION_STATUS_UNDEPLOYING)) { + return false; + } - assertEquals( - String.format("Application status did not change to Created: [application-id] %s", applicationName), ++ assertEquals(String.format("Application status did not change to Created: [application-id] %s", applicationName), + APPLICATION_STATUS_CREATED, applicationContext.getStatus()); + return true; + } + + /** + * Assert application activation + * ++ * @param tenantId + * @param applicationName + */ - public void assertGroupInstanceCount(String applicationName, String groupAlias, int count) { ++ public void assertGroupInstanceCount(String applicationName, String groupAlias, int count, int tenantId) { + long startTime = System.currentTimeMillis(); - Application application = ApplicationManager.getApplications().getApplication(applicationName); ++ Application application = ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId); + if (application != null) { + Group group = application.getGroupRecursively(groupAlias); + while (group.getInstanceContextCount() != count) { + try { + Thread.sleep(1000); + } catch (InterruptedException ignore) { + } + if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { + log.error("Group instance min count check failed within timeout period"); + break; + } + } + for (GroupInstance instance : group.getInstanceIdToInstanceContextMap().values()) { + while (!instance.getStatus().equals(GroupStatus.Active)) { + try { + Thread.sleep(1000); + } catch (InterruptedException ignore) { + } + if ((System.currentTimeMillis() - startTime) > APPLICATION_ACTIVATION_TIMEOUT) { + log.error("Application did not activate within timeout period"); + break; + } + } + } - assertEquals( - String.format("Application status did not change to active: [application-id] %s", applicationName), ++ assertEquals(String.format("Application status did not change to active: [application-id] %s", applicationName), + group.getInstanceContextCount(), count); + } + assertNotNull(String.format("Application is not found: [application-id] %s", applicationName), application); + + } + + public void assertApplicationNotExists(String applicationName) { + Application application = ApplicationManager.getApplications().getApplication(applicationName); - assertNull(String.format("Application is found in the topology : [application-id] %s", applicationName), - application); ++ assertNull(String.format("Application is found in the topology : [application-id] %s", applicationName), application); + } + + private void addTopologyEventListeners() { + topologyEventReceiver.addEventListener(new MemberTerminatedEventListener() { + @Override + protected void onEvent(Event event) { + MemberTerminatedEvent memberTerminatedEvent = (MemberTerminatedEvent) event; + getTerminatedMembers().put(memberTerminatedEvent.getMemberId(), System.currentTimeMillis()); - getActivateddMembers().remove(((MemberTerminatedEvent) event).getMemberId()); - getCreatedMembers().remove(((MemberTerminatedEvent) event).getMemberId()); - getInActiveMembers().remove(((MemberTerminatedEvent) event).getMemberId()); - getTerminatingMembers().remove(((MemberTerminatedEvent) event).getMemberId()); ++ + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceCreatedEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceCreatedEvent event1 = (ClusterInstanceCreatedEvent) event; + String clusterId = event1.getClusterId(); + getCreatedMembers().put(clusterId, System.currentTimeMillis()); + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceActivatedEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceActivatedEvent event1 = (ClusterInstanceActivatedEvent) event; + String clusterId = event1.getClusterId(); + getActivateddMembers().put(clusterId, System.currentTimeMillis()); + + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceInactivateEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceInactivateEvent event1 = (ClusterInstanceInactivateEvent) event; + String clusterId = event1.getClusterId(); + getInActiveMembers().put(clusterId, System.currentTimeMillis()); + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceTerminatedEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceTerminatedEvent event1 = (ClusterInstanceTerminatedEvent) event; + String clusterId = event1.getClusterId(); + getTerminatedMembers().put(clusterId, System.currentTimeMillis()); + } + }); + + topologyEventReceiver.addEventListener(new ClusterInstanceTerminatingEventListener() { + @Override + protected void onEvent(Event event) { + ClusterInstanceTerminatingEvent event1 = (ClusterInstanceTerminatingEvent) event; + String clusterId = event1.getClusterId(); + getTerminatingMembers().put(clusterId, System.currentTimeMillis()); + } + }); + + } + + private void addApplicationEventListeners() { + + applicationsEventReceiver.addEventListener(new GroupInstanceCreatedEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceCreatedEvent event1 = (GroupInstanceCreatedEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getGroupInstance().getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getCreatedMembers().put(id, System.currentTimeMillis()); + + } + }); + + applicationsEventReceiver.addEventListener(new GroupInstanceActivatedEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceActivatedEvent event1 = (GroupInstanceActivatedEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getActivateddMembers().put(id, System.currentTimeMillis()); + } + }); + + applicationsEventReceiver.addEventListener(new GroupInstanceInactivateEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceInactivatedEvent event1 = (GroupInstanceInactivatedEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getInActiveMembers().put(id, System.currentTimeMillis()); + } + }); + + applicationsEventReceiver.addEventListener(new GroupInstanceTerminatedEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceTerminatedEvent event1 = (GroupInstanceTerminatedEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getTerminatedMembers().put(id, System.currentTimeMillis()); + } + }); + + applicationsEventReceiver.addEventListener(new GroupInstanceTerminatingEventListener() { + @Override + protected void onEvent(Event event) { + GroupInstanceTerminatingEvent event1 = (GroupInstanceTerminatingEvent) event; + String appId = event1.getAppId(); + String groupId = event1.getGroupId(); + String instanceId = event1.getInstanceId(); + String id = generateId(appId, groupId, instanceId); + getTerminatingMembers().put(id, System.currentTimeMillis()); + } + }); + } + + public String generateId(String appId, String groupId, String instanceId) { + return appId + "-" + groupId + "-" + instanceId; + } + - public String getClusterIdFromAlias(String applicationId, String alias) { - Application application = ApplicationManager.getApplications().getApplication(applicationId); ++ public String getClusterIdFromAlias(String applicationId, String alias,int tenantId) { ++ Application application = ApplicationManager.getApplications().getApplicationByTenant(applicationId,tenantId); + assertNotNull(application); + + ClusterDataHolder dataHolder = application.getClusterDataHolderRecursivelyByAlias(alias); + assertNotNull(dataHolder); + + return dataHolder.getClusterId(); + } + + public void removeMembersFromMaps(String applicationId) { - for (Map.Entry<String, Long> entry : getActivateddMembers().entrySet()) { - if (entry.getKey().contains(applicationId)) { ++ for(Map.Entry<String, Long> entry: getActivateddMembers().entrySet()) { ++ if(entry.getKey().contains(applicationId)) { + getActivateddMembers().remove(entry.getKey()); + } + } + - for (Map.Entry<String, Long> entry : getTerminatedMembers().entrySet()) { - if (entry.getKey().contains(applicationId)) { ++ for(Map.Entry<String, Long> entry: getTerminatedMembers().entrySet()) { ++ if(entry.getKey().contains(applicationId)) { + getTerminatedMembers().remove(entry.getKey()); + } + } + } + + public Map<String, Long> getTerminatedMembers() { + return terminatedMembers; + } + + public void setTerminatedMembers(Map<String, Long> terminatedMembers) { + this.terminatedMembers = terminatedMembers; + } + + public Map<String, Long> getTerminatingMembers() { + return terminatingMembers; + } + + public void setTerminatingMembers(Map<String, Long> terminatingMembers) { + this.terminatingMembers = terminatingMembers; + } + + public Map<String, Long> getCreatedMembers() { + return createdMembers; + } + + public void setCreatedMembers(Map<String, Long> createdMembers) { + this.createdMembers = createdMembers; + } + + public Map<String, Long> getInActiveMembers() { + return inActiveMembers; + } + + public void setInActiveMembers(Map<String, Long> inActiveMembers) { + this.inActiveMembers = inActiveMembers; + } + + public Map<String, Long> getActivateddMembers() { + return activateddMembers; + } + + public void setActivateddMembers(Map<String, Long> activateddMembers) { + this.activateddMembers = activateddMembers; + } + + public List<Member> getMembersForApplication(String applicationId) { + Application application = ApplicationManager.getApplications().getApplication(applicationId); + assertNotNull(String.format("Application is not found: [application-id] %s", applicationId), application); + Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); + List<Member> memberList = new ArrayList<>(); + for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { + String serviceName = clusterDataHolder.getServiceType(); + String clusterId = clusterDataHolder.getClusterId(); + Service service = TopologyManager.getTopology().getService(serviceName); + assertNotNull( + String.format("Service is not found: [application-id] %s [service] %s", applicationId, serviceName), + service); + Cluster cluster = service.getCluster(clusterId); + assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", + applicationId, serviceName, clusterId), cluster); + for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { + for (Member member : cluster.getMembers()) { + memberList.add(member); + } + } + } + return memberList; + } + }
http://git-wip-us.apache.org/repos/asf/stratos/blob/aece0c4a/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SingleClusterScalingTestCase.java ---------------------------------------------------------------------- diff --cc products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SingleClusterScalingTestCase.java index 0000000,9598df0..e84bc3c mode 000000,100644..100644 --- a/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SingleClusterScalingTestCase.java +++ b/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/application/SingleClusterScalingTestCase.java @@@ -1,0 -1,358 +1,7 @@@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ + package org.apache.stratos.integration.tests.application; + -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.stratos.common.beans.application.ApplicationBean; -import org.apache.stratos.common.beans.policy.deployment.ApplicationPolicyBean; -import org.apache.stratos.integration.common.RestConstants; -import org.apache.stratos.integration.common.TopologyHandler; -import org.apache.stratos.integration.tests.StratosIntegrationTest; -import org.apache.stratos.messaging.domain.application.Application; -import org.apache.stratos.messaging.domain.application.ApplicationStatus; -import org.apache.stratos.messaging.domain.application.ClusterDataHolder; -import org.apache.stratos.messaging.domain.instance.ClusterInstance; -import org.apache.stratos.messaging.domain.topology.Cluster; -import org.apache.stratos.messaging.domain.topology.Member; -import org.apache.stratos.messaging.domain.topology.MemberStatus; -import org.apache.stratos.messaging.domain.topology.Service; -import org.apache.stratos.messaging.message.receiver.application.ApplicationManager; -import org.apache.stratos.messaging.message.receiver.topology.TopologyManager; -import org.testng.annotations.Test; - -import java.util.Set; - -import static org.testng.Assert.assertEquals; -import static org.testng.AssertJUnit.assertNotNull; - + /** - * This will handle the scale-up and scale-down of a particular cluster bursting test cases ++ * Created by gayan on 11/2/15. + */ -public class SingleClusterScalingTestCase extends StratosIntegrationTest { - private static final Log log = LogFactory.getLog(SingleClusterScalingTestCase.class); - private static final String RESOURCES_PATH = "/single-cluster-scaling-test"; - private static final int CLUSTER_SCALE_UP_TIMEOUT = 180000; - private static final int CLUSTER_SCALE_DOWN_TIMEOUT = 360000; - private int activeInstancesAfterScaleup = 0; - - @Test(timeOut = APPLICATION_TEST_TIMEOUT, groups = {"stratos.application.deployment"}) - public void testDeployApplication() throws Exception { - TopologyHandler topologyHandler = TopologyHandler.getInstance(); - String autoscalingPolicyId = "autoscaling-policy-single-cluster-scaling-test"; - - boolean addedScalingPolicy = restClient.addEntity(RESOURCES_PATH + RestConstants.AUTOSCALING_POLICIES_PATH - + "/" + autoscalingPolicyId + ".json", - RestConstants.AUTOSCALING_POLICIES, RestConstants.AUTOSCALING_POLICIES_NAME); - assertEquals(addedScalingPolicy, true); - - boolean addedC1 = restClient.addEntity( - RESOURCES_PATH + RestConstants.CARTRIDGES_PATH + "/" + "c7-single-cluster-scaling-test.json", - RestConstants.CARTRIDGES, RestConstants.CARTRIDGES_NAME); - assertEquals(addedC1, true); - - boolean addedN1 = restClient.addEntity(RESOURCES_PATH + RestConstants.NETWORK_PARTITIONS_PATH + "/" + - "network-partition-single-cluster-scaling-test.json", - RestConstants.NETWORK_PARTITIONS, RestConstants.NETWORK_PARTITIONS_NAME); - assertEquals(addedN1, true); - - boolean addedDep = restClient.addEntity(RESOURCES_PATH + RestConstants.DEPLOYMENT_POLICIES_PATH + "/" + - "deployment-policy-single-cluster-scaling-test.json", - RestConstants.DEPLOYMENT_POLICIES, RestConstants.DEPLOYMENT_POLICIES_NAME); - assertEquals(addedDep, true); - - boolean added = restClient.addEntity(RESOURCES_PATH + RestConstants.APPLICATIONS_PATH + "/" + - "single-cluster-scaling-test.json", RestConstants.APPLICATIONS, - RestConstants.APPLICATIONS_NAME); - assertEquals(added, true); - - ApplicationBean bean = (ApplicationBean) restClient.getEntity(RestConstants.APPLICATIONS, - "single-cluster-scaling-test", ApplicationBean.class, RestConstants.APPLICATIONS_NAME); - assertEquals(bean.getApplicationId(), "single-cluster-scaling-test"); - - boolean addAppPolicy = restClient.addEntity(RESOURCES_PATH + RestConstants.APPLICATION_POLICIES_PATH + "/" + - "application-policy-single-cluster-scaling-test.json", RestConstants.APPLICATION_POLICIES, - RestConstants.APPLICATION_POLICIES_NAME); - assertEquals(addAppPolicy, true); - - ApplicationPolicyBean policyBean = (ApplicationPolicyBean) restClient.getEntity( - RestConstants.APPLICATION_POLICIES, - "application-policy-single-cluster-scaling-test", ApplicationPolicyBean.class, - RestConstants.APPLICATION_POLICIES_NAME); - - //deploy the application - String resourcePath = RestConstants.APPLICATIONS + "/" + "single-cluster-scaling-test" + - RestConstants.APPLICATIONS_DEPLOY + "/" + "application-policy-single-cluster-scaling-test"; - boolean deployed = restClient.deployEntity(resourcePath, - RestConstants.APPLICATIONS_NAME); - assertEquals(deployed, true); - - //Application active handling - topologyHandler.assertApplicationStatus(bean.getApplicationId() - , ApplicationStatus.Active); - - //Cluster active handling - topologyHandler.assertClusterActivation(bean.getApplicationId()); - - //Verifying whether members got created using round robin algorithm - assertClusterWithScalingup(bean.getApplicationId()); - - //assert scale-down - assertClusterWithScaleDown(bean.getApplicationId()); - - //Check whether cluster could scale-down upto the minimum - assertClusterScaleDownToMinimumCount(bean.getApplicationId()); - - boolean removedAuto = restClient.removeEntity(RestConstants.AUTOSCALING_POLICIES, - autoscalingPolicyId, RestConstants.AUTOSCALING_POLICIES_NAME); - assertEquals(removedAuto, false); - - boolean removedNet = restClient.removeEntity(RestConstants.NETWORK_PARTITIONS, - "network-partition-single-cluster-scaling-test", - RestConstants.NETWORK_PARTITIONS_NAME); - //Trying to remove the used network partition - assertEquals(removedNet, false); - - boolean removedDep = restClient.removeEntity(RestConstants.DEPLOYMENT_POLICIES, - "deployment-policy-single-cluster-scaling-test", RestConstants.DEPLOYMENT_POLICIES_NAME); - assertEquals(removedDep, false); - - //Un-deploying the application - String resourcePathUndeploy = RestConstants.APPLICATIONS + "/" + "single-cluster-scaling-test" + - RestConstants.APPLICATIONS_UNDEPLOY; - - boolean unDeployed = restClient.undeployEntity(resourcePathUndeploy, - RestConstants.APPLICATIONS_NAME); - assertEquals(unDeployed, true); - - boolean undeploy = topologyHandler.assertApplicationUndeploy("single-cluster-scaling-test"); - if (!undeploy) { - //Need to forcefully undeploy the application - log.info("Force undeployment is going to start for the [application] " + "single-cluster-scaling-test"); - - restClient.undeployEntity(RestConstants.APPLICATIONS + "/" + "single-cluster-scaling-test" + - RestConstants.APPLICATIONS_UNDEPLOY + "?force=true", RestConstants.APPLICATIONS); - - boolean forceUndeployed = topologyHandler.assertApplicationUndeploy("single-cluster-scaling-test"); - assertEquals(forceUndeployed, true, String.format("Forceful undeployment failed for the application %s", - "single-cluster-scaling-test")); - } - - boolean removed = restClient.removeEntity(RestConstants.APPLICATIONS, "single-cluster-scaling-test", - RestConstants.APPLICATIONS_NAME); - assertEquals(removed, true); - - ApplicationBean beanRemoved = (ApplicationBean) restClient.getEntity(RestConstants.APPLICATIONS, - "single-cluster-scaling-test", ApplicationBean.class, RestConstants.APPLICATIONS_NAME); - assertEquals(beanRemoved, null); - - boolean removedC1 = restClient.removeEntity(RestConstants.CARTRIDGES, "c7-single-cluster-scaling-test", - RestConstants.CARTRIDGES_NAME); - assertEquals(removedC1, true); - - - removedAuto = restClient.removeEntity(RestConstants.AUTOSCALING_POLICIES, - autoscalingPolicyId, RestConstants.AUTOSCALING_POLICIES_NAME); - assertEquals(removedAuto, true); - - removedDep = restClient.removeEntity(RestConstants.DEPLOYMENT_POLICIES, - "deployment-policy-single-cluster-scaling-test", RestConstants.DEPLOYMENT_POLICIES_NAME); - assertEquals(removedDep, true); - - removedNet = restClient.removeEntity(RestConstants.NETWORK_PARTITIONS, - "network-partition-single-cluster-scaling-test", RestConstants.NETWORK_PARTITIONS_NAME); - assertEquals(removedNet, false); - - - boolean removeAppPolicy = restClient.removeEntity(RestConstants.APPLICATION_POLICIES, - "application-policy-single-cluster-scaling-test", RestConstants.APPLICATION_POLICIES_NAME); - assertEquals(removeAppPolicy, true); - - removedNet = restClient.removeEntity(RestConstants.NETWORK_PARTITIONS, - "network-partition-single-cluster-scaling-test", RestConstants.NETWORK_PARTITIONS_NAME); - assertEquals(removedNet, true); - } - - /** - * Assert application activation - * - * @param applicationName - */ - private void assertClusterWithScalingup(String applicationName) { - Application application = ApplicationManager.getApplications().getApplication(applicationName); - assertNotNull(String.format("Application is not found: [application-id] %s", - applicationName), application); - boolean clusterScaleup = false; - String clusterId = null; - long startTime = System.currentTimeMillis(); - while (!clusterScaleup) { - try { - Thread.sleep(1000); - } - catch (InterruptedException ignore) { - } - Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); - for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { - String serviceName = clusterDataHolder.getServiceType(); - clusterId = clusterDataHolder.getClusterId(); - Service service = TopologyManager.getTopology().getService(serviceName); - assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", - applicationName, serviceName), service); - - Cluster cluster = service.getCluster(clusterId); - assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", - applicationName, serviceName, clusterId), cluster); - for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { - int activeInstances = 0; - for (Member member : cluster.getMembers()) { - if (member.getClusterInstanceId().equals(instance.getInstanceId())) { - if (member.getStatus().equals(MemberStatus.Active)) { - activeInstances++; - } - } - } - - clusterScaleup = activeInstances > clusterDataHolder.getMinInstances(); - if (clusterScaleup) { - activeInstancesAfterScaleup = activeInstances; - break; - } - } - application = ApplicationManager.getApplications().getApplication(applicationName); - if ((System.currentTimeMillis() - startTime) > CLUSTER_SCALE_UP_TIMEOUT) { - break; - } - } - } - assertEquals(true, clusterScaleup, String.format("Cluster did not get scaled up: [cluster-id] %s", clusterId)); - } - - /** - * Assert application activation - * - * @param applicationName - */ - private void assertClusterWithScaleDown(String applicationName) { - Application application = ApplicationManager.getApplications().getApplication(applicationName); - assertNotNull(String.format("Application is not found: [application-id] %s", - applicationName), application); - boolean clusterScaleDown = false; - String clusterId = null; - long startTime = System.currentTimeMillis(); - while (!clusterScaleDown) { - try { - Thread.sleep(1000); - } - catch (InterruptedException ignore) { - } - Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); - for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { - String serviceName = clusterDataHolder.getServiceType(); - clusterId = clusterDataHolder.getClusterId(); - Service service = TopologyManager.getTopology().getService(serviceName); - assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", - applicationName, serviceName), service); - - Cluster cluster = service.getCluster(clusterId); - assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", - applicationName, serviceName, clusterId), cluster); - for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { - int activeInstances = 0; - for (Member member : cluster.getMembers()) { - if (member.getClusterInstanceId().equals(instance.getInstanceId())) { - if (member.getStatus().equals(MemberStatus.Active)) { - activeInstances++; - } - } - } - - if (activeInstances > activeInstancesAfterScaleup) { - activeInstancesAfterScaleup = activeInstances; - } - - clusterScaleDown = activeInstancesAfterScaleup - 1 == activeInstances; - if (clusterScaleDown) { - break; - } - - } - - application = ApplicationManager.getApplications().getApplication(applicationName); - if ((System.currentTimeMillis() - startTime) > CLUSTER_SCALE_DOWN_TIMEOUT) { - break; - } - } - } - assertEquals(clusterScaleDown, true, - String.format("Cluster did not get scaled up: [cluster-id] %s", clusterId)); - } - - /** - * Assert application activation - * - * @param applicationName - */ - private void assertClusterScaleDownToMinimumCount(String applicationName) { - Application application = ApplicationManager.getApplications().getApplication(applicationName); - assertNotNull(String.format("Application is not found: [application-id] %s", - applicationName), application); - boolean clusterScaleDown = false; - String clusterId = null; - long startTime = System.currentTimeMillis(); - while (!clusterScaleDown) { - try { - Thread.sleep(1000); - } - catch (InterruptedException ignore) { - } - Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively(); - for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) { - String serviceName = clusterDataHolder.getServiceType(); - clusterId = clusterDataHolder.getClusterId(); - Service service = TopologyManager.getTopology().getService(serviceName); - assertNotNull(String.format("Service is not found: [application-id] %s [service] %s", - applicationName, serviceName), service); - - Cluster cluster = service.getCluster(clusterId); - assertNotNull(String.format("Cluster is not found: [application-id] %s [service] %s [cluster-id] %s", - applicationName, serviceName, clusterId), cluster); - for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) { - int activeInstances = 0; - for (Member member : cluster.getMembers()) { - if (member.getClusterInstanceId().equals(instance.getInstanceId())) { - if (member.getStatus().equals(MemberStatus.Active)) { - activeInstances++; - } - } - } - clusterScaleDown = activeInstances == clusterDataHolder.getMinInstances(); - if (clusterScaleDown) { - break; - } - } - application = ApplicationManager.getApplications().getApplication(applicationName); - if ((System.currentTimeMillis() - startTime) > CLUSTER_SCALE_DOWN_TIMEOUT) { - break; - } - } - } - assertEquals(clusterScaleDown, true, - String.format("Cluster did not get scaled up: [cluster-id] %s", clusterId)); - } -} ++public class SingleClusterScalingTestCase { ++} http://git-wip-us.apache.org/repos/asf/stratos/blob/aece0c4a/products/stratos/modules/integration/test-integration/src/test/resources/common/jndi.properties ---------------------------------------------------------------------- diff --cc products/stratos/modules/integration/test-integration/src/test/resources/common/jndi.properties index 0000000,a2cfea7..e69de29 mode 000000,100644..100644 --- a/products/stratos/modules/integration/test-integration/src/test/resources/common/jndi.properties +++ b/products/stratos/modules/integration/test-integration/src/test/resources/common/jndi.properties
