From 4a4f14aaf8907f50a0a4c6d7b90c24b126158587 Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Thu, 19 Feb 2015 14:23:01 +0530 Subject: [PATCH 01/10] JCA - Code cleanup - Removed commented code, obsolete classes ArtifactCopyTask and RepositoryFileListener --- .../cartridge/agent/ArtifactCopyTask.java | 39 ----------- .../cartridge/agent/CartridgeAgent.java | 66 +------------------ .../agent/RepositoryFileListener.java | 41 ------------ 3 files changed, 1 insertion(+), 145 deletions(-) delete mode 100644 components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/ArtifactCopyTask.java delete mode 100644 components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/ArtifactCopyTask.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/ArtifactCopyTask.java deleted file mode 100644 index 75a5d1e12e..0000000000 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/ArtifactCopyTask.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * - */ -package org.apache.stratos.cartridge.agent; - -import java.io.File; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.stratos.cartridge.agent.extensions.ExtensionHandler; - -/** - * - */ -public class ArtifactCopyTask implements Runnable { - - private static final Log log = LogFactory.getLog(ArtifactCopyTask.class); - private final ExtensionHandler extensionHandler; - private String source; - private String destination; - - public ArtifactCopyTask(String src, String des) { - this.source = src; - this.destination = des; - extensionHandler = CartridgeAgent.getExtensionHandler(); - } - - @Override - public void run() { - if (log.isDebugEnabled()) { - log.debug("Executing Artifact Copy Task source[" + source +"] destination[" + destination +"] "); - } - - if (new File(destination).exists()) { - extensionHandler.onCopyArtifactsExtension(source, destination); - } - } - -} diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java index 51d466bdbd..4bb3a66c41 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java @@ -32,30 +32,7 @@ import org.apache.stratos.cartridge.agent.extensions.ExtensionHandler; import org.apache.stratos.cartridge.agent.util.CartridgeAgentConstants; import org.apache.stratos.cartridge.agent.util.CartridgeAgentUtils; -import org.apache.stratos.messaging.event.Event; -import org.apache.stratos.messaging.event.instance.notifier.ArtifactUpdatedEvent; -import org.apache.stratos.messaging.event.instance.notifier.InstanceCleanupClusterEvent; -import org.apache.stratos.messaging.event.instance.notifier.InstanceCleanupMemberEvent; -import org.apache.stratos.messaging.event.domain.mapping.DomainMappingAddedEvent; -import org.apache.stratos.messaging.event.domain.mapping.DomainMappingRemovedEvent; -import org.apache.stratos.messaging.listener.instance.notifier.ArtifactUpdateEventListener; -import org.apache.stratos.messaging.listener.instance.notifier.InstanceCleanupClusterEventListener; -import org.apache.stratos.messaging.listener.instance.notifier.InstanceCleanupMemberEventListener; -import org.apache.stratos.messaging.listener.domain.mapping.DomainMappingAddedEventListener; -import org.apache.stratos.messaging.listener.domain.mapping.DomainMappingRemovedEventListener; -import org.apache.stratos.messaging.message.receiver.instance.notifier.InstanceNotifierEventReceiver; -import org.apache.stratos.messaging.message.receiver.tenant.TenantEventReceiver; -import org.apache.stratos.messaging.event.tenant.CompleteTenantEvent; -import org.apache.stratos.messaging.event.tenant.TenantSubscribedEvent; -import org.apache.stratos.messaging.event.tenant.TenantUnSubscribedEvent; -import org.apache.stratos.messaging.event.topology.*; -import org.apache.stratos.messaging.listener.tenant.CompleteTenantEventListener; -import org.apache.stratos.messaging.listener.tenant.TenantSubscribedEventListener; -import org.apache.stratos.messaging.listener.tenant.TenantUnSubscribedEventListener; -import org.apache.stratos.messaging.listener.topology.*; -import org.apache.stratos.messaging.message.receiver.tenant.TenantManager; -import org.apache.stratos.messaging.message.receiver.topology.TopologyEventReceiver; -import org.apache.stratos.messaging.message.receiver.topology.TopologyManager; + import java.util.List; @@ -176,25 +153,6 @@ public void run() { // Check repo url String repoUrl = CartridgeAgentConfiguration.getInstance().getRepoUrl(); - /*if(CartridgeAgentConfiguration.getInstance().isMultitenant()) { - if (CartridgeAgentConfiguration.getInstance().isCommitsEnabled()) { - log.info(" Commits enabled. Starting File listener "); - ScheduledExecutorService scheduler = Executors - .newScheduledThreadPool(1); - scheduler.scheduleWithFixedDelay(new RepositoryFileListener(), 0, - 10, TimeUnit.SECONDS); - } - - // Start super tenant artifact copy task - // from temp location to super tenant app path - //ScheduledExecutorService scheduler = Executors - // .newScheduledThreadPool(1); - //scheduler.scheduleWithFixedDelay(new ArtifactCopyTask( - // CartridgeAgentConstants.SUPERTENANT_TEMP_PATH, - // CartridgeAgentConfiguration.getInstance().getAppPath()+ "/repository/deployment/server/" - // ), - // 0, 10, TimeUnit.SECONDS); - } */ if (log.isInfoEnabled()) { log.info("Cartridge agent getRepoUrl done"); @@ -217,28 +175,7 @@ public void run() { if (log.isInfoEnabled()) { log.info("Cartridge agent - artifact repository found"); } - //Start periodical file processor task - /*if (CartridgeAgentConfiguration.getInstance().isCommitsEnabled()) { - log.info(" Commits enabled. Starting File listener "); - ScheduledExecutorService scheduler = Executors - .newScheduledThreadPool(1); - scheduler.scheduleWithFixedDelay(new RepositoryFileListener(), 0, - 10, TimeUnit.SECONDS); - } */ } - -// if (CartridgeAgentConfiguration.getInstance().isInternalRepo()) { -// // Start periodic file copy for super tenant -// // From repo/deployment/server to /tmp/-1234 -// -// ScheduledExecutorService scheduler = Executors -// .newScheduledThreadPool(1); -// scheduler.scheduleWithFixedDelay( -// new ArtifactCopyTask(CartridgeAgentConfiguration.getInstance().getAppPath() -// + "/repository/deployment/server/", -// CartridgeAgentConstants.SUPERTENANT_TEMP_PATH), 0, -// 10, TimeUnit.SECONDS); -// } String persistenceMappingsPayload = CartridgeAgentConfiguration.getInstance().getPersistenceMappings(); if (persistenceMappingsPayload != null) { @@ -340,7 +277,6 @@ private static void publishLogs(LogPublisherManager logPublisherManager) { List logFilePaths = CartridgeAgentConfiguration.getInstance().getLogFilePaths(); if (logFilePaths == null) { log.error("No valid log file paths found, no logs will be published"); - return; } else { // initialize the log publishing try { diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java deleted file mode 100644 index 59163d5b4b..0000000000 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.stratos.cartridge.agent; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.stratos.cartridge.agent.artifact.deployment.synchronizer.git.impl.GitBasedArtifactRepository; - -public class RepositoryFileListener implements Runnable { - - private static final Log log = LogFactory.getLog(RepositoryFileListener.class); - - @Override - public void run() { - if(log.isDebugEnabled()) { - log.debug("Executing repository file listener"); - } - //boolean commitStatus = GitBasedArtifactRepository.getInstance().commit(); - //if(log.isDebugEnabled()) { - // log.debug("Commit status: " + commitStatus); - // } - } - -} From 9c3b14464fbdccd03adb556e1e2ec7e64b4a40fa Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Thu, 19 Feb 2015 18:13:15 +0530 Subject: [PATCH 02/10] JCA - Added integration test for Java cartridge agent --- .../pom.xml | 78 +++- .../agent/test/JavaCartridgeAgentTest.java | 404 ++++++++++++++++++ .../src/test/resources/conf/jndi.properties | 22 + .../src/test/resources/conf/log4j.properties | 41 ++ .../test/resources/conf/mqtttopic.properties | 21 + .../src/test/resources/jndi.properties | 22 + .../src/test/resources/log4j.properties | 41 ++ .../src/test/resources/payload/launch-params | 1 + 8 files changed, 616 insertions(+), 14 deletions(-) create mode 100644 components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java create mode 100644 components/org.apache.stratos.cartridge.agent/src/test/resources/conf/jndi.properties create mode 100644 components/org.apache.stratos.cartridge.agent/src/test/resources/conf/log4j.properties create mode 100644 components/org.apache.stratos.cartridge.agent/src/test/resources/conf/mqtttopic.properties create mode 100644 components/org.apache.stratos.cartridge.agent/src/test/resources/jndi.properties create mode 100644 components/org.apache.stratos.cartridge.agent/src/test/resources/log4j.properties create mode 100644 components/org.apache.stratos.cartridge.agent/src/test/resources/payload/launch-params diff --git a/components/org.apache.stratos.cartridge.agent/pom.xml b/components/org.apache.stratos.cartridge.agent/pom.xml index c1021f420a..a6cf7651eb 100644 --- a/components/org.apache.stratos.cartridge.agent/pom.xml +++ b/components/org.apache.stratos.cartridge.agent/pom.xml @@ -17,22 +17,67 @@ # specific language governing permissions and limitations # under the License. --> - + - - org.apache.stratos - stratos-components-parent - 4.1.0-SNAPSHOT - + + org.apache.stratos + stratos-components-parent + 4.1.0-SNAPSHOT + - 4.0.0 - org.apache.stratos.cartridge.agent - jar - Apache Stratos - Cartridge Agent - Apache Stratos Cartridge Agent - http://apache.org + 4.0.0 + org.apache.stratos.cartridge.agent + jar + Apache Stratos - Cartridge Agent + Apache Stratos Cartridge Agent + http://apache.org - + + + default + + true + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*/JavaCartridgeAgentTest.java + + + + + + + + live + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*/JavaCartridgeAgentTest.java + + + + + + + + + + + + net.lingala.zip4j + zip4j + 1.3.2 + org.apache.stratos org.apache.stratos.common @@ -68,6 +113,11 @@ commons-io 2.0 + + org.apache.commons + commons-exec + 1.0.1 + org.eclipse.jgit.wso2 org.eclipse.jgit @@ -89,5 +139,5 @@ org.wso2.carbon.databridge.agent.thrift ${wso2carbon.version} - + diff --git a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java new file mode 100644 index 0000000000..f004b379fe --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java @@ -0,0 +1,404 @@ +/* + * 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.cartridge.agent.test; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import org.apache.commons.exec.*; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.stratos.common.threading.StratosThreadPool; +import org.apache.stratos.messaging.broker.publish.EventPublisher; +import org.apache.stratos.messaging.broker.publish.EventPublisherPool; +import org.apache.stratos.messaging.domain.topology.*; +import org.apache.stratos.messaging.event.Event; +import org.apache.stratos.messaging.event.instance.notifier.ArtifactUpdatedEvent; +import org.apache.stratos.messaging.event.topology.CompleteTopologyEvent; +import org.apache.stratos.messaging.event.topology.MemberInitializedEvent; +import org.apache.stratos.messaging.listener.instance.status.InstanceActivatedEventListener; +import org.apache.stratos.messaging.listener.instance.status.InstanceStartedEventListener; +import org.apache.stratos.messaging.message.receiver.instance.status.InstanceStatusEventReceiver; +import org.apache.stratos.messaging.message.receiver.topology.TopologyEventReceiver; +import org.apache.stratos.messaging.util.MessagingUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.net.ServerSocket; +import java.util.*; +import java.util.concurrent.ExecutorService; + +import static junit.framework.Assert.assertTrue; + +/** + * Created by chamilad on 2/19/15. + */ +public class JavaCartridgeAgentTest { + + private static final Log log = LogFactory.getLog(JavaCartridgeAgentTest.class); + private static final long TIMEOUT = 120000; + + private static final String NEW_LINE = System.getProperty("line.separator"); + private static final String CLUSTER_ID = "php.php.domain"; + private static final String DEPLOYMENT_POLICY_NAME = "deployment-policy-1"; + private static final String AUTOSCALING_POLICY_NAME = "autoscaling-policy-1"; + private static final String APP_ID = "application-1"; + private static final String MEMBER_ID = "php.member-1"; + private static final String CLUSTER_INSTANCE_ID = "cluster-1-instance-1"; + private static final String NETWORK_PARTITION_ID = "network-partition-1"; + private static final String PARTITION_ID = "partition-1"; + private static final String TENANT_ID = "-1234"; + private static final String SERVICE_NAME = "php"; + public static final String AGENT_NAME = "apache-stratos-cartridge-agent-4.1.0-SNAPSHOT"; + private static HashMap executorList; + private static ArrayList serverSocketList; + private String agentHome; + private boolean[] instanceStarted; + private boolean[] instanceActivated; + private ByteArrayOutputStreamLocal outputStream; + + @BeforeClass + public static void setupOneTime(){ + System.setProperty("jndi.properties.dir", getResourcesFolderPath()); + + } + + @Before + public void setup() { + serverSocketList = new ArrayList(); + executorList = new HashMap(); + + agentHome = setupJavaAgent(); + + ExecutorService executorService = StratosThreadPool.getExecutorService("TEST_THREAD_POOL", 5); + TopologyEventReceiver topologyEventReceiver = new TopologyEventReceiver(); + topologyEventReceiver.setExecutorService(executorService); + topologyEventReceiver.execute(); + + InstanceStatusEventReceiver instanceStatusEventReceiver = new InstanceStatusEventReceiver(); + instanceStatusEventReceiver.setExecutorService(executorService); + instanceStatusEventReceiver.execute(); + + instanceStarted = new boolean[1]; + instanceStatusEventReceiver.addEventListener(new InstanceStartedEventListener() { + @Override + protected void onEvent(Event event) { + log.info("Instance started event received"); + instanceStarted[0] = true; + } + }); + + + instanceActivated = new boolean[1]; + instanceStatusEventReceiver.addEventListener(new InstanceActivatedEventListener() { + @Override + protected void onEvent(Event event) { + log.info("Instance activated event received"); + instanceActivated[0] = true; + } + }); + + log.info("Starting Java cartridge agent..."); + outputStream = executeCommand("bash stratos.sh", new File(this.agentHome + "/bin")); + + startServerSocket(7711); + } + + @After + public void teardown() { + for (Map.Entry entry : executorList.entrySet()) { + try { + String commandText = entry.getKey(); + Executor executor = entry.getValue(); + ExecuteWatchdog watchdog = executor.getWatchdog(); + if (watchdog != null) { + log.info("Terminating process: " + commandText); + watchdog.destroyProcess(); + } + File workingDirectory = executor.getWorkingDirectory(); + if (workingDirectory != null) { + log.info("Cleaning working directory: " + workingDirectory.getAbsolutePath()); + FileUtils.deleteDirectory(workingDirectory); + } + } catch (Exception ignore) { + } + } + for (ServerSocket serverSocket : serverSocketList) { + try { + log.info("Stopping socket server: " + serverSocket.getLocalSocketAddress()); + serverSocket.close(); + } catch (IOException ignore) { + } + } + } + + private String setupJavaAgent() { + try { + String jcaZipSource = getResourcesFolderPath() + "/../../../../products/cartridge-agent/modules/distribution/target/" + AGENT_NAME + ".zip"; + String testHome = getResourcesFolderPath() + "/../" + UUID.randomUUID() + "/"; + String agentHome = testHome + AGENT_NAME; + log.debug("Extracting Java Cartridge Agent to test folder"); + ZipFile agentZip = new ZipFile(jcaZipSource); + agentZip.extractAll(testHome); + + log.debug("Copying test payload file"); + String srcPayloadPath = getResourcesFolderPath() + "/../../src/test/resources/payload"; + String destPayloadPath = agentHome + "/payload"; + FileUtils.copyDirectory(new File(srcPayloadPath), new File(destPayloadPath)); + + log.debug("Copying test conf files"); + String srcConf = getResourcesFolderPath() + "/../../src/test/resources/conf"; + String destConf = agentHome + "/conf"; + FileUtils.copyDirectory(new File(srcConf), new File(destConf)); + + log.info("Java Cartridge Agent setup complete."); + + return agentHome; + } catch (IOException e) { + String message = "Could not copy cartridge agent distribution"; + log.error(message, e); + throw new RuntimeException(message, e); + } catch (ZipException e) { + String message = "Could not unzip cartridge agent distribution"; + log.error(message, e); + throw new RuntimeException(message, e); + } + } + + private static String getResourcesFolderPath() { + return StringUtils.removeEnd(JavaCartridgeAgentTest.class.getResource("/").getPath(), File.separator); + } + + @Test + public void testJavaCartridgeAgent() throws Exception { + + + Thread communicatorThread = new Thread(new Runnable() { + @Override + public void run() { + List outputLines = new ArrayList(); + while (!outputStream.isClosed()) { + List newLines = getNewLines(outputLines, outputStream.toString()); + if (newLines.size() > 0) { + for (String line : newLines) { + if (line.contains("Cartridge agent topology receiver thread started, waiting for event messages")) { + sleep(2000); + // Send complete topology event + log.info("Publishing complete topology event..."); + Topology topology = createTestTopology(); + CompleteTopologyEvent completeTopologyEvent = new CompleteTopologyEvent(topology); + publishEvent(completeTopologyEvent); + log.info("Complete topology event published"); + + sleep(5000); + // Publish member initialized event + log.info("Publishing member initialized event..."); + MemberInitializedEvent memberInitializedEvent = new MemberInitializedEvent( + SERVICE_NAME, CLUSTER_ID, CLUSTER_INSTANCE_ID, MEMBER_ID, NETWORK_PARTITION_ID, PARTITION_ID + ); + publishEvent(memberInitializedEvent); + log.info("Member initialized event published"); + + // Simulate server socket + startServerSocket(9080); + } + if (line.contains("Cartridge agent - artifact repository found")) { + // Send artifact updated event + ArtifactUpdatedEvent artifactUpdatedEvent = new ArtifactUpdatedEvent(); + artifactUpdatedEvent.setClusterId(CLUSTER_ID); + artifactUpdatedEvent.setTenantId(TENANT_ID); + artifactUpdatedEvent.setRepoURL("https://bitbucket.org/testapache2211/opentestrepo1.git"); + String topicName = MessagingUtil.getMessageTopicName(artifactUpdatedEvent); + EventPublisher eventPublisher = EventPublisherPool.getPublisher(topicName); + eventPublisher.publish(artifactUpdatedEvent); + } + if (line.contains("Exception in thread") || line.contains("ERROR")) { + //throw new RuntimeException(line); + } + log.info(line); + } + } + + if (instanceActivated[0]) { + break; + } + sleep(500); + } + } + }); + + communicatorThread.start(); + +// assertTrue("Instance started event was not received", instanceStarted[0]); +// assertTrue("Instance activated event was not received", instanceActivated[0]); + } + + /** + * Publish messaging event + * @param event + */ + private void publishEvent(Event event) { + String topicName = MessagingUtil.getMessageTopicName(event); + EventPublisher eventPublisher = EventPublisherPool.getPublisher(topicName); + eventPublisher.publish(event); + } + + /** + * Start server socket + * @param port + */ + private void startServerSocket(final int port) { + Thread socketThread = new Thread(new Runnable() { + @Override + public void run() { + try { + ServerSocket serverSocket = new ServerSocket(port); + serverSocket.accept(); + serverSocketList.add(serverSocket); + } catch (IOException e) { + String message = "Could not start server socket: [port] " + port; + log.error(message, e); + throw new RuntimeException(message, e); + } + } + }); + socketThread.start(); + } + + /** + * Execute shell command + * + * @param commandText + */ + private ByteArrayOutputStreamLocal executeCommand(String commandText, File workingDir) { + final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); + try { + CommandLine commandline = CommandLine.parse(commandText); + DefaultExecutor exec = new DefaultExecutor(); + exec.setWorkingDirectory(workingDir); + PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); + exec.setStreamHandler(streamHandler); + ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); + exec.setWatchdog(watchdog); + exec.execute(commandline, new ExecuteResultHandler() { + @Override + public void onProcessComplete(int i) { + log.info("Agent process completed"); + } + + @Override + public void onProcessFailed(ExecuteException e) { + log.error("Agent process failed", e); + } + }); + executorList.put(commandText, exec); + return outputStream; + } catch (Exception e) { + log.error(outputStream.toString(), e); + throw new RuntimeException(e); + } + } + + /** + * Create test topology + * + * @return + */ + private Topology createTestTopology() { + Topology topology = new Topology(); + Service service = new Service(SERVICE_NAME, ServiceType.SingleTenant); + topology.addService(service); + + Cluster cluster = new Cluster(service.getServiceName(), CLUSTER_ID, DEPLOYMENT_POLICY_NAME, + AUTOSCALING_POLICY_NAME, APP_ID); + service.addCluster(cluster); + + Member member = new Member(service.getServiceName(), cluster.getClusterId(), MEMBER_ID, + CLUSTER_INSTANCE_ID, NETWORK_PARTITION_ID, PARTITION_ID, System.currentTimeMillis()); + member.setDefaultPrivateIP("10.0.0.1"); + member.setDefaultPublicIP("20.0.0.1"); + Properties properties = new Properties(); + properties.setProperty("prop1", "value1"); + member.setProperties(properties); + member.setStatus(MemberStatus.Created); + cluster.addMember(member); + + return topology; + } + + /** + * Return new lines found in the output + * + * @param currentOutputLines current output lines + * @param output output + * @return + */ + private List getNewLines(List currentOutputLines, String output) { + List newLines = new ArrayList(); + + if (StringUtils.isNotBlank(output)) { + String[] lines = output.split(NEW_LINE); + if (lines != null) { + for (String line : lines) { + if (!currentOutputLines.contains(line)) { + currentOutputLines.add(line); + newLines.add(line); + } + } + } + } + return newLines; + } + + /** + * Sleep current thread + * + * @param time + */ + private void sleep(long time) { + try { + Thread.sleep(time); + } catch (InterruptedException ignore) { + } + } + + /** + * Implements ByteArrayOutputStream.isClosed() method + */ + private class ByteArrayOutputStreamLocal extends ByteArrayOutputStream { + private boolean closed; + + @Override + public void close() throws IOException { + super.close(); + closed = true; + } + + public boolean isClosed() { + return closed; + } + } +} diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/jndi.properties b/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/jndi.properties new file mode 100644 index 0000000000..21d74207b9 --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/jndi.properties @@ -0,0 +1,22 @@ +# +# 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. +# + +connectionfactoryName=TopicConnectionFactory +java.naming.provider.url=tcp://localhost:61616 +java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/log4j.properties b/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/log4j.properties new file mode 100644 index 0000000000..2426b3b01b --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/log4j.properties @@ -0,0 +1,41 @@ +# +# 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. +# + +# Set root logger level and appenders +log4j.rootLogger=INFO, CONSOLE_APPENDER, FILE_APPENDER + +# CONSOLE_APPENDER is set to be a ConsoleAppender. +log4j.appender.CONSOLE_APPENDER=org.apache.log4j.ConsoleAppender + +# The standard error log where all the warnings, errors and fatal errors will be logged +log4j.appender.FILE_APPENDER=org.apache.log4j.FileAppender +log4j.appender.FILE_APPENDER.File=/var/log/apache-stratos/cartridge-agent.log +log4j.appender.FILE_APPENDER.layout=org.apache.log4j.PatternLayout +log4j.appender.FILE_APPENDER.layout.ConversionPattern=%d{ISO8601} [%X{ip}-%X{host}] [%t] %5p %c{1} %m%n +log4j.appender.FILE_APPENDER.threshold=DEBUG + +# CONSOLE_APPENDER uses PatternLayout. +log4j.appender.CONSOLE_APPENDER.layout=org.apache.log4j.PatternLayout +log4j.appender.CONSOLE_APPENDER.layout.ConversionPattern=[%d{ISO8601}] %5p - [%c{1}] %m%n + +log4j.logger.org.apache.stratos.cartridge.agent=DEBUG +log4j.logger.org.apache.stratos.cartridge.agent.statistics.publisher=ERROR +log4j.logger.org.apache.stratos.messaging=INFO +log4j.logger.org.wso2.andes.client=ERROR +log4j.logger.org.apache.activemq.jndi.ActiveMQInitialContextFactory=ERROR \ No newline at end of file diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/mqtttopic.properties b/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/mqtttopic.properties new file mode 100644 index 0000000000..823c1a9fe6 --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/conf/mqtttopic.properties @@ -0,0 +1,21 @@ +# 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. +# + +mqtturl=tcp://localhost:1883 +clientID=stratos +tempfilelocation=/tmp diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/jndi.properties b/components/org.apache.stratos.cartridge.agent/src/test/resources/jndi.properties new file mode 100644 index 0000000000..21d74207b9 --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/jndi.properties @@ -0,0 +1,22 @@ +# +# 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. +# + +connectionfactoryName=TopicConnectionFactory +java.naming.provider.url=tcp://localhost:61616 +java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/log4j.properties b/components/org.apache.stratos.cartridge.agent/src/test/resources/log4j.properties new file mode 100644 index 0000000000..c0c6e78cce --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/log4j.properties @@ -0,0 +1,41 @@ +# +# 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. +# + +# Set root logger level and appenders +log4j.rootLogger=INFO, CONSOLE_APPENDER, FILE_APPENDER + +# CONSOLE_APPENDER is set to be a ConsoleAppender. +log4j.appender.CONSOLE_APPENDER=org.apache.log4j.ConsoleAppender + +# The standard error log where all the warnings, errors and fatal errors will be logged +log4j.appender.FILE_APPENDER=org.apache.log4j.FileAppender +log4j.appender.FILE_APPENDER.File=cartridge-agent.log +log4j.appender.FILE_APPENDER.layout=org.apache.log4j.PatternLayout +log4j.appender.FILE_APPENDER.layout.ConversionPattern=%d{ISO8601} [%X{ip}-%X{host}] [%t] %5p %c{1} %m%n +log4j.appender.FILE_APPENDER.threshold=DEBUG + +# CONSOLE_APPENDER uses PatternLayout. +log4j.appender.CONSOLE_APPENDER.layout=org.apache.log4j.PatternLayout +log4j.appender.CONSOLE_APPENDER.layout.ConversionPattern=[%d{ISO8601}] %5p - [%c{1}] %m%n + +log4j.logger.org.apache.stratos.cartridge.agent=INFO +log4j.logger.org.apache.stratos.messaging=INFO +log4j.logger.org.apache.stratos.common.util=DEBUG +log4j.logger.org.wso2.andes.client=ERROR +log4j.logger.org.apache.activemq.jndi.ActiveMQInitialContextFactory=ERROR \ No newline at end of file diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/payload/launch-params b/components/org.apache.stratos.cartridge.agent/src/test/resources/payload/launch-params new file mode 100644 index 0000000000..858d622d9a --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/payload/launch-params @@ -0,0 +1 @@ +APPLICATION_ID=application1,SERVICE_NAME=php,HOST_NAME=php.php.stratos.org,MULTITENANT=false,TENANT_ID=-1234,TENANT_RANGE=*,CARTRIDGE_ALIAS=php,CLUSTER_ID=php.php.domain,CLUSTER_INSTANCE_ID=single-cartridge-app-1,CARTRIDGE_KEY=PUjpXCLujDhYr5A6,DEPLOYMENT=default,REPO_URL=https://github.com/imesh/stratos-php-applications.git,PORTS=9080,PUPPET_IP=127.0.0.1,PUPPET_HOSTNAME=puppet.apache.stratos.org,PUPPET_ENV=false,MEMBER_ID=php.member-1,LB_CLUSTER_ID=null,NETWORK_PARTITION_ID=network-p1,PARTITION_ID=p1,APPLICATION_PATH=/tmp/stratos-pca-test-app-path/,MIN_COUNT=1,INTERNAL=false,CLUSTERING_PRIMARY_KEY=A,LOG_FILE_PATHS=/tmp/temp.log,PERSISTENCE_MAPPING=null,MP_IP=192.168.1.4,MB_PORT=1883 \ No newline at end of file From 69bca0554731a1930feb80a9e8b6252aa513af2f Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Sat, 21 Feb 2015 02:00:11 +0530 Subject: [PATCH 03/10] JCA - integration test - log entries for debugging --- .../agent/test/JavaCartridgeAgentTest.java | 27 ++++-- .../src/test/resources/bin/stratos.sh | 82 +++++++++++++++++++ 2 files changed, 103 insertions(+), 6 deletions(-) create mode 100755 components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh diff --git a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java index f004b379fe..a58473e7b5 100644 --- a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java +++ b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java @@ -59,7 +59,7 @@ public class JavaCartridgeAgentTest { private static final Log log = LogFactory.getLog(JavaCartridgeAgentTest.class); - private static final long TIMEOUT = 120000; + private static final long TIMEOUT = 200000; private static final String NEW_LINE = System.getProperty("line.separator"); private static final String CLUSTER_ID = "php.php.domain"; @@ -121,10 +121,11 @@ protected void onEvent(Event event) { } }); - log.info("Starting Java cartridge agent..."); - outputStream = executeCommand("bash stratos.sh", new File(this.agentHome + "/bin")); - startServerSocket(7711); + + log.info("Starting Java cartridge agent..."); + String binPath = agentHome + "/bin"; + outputStream = executeCommand("bash stratos.sh > /tmp/agent.screen.log 2>&1 &", new File(binPath)); } @After @@ -200,10 +201,13 @@ public void testJavaCartridgeAgent() throws Exception { @Override public void run() { List outputLines = new ArrayList(); + log.info("LOG11111111111111"); while (!outputStream.isClosed()) { + log.info("LOG"); List newLines = getNewLines(outputLines, outputStream.toString()); if (newLines.size() > 0) { for (String line : newLines) { + log.info("LOG22222222222"); if (line.contains("Cartridge agent topology receiver thread started, waiting for event messages")) { sleep(2000); // Send complete topology event @@ -252,8 +256,15 @@ public void run() { communicatorThread.start(); -// assertTrue("Instance started event was not received", instanceStarted[0]); -// assertTrue("Instance activated event was not received", instanceActivated[0]); + while (!instanceActivated){ + log.info("LOGWAIT0000000000000000000000000"); + wait(2000); + } + + log.info("ASSERTIONS"); + + assertTrue("Instance started event was not received", instanceStarted); + assertTrue("Instance activated event was not received", instanceActivated); } /** @@ -299,10 +310,14 @@ private ByteArrayOutputStreamLocal executeCommand(String commandText, File worki CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); exec.setWorkingDirectory(workingDir); + if (workingDir != null) { + exec.setWorkingDirectory(workingDir); + } PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); exec.setWatchdog(watchdog); + log.info("Executing command: " + commandText + (workingDir == null ? "" : " at " + workingDir.getCanonicalPath())); exec.execute(commandline, new ExecuteResultHandler() { @Override public void onProcessComplete(int i) { diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh b/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh new file mode 100755 index 0000000000..7283f759de --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh @@ -0,0 +1,82 @@ +#!/bin/bash +# -------------------------------------------------------------- +# +# 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. +# +# -------------------------------------------------------------- + +echo "Starting cartridge agent..." +script_path="$( cd -P "$( dirname "$SOURCE" )" && pwd )/`dirname $0`" +lib_path=${script_path}/../lib/ +echo "LIB PATH : ${lib_path}" +class_path=`echo ${lib_path}/*.jar | tr ' ' ':'` +properties="-Dmb.ip=MB-IP + -Dmb.port=MB-PORT + -Dlisten.address=localhost + -Dthrift.receiver.ip=CEP-IP + -Dthrift.receiver.port=CEP-PORT + -Djndi.properties.template.file.path=${script_path}/../conf/templates/jndi.properties.template + -Djndi.properties.dir=${script_path}/../conf + -Dlog4j.configuration=file://${script_path}/../conf/log4j.properties + -Dparam.file.path=${script_path}/../payload/launch-params + -Dextensions.dir=${script_path}/../extensions + -Dcep.stats.publisher.enabled=true + -Dlb.private.ip= + -Dlb.public.ip= + -Djavax.net.ssl.trustStore=CERT-TRUSTSTORE + -Djavax.net.ssl.trustStorePassword=TRUSTSTORE-PASSWORD + -Denable.artifact.update=true + -Dauto.commit=false + -Dauto.checkout=true + -Dartifact.update.interval=15 + -Denable.data.publisher=ENABLE-DATA-PUBLISHER + -Dmonitoring.server.ip=MONITORING-SERVER-IP + -Dmonitoring.server.port=MONITORING-SERVER-PORT + -Dmonitoring.server.secure.port=MONITORING-SERVER-SECURE-PORT + -Dmonitoring.server.admin.username=MONITORING-SERVER-ADMIN-USERNAME + -Dmonitoring.server.admin.password=MONITORING-SERVER-ADMIN-PASSWORD + -Dlog.file.paths=LOG_FILE_PATHS + -DAPP_PATH=APP_PATH + -Dsuper.tenant.repository.path=/repository/deployment/server/ + -Dtenant.repository.path=/repository/tenants/ + -Dextension.instance.started=instance-started.sh + -Dextension.start.servers=start-servers.sh + -Dextension.instance.activated=instance-activated.sh + -Dextension.artifacts.updated=artifacts-updated.sh + -Dextension.clean=clean.sh + -Dextension.mount.volumes=mount_volumes.sh + -Dextension.member.started=member-started.sh + -Dextension.member.activated=member-activated.sh + -Dextension.member.suspended=member-suspended.sh + -Dextension.member.terminated=member-terminated.sh + -Dextension.complete.topology=complete-topology.sh + -Dextension.complete.tenant=complete-tenant.sh + -Dextension.subscription.domain.added=subscription-domain-added.sh + -Dextension.subscription.domain.removed=subscription-domain-removed.sh + -Dextension.artifacts.copy=artifacts-copy.sh + -Dextension.tenant.subscribed=tenant-subscribed.sh + -Dextension.tenant.unsubscribed=tenant-unsubscribed.sh" + +# Uncomment below line to enable remote debugging +#debug="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" + +java -cp "${class_path}" ${properties} ${debug} org.apache.stratos.cartridge.agent.Main + +# If you want to add your custom Cartridge Agent implementation, Please provide an implementation of +# org.apache.stratos.cartridge.agent.CartridgeAgent as an argument to the Main class +#java -cp "${class_path}" ${properties} ${debug} org.apache.stratos.cartridge.agent.Main org.apache.stratos.cartridge.agent.CartridgeAgentABC From bae210e9f8bea9a289f9cc086c554894327585b3 Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Sat, 21 Feb 2015 02:48:33 +0530 Subject: [PATCH 04/10] JCA - Integration test - Fixed thread.sleep issue. Unzip from cartridge-agent product directly --- .../agent/test/JavaCartridgeAgentTest.java | 84 +++++++++++-------- .../src/test/resources/bin/stratos.sh | 27 +++--- 2 files changed, 61 insertions(+), 50 deletions(-) mode change 100755 => 100644 components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh diff --git a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java index a58473e7b5..fa4b7eee17 100644 --- a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java +++ b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java @@ -54,7 +54,7 @@ import static junit.framework.Assert.assertTrue; /** - * Created by chamilad on 2/19/15. + * An integration test that verifies the functionality of the Java cartridge agent */ public class JavaCartridgeAgentTest { @@ -75,13 +75,12 @@ public class JavaCartridgeAgentTest { public static final String AGENT_NAME = "apache-stratos-cartridge-agent-4.1.0-SNAPSHOT"; private static HashMap executorList; private static ArrayList serverSocketList; - private String agentHome; - private boolean[] instanceStarted; - private boolean[] instanceActivated; + private boolean instanceStarted; + private boolean instanceActivated; private ByteArrayOutputStreamLocal outputStream; @BeforeClass - public static void setupOneTime(){ + public static void oneTimeSetUp(){ System.setProperty("jndi.properties.dir", getResourcesFolderPath()); } @@ -91,7 +90,7 @@ public void setup() { serverSocketList = new ArrayList(); executorList = new HashMap(); - agentHome = setupJavaAgent(); + String agentHome = setupJavaAgent(); ExecutorService executorService = StratosThreadPool.getExecutorService("TEST_THREAD_POOL", 5); TopologyEventReceiver topologyEventReceiver = new TopologyEventReceiver(); @@ -102,22 +101,22 @@ public void setup() { instanceStatusEventReceiver.setExecutorService(executorService); instanceStatusEventReceiver.execute(); - instanceStarted = new boolean[1]; + instanceStarted = false; instanceStatusEventReceiver.addEventListener(new InstanceStartedEventListener() { @Override protected void onEvent(Event event) { log.info("Instance started event received"); - instanceStarted[0] = true; + instanceStarted = true; } }); - instanceActivated = new boolean[1]; + instanceActivated = false; instanceStatusEventReceiver.addEventListener(new InstanceActivatedEventListener() { @Override protected void onEvent(Event event) { log.info("Instance activated event received"); - instanceActivated[0] = true; + instanceActivated = true; } }); @@ -125,11 +124,12 @@ protected void onEvent(Event event) { log.info("Starting Java cartridge agent..."); String binPath = agentHome + "/bin"; - outputStream = executeCommand("bash stratos.sh > /tmp/agent.screen.log 2>&1 &", new File(binPath)); + outputStream = executeCommand("bash stratos.sh", new File(binPath)); + } @After - public void teardown() { + public void tearDown() { for (Map.Entry entry : executorList.entrySet()) { try { String commandText = entry.getKey(); @@ -139,11 +139,11 @@ public void teardown() { log.info("Terminating process: " + commandText); watchdog.destroyProcess(); } - File workingDirectory = executor.getWorkingDirectory(); - if (workingDirectory != null) { - log.info("Cleaning working directory: " + workingDirectory.getAbsolutePath()); - FileUtils.deleteDirectory(workingDirectory); - } +// File workingDirectory = executor.getWorkingDirectory(); +// if (workingDirectory != null) { +// log.info("Cleaning working directory: " + workingDirectory.getAbsolutePath()); +// FileUtils.deleteDirectory(workingDirectory); +// } } catch (Exception ignore) { } } @@ -158,32 +158,52 @@ public void teardown() { private String setupJavaAgent() { try { + log.info("Setting up Java cartridge agent test setup"); String jcaZipSource = getResourcesFolderPath() + "/../../../../products/cartridge-agent/modules/distribution/target/" + AGENT_NAME + ".zip"; String testHome = getResourcesFolderPath() + "/../" + UUID.randomUUID() + "/"; - String agentHome = testHome + AGENT_NAME; + File agentHome = new File(testHome + AGENT_NAME); log.debug("Extracting Java Cartridge Agent to test folder"); ZipFile agentZip = new ZipFile(jcaZipSource); agentZip.extractAll(testHome); - log.debug("Copying test payload file"); + log.info("Copying agent jar"); + String agentJar = "org.apache.stratos.cartridge.agent-4.1.0-SNAPSHOT.jar"; + String agentJarSource = getResourcesFolderPath() + "/../" + agentJar; + String agentJarDest = agentHome.getCanonicalPath() + "/lib/" + agentJar; + FileUtils.copyFile(new File(agentJarSource), new File(agentJarDest)); + + log.info("Copying test payload file"); String srcPayloadPath = getResourcesFolderPath() + "/../../src/test/resources/payload"; String destPayloadPath = agentHome + "/payload"; FileUtils.copyDirectory(new File(srcPayloadPath), new File(destPayloadPath)); - log.debug("Copying test conf files"); + log.info("Copying test conf files"); String srcConf = getResourcesFolderPath() + "/../../src/test/resources/conf"; String destConf = agentHome + "/conf"; FileUtils.copyDirectory(new File(srcConf), new File(destConf)); - log.info("Java Cartridge Agent setup complete."); + log.info("Copying test stratos.sh script"); + String srcBin = getResourcesFolderPath() + "/../../src/test/resources/bin"; + String destBin = agentHome + "/bin"; + FileUtils.copyDirectory(new File(srcBin), new File(destBin)); + + log.info("Changing stratos.sh permissions"); + this.executeCommand("chmod +x " + agentHome.getCanonicalPath() + "/bin/stratos.sh", null); + log.info("Changed permissions for stratos.sh"); + + log.info("Changing extension scripts permissions"); + String outputStream = this.executeCommand("chmod -R +x " + agentHome.getCanonicalPath() + "/extensions/*.sh", null).toString(); + log.info("Changed permissions for extensions : " + outputStream); - return agentHome; + log.info("Java cartridge agent setup complete."); + + return agentHome.getCanonicalPath(); } catch (IOException e) { String message = "Could not copy cartridge agent distribution"; log.error(message, e); throw new RuntimeException(message, e); } catch (ZipException e) { - String message = "Could not unzip cartridge agent distribution"; + String message = "Could not unzip cartridge agent distribution. Please make sure to build /products/cartridge-agent first."; log.error(message, e); throw new RuntimeException(message, e); } @@ -195,19 +215,14 @@ private static String getResourcesFolderPath() { @Test public void testJavaCartridgeAgent() throws Exception { - - Thread communicatorThread = new Thread(new Runnable() { @Override public void run() { List outputLines = new ArrayList(); - log.info("LOG11111111111111"); while (!outputStream.isClosed()) { - log.info("LOG"); List newLines = getNewLines(outputLines, outputStream.toString()); if (newLines.size() > 0) { for (String line : newLines) { - log.info("LOG22222222222"); if (line.contains("Cartridge agent topology receiver thread started, waiting for event messages")) { sleep(2000); // Send complete topology event @@ -246,7 +261,7 @@ public void run() { } } - if (instanceActivated[0]) { + if (instanceActivated) { break; } sleep(500); @@ -257,12 +272,9 @@ public void run() { communicatorThread.start(); while (!instanceActivated){ - log.info("LOGWAIT0000000000000000000000000"); - wait(2000); + sleep(2000); } - log.info("ASSERTIONS"); - assertTrue("Instance started event was not received", instanceStarted); assertTrue("Instance activated event was not received", instanceActivated); } @@ -304,7 +316,7 @@ public void run() { * * @param commandText */ - private ByteArrayOutputStreamLocal executeCommand(String commandText, File workingDir) { + private ByteArrayOutputStreamLocal executeCommand(final String commandText, File workingDir) { final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); try { CommandLine commandline = CommandLine.parse(commandText); @@ -321,12 +333,12 @@ private ByteArrayOutputStreamLocal executeCommand(String commandText, File worki exec.execute(commandline, new ExecuteResultHandler() { @Override public void onProcessComplete(int i) { - log.info("Agent process completed"); + log.info(commandText + " process completed"); } @Override public void onProcessFailed(ExecuteException e) { - log.error("Agent process failed", e); + log.error(commandText + " process failed", e); } }); executorList.put(commandText, exec); diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh b/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh old mode 100755 new mode 100644 index 7283f759de..51f9f957c7 --- a/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh @@ -23,13 +23,12 @@ echo "Starting cartridge agent..." script_path="$( cd -P "$( dirname "$SOURCE" )" && pwd )/`dirname $0`" lib_path=${script_path}/../lib/ -echo "LIB PATH : ${lib_path}" class_path=`echo ${lib_path}/*.jar | tr ' ' ':'` -properties="-Dmb.ip=MB-IP - -Dmb.port=MB-PORT +properties="-Dmb.ip=localhost + -Dmb.port=1883 -Dlisten.address=localhost - -Dthrift.receiver.ip=CEP-IP - -Dthrift.receiver.port=CEP-PORT + -Dthrift.receiver.ip=localhost + -Dthrift.receiver.port=7711 -Djndi.properties.template.file.path=${script_path}/../conf/templates/jndi.properties.template -Djndi.properties.dir=${script_path}/../conf -Dlog4j.configuration=file://${script_path}/../conf/log4j.properties @@ -38,20 +37,20 @@ properties="-Dmb.ip=MB-IP -Dcep.stats.publisher.enabled=true -Dlb.private.ip= -Dlb.public.ip= - -Djavax.net.ssl.trustStore=CERT-TRUSTSTORE - -Djavax.net.ssl.trustStorePassword=TRUSTSTORE-PASSWORD + -Djavax.net.ssl.trustStore=client-truststore.jks + -Djavax.net.ssl.trustStorePassword=wso2carbon -Denable.artifact.update=true -Dauto.commit=false -Dauto.checkout=true -Dartifact.update.interval=15 - -Denable.data.publisher=ENABLE-DATA-PUBLISHER - -Dmonitoring.server.ip=MONITORING-SERVER-IP - -Dmonitoring.server.port=MONITORING-SERVER-PORT - -Dmonitoring.server.secure.port=MONITORING-SERVER-SECURE-PORT - -Dmonitoring.server.admin.username=MONITORING-SERVER-ADMIN-USERNAME - -Dmonitoring.server.admin.password=MONITORING-SERVER-ADMIN-PASSWORD + -Denable.data.publisher=false + -Dmonitoring.server.ip=localhost + -Dmonitoring.server.port=7611 + -Dmonitoring.server.secure.port=7711 + -Dmonitoring.server.admin.username=admin + -Dmonitoring.server.admin.password=admin -Dlog.file.paths=LOG_FILE_PATHS - -DAPP_PATH=APP_PATH + -DAPP_PATH=/tmp/test-jca-source -Dsuper.tenant.repository.path=/repository/deployment/server/ -Dtenant.repository.path=/repository/tenants/ -Dextension.instance.started=instance-started.sh From 06a448df5dccbf5624fdf387e9be1bbb126fd6ac Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Fri, 20 Feb 2015 15:28:33 +0530 Subject: [PATCH 05/10] JCA - Moved port check to instance activated event publishing. If ports are not active InstanceActivatedEvent will not be published. JCA - Code cleanup - CartridgeAgent.java --- .../cartridge/agent/CartridgeAgent.java | 93 ++++++++----------- .../CartridgeAgentEventPublisher.java | 81 ++++++++++------ .../agent/util/CartridgeAgentUtils.java | 13 ++- 3 files changed, 101 insertions(+), 86 deletions(-) diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java index 4bb3a66c41..6a22286e80 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java @@ -31,7 +31,7 @@ import org.apache.stratos.cartridge.agent.extensions.DefaultExtensionHandler; import org.apache.stratos.cartridge.agent.extensions.ExtensionHandler; import org.apache.stratos.cartridge.agent.util.CartridgeAgentConstants; -import org.apache.stratos.cartridge.agent.util.CartridgeAgentUtils; +import org.apache.stratos.cartridge.agent.util.ExtensionUtils; import java.util.List; @@ -47,11 +47,6 @@ public class CartridgeAgent implements Runnable { private CartridgeAgentEventListeners eventListenerns; - // We have an asynchronous activity running to respond to ADC updates. We want to ensure - // that no publishInstanceActivatedEvent() call is made *before* the port activation test - // has succeeded. This flag controls that. - private boolean portsActivated; - @Override public void run() { if (log.isInfoEnabled()) { @@ -61,60 +56,60 @@ public void run() { eventListenerns = new CartridgeAgentEventListeners(); validateRequiredSystemProperties(); - - if (log.isInfoEnabled()) { log.info("Cartridge agent validated system properties done"); } - // Start instance notifier listener thread - portsActivated = false; - subscribeToTopicsAndRegisterListeners(); - - if (log.isInfoEnabled()) { - log.info("Cartridge agent subscribeToTopicsAndRegisterListeners done"); - } - // Start topology event receiver thread registerTopologyEventListeners(); - if (log.isInfoEnabled()) { log.info("Cartridge agent registerTopologyEventListeners done"); } + + if (log.isInfoEnabled()){ + log.info("Waiting for CompleteTopologyEvent.."); + } + + ExtensionUtils.waitForCompleteTopology(); + if (log.isInfoEnabled()){ + log.info("CompleteTopologyEvent received."); + } + + // wait till the member spawned event + while (!CartridgeAgentConfiguration.getInstance().isInitialized()) { + try { + if (log.isDebugEnabled()) { + log.info("Waiting for Cartridge Agent to be initialized..."); + } + Thread.sleep(1000); + } catch (InterruptedException ignore) { + } + } + + if (log.isInfoEnabled()) { + log.info("Cartridge agent initialized done"); + } + + // Start instance notifier listener thread + registerInstanceNotifierEventListeners(); + if (log.isInfoEnabled()) { + log.info("Cartridge agent registerInstanceNotifierEventListeners done"); + } // Start tenant event receiver thread registerTenantEventListeners(); - if (log.isInfoEnabled()) { - log.info("Cartridge agent registerApplicationEventListeners done"); + log.info("Cartridge agent registerTenantEventListeners done"); } // Start application event receiver thread registerApplicationEventListeners(); - - if (log.isInfoEnabled()) { log.info("Cartridge agent registering all event listeners ... done"); } - - // wait till the member spawned event - while (!CartridgeAgentConfiguration.getInstance().isInitialized()) { - try { - if (log.isDebugEnabled()) { - log.info("Waiting for Cartridge Agent to be initialized..."); - } - Thread.sleep(1000); - } catch (InterruptedException ignore) { - } - } - - if (log.isInfoEnabled()) { - log.info("Cartridge agent initialized done"); - } // Execute instance started shell script extensionHandler.onInstanceStartedEvent(); - if (log.isInfoEnabled()) { log.info("Cartridge agent onInstanceStartedEvent done"); } @@ -135,45 +130,33 @@ public void run() { log.info("Cartridge agent startServerExtension done"); } - // Wait for all ports to be active - CartridgeAgentUtils.waitUntilPortsActive(CartridgeAgentConfiguration.getInstance().getListenAddress(), - CartridgeAgentConfiguration.getInstance().getPorts()); - portsActivated = true; - - if (log.isInfoEnabled()) { - log.info("Cartridge agent portsActivated done"); - } - // Publish instance activated event CartridgeAgentEventPublisher.publishInstanceActivatedEvent(); - if (log.isInfoEnabled()) { log.info("Cartridge agent publishInstanceActivatedEvent done"); } // Check repo url String repoUrl = CartridgeAgentConfiguration.getInstance().getRepoUrl(); - if (log.isInfoEnabled()) { log.info("Cartridge agent getRepoUrl done"); } if ("null".equals(repoUrl) || StringUtils.isBlank(repoUrl)) { if (log.isInfoEnabled()) { - log.info("No artifact repository found"); + log.info("No artifact repository found. Publishing InstanceActivatedEvent."); } + + // Publish instance activated event + CartridgeAgentEventPublisher.publishInstanceActivatedEvent(); // Execute instance activated shell script extensionHandler.onInstanceActivatedEvent(); - if (log.isInfoEnabled()) { log.info("Cartridge agent onInstanceActivatedEvent done"); } - - // Publish instance activated event - CartridgeAgentEventPublisher.publishInstanceActivatedEvent(); } else { if (log.isInfoEnabled()) { - log.info("Cartridge agent - artifact repository found"); + log.info("Artifact repository found. Waiting for ArtifactUpdatedEvent to commence cloning."); } } @@ -197,7 +180,7 @@ public void run() { logPublisherManager.stop(); } - protected void subscribeToTopicsAndRegisterListeners() { + protected void registerInstanceNotifierEventListeners() { if (log.isDebugEnabled()) { log.debug("SsubscribeToTopicsAndRegisterListeners before"); } diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/event/publisher/CartridgeAgentEventPublisher.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/event/publisher/CartridgeAgentEventPublisher.java index 8aa6e7869a..9a4e0c26b5 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/event/publisher/CartridgeAgentEventPublisher.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/event/publisher/CartridgeAgentEventPublisher.java @@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.stratos.cartridge.agent.config.CartridgeAgentConfiguration; import org.apache.stratos.cartridge.agent.statistics.publisher.HealthStatisticsNotifier; +import org.apache.stratos.cartridge.agent.util.CartridgeAgentUtils; import org.apache.stratos.messaging.broker.publish.EventPublisher; import org.apache.stratos.messaging.broker.publish.EventPublisherPool; import org.apache.stratos.messaging.event.instance.status.InstanceActivatedEvent; @@ -32,6 +33,8 @@ import org.apache.stratos.messaging.event.instance.status.InstanceStartedEvent; import org.apache.stratos.messaging.util.MessagingUtil; +import java.util.List; + /** * Cartridge agent event publisher. */ @@ -102,35 +105,55 @@ public InstanceStartedEvent(String applicationId, String serviceName, String clu public static void publishInstanceActivatedEvent() { if (!isActivated()) { - if (log.isInfoEnabled()) { - log.info("Publishing instance activated event"); - } - InstanceActivatedEvent event = new InstanceActivatedEvent( - CartridgeAgentConfiguration.getInstance().getServiceName(), - CartridgeAgentConfiguration.getInstance().getClusterId(), - CartridgeAgentConfiguration.getInstance().getMemberId(), - CartridgeAgentConfiguration.getInstance().getInstanceId(), - CartridgeAgentConfiguration.getInstance().getClusterInstanceId(), - CartridgeAgentConfiguration.getInstance().getNetworkPartitionId(), - CartridgeAgentConfiguration.getInstance().getPartitionId()); - - // Event publisher connection will - String topic = MessagingUtil.getMessageTopicName(event); - EventPublisher eventPublisher = EventPublisherPool - .getPublisher(topic); - eventPublisher.publish(event); - if (log.isInfoEnabled()) { - log.info("Instance activated event published"); - } - - if (log.isInfoEnabled()) { - log.info("Starting health statistics notifier"); - } - Thread thread = new Thread(new HealthStatisticsNotifier()); - thread.start(); - setActivated(true); - if (log.isInfoEnabled()) { - log.info("Health statistics notifier started"); + // Wait for all ports to be active, if ports are not activated, do not publish instance activated since + // the service is not up + List ports = CartridgeAgentConfiguration.getInstance().getPorts(); + String listenAddress = CartridgeAgentConfiguration.getInstance().getListenAddress(); + boolean portsActivated = CartridgeAgentUtils.waitUntilPortsActive(listenAddress, ports); + + if (portsActivated) { + if (log.isInfoEnabled()) { + log.info("Publishing instance activated event"); + } + InstanceActivatedEvent event = new InstanceActivatedEvent( + CartridgeAgentConfiguration.getInstance().getServiceName(), + CartridgeAgentConfiguration.getInstance().getClusterId(), + CartridgeAgentConfiguration.getInstance().getMemberId(), + CartridgeAgentConfiguration.getInstance().getInstanceId(), + CartridgeAgentConfiguration.getInstance().getClusterInstanceId(), + CartridgeAgentConfiguration.getInstance().getNetworkPartitionId(), + CartridgeAgentConfiguration.getInstance().getPartitionId()); + + // Event publisher connection will + String topic = MessagingUtil.getMessageTopicName(event); + EventPublisher eventPublisher = EventPublisherPool + .getPublisher(topic); + eventPublisher.publish(event); + if (log.isInfoEnabled()) { + log.info("Instance activated event published"); + } + + if (log.isInfoEnabled()) { + log.info("Starting health statistics notifier"); + } + Thread thread = new Thread(new HealthStatisticsNotifier()); + thread.start(); + setActivated(true); + if (log.isInfoEnabled()) { + log.info("Health statistics notifier started"); + } + } else { + if (log.isInfoEnabled()) { + // There would not be a large number of ports + String portsStr = ""; + for (Integer port: ports){ + portsStr += port + ", "; + } + log.info(String.format( + "Ports activation timed out. Aborting InstanceActivatedEvent publishing. [IPAdress] %s [Ports] %s", + listenAddress, + portsStr)); + } } } else { if (log.isWarnEnabled()) { diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentUtils.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentUtils.java index 978ae5f51f..3dfe50a566 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentUtils.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentUtils.java @@ -79,7 +79,13 @@ public static String decryptPassword(String repoUserPassword) { return decryptPassword; } - public static void waitUntilPortsActive(String ipAddress, List ports) { + /** + * Blocks until the provided ports are activated on the provided ip address or the port check timeout exceeds + * @param ipAddress + * @param ports + * @return true if ports are activated, false if timeout exceeded + */ + public static boolean waitUntilPortsActive(String ipAddress, List ports) { long portCheckTimeOut = 1000 * 60 * 10; String portCheckTimeOutStr = System.getProperty("port.check.timeout"); if (StringUtils.isNotBlank(portCheckTimeOutStr)) { @@ -99,16 +105,19 @@ public static void waitUntilPortsActive(String ipAddress, List ports) { long endTime = System.currentTimeMillis(); long duration = endTime - startTime; if (duration > portCheckTimeOut) { - return; + return false; } try { Thread.sleep(5000); } catch (InterruptedException e) { } } + if(log.isInfoEnabled()) { log.info("Ports activated: [ip] " + ipAddress + " [ports] "+ports); } + + return true; } public static boolean checkPortsActive(String ipAddress, List ports) { From 27d23045fb86091ee412f0346dccf607b6eb0d63 Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Sat, 21 Feb 2015 03:33:59 +0530 Subject: [PATCH 06/10] JCA - Included thrift-client-config in the product JCA - Fixed topology lock error and unchecked instance activated event publishing found when testing JCA - Integration test - Fixed permission issue for extensions in the test folder --- .../cartridge/agent/CartridgeAgent.java | 6 ---- .../agent/CartridgeAgentEventListeners.java | 28 +++++++++---------- .../agent/test/JavaCartridgeAgentTest.java | 10 +++++-- .../src/test/resources/bin/stratos.sh | 3 +- .../distribution/src/main/assembly/bin.xml | 8 ++++++ .../distribution/src/main/bin/stratos.sh | 3 +- .../src/main/conf/thrift-client-config.xml | 23 +++++++++++++++ 7 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 products/cartridge-agent/modules/distribution/src/main/conf/thrift-client-config.xml diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java index 6a22286e80..4d8cc70b58 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java @@ -130,12 +130,6 @@ public void run() { log.info("Cartridge agent startServerExtension done"); } - // Publish instance activated event - CartridgeAgentEventPublisher.publishInstanceActivatedEvent(); - if (log.isInfoEnabled()) { - log.info("Cartridge agent publishInstanceActivatedEvent done"); - } - // Check repo url String repoUrl = CartridgeAgentConfiguration.getInstance().getRepoUrl(); if (log.isInfoEnabled()) { diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgentEventListeners.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgentEventListeners.java index 53faccc252..6a3433b714 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgentEventListeners.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgentEventListeners.java @@ -205,13 +205,13 @@ private void addTopologyEventListeners() { topologyEventReceiver.addEventListener(new MemberInitializedEventListener() { @Override protected void onEvent(Event event) { - try { - boolean initialized = CartridgeAgentConfiguration.getInstance().isInitialized(); - if (initialized) { - // no need to process this event, if the member is initialized. - return; - } - TopologyManager.acquireReadLock(); + boolean initialized = CartridgeAgentConfiguration.getInstance().isInitialized(); + if (initialized) { + // no need to process this event, if the member is initialized. + return; + } + try { + TopologyManager.acquireReadLock(); if (log.isDebugEnabled()) { log.debug("Member initialized event received"); } @@ -230,13 +230,13 @@ protected void onEvent(Event event) { topologyEventReceiver.addEventListener(new MemberCreatedEventListener() { @Override protected void onEvent(Event event) { - try { - boolean initialized = CartridgeAgentConfiguration.getInstance().isInitialized(); - if (initialized) { - // no need to process this event, if the member is initialized. - return; - } - TopologyManager.acquireReadLock(); + boolean initialized = CartridgeAgentConfiguration.getInstance().isInitialized(); + if (initialized) { + // no need to process this event, if the member is initialized. + return; + } + try { + TopologyManager.acquireReadLock(); if (log.isDebugEnabled()) { log.debug("Member created event received"); } diff --git a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java index fa4b7eee17..72d48030d9 100644 --- a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java +++ b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java @@ -192,7 +192,13 @@ private String setupJavaAgent() { log.info("Changed permissions for stratos.sh"); log.info("Changing extension scripts permissions"); - String outputStream = this.executeCommand("chmod -R +x " + agentHome.getCanonicalPath() + "/extensions/*.sh", null).toString(); +// String outputStream = this.executeCommand("chmod -R +x " + agentHome.getCanonicalPath() + "/extensions/*.sh", null).toString(); + File extensionsPath = new File(agentHome.getCanonicalPath() + "/extensions/"); + File[] extensions = extensionsPath.listFiles(); + for (File extension:extensions){ + extension.setExecutable(true); +// Files.setPosixFilePermissions(extension.getCanonicalPath()) + } log.info("Changed permissions for extensions : " + outputStream); log.info("Java cartridge agent setup complete."); @@ -244,7 +250,7 @@ public void run() { // Simulate server socket startServerSocket(9080); } - if (line.contains("Cartridge agent - artifact repository found")) { + if (line.contains("Artifact repository found")) { // Send artifact updated event ArtifactUpdatedEvent artifactUpdatedEvent = new ArtifactUpdatedEvent(); artifactUpdatedEvent.setClusterId(CLUSTER_ID); diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh b/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh index 51f9f957c7..69a5034299 100644 --- a/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh @@ -27,8 +27,7 @@ class_path=`echo ${lib_path}/*.jar | tr ' ' ':'` properties="-Dmb.ip=localhost -Dmb.port=1883 -Dlisten.address=localhost - -Dthrift.receiver.ip=localhost - -Dthrift.receiver.port=7711 + -Dthrift.client.config.file.path=${script_path}/../conf/thrift-client-config.xml -Djndi.properties.template.file.path=${script_path}/../conf/templates/jndi.properties.template -Djndi.properties.dir=${script_path}/../conf -Dlog4j.configuration=file://${script_path}/../conf/log4j.properties diff --git a/products/cartridge-agent/modules/distribution/src/main/assembly/bin.xml b/products/cartridge-agent/modules/distribution/src/main/assembly/bin.xml index 93c82fd91c..4c5ed14ad2 100644 --- a/products/cartridge-agent/modules/distribution/src/main/assembly/bin.xml +++ b/products/cartridge-agent/modules/distribution/src/main/assembly/bin.xml @@ -65,6 +65,14 @@ jndi.properties + + ${project.basedir}/src/main/conf + /conf + 0600 + + thrift-client-config.xml + + ${project.basedir}/src/main/extensions /extensions diff --git a/products/cartridge-agent/modules/distribution/src/main/bin/stratos.sh b/products/cartridge-agent/modules/distribution/src/main/bin/stratos.sh index 2636e74c77..f92575a881 100644 --- a/products/cartridge-agent/modules/distribution/src/main/bin/stratos.sh +++ b/products/cartridge-agent/modules/distribution/src/main/bin/stratos.sh @@ -27,8 +27,7 @@ class_path=`echo ${lib_path}/*.jar | tr ' ' ':'` properties="-Dmb.ip=MB-IP -Dmb.port=MB-PORT -Dlisten.address=localhost - -Dthrift.receiver.ip=CEP-IP - -Dthrift.receiver.port=CEP-PORT + -Dthrift.client.config.file.path=${script_path}/../conf/thrift-client-config.xml -Djndi.properties.template.file.path=${script_path}/../conf/templates/jndi.properties.template -Djndi.properties.dir=${script_path}/../conf -Dlog4j.configuration=file://${script_path}/../conf/log4j.properties diff --git a/products/cartridge-agent/modules/distribution/src/main/conf/thrift-client-config.xml b/products/cartridge-agent/modules/distribution/src/main/conf/thrift-client-config.xml new file mode 100644 index 0000000000..4d51fc6bfe --- /dev/null +++ b/products/cartridge-agent/modules/distribution/src/main/conf/thrift-client-config.xml @@ -0,0 +1,23 @@ + + + + + admin + admin + localhost + 7611 + \ No newline at end of file From bb5b0de1456e550a47377c5008b2ff4e1fc0e7a9 Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Sat, 21 Feb 2015 21:56:45 +0530 Subject: [PATCH 07/10] JCA - Integration Test improvments --- .../agent/test/JavaCartridgeAgentTest.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java index 72d48030d9..5918abf18e 100644 --- a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java +++ b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java @@ -21,6 +21,7 @@ import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.progress.ProgressMonitor; import org.apache.commons.exec.*; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; @@ -78,11 +79,12 @@ public class JavaCartridgeAgentTest { private boolean instanceStarted; private boolean instanceActivated; private ByteArrayOutputStreamLocal outputStream; + private TopologyEventReceiver topologyEventReceiver; + private InstanceStatusEventReceiver instanceStatusEventReceiver; @BeforeClass public static void oneTimeSetUp(){ System.setProperty("jndi.properties.dir", getResourcesFolderPath()); - } @Before @@ -93,11 +95,11 @@ public void setup() { String agentHome = setupJavaAgent(); ExecutorService executorService = StratosThreadPool.getExecutorService("TEST_THREAD_POOL", 5); - TopologyEventReceiver topologyEventReceiver = new TopologyEventReceiver(); + topologyEventReceiver = new TopologyEventReceiver(); topologyEventReceiver.setExecutorService(executorService); topologyEventReceiver.execute(); - InstanceStatusEventReceiver instanceStatusEventReceiver = new InstanceStatusEventReceiver(); + instanceStatusEventReceiver = new InstanceStatusEventReceiver(); instanceStatusEventReceiver.setExecutorService(executorService); instanceStatusEventReceiver.execute(); @@ -154,6 +156,12 @@ public void tearDown() { } catch (IOException ignore) { } } + + this.instanceStatusEventReceiver.terminate(); + this.topologyEventReceiver.terminate(); + + this.instanceActivated = false; + this.instanceStarted = false; } private String setupJavaAgent() { @@ -162,9 +170,18 @@ private String setupJavaAgent() { String jcaZipSource = getResourcesFolderPath() + "/../../../../products/cartridge-agent/modules/distribution/target/" + AGENT_NAME + ".zip"; String testHome = getResourcesFolderPath() + "/../" + UUID.randomUUID() + "/"; File agentHome = new File(testHome + AGENT_NAME); - log.debug("Extracting Java Cartridge Agent to test folder"); + log.info("Extracting Java Cartridge Agent to test folder"); ZipFile agentZip = new ZipFile(jcaZipSource); + ProgressMonitor zipProgresMonitor = agentZip.getProgressMonitor(); agentZip.extractAll(testHome); + while (zipProgresMonitor.getPercentDone() < 100) { + log.info("Extracting: " + zipProgresMonitor.getPercentDone()); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } log.info("Copying agent jar"); String agentJar = "org.apache.stratos.cartridge.agent-4.1.0-SNAPSHOT.jar"; @@ -188,16 +205,14 @@ private String setupJavaAgent() { FileUtils.copyDirectory(new File(srcBin), new File(destBin)); log.info("Changing stratos.sh permissions"); - this.executeCommand("chmod +x " + agentHome.getCanonicalPath() + "/bin/stratos.sh", null); + new File(agentHome.getCanonicalPath() + "/bin/stratos.sh").setExecutable(true); log.info("Changed permissions for stratos.sh"); log.info("Changing extension scripts permissions"); -// String outputStream = this.executeCommand("chmod -R +x " + agentHome.getCanonicalPath() + "/extensions/*.sh", null).toString(); File extensionsPath = new File(agentHome.getCanonicalPath() + "/extensions/"); File[] extensions = extensionsPath.listFiles(); for (File extension:extensions){ extension.setExecutable(true); -// Files.setPosixFilePermissions(extension.getCanonicalPath()) } log.info("Changed permissions for extensions : " + outputStream); @@ -327,7 +342,6 @@ private ByteArrayOutputStreamLocal executeCommand(final String commandText, File try { CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); - exec.setWorkingDirectory(workingDir); if (workingDir != null) { exec.setWorkingDirectory(workingDir); } From 3ba7635d818237f87e6bbd9a169793d6fac7e713 Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Mon, 23 Feb 2015 00:52:56 +0530 Subject: [PATCH 08/10] JCA - Integration test fixes JCA - Fixed APP_PATH var name, thrift-client-config port, removed unused constants --- .../config/CartridgeAgentConfiguration.java | 2 +- .../agent/util/CartridgeAgentConstants.java | 11 +-- .../agent/test/JavaCartridgeAgentTest.java | 83 ++++++++++++++++--- .../src/test/resources/bin/stratos.sh | 2 +- .../src/test/resources/payload/launch-params | 2 +- .../src/main/conf/thrift-client-config.xml | 2 +- 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/config/CartridgeAgentConfiguration.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/config/CartridgeAgentConfiguration.java index 70f62ef0ca..b89ae28d0b 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/config/CartridgeAgentConfiguration.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/config/CartridgeAgentConfiguration.java @@ -98,7 +98,7 @@ private CartridgeAgentConfiguration() { //self.cartridge_key = self.read_property(cartridgeagentconstants.CARTRIDGE_KEY) cartridgeKey = readParameterValue(CartridgeAgentConstants.CARTRIDGE_KEY); //self.app_path = self.read_property(cartridgeagentconstants.APPLICATION_PATH, False) - appPath = readParameterValue(CartridgeAgentConstants.APP_PATH); + appPath = readParameterValue(CartridgeAgentConstants.APPLICATION_PATH); //self.repo_url = self.read_property(cartridgeagentconstants.REPO_URL, False) repoUrl = readParameterValue(CartridgeAgentConstants.REPO_URL); //self.ports = str(self.read_property(cartridgeagentconstants.PORTS)).split("|") diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentConstants.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentConstants.java index b1db135541..b306d9fc93 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentConstants.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentConstants.java @@ -27,17 +27,8 @@ public class CartridgeAgentConstants implements Serializable{ public static final String PARAM_FILE_PATH = "param.file.path"; public static final String EXTENSIONS_DIR = "extensions.dir"; - public static final String INSTANCE_STARTED_SH = "instance-started.sh"; - public static final String START_SERVERS_SH = "start-servers.sh"; - public static final String INSTANCE_ACTIVATED_SH = "instance-activated.sh"; - public static final String ARTIFACTS_UPDATED_SH = "artifacts-updated.sh"; - public static final String CLEAN_UP_SH = "clean.sh"; - public static final String MOUNT_VOLUMES_SH = "mount_volumes.sh"; - public static final String SUBSCRIPTION_DOMAIN_ADDED_SH = "subscription-domain-added.sh"; - public static final String SUBSCRIPTION_DOMAIN_REMOVED_SH = "subscription-domain-removed.sh"; - public static final String CARTRIDGE_KEY = "CARTRIDGE_KEY"; - public static final String APP_PATH = "APP_PATH"; + public static final String APPLICATION_PATH = "APPLICATION_PATH"; public static final String APPLICATION_ID = "APPLICATION_ID"; public static final String SERVICE_GROUP = "SERIVCE_GROUP"; public static final String SERVICE_NAME = "SERVICE_NAME"; diff --git a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java index 5918abf18e..c077536fbf 100644 --- a/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java +++ b/components/org.apache.stratos.cartridge.agent/src/test/java/org/apache/stratos/cartridge/agent/test/JavaCartridgeAgentTest.java @@ -44,6 +44,8 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import java.io.ByteArrayOutputStream; import java.io.File; @@ -57,6 +59,7 @@ /** * An integration test that verifies the functionality of the Java cartridge agent */ +@RunWith(Parameterized.class) public class JavaCartridgeAgentTest { private static final Log log = LogFactory.getLog(JavaCartridgeAgentTest.class); @@ -76,12 +79,19 @@ public class JavaCartridgeAgentTest { public static final String AGENT_NAME = "apache-stratos-cartridge-agent-4.1.0-SNAPSHOT"; private static HashMap executorList; private static ArrayList serverSocketList; + private final ArtifactUpdatedEvent artifactUpdatedEvent; + private final Boolean expectedResult; private boolean instanceStarted; private boolean instanceActivated; private ByteArrayOutputStreamLocal outputStream; private TopologyEventReceiver topologyEventReceiver; private InstanceStatusEventReceiver instanceStatusEventReceiver; + public JavaCartridgeAgentTest(ArtifactUpdatedEvent artifactUpdatedEvent, Boolean expectedResult) { + this.artifactUpdatedEvent = artifactUpdatedEvent; + this.expectedResult = expectedResult; + } + @BeforeClass public static void oneTimeSetUp(){ System.setProperty("jndi.properties.dir", getResourcesFolderPath()); @@ -153,10 +163,17 @@ public void tearDown() { try { log.info("Stopping socket server: " + serverSocket.getLocalSocketAddress()); serverSocket.close(); - } catch (IOException ignore) { + } catch (IOException e) { + log.info("Couldn't stop socket server " + serverSocket.getLocalSocketAddress() + ", " + e.getMessage()); } } + try { + log.info("Deleting source checkout folder..."); + FileUtils.deleteDirectory(new File("/tmp/test-jca-source")); + } catch (Exception ignore){ + } + this.instanceStatusEventReceiver.terminate(); this.topologyEventReceiver.terminate(); @@ -164,6 +181,50 @@ public void tearDown() { this.instanceStarted = false; } + /** + * This method returns a collection of {@link org.apache.stratos.messaging.event.instance.notifier.ArtifactUpdatedEvent} + * objects as parameters to the test + * @return + */ + @Parameterized.Parameters + public static Collection getArtifactUpdatedEventsAsParams(){ + ArtifactUpdatedEvent publicRepoEvent = createTestArtifactUpdatedEvent(); + + ArtifactUpdatedEvent privateRepoEvent = createTestArtifactUpdatedEvent(); + privateRepoEvent.setRepoURL("https://bitbucket.org/testapache2211/testrepo.git"); + privateRepoEvent.setRepoUserName("testapache2211"); + privateRepoEvent.setRepoPassword("RExPDGa4GkPJj4kJDzSROQ=="); + + ArtifactUpdatedEvent privateRepoEvent2 = createTestArtifactUpdatedEvent(); + privateRepoEvent2.setRepoURL("https://testapache2211@bitbucket.org/testapache2211/testrepo.git"); + privateRepoEvent2.setRepoUserName("testapache2211"); + privateRepoEvent2.setRepoPassword("RExPDGa4GkPJj4kJDzSROQ=="); + + return Arrays.asList(new Object[][]{ + {publicRepoEvent, true}, + {privateRepoEvent, true}, + {privateRepoEvent2, true} + }); + + } + + /** + * Creates an {@link org.apache.stratos.messaging.event.instance.notifier.ArtifactUpdatedEvent} object with a public + * repository URL + * @return + */ + private static ArtifactUpdatedEvent createTestArtifactUpdatedEvent() { + ArtifactUpdatedEvent publicRepoEvent = new ArtifactUpdatedEvent(); + publicRepoEvent.setClusterId(CLUSTER_ID); + publicRepoEvent.setTenantId(TENANT_ID); + publicRepoEvent.setRepoURL("https://bitbucket.org/testapache2211/opentestrepo1.git"); + return publicRepoEvent; + } + + /** + * Setup the JCA test path, copy test configurations + * @return + */ private String setupJavaAgent() { try { log.info("Setting up Java cartridge agent test setup"); @@ -230,11 +291,15 @@ private String setupJavaAgent() { } } + /** + * Get current folder path + * @return + */ private static String getResourcesFolderPath() { return StringUtils.removeEnd(JavaCartridgeAgentTest.class.getResource("/").getPath(), File.separator); } - @Test + @Test(timeout = TIMEOUT) public void testJavaCartridgeAgent() throws Exception { Thread communicatorThread = new Thread(new Runnable() { @Override @@ -267,13 +332,7 @@ public void run() { } if (line.contains("Artifact repository found")) { // Send artifact updated event - ArtifactUpdatedEvent artifactUpdatedEvent = new ArtifactUpdatedEvent(); - artifactUpdatedEvent.setClusterId(CLUSTER_ID); - artifactUpdatedEvent.setTenantId(TENANT_ID); - artifactUpdatedEvent.setRepoURL("https://bitbucket.org/testapache2211/opentestrepo1.git"); - String topicName = MessagingUtil.getMessageTopicName(artifactUpdatedEvent); - EventPublisher eventPublisher = EventPublisherPool.getPublisher(topicName); - eventPublisher.publish(artifactUpdatedEvent); + publishEvent(artifactUpdatedEvent); } if (line.contains("Exception in thread") || line.contains("ERROR")) { //throw new RuntimeException(line); @@ -297,7 +356,7 @@ public void run() { } assertTrue("Instance started event was not received", instanceStarted); - assertTrue("Instance activated event was not received", instanceActivated); + assertTrue("Instance activated event was not received", instanceActivated == expectedResult); } /** @@ -320,12 +379,12 @@ private void startServerSocket(final int port) { public void run() { try { ServerSocket serverSocket = new ServerSocket(port); - serverSocket.accept(); serverSocketList.add(serverSocket); + serverSocket.accept(); } catch (IOException e) { String message = "Could not start server socket: [port] " + port; log.error(message, e); - throw new RuntimeException(message, e); +// throw new RuntimeException(message, e); } } }); diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh b/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh index 69a5034299..8d066b50fd 100644 --- a/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/bin/stratos.sh @@ -49,7 +49,7 @@ properties="-Dmb.ip=localhost -Dmonitoring.server.admin.username=admin -Dmonitoring.server.admin.password=admin -Dlog.file.paths=LOG_FILE_PATHS - -DAPP_PATH=/tmp/test-jca-source + -DAPPLICATION_PATH=/tmp/test-jca-source -Dsuper.tenant.repository.path=/repository/deployment/server/ -Dtenant.repository.path=/repository/tenants/ -Dextension.instance.started=instance-started.sh diff --git a/components/org.apache.stratos.cartridge.agent/src/test/resources/payload/launch-params b/components/org.apache.stratos.cartridge.agent/src/test/resources/payload/launch-params index 858d622d9a..da60eb6aa1 100644 --- a/components/org.apache.stratos.cartridge.agent/src/test/resources/payload/launch-params +++ b/components/org.apache.stratos.cartridge.agent/src/test/resources/payload/launch-params @@ -1 +1 @@ -APPLICATION_ID=application1,SERVICE_NAME=php,HOST_NAME=php.php.stratos.org,MULTITENANT=false,TENANT_ID=-1234,TENANT_RANGE=*,CARTRIDGE_ALIAS=php,CLUSTER_ID=php.php.domain,CLUSTER_INSTANCE_ID=single-cartridge-app-1,CARTRIDGE_KEY=PUjpXCLujDhYr5A6,DEPLOYMENT=default,REPO_URL=https://github.com/imesh/stratos-php-applications.git,PORTS=9080,PUPPET_IP=127.0.0.1,PUPPET_HOSTNAME=puppet.apache.stratos.org,PUPPET_ENV=false,MEMBER_ID=php.member-1,LB_CLUSTER_ID=null,NETWORK_PARTITION_ID=network-p1,PARTITION_ID=p1,APPLICATION_PATH=/tmp/stratos-pca-test-app-path/,MIN_COUNT=1,INTERNAL=false,CLUSTERING_PRIMARY_KEY=A,LOG_FILE_PATHS=/tmp/temp.log,PERSISTENCE_MAPPING=null,MP_IP=192.168.1.4,MB_PORT=1883 \ No newline at end of file +APPLICATION_ID=application1,SERVICE_NAME=php,HOST_NAME=php.php.stratos.org,MULTITENANT=false,TENANT_ID=-1234,TENANT_RANGE=*,CARTRIDGE_ALIAS=php,CLUSTER_ID=php.php.domain,CLUSTER_INSTANCE_ID=single-cartridge-app-1,CARTRIDGE_KEY=PUjpXCLujDhYr5A6,DEPLOYMENT=default,REPO_URL=https://github.com/imesh/stratos-php-applications.git,PORTS=9080,PUPPET_IP=127.0.0.1,PUPPET_HOSTNAME=puppet.apache.stratos.org,PUPPET_ENV=false,MEMBER_ID=php.member-1,LB_CLUSTER_ID=null,NETWORK_PARTITION_ID=network-p1,PARTITION_ID=p1,APPLICATION_PATH=/tmp/test-jca-source,MIN_COUNT=1,INTERNAL=false,CLUSTERING_PRIMARY_KEY=A,LOG_FILE_PATHS=/tmp/temp.log,PERSISTENCE_MAPPING=null,MP_IP=192.168.1.4,MB_PORT=1883 \ No newline at end of file diff --git a/products/cartridge-agent/modules/distribution/src/main/conf/thrift-client-config.xml b/products/cartridge-agent/modules/distribution/src/main/conf/thrift-client-config.xml index 4d51fc6bfe..092f4bf29b 100644 --- a/products/cartridge-agent/modules/distribution/src/main/conf/thrift-client-config.xml +++ b/products/cartridge-agent/modules/distribution/src/main/conf/thrift-client-config.xml @@ -19,5 +19,5 @@ admin admin localhost - 7611 + 7711 \ No newline at end of file From dd8676d5d8183f806ba565d55c32aac2042eafe4 Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Mon, 23 Feb 2015 01:45:20 +0530 Subject: [PATCH 09/10] JCA - Removed unused code in extensions managment. --- .../pom.xml | 39 -- .../extensions/DefaultExtensionHandler.java | 622 +----------------- .../cartridge/agent/util/ExtensionUtils.java | 108 ++- 3 files changed, 76 insertions(+), 693 deletions(-) diff --git a/components/org.apache.stratos.cartridge.agent/pom.xml b/components/org.apache.stratos.cartridge.agent/pom.xml index a6cf7651eb..a4f5d99361 100644 --- a/components/org.apache.stratos.cartridge.agent/pom.xml +++ b/components/org.apache.stratos.cartridge.agent/pom.xml @@ -33,45 +33,6 @@ Apache Stratos Cartridge Agent http://apache.org - - - default - - true - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*/JavaCartridgeAgentTest.java - - - - - - - - live - - - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*/JavaCartridgeAgentTest.java - - - - - - - - - net.lingala.zip4j diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/extensions/DefaultExtensionHandler.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/extensions/DefaultExtensionHandler.java index 20a18ddb1b..8112d470f4 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/extensions/DefaultExtensionHandler.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/extensions/DefaultExtensionHandler.java @@ -45,7 +45,6 @@ import org.apache.stratos.messaging.event.tenant.TenantUnSubscribedEvent; import org.apache.stratos.messaging.event.topology.*; import org.apache.stratos.messaging.message.receiver.tenant.TenantManager; -import org.apache.stratos.messaging.message.receiver.topology.TopologyManager; import java.lang.reflect.Type; import java.util.*; @@ -60,7 +59,6 @@ public class DefaultExtensionHandler implements ExtensionHandler { }.getType(); private static final Type serviceType = new TypeToken>() { }.getType(); - private final ArrayList wkMembers = new ArrayList(); @Override public void onInstanceStartedEvent() { @@ -112,6 +110,7 @@ public void onArtifactUpdatedEvent(ArtifactUpdatedEvent artifactUpdatedEvent) { if (log.isInfoEnabled()) { log.info("Executing git checkout"); } + RepositoryInformation repoInformation = new RepositoryInformation(); repoInformation.setRepoUsername(repoUsername); if (repoPassword == null) { @@ -119,6 +118,7 @@ public void onArtifactUpdatedEvent(ArtifactUpdatedEvent artifactUpdatedEvent) { } else { repoInformation.setRepoPassword(repoPassword); } + repoInformation.setRepoUrl(repoURL); repoInformation.setRepoPath(localRepoPath); repoInformation.setTenantId(tenantId); @@ -129,6 +129,7 @@ public void onArtifactUpdatedEvent(ArtifactUpdatedEvent artifactUpdatedEvent) { } catch (Exception e) { log.error(e); } + Map env = new HashMap(); env.put("STRATOS_ARTIFACT_UPDATED_CLUSTER_ID", artifactUpdatedEvent.getClusterId()); env.put("STRATOS_ARTIFACT_UPDATED_TENANT_ID", artifactUpdatedEvent.getTenantId()); @@ -222,7 +223,6 @@ private void cleanup() { CartridgeAgentEventPublisher.publishInstanceReadyToShutdownEvent(); } - @Override public void onMemberActivatedEvent(MemberActivatedEvent memberActivatedEvent) { if (log.isInfoEnabled()) { @@ -235,92 +235,16 @@ public void onMemberActivatedEvent(MemberActivatedEvent memberActivatedEvent) { log.debug("Member activated event msg:" + msg); } - boolean isConsistent = ExtensionUtils.checkTopologyConsistency(memberActivatedEvent.getServiceName(), + boolean memberInitialized = ExtensionUtils.checkTopologyConsistency(memberActivatedEvent.getServiceName(), memberActivatedEvent.getClusterId(), memberActivatedEvent.getMemberId()); - if (!isConsistent) { + if (!memberInitialized) { if (log.isErrorEnabled()) { - log.error("Topology is inconsistent...failed to execute member activated event"); + log.error("Member has not initialized. Failed to execute member activated event"); } return; } - Topology topology = TopologyManager.getTopology(); - Service service = topology.getService(memberActivatedEvent.getServiceName()); - Cluster cluster = service.getCluster(memberActivatedEvent.getClusterId()); - String lbClusterId = cluster.getMember(memberActivatedEvent.getMemberId()).getLbClusterId(); - Member member = cluster.getMember(memberActivatedEvent.getMemberId()); - - // check whether member activated event is received from the same cluster, lbcluster or cartridge group - if (ExtensionUtils.isRelevantMemberEvent(memberActivatedEvent.getServiceName(), memberActivatedEvent.getClusterId(), lbClusterId)) { - Map env = new HashMap(); - env.put("STRATOS_MEMBER_ACTIVATED_MEMBER_IP", memberActivatedEvent.getDefaultPrivateIP()); - env.put("STRATOS_MEMBER_ACTIVATED_MEMBER_ID", memberActivatedEvent.getMemberId()); - env.put("STRATOS_MEMBER_ACTIVATED_CLUSTER_ID", memberActivatedEvent.getClusterId()); - env.put("STRATOS_MEMBER_ACTIVATED_LB_CLUSTER_ID", lbClusterId); - env.put("STRATOS_MEMBER_ACTIVATED_NETWORK_PARTITION_ID", memberActivatedEvent.getNetworkPartitionId()); - env.put("STRATOS_MEMBER_ACTIVATED_SERVICE_NAME", memberActivatedEvent.getServiceName()); - - Collection ports = memberActivatedEvent.getPorts(); - String ports_str = ""; - for (Port port : ports) { - ports_str += port.getProtocol() + "," + port.getValue() + "," + port.getProxy() + "|"; - } - env.put("STRATOS_MEMBER_ACTIVATED_PORTS", ports_str); - - Collection members = cluster.getMembers(); - env.put("STRATOS_MEMBER_ACTIVATED_MEMBER_LIST_JSON", gson.toJson(members, memberType)); - String[] memberIps = ExtensionUtils.getLbMemberIp(lbClusterId); - if (memberIps != null && memberIps.length > 1) { - env.put("STRATOS_MEMBER_ACTIVATED_LB_IP", memberIps[0]); - env.put("STRATOS_MEMBER_ACTIVATED_LB_PUBLIC_IP", memberIps[1]); - } - env.put("STRATOS_TOPOLOGY_JSON", gson.toJson(topology.getServices(), serviceType)); - ExtensionUtils.addProperties(service.getProperties(), env, "MEMBER_ACTIVATED_SERVICE_PROPERTY"); - ExtensionUtils.addProperties(cluster.getProperties(), env, "MEMBER_ACTIVATED_CLUSTER_PROPERTY"); - ExtensionUtils.addProperties(member.getProperties(), env, "MEMBER_ACTIVATED_MEMBER_PROPERTY"); - - // if clustering is enabled check activated member is WK member - String flagClustering = CartridgeAgentConfiguration.getInstance().getIsClustered(); - - // if WK member is re-spawned, update axis2.xml - if (member.getProperties() != null && "true".equals(member.getProperties().getProperty(CartridgeAgentConstants.CLUSTERING_PRIMARY_KEY)) && - flagClustering != null && "true".equals(flagClustering.toLowerCase())) { - if (log.isDebugEnabled()) { - log.debug(" If WK member is re-spawned, update axis2.xml "); - } - boolean hasWKIpChanged = true; - for (Member m : this.wkMembers) { - if (m.getDefaultPrivateIP().equals(memberActivatedEvent.getDefaultPrivateIP())) { - hasWKIpChanged = false; - } - } - if (log.isDebugEnabled()) { - log.debug(" hasWKIpChanged " + hasWKIpChanged); - } - int minCount = Integer.parseInt(CartridgeAgentConfiguration.getInstance().getMinCount()); - boolean isWKMemberGroupReady = isWKMemberGroupReady(env, minCount); - if (log.isDebugEnabled()) { - log.debug("minCount " + minCount); - log.debug("isWKMemberGroupReady " + isWKMemberGroupReady); - } - if (hasWKIpChanged && isWKMemberGroupReady) { - if (log.isDebugEnabled()) { - log.debug("Setting env var STRATOS_UPDATE_WK_IP to true"); - } - env.put("STRATOS_UPDATE_WK_IP", "true"); - } - } - if (log.isDebugEnabled()) { - log.debug("Setting env var STRATOS_CLUSTERING to " + flagClustering); - } - env.put("STRATOS_CLUSTERING", flagClustering); - env.put("STRATOS_WK_MEMBER_COUNT", CartridgeAgentConfiguration.getInstance().getMinCount()); - ExtensionUtils.executeMemberActivatedExtension(env); - } else { - if (log.isDebugEnabled()) { - log.debug("Member activated event is not relevant...skipping agent extension"); - } - } + ExtensionUtils.executeMemberActivatedExtension(Collections.emptyMap()); } @Override @@ -352,7 +276,6 @@ public void onCompleteTopologyEvent(CompleteTopologyEvent completeTopologyEvent) ExtensionUtils.executeCompleteTopologyExtension(env); } - @Override public void onMemberInitializedEvent(MemberInitializedEvent memberInitializedEvent) { if (log.isDebugEnabled()) { @@ -362,27 +285,24 @@ public void onMemberInitializedEvent(MemberInitializedEvent memberInitializedEve String clusterIdInPayload = CartridgeAgentConfiguration.getInstance().getClusterId(); String memberIdInPayload = CartridgeAgentConfiguration.getInstance().getMemberId(); - - boolean isConsistent = ExtensionUtils.checkTopologyConsistency(serviceNameInPayload, clusterIdInPayload, memberIdInPayload); - if (!isConsistent) { +// boolean memberInitialized = ExtensionUtils.checkTopologyConsistency(serviceNameInPayload, clusterIdInPayload, memberIdInPayload); + boolean memberExists = ExtensionUtils.memberExistsInTopology(serviceNameInPayload, clusterIdInPayload, memberIdInPayload); + if (!memberExists) { // if this member isn't there in the complete topology if (log.isDebugEnabled()) { - log.debug("Member initialized event, topology is inconsistent"); + log.debug("Member does not exist in topology, or not in initialized state."); } - return; } else { if (log.isDebugEnabled()) { - log.debug("Member initialized event, topology is consistent, agent is initialized"); + log.debug("Member has initialized, topology is consistent, agent is initialized"); } + CartridgeAgentConfiguration.getInstance().setInitialized(true); } - } - @Override public void onMemberCreatedEvent(MemberCreatedEvent memberCreatedEvent) { - // listen to this just to get updated faster about the member initialization if (log.isDebugEnabled()) { log.debug("Instance Spawned event received"); @@ -390,11 +310,9 @@ public void onMemberCreatedEvent(MemberCreatedEvent memberCreatedEvent) { String serviceNameInPayload = CartridgeAgentConfiguration.getInstance().getServiceName(); String clusterIdInPayload = CartridgeAgentConfiguration.getInstance().getClusterId(); String memberIdInPayload = CartridgeAgentConfiguration.getInstance().getMemberId(); - boolean isConsistent = ExtensionUtils.checkTopologyConsistency(serviceNameInPayload, clusterIdInPayload, memberIdInPayload); - if (!isConsistent) { - // if this event is not relevant to this member - return; - } else { + + boolean memberInitialized = ExtensionUtils.checkTopologyConsistency(serviceNameInPayload, clusterIdInPayload, memberIdInPayload); + if (memberInitialized) { CartridgeAgentConfiguration.getInstance().setInitialized(true); } } @@ -425,50 +343,16 @@ public void onMemberTerminatedEvent(MemberTerminatedEvent memberTerminatedEvent) log.debug("Member terminated event msg:" + msg); } - boolean isConsistent = ExtensionUtils.checkTopologyConsistency(memberTerminatedEvent.getServiceName(), + boolean memberInitialized = ExtensionUtils.checkTopologyConsistency(memberTerminatedEvent.getServiceName(), memberTerminatedEvent.getClusterId(), memberTerminatedEvent.getMemberId()); - if (!isConsistent) { + if (!memberInitialized) { if (log.isErrorEnabled()) { - log.error("Topology is inconsistent...failed to execute member terminated event"); + log.error("Member has not initialized. Failed to execute member terminated event"); } return; } - Topology topology = TopologyManager.getTopology(); - Service service = topology.getService(memberTerminatedEvent.getServiceName()); - Cluster cluster = service.getCluster(memberTerminatedEvent.getClusterId()); - Member terminatedMember = cluster.getMember(memberTerminatedEvent.getMemberId()); - String lbClusterId = cluster.getMember(memberTerminatedEvent.getClusterId()).getLbClusterId(); - - // check whether terminated member is within the same cluster, LB cluster or cartridge group - if (ExtensionUtils.isRelevantMemberEvent(memberTerminatedEvent.getServiceName(), - memberTerminatedEvent.getClusterId(), lbClusterId)) { - - Collection members = cluster.getMembers(); - Map env = new HashMap(); - env.put("STRATOS_MEMBER_TERMINATED_MEMBER_IP", terminatedMember.getDefaultPrivateIP()); - env.put("STRATOS_MEMBER_TERMINATED_MEMBER_ID", memberTerminatedEvent.getMemberId()); - env.put("STRATOS_MEMBER_TERMINATED_CLUSTER_ID", memberTerminatedEvent.getClusterId()); - env.put("STRATOS_MEMBER_TERMINATED_LB_CLUSTER_ID", lbClusterId); - env.put("STRATOS_MEMBER_TERMINATED_NETWORK_PARTITION_ID", memberTerminatedEvent.getNetworkPartitionId()); - env.put("STRATOS_MEMBER_TERMINATED_SERVICE_NAME", memberTerminatedEvent.getServiceName()); - env.put("STRATOS_MEMBER_TERMINATED_MEMBER_LIST_JSON", gson.toJson(members, memberType)); - env.put("STRATOS_TOPOLOGY_JSON", gson.toJson(topology.getServices(), serviceType)); - String[] memberIps = ExtensionUtils.getLbMemberIp(lbClusterId); - if (memberIps != null && memberIps.length > 1) { - env.put("STRATOS_MEMBER_TERMINATED_LB_IP", memberIps[0]); - env.put("STRATOS_MEMBER_TERMINATED_LB_PUBLIC_IP", memberIps[1]); - } - - ExtensionUtils.addProperties(service.getProperties(), env, "MEMBER_TERMINATED_SERVICE_PROPERTY"); - ExtensionUtils.addProperties(cluster.getProperties(), env, "MEMBER_TERMINATED_CLUSTER_PROPERTY"); - ExtensionUtils.addProperties(terminatedMember.getProperties(), env, "MEMBER_TERMINATED_MEMBER_PROPERTY"); - ExtensionUtils.executeMemberTerminatedExtension(env); - } else { - if (log.isDebugEnabled()) { - log.debug("Member terminated event is not relevant...skipping agent extension"); - } - } + ExtensionUtils.executeMemberTerminatedExtension(Collections.emptyMap()); } @Override @@ -483,49 +367,16 @@ public void onMemberSuspendedEvent(MemberSuspendedEvent memberSuspendedEvent) { log.debug("Member suspended event msg:" + msg); } - boolean isConsistent = ExtensionUtils.checkTopologyConsistency(memberSuspendedEvent.getServiceName(), + boolean memberInitialized = ExtensionUtils.checkTopologyConsistency(memberSuspendedEvent.getServiceName(), memberSuspendedEvent.getClusterId(), memberSuspendedEvent.getMemberId()); - if (!isConsistent) { + if (!memberInitialized) { if (log.isErrorEnabled()) { - log.error("Topology is inconsistent...failed to execute member suspended event"); + log.error("Member has not initialized. Failed to execute member suspended event"); } return; } - String clusterId = memberSuspendedEvent.getClusterId(); - Topology topology = TopologyManager.getTopology(); - Service service = topology.getService(memberSuspendedEvent.getServiceName()); - Cluster cluster = service.getCluster(memberSuspendedEvent.getClusterId()); - Member suspendedMember = cluster.getMember(memberSuspendedEvent.getMemberId()); - String lbClusterId = cluster.getMember(memberSuspendedEvent.getClusterId()).getLbClusterId(); - - // check whether new member is in the same member cluster or LB cluster of this instance - if (ExtensionUtils.isRelevantMemberEvent(memberSuspendedEvent.getServiceName(), - memberSuspendedEvent.getClusterId(), lbClusterId)) { - Collection members = cluster.getMembers(); - Map env = new HashMap(); - env.put("STRATOS_MEMBER_SUSPENDED_MEMBER_IP", suspendedMember.getDefaultPrivateIP()); - env.put("STRATOS_MEMBER_SUSPENDED_MEMBER_ID", memberSuspendedEvent.getMemberId()); - env.put("STRATOS_MEMBER_SUSPENDED_CLUSTER_ID", memberSuspendedEvent.getClusterId()); - env.put("STRATOS_MEMBER_SUSPENDED_LB_CLUSTER_ID", lbClusterId); - env.put("STRATOS_MEMBER_SUSPENDED_NETWORK_PARTITION_ID", memberSuspendedEvent.getNetworkPartitionId()); - env.put("STRATOS_MEMBER_SUSPENDED_SERVICE_NAME", memberSuspendedEvent.getServiceName()); - env.put("STRATOS_MEMBER_SUSPENDED_MEMBER_LIST_JSON", gson.toJson(members, memberType)); - env.put("STRATOS_TOPOLOGY_JSON", gson.toJson(topology.getServices(), serviceType)); - String[] memberIps = ExtensionUtils.getLbMemberIp(lbClusterId); - if (memberIps != null && memberIps.length > 1) { - env.put("STRATOS_MEMBER_SUSPENDED_LB_IP", memberIps[0]); - env.put("STRATOS_MEMBER_SUSPENDED_LB_PUBLIC_IP", memberIps[1]); - } - ExtensionUtils.addProperties(service.getProperties(), env, "MEMBER_SUSPENDED_SERVICE_PROPERTY"); - ExtensionUtils.addProperties(cluster.getProperties(), env, "MEMBER_SUSPENDED_CLUSTER_PROPERTY"); - ExtensionUtils.addProperties(suspendedMember.getProperties(), env, "MEMBER_SUSPENDED_MEMBER_PROPERTY"); - ExtensionUtils.executeMemberSuspendedExtension(env); - } else { - if (log.isDebugEnabled()) { - log.debug("Member suspended event is not relevant...skipping agent extension"); - } - } + ExtensionUtils.executeMemberSuspendedExtension(Collections.emptyMap()); } @Override @@ -540,373 +391,16 @@ public void onMemberStartedEvent(MemberStartedEvent memberStartedEvent) { log.debug("Member started event msg:" + msg); } - boolean isConsistent = ExtensionUtils.checkTopologyConsistency(memberStartedEvent.getServiceName(), + boolean memberInitialized = ExtensionUtils.checkTopologyConsistency(memberStartedEvent.getServiceName(), memberStartedEvent.getClusterId(), memberStartedEvent.getMemberId()); - if (!isConsistent) { + if (!memberInitialized) { if (log.isErrorEnabled()) { - log.error("Topology is inconsistent...failed to execute member started event"); + log.error("Member has not initialized. Failed to execute member started event"); } return; } - String clusterId = memberStartedEvent.getClusterId(); - Topology topology = TopologyManager.getTopology(); - Service service = topology.getService(memberStartedEvent.getServiceName()); - Cluster cluster = service.getCluster(memberStartedEvent.getClusterId()); - Member startedMember = cluster.getMember(memberStartedEvent.getMemberId()); - String lbClusterId = cluster.getMember(memberStartedEvent.getMemberId()).getLbClusterId(); - - // check whether new member is in the same member cluster or LB cluster of this instance - if (ExtensionUtils.isRelevantMemberEvent(memberStartedEvent.getServiceName(), - memberStartedEvent.getClusterId(), lbClusterId)) { - Collection members = cluster.getMembers(); - Map env = new HashMap(); - env.put("STRATOS_MEMBER_STARTED_MEMBER_IP", startedMember.getDefaultPrivateIP()); - env.put("STRATOS_MEMBER_STARTED_MEMBER_ID", memberStartedEvent.getMemberId()); - env.put("STRATOS_MEMBER_STARTED_CLUSTER_ID", memberStartedEvent.getClusterId()); - env.put("STRATOS_MEMBER_STARTED_LB_CLUSTER_ID", lbClusterId); - env.put("STRATOS_MEMBER_STARTED_NETWORK_PARTITION_ID", memberStartedEvent.getNetworkPartitionId()); - env.put("STRATOS_MEMBER_STARTED_SERVICE_NAME", memberStartedEvent.getServiceName()); - env.put("STRATOS_MEMBER_STARTED_MEMBER_LIST_JSON", gson.toJson(members, memberType)); - env.put("STRATOS_MEMBER_STARTED_TOPOLOGY_JSON", gson.toJson(topology.getServices(), serviceType)); - String[] memberIps = ExtensionUtils.getLbMemberIp(lbClusterId); - if (memberIps != null && memberIps.length > 1) { - env.put("STRATOS_MEMBER_STARTED_LB_IP", memberIps[0]); - env.put("STRATOS_MEMBER_STARTED_LB_PUBLIC_IP", memberIps[1]); - } - ExtensionUtils.addProperties(service.getProperties(), env, "MEMBER_STARTED_SERVICE_PROPERTY"); - ExtensionUtils.addProperties(cluster.getProperties(), env, "MEMBER_STARTED_CLUSTER_PROPERTY"); - ExtensionUtils.addProperties(startedMember.getProperties(), env, "MEMBER_STARTED_MEMBER_PROPERTY"); - ExtensionUtils.executeMemberStartedExtension(env); - } else { - if (log.isDebugEnabled()) { - log.debug("Member started event is not relevant...skipping agent extension"); - } - } - } - - private boolean isWKMemberGroupReady(Map envParameters, int minCount) { - Topology topology = TopologyManager.getTopology(); - if (topology == null || !topology.isInitialized()) { - return false; - } - String serviceGroupInPayload = CartridgeAgentConfiguration.getInstance().getServiceGroup(); - if (serviceGroupInPayload != null) { - envParameters.put("STRATOS_SERVICE_GROUP", serviceGroupInPayload); - } - - // clustering logic for apimanager - if (serviceGroupInPayload != null && serviceGroupInPayload.equals("apim")) { - - // handle apistore and publisher case - if (CartridgeAgentConfiguration.getInstance().getServiceName().equals("apistore") || - CartridgeAgentConfiguration.getInstance().getServiceName().equals("publisher")) { - - Collection apistoreClusterCollection = topology.getService("apistore").getClusters(); - Collection publisherClusterCollection = topology.getService("publisher").getClusters(); - - List apistoreMemberList = new ArrayList(); - for (Member member : apistoreClusterCollection.iterator().next().getMembers()) { - if (member.getStatus().equals(MemberStatus.Starting) || member.getStatus().equals(MemberStatus.Active)) { - apistoreMemberList.add(member); - this.wkMembers.add(member); - } - } - if (apistoreMemberList.isEmpty()) { - if (log.isDebugEnabled()) { - log.debug("API Store members not yet created"); - } - return false; - } - Member apistoreMember = apistoreMemberList.get(0); - envParameters.put("STRATOS_WK_APISTORE_MEMBER_IP", apistoreMember.getDefaultPrivateIP()); - if (log.isDebugEnabled()) { - log.debug("STRATOS_WK_APISTORE_MEMBER_IP: " + apistoreMember.getDefaultPrivateIP()); - } - - List publisherMemberList = new ArrayList(); - for (Member member : publisherClusterCollection.iterator().next().getMembers()) { - if (member.getStatus().equals(MemberStatus.Starting) || member.getStatus().equals(MemberStatus.Active)) { - publisherMemberList.add(member); - this.wkMembers.add(member); - } - } - if (publisherMemberList.isEmpty()) { - if (log.isDebugEnabled()) { - log.debug("API Publisher members not yet created"); - } - return false; - } - Member publisherMember = publisherMemberList.get(0); - envParameters.put("STRATOS_WK_PUBLISHER_MEMBER_IP", publisherMember.getDefaultPrivateIP()); - if (log.isDebugEnabled()) { - log.debug("STRATOS_WK_PUBLISHER_MEMBER_IP: " + publisherMember.getDefaultPrivateIP()); - } - - return true; - - } else if (CartridgeAgentConfiguration.getInstance().getServiceName().equals("gatewaymgt") || - CartridgeAgentConfiguration.getInstance().getServiceName().equals("gateway")) { - - if (CartridgeAgentConfiguration.getInstance().getDeployment() != null) { - // check if deployment is Manager Worker separated - if (CartridgeAgentConfiguration.getInstance().getDeployment().equalsIgnoreCase(CartridgeAgentConstants.DEPLOYMENT_MANAGER) || - CartridgeAgentConfiguration.getInstance().getDeployment().equalsIgnoreCase(CartridgeAgentConstants.DEPLOYMENT_WORKER)) { - - log.info("Deployment pattern for the node: " + CartridgeAgentConfiguration.getInstance().getDeployment()); - envParameters.put("DEPLOYMENT", CartridgeAgentConfiguration.getInstance().getDeployment()); - // check if WKA members of Manager Worker separated deployment is ready - return isManagerWorkerWKAGroupReady(envParameters); - } - } - - - } else if (CartridgeAgentConfiguration.getInstance().getServiceName().equals("keymanager")) { - return true; - } - } else { - - if (CartridgeAgentConfiguration.getInstance().getDeployment() != null) { - // check if deployment is Manager Worker separated - if (CartridgeAgentConfiguration.getInstance().getDeployment().equalsIgnoreCase(CartridgeAgentConstants.DEPLOYMENT_MANAGER) || - CartridgeAgentConfiguration.getInstance().getDeployment().equalsIgnoreCase(CartridgeAgentConstants.DEPLOYMENT_WORKER)) { - log.info("Deployment pattern for the node: " + CartridgeAgentConfiguration.getInstance().getDeployment()); - envParameters.put("DEPLOYMENT", CartridgeAgentConfiguration.getInstance().getDeployment()); - // check if WKA members of Manager Worker separated deployment is ready - return isManagerWorkerWKAGroupReady(envParameters); - } - } - - String serviceNameInPayload = CartridgeAgentConfiguration.getInstance().getServiceName(); - String clusterIdInPayload = CartridgeAgentConfiguration.getInstance().getClusterId(); - Service service = topology.getService(serviceNameInPayload); - Cluster cluster = service.getCluster(clusterIdInPayload); - - List wkMembers = new ArrayList(); - for (Member member : cluster.getMembers()) { - if (member.getProperties() != null && - member.getProperties().containsKey("PRIMARY") && - member.getProperties().getProperty("PRIMARY").toLowerCase().equals("true") && - (member.getStatus().equals(MemberStatus.Starting) || member.getStatus().equals(MemberStatus.Active)) - ) { - wkMembers.add(member); - this.wkMembers.add(member); - if (log.isDebugEnabled()) { - log.debug("Found WKA: STRATOS_WK_MEMBER_IP: " + member.getDefaultPrivateIP()); - } - } - } - if (wkMembers.size() >= minCount) { - int idx = 0; - for (Member member : wkMembers) { - envParameters.put("STRATOS_WK_MEMBER_" + idx + "_IP", member.getDefaultPrivateIP()); - if (log.isDebugEnabled()) { - log.debug("STRATOS_WK_MEMBER_" + idx + "_IP: " + member.getDefaultPrivateIP()); - } - idx++; - } - return true; - } - } - return false; - } - - // generic worker manager separated clustering logic - private boolean isManagerWorkerWKAGroupReady(Map envParameters) { - - // for this, we need both manager cluster service name and worker cluster service name - String managerServiceName = CartridgeAgentConfiguration.getInstance().getManagerServiceName(); - String workerServiceName = CartridgeAgentConfiguration.getInstance().getWorkerServiceName(); - - // managerServiceName and workerServiceName both should not be null /empty - if (managerServiceName == null || managerServiceName.isEmpty()) { - log.error("Manager service name [ " + managerServiceName + " ] is invalid"); - return false; - } - if (workerServiceName == null || workerServiceName.isEmpty()) { - log.error("Worker service name [ " + workerServiceName + " ] is invalid"); - return false; - } - - boolean minManagerInstancesAvailable = false; - boolean minWorkerInstancesAvailable = false; - - TopologyManager.acquireReadLock(); - - try { - Service managerService = TopologyManager.getTopology().getService(managerServiceName); - Service workerService = TopologyManager.getTopology().getService(workerServiceName); - - if (managerService == null) { - log.warn("Service [ " + managerServiceName + " ] is not found"); - return false; - } - - if (workerService == null) { - log.warn("Service [ " + workerServiceName + " ] is not found"); - return false; - } - - // manager clusters - Collection managerClusters = managerService.getClusters(); - if (managerClusters == null || managerClusters.isEmpty()) { - log.warn("No clusters found for service [ " + managerServiceName + " ]"); - return false; - } - - int managerMinInstanceCount = 1; - boolean managerMinInstanceCountFound = false; - - List managerWkaMembers = new ArrayList(); - for (Member member : managerClusters.iterator().next().getMembers()) { - - if (member.getProperties() != null && member.getProperties().containsKey("PRIMARY") && - member.getProperties().getProperty("PRIMARY").toLowerCase().equals("true") && - (member.getStatus().equals(MemberStatus.Starting) || member.getStatus().equals(MemberStatus.Active))) { - - managerWkaMembers.add(member); - this.wkMembers.add(member); - - // get the min instance count - if (!managerMinInstanceCountFound) { - managerMinInstanceCount = getMinInstanceCountFromMemberProperties(member); - managerMinInstanceCountFound = true; - log.info("Manager min instance count: " + managerMinInstanceCount); - - } - } - } - - if (managerWkaMembers.size() >= managerMinInstanceCount) { - minManagerInstancesAvailable = true; - int idx = 0; - for (Member member : managerWkaMembers) { - envParameters.put("STRATOS_WK_MANAGER_MEMBER_" + idx + "_IP", member.getDefaultPrivateIP()); - if (log.isDebugEnabled()) { - log.debug("STRATOS_WK_MANAGER_MEMBER_" + idx + "_IP: " + member.getDefaultPrivateIP()); - } - idx++; - } - - envParameters.put("STRATOS_WK_MANAGER_MEMBER_COUNT", Integer.toString(managerMinInstanceCount)); - } - - // If all the manager members are non primary and is greate than or equal to mincount, - // minManagerInstancesAvailable should be true - boolean allManagersNonPrimary = true; - for (Member member : managerClusters.iterator().next().getMembers()) { - - // get the min instance count - if (!managerMinInstanceCountFound) { - managerMinInstanceCount = getMinInstanceCountFromMemberProperties(member); - managerMinInstanceCountFound = true; - log.info("Manager min instance count when allManagersNonPrimary true : " + managerMinInstanceCount); - } - - if (member.getProperties() != null && member.getProperties().containsKey("PRIMARY") && - member.getProperties().getProperty("PRIMARY").toLowerCase().equals("true")) { - allManagersNonPrimary = false; - break; - } - } - if (log.isDebugEnabled()) { - log.debug(" allManagerNonPrimary & managerMinInstanceCount [" - + allManagersNonPrimary + "], [" + managerMinInstanceCount + "] "); - } - if (allManagersNonPrimary && managerClusters.size() >= managerMinInstanceCount) { - minManagerInstancesAvailable = true; - } - - // worker cluster - Collection workerClusters = workerService.getClusters(); - if (workerClusters == null || workerClusters.isEmpty()) { - log.warn("No clusters found for service [ " + workerServiceName + " ]"); - return false; - } - - int workerMinInstanceCount = 1; - boolean workerMinInstanceCountFound = false; - - List workerWkaMembers = new ArrayList(); - for (Member member : workerClusters.iterator().next().getMembers()) { - if (log.isDebugEnabled()) { - log.debug("Checking member : " + member.getMemberId()); - } - if (member.getProperties() != null && - member.getProperties().containsKey("PRIMARY") && - member.getProperties().getProperty("PRIMARY").toLowerCase().equals("true") && - (member.getStatus().equals(MemberStatus.Starting) || member.getStatus().equals(MemberStatus.Active))) { - if (log.isDebugEnabled()) { - log.debug("Added worker member " + member.getMemberId()); - } - workerWkaMembers.add(member); - this.wkMembers.add(member); - - // get the min instance count - if (!workerMinInstanceCountFound) { - workerMinInstanceCount = getMinInstanceCountFromMemberProperties(member); - workerMinInstanceCountFound = true; - if (log.isDebugEnabled()) { - log.debug("Worker min instance count: " + workerMinInstanceCount); - } - } - } - } - - if (workerWkaMembers.size() >= workerMinInstanceCount) { - minWorkerInstancesAvailable = true; - int idx = 0; - for (Member member : workerWkaMembers) { - envParameters.put("STRATOS_WK_WORKER_MEMBER_" + idx + "_IP", member.getDefaultPrivateIP()); - if (log.isDebugEnabled()) { - log.debug("STRATOS_WK_WORKER_MEMBER_" + idx + "_IP: " + member.getDefaultPrivateIP()); - } - idx++; - } - - envParameters.put("STRATOS_WK_WORKER_MEMBER_COUNT", Integer.toString(workerMinInstanceCount)); - } - - } finally { - TopologyManager.releaseReadLock(); - } - - if (log.isDebugEnabled()) { - log.debug(" Returnning values minManagerInstancesAvailable && minWorkerInstancesAvailable [" + - minManagerInstancesAvailable + "], [" + minWorkerInstancesAvailable + "] "); - } - return (minManagerInstancesAvailable && minWorkerInstancesAvailable); - } - - - private int getMinInstanceCountFromMemberProperties(Member member) { - - // default value is 1 - int minInstanceCount = 1; - - if (member.getProperties().containsKey("MIN_COUNT")) { - minInstanceCount = Integer.parseInt(member.getProperties().getProperty("MIN_COUNT")); - } - - return minInstanceCount; - } - - private void waitForWKMembers(Map envParameters) { - int minCount = Integer.parseInt(CartridgeAgentConfiguration.getInstance().getMinCount()); - boolean isWKMemberGroupReady = false; - while (!isWKMemberGroupReady) { - if (log.isInfoEnabled()) { - log.info(String.format("Waiting for %d well known members...", minCount)); - } - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - } - - TopologyManager.acquireReadLock(); - isWKMemberGroupReady = isWKMemberGroupReady(envParameters, minCount); - TopologyManager.releaseReadLock(); - } + ExtensionUtils.executeMemberStartedExtension(Collections.emptyMap()); } @Override @@ -921,51 +415,16 @@ public void startServerExtension() { String clusterIdInPayload = CartridgeAgentConfiguration.getInstance().getClusterId(); String memberIdInPayload = CartridgeAgentConfiguration.getInstance().getMemberId(); - try { - TopologyManager.acquireReadLock(); - boolean isConsistent = ExtensionUtils.checkTopologyConsistency(serviceNameInPayload, - clusterIdInPayload, memberIdInPayload); - if (!isConsistent) { - if (log.isErrorEnabled()) { - log.error("Topology is inconsistent...failed to execute start server event"); - } - return; - } - Topology topology = TopologyManager.getTopology(); - Service service = topology.getService(serviceNameInPayload); - Cluster cluster = service.getCluster(clusterIdInPayload); - - // store environment variable parameters to be passed to extension shell script - Map env = new HashMap(); - - // if clustering is enabled wait until all well known members have started - String flagClustering = CartridgeAgentConfiguration.getInstance().getIsClustered(); - if (flagClustering != null && flagClustering.toLowerCase().equals("true")) { - env.put("STRATOS_CLUSTERING", "true"); - env.put("STRATOS_WK_MEMBER_COUNT", CartridgeAgentConfiguration.getInstance().getMinCount()); - if (CartridgeAgentConfiguration.getInstance().getIsPrimary().toLowerCase().equals("true")) { - env.put("STRATOS_PRIMARY", "true"); - } else { - env.put("STRATOS_PRIMARY", "false"); - } - TopologyManager.releaseReadLock(); - waitForWKMembers(env); - if (log.isInfoEnabled()) { - log.info(String.format("All well known members have started! Resuming start server extension...")); - } - TopologyManager.acquireReadLock(); - } - - env.put("STRATOS_TOPOLOGY_JSON", gson.toJson(topology.getServices(), serviceType)); - env.put("STRATOS_MEMBER_LIST_JSON", gson.toJson(cluster.getMembers(), memberType)); - ExtensionUtils.executeStartServersExtension(env); - } catch (Exception e) { + boolean isConsistent = ExtensionUtils.checkTopologyConsistency(serviceNameInPayload, + clusterIdInPayload, memberIdInPayload); + if (!isConsistent) { if (log.isErrorEnabled()) { - log.error("Error processing start servers event", e); + log.error("Topology is inconsistent...Failed to execute start server event"); } - } finally { - TopologyManager.releaseReadLock(); + return; } + + ExtensionUtils.executeStartServersExtension(Collections.emptyMap()); } @Override @@ -1087,17 +546,6 @@ public void onTenantUnSubscribedEvent(TenantUnSubscribedEvent tenantUnSubscribed //ApplicationSignUpRemovedEvent @Override public void onApplicationSignUpRemovedEvent(ApplicationSignUpRemovedEvent applicationSignUpRemovedEvent) { - /* - * self.log.info( - "Tenant unsubscribed event received: [tenant] " + application_signup_removal_event.tenantId + - " [application ID] " + application_signup_removal_event.applicationId - ) - - if self.cartridge_agent_config.service_name == application_signup_removal_event.applicationId: - agentgithandler.AgentGitHandler.remove_repo(application_signup_removal_event.tenant_id) - - extensionutils.execute_application_signup_removal_extension({}) - */ if (log.isInfoEnabled()) { log.info(String.format("applicationSignUpRemovedEvent event received: [appId] %s [tenantId] %s ", applicationSignUpRemovedEvent.getApplicationId(), applicationSignUpRemovedEvent.getTenantId())); diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/ExtensionUtils.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/ExtensionUtils.java index 79ac6041a0..2164f9c617 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/ExtensionUtils.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/ExtensionUtils.java @@ -24,10 +24,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.stratos.cartridge.agent.config.CartridgeAgentConfiguration; import org.apache.stratos.common.util.CommandUtils; -import org.apache.stratos.messaging.domain.topology.Cluster; -import org.apache.stratos.messaging.domain.topology.Member; -import org.apache.stratos.messaging.domain.topology.Service; -import org.apache.stratos.messaging.domain.topology.Topology; +import org.apache.stratos.messaging.domain.topology.*; import org.apache.stratos.messaging.message.receiver.topology.TopologyManager; import java.io.File; @@ -135,66 +132,6 @@ public static String[] getLbMemberIp(String lbClusterId) { return null; } - public static boolean isRelevantMemberEvent(String serviceName, String clusterId, String lbClusterId) { - String clusterIdInPayload = CartridgeAgentConfiguration.getInstance().getClusterId(); - if (clusterIdInPayload == null) { - return false; - } - Topology topology = TopologyManager.getTopology(); - if (topology == null || !topology.isInitialized()) { - return false; - } - - if (clusterIdInPayload.equals(clusterId)) { - return true; - } - - if (clusterIdInPayload.equals(lbClusterId)) { - return true; - } - - String serviceGroupInPayload = CartridgeAgentConfiguration.getInstance().getServiceGroup(); - if (serviceGroupInPayload != null) { - Properties serviceProperties = topology.getService(serviceName).getProperties(); - if (serviceProperties == null) { - return false; - } - String memberServiceGroup = serviceProperties.getProperty(CartridgeAgentConstants.SERVICE_GROUP_TOPOLOGY_KEY); - if (memberServiceGroup != null && memberServiceGroup.equals(serviceGroupInPayload)) { - if(serviceName.equals(CartridgeAgentConfiguration.getInstance().getServiceName())) { - if (log.isDebugEnabled()) { - log.debug("Service names are same"); - } - return true; - }else if(CartridgeAgentConfiguration.getInstance().getServiceName().equals("apistore") && "publisher".equals(serviceName)) { - if (log.isDebugEnabled()) { - log.debug("Service name in payload is [store]. Serivce name in event is ["+serviceName+"] "); - } - return true; - }else if(CartridgeAgentConfiguration.getInstance().getServiceName().equals("publisher") && "apistore".equals(serviceName)) { - if (log.isDebugEnabled()) { - log.debug("Service name in payload is [publisher]. Serivce name in event is ["+serviceName+"] "); - } - return true; - }else if(CartridgeAgentConstants.DEPLOYMENT_WORKER.equals(CartridgeAgentConfiguration.getInstance().getDeployment()) && - serviceName.equals(CartridgeAgentConfiguration.getInstance().getManagerServiceName())) { - if (log.isDebugEnabled()) { - log.debug("Deployment is worker. Worker's manager service name & service name in event are same"); - } - return true; - }else if (CartridgeAgentConstants.DEPLOYMENT_MANAGER.equals(CartridgeAgentConfiguration.getInstance().getDeployment()) && - serviceName.equals(CartridgeAgentConfiguration.getInstance().getWorkerServiceName())) { - if (log.isDebugEnabled()) { - log.debug("Deployment is manager. Manager's worker service name & service name in event are same"); - } - return true; - } - } - } - - return false; - } - private static Map cleanProcessParameters(Map envParameters) { Iterator> iter = envParameters.entrySet().iterator(); while (iter.hasNext()) { @@ -510,7 +447,7 @@ public static void executeTenantSubscribedExtension(Map envParam String command = prepareCommand(script); addPayloadParameters(envParameters); cleanProcessParameters(envParameters); - String output = CommandUtils.executeCommand(command, envParameters); + CommandUtils.executeCommand(command, envParameters); } catch (Exception e) { log.error("Could not execute tenant subscribed extension", e); } @@ -525,7 +462,7 @@ public static void executeTenantUnSubscribedExtension(Map envPar String command = prepareCommand(script); addPayloadParameters(envParameters); cleanProcessParameters(envParameters); - String output = CommandUtils.executeCommand(command, envParameters); + CommandUtils.executeCommand(command, envParameters); } catch (Exception e) { log.error("Could not execute tenant un-subscribed extension", e); } @@ -550,7 +487,7 @@ public static void waitForCompleteTopology() { } } - public static boolean checkTopologyConsistency(String serviceName, String clusterId, String memberId){ + public static boolean checkTopologyConsistency(String serviceName, String clusterId, String memberId) { Topology topology = TopologyManager.getTopology(); Service service = topology.getService(serviceName); if (service == null) { @@ -559,6 +496,7 @@ public static boolean checkTopologyConsistency(String serviceName, String cluste } return false; } + Cluster cluster = service.getCluster(clusterId); if (cluster == null) { if (log.isErrorEnabled()) { @@ -566,13 +504,49 @@ public static boolean checkTopologyConsistency(String serviceName, String cluste } return false; } + Member activatedMember = cluster.getMember(memberId); if (activatedMember == null) { if (log.isErrorEnabled()) { log.error(String.format("Member id not found in topology [member] %s", memberId)); } return false; + } else if (activatedMember.getStatus() != MemberStatus.Initialized) { + if (log.isErrorEnabled()) { + log.error(String.format("Member found in topology, but not in initialized state [member] %s", memberId)); + } + return false; + } + + return true; + } + + public static boolean memberExistsInTopology(String serviceNameInPayload, String clusterIdInPayload, String memberIdInPayload) { + Topology topology = TopologyManager.getTopology(); + Service service = topology.getService(serviceNameInPayload); + if (service == null) { + if (log.isErrorEnabled()) { + log.error(String.format("Service not found in topology [service] %s", serviceNameInPayload)); + } + return false; } + + Cluster cluster = service.getCluster(clusterIdInPayload); + if (cluster == null) { + if (log.isErrorEnabled()) { + log.error(String.format("Cluster id not found in topology [cluster] %s", clusterIdInPayload)); + } + return false; + } + + Member activatedMember = cluster.getMember(memberIdInPayload); + if (activatedMember == null) { + if (log.isErrorEnabled()) { + log.error(String.format("Member id not found in topology [member] %s", memberIdInPayload)); + } + return false; + } + return true; } } From 8bf0894a061fe6031ca66e0cb43f2dd862553a61 Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Mon, 23 Feb 2015 12:31:29 +0530 Subject: [PATCH 10/10] JCA - Refactored member checking in ExtensionUtils --- .../extensions/DefaultExtensionHandler.java | 5 +- .../cartridge/agent/util/ExtensionUtils.java | 61 ++++++++----------- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/extensions/DefaultExtensionHandler.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/extensions/DefaultExtensionHandler.java index 8112d470f4..e4d22b6dd5 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/extensions/DefaultExtensionHandler.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/extensions/DefaultExtensionHandler.java @@ -285,9 +285,8 @@ public void onMemberInitializedEvent(MemberInitializedEvent memberInitializedEve String clusterIdInPayload = CartridgeAgentConfiguration.getInstance().getClusterId(); String memberIdInPayload = CartridgeAgentConfiguration.getInstance().getMemberId(); -// boolean memberInitialized = ExtensionUtils.checkTopologyConsistency(serviceNameInPayload, clusterIdInPayload, memberIdInPayload); - boolean memberExists = ExtensionUtils.memberExistsInTopology(serviceNameInPayload, clusterIdInPayload, memberIdInPayload); - if (!memberExists) { + Member activatedMember = ExtensionUtils.getMemberFromTopology(serviceNameInPayload, clusterIdInPayload, memberIdInPayload); + if (activatedMember == null) { // if this member isn't there in the complete topology if (log.isDebugEnabled()) { log.debug("Member does not exist in topology, or not in initialized state."); diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/ExtensionUtils.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/ExtensionUtils.java index 2164f9c617..f395af7999 100644 --- a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/ExtensionUtils.java +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/ExtensionUtils.java @@ -487,31 +487,17 @@ public static void waitForCompleteTopology() { } } + /** + * Check if the specified member is in the topology and is in MemberStatus.Initialized state + * @param serviceName + * @param clusterId + * @param memberId + * @return true if member is present in the topology and in initialized state, false otherwise + */ public static boolean checkTopologyConsistency(String serviceName, String clusterId, String memberId) { - Topology topology = TopologyManager.getTopology(); - Service service = topology.getService(serviceName); - if (service == null) { - if (log.isErrorEnabled()) { - log.error(String.format("Service not found in topology [service] %s", serviceName)); - } - return false; - } - - Cluster cluster = service.getCluster(clusterId); - if (cluster == null) { - if (log.isErrorEnabled()) { - log.error(String.format("Cluster id not found in topology [cluster] %s", clusterId)); - } - return false; - } - Member activatedMember = cluster.getMember(memberId); - if (activatedMember == null) { - if (log.isErrorEnabled()) { - log.error(String.format("Member id not found in topology [member] %s", memberId)); - } - return false; - } else if (activatedMember.getStatus() != MemberStatus.Initialized) { + Member activatedMember = getMemberFromTopology(serviceName, clusterId, memberId); + if (activatedMember.getStatus() != MemberStatus.Initialized) { if (log.isErrorEnabled()) { log.error(String.format("Member found in topology, but not in initialized state [member] %s", memberId)); } @@ -521,32 +507,39 @@ public static boolean checkTopologyConsistency(String serviceName, String cluste return true; } - public static boolean memberExistsInTopology(String serviceNameInPayload, String clusterIdInPayload, String memberIdInPayload) { + /** + * Gets the specified member from the topology + * @param serviceName + * @param clusterId + * @param memberId + * @return {@link org.apache.stratos.messaging.domain.topology.Member} if member is in the topology, null otherwise + */ + public static Member getMemberFromTopology(String serviceName, String clusterId, String memberId) { Topology topology = TopologyManager.getTopology(); - Service service = topology.getService(serviceNameInPayload); + Service service = topology.getService(serviceName); if (service == null) { if (log.isErrorEnabled()) { - log.error(String.format("Service not found in topology [service] %s", serviceNameInPayload)); + log.error(String.format("Service not found in topology [service] %s", serviceName)); } - return false; + return null; } - Cluster cluster = service.getCluster(clusterIdInPayload); + Cluster cluster = service.getCluster(clusterId); if (cluster == null) { if (log.isErrorEnabled()) { - log.error(String.format("Cluster id not found in topology [cluster] %s", clusterIdInPayload)); + log.error(String.format("Cluster id not found in topology [cluster] %s", clusterId)); } - return false; + return null; } - Member activatedMember = cluster.getMember(memberIdInPayload); + Member activatedMember = cluster.getMember(memberId); if (activatedMember == null) { if (log.isErrorEnabled()) { - log.error(String.format("Member id not found in topology [member] %s", memberIdInPayload)); + log.error(String.format("Member id not found in topology [member] %s", memberId)); } - return false; + return null; } - return true; + return activatedMember; } }