modules/common/cassandra-common-itests/src/main/java/org/rhq/cassandra/CCMTestNGListener.java | 167 ++++++++++ modules/common/cassandra-common-itests/src/main/java/org/rhq/cassandra/CassandraClusterManager.java | 167 ---------- modules/common/cassandra-common-itests/src/test/java/org/rhq/cassandra/common/BootstrapDeployerTest.java | 4 modules/common/cassandra-common/src/main/java/org/rhq/cassandra/BootstrapDeployer.java | 48 ++ modules/common/cassandra-common/src/main/resources/logging.properties | 27 + modules/common/cassandra-common/src/main/resources/module/main/module.xml | 2 modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/CassandraIntegrationTest.java | 4 7 files changed, 244 insertions(+), 175 deletions(-)
New commits: commit 8e89be92358a089cdc298300f8ed043b32d078c8 Author: John Sanda jsanda@redhat.com Date: Thu Dec 20 14:42:08 2012 -0500
Renaming testng listener as part of refactoring for CassandraClustermanager
diff --git a/modules/common/cassandra-common-itests/src/main/java/org/rhq/cassandra/CCMTestNGListener.java b/modules/common/cassandra-common-itests/src/main/java/org/rhq/cassandra/CCMTestNGListener.java new file mode 100644 index 0000000..750145a --- /dev/null +++ b/modules/common/cassandra-common-itests/src/main/java/org/rhq/cassandra/CCMTestNGListener.java @@ -0,0 +1,167 @@ +/* + * + * * RHQ Management Platform + * * Copyright (C) 2005-2012 Red Hat, Inc. + * * All rights reserved. + * * + * * This program is free software; you can redistribute it and/or modify + * * it under the terms of the GNU General Public License, version 2, as + * * published by the Free Software Foundation, and/or the GNU Lesser + * * General Public License, version 2.1, also as published by the Free + * * Software Foundation. + * * + * * This program is distributed in the hope that it will be useful, + * * but WITHOUT ANY WARRANTY; without even the implied warranty of + * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * * GNU General Public License and the GNU Lesser General Public License + * * for more details. + * * + * * You should have received a copy of the GNU General Public License + * * and the GNU Lesser General Public License along with this program; + * * if not, write to the Free Software Foundation, Inc., + * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +package org.rhq.cassandra; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.IInvokedMethod; +import org.testng.IInvokedMethodListener; +import org.testng.ITestResult; + +import org.rhq.cassandra.schema.SchemaManager; +import org.rhq.core.util.stream.StreamUtil; + +/** + * @author John Sanda + */ +public class CCMTestNGListener implements IInvokedMethodListener { + + private final Log log = LogFactory.getLog(CCMTestNGListener.class); + + @Override + public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult testResult) { + Method method = invokedMethod.getTestMethod().getConstructorOrMethod().getMethod(); + if (method.isAnnotationPresent(DeployCluster.class)) { + try { + deployCluster(method.getAnnotation(DeployCluster.class)); + } catch (CassandraException e) { + log.warn("Failed to deploy cluster", e); + } + } + } + + @Override + public void afterInvocation(IInvokedMethod invokedMethod, ITestResult testResult) { + Method method = invokedMethod.getTestMethod().getConstructorOrMethod().getMethod(); + if (method.isAnnotationPresent(ShutdownCluster.class)) { + try { + Boolean skipShutdown = Boolean.valueOf( + System.getProperty("rhq.cassandra.cluster.skip-shutdown", "false")); + if (!skipShutdown) { + shutdownCluster(); + } + } catch (Exception e) { + log.warn("An error occurred while shutting down the cluster", e); + } + } + } + + private void deployCluster(DeployCluster annotation) throws CassandraException { + File basedir = new File("target"); + File clusterDir = new File(basedir, "cassandra"); + + int numNodes = annotation.numNodes(); + DeploymentOptions deploymentOptions = new DeploymentOptions(); + deploymentOptions.setClusterDir(clusterDir.getAbsolutePath()); + deploymentOptions.setNumNodes(numNodes); + deploymentOptions.setUsername(annotation.username()); + deploymentOptions.setPassword(annotation.password()); + try { + deploymentOptions.load(); + } catch (IOException e) { + throw new RuntimeException("Unable to load deployment options.", e); + } + + BootstrapDeployer deployer = new BootstrapDeployer(); + deployer.setDeploymentOptions(deploymentOptions); + + deployer.deploy(); + + ClusterInitService clusterInitService = new ClusterInitService(); + List<CassandraNode> cassandraHosts = getCassandraHosts(deployer.getCassandraHosts()); + + if (annotation.waitForClusterToStart()) { + clusterInitService.waitForClusterToStart(cassandraHosts); + } + + if (annotation.waitForSchemaAgreement()) { + // TODO do not hard code cluster name + // I am ok with hard coding the cluster name for now as it is only required + // by the Hector API, and it is to be determined whether or not we will continue + // using Hector. If we wind up directly using the underlying Thrift API, there + // is no cluster name argument. + // + // jsanda + clusterInitService.waitForSchemaAgreement("rhq", cassandraHosts); + } + + String[] hostNames = getHostNames(deployer.getCassandraHosts()); + SchemaManager schemaManager = new SchemaManager(annotation.username(), annotation.password(), hostNames); + if (!schemaManager.schemaExists()) { + schemaManager.createSchema(); + } + schemaManager.updateSchema(); + schemaManager.shutdown(); + } + + private void shutdownCluster() throws Exception { + File basedir = new File("target"); + File clusterDir = new File(basedir, "cassandra"); + killNode(new File(clusterDir, "node0")); + killNode(new File(clusterDir, "node1")); + } + + private void killNode(File nodeDir) throws Exception { + long pid = getPid(nodeDir); + CLibrary.kill((int) pid, 9); + } + + private long getPid(File nodeDir) throws IOException { + File binDir = new File(nodeDir, "bin"); + StringWriter writer = new StringWriter(); + StreamUtil.copy(new FileReader(new File(binDir, "cassandra.pid")), writer); + + return Long.parseLong(writer.getBuffer().toString()); + } + + private List<CassandraNode> getCassandraHosts(String hosts) { + List<CassandraNode> cassandraHosts = new ArrayList<CassandraNode>(); + + for (String s : hosts.split(",")) { + String[] params = s.split(":"); + cassandraHosts.add(new CassandraNode(params[0], Integer.parseInt(params[1]))); + + } + return cassandraHosts; + } + + private String[] getHostNames(String hosts) { + List<String> hostNames = new ArrayList<String>(); + for (String s : hosts.split(",")) { + String[] params = s.split(":"); + hostNames.add(params[0]); + } + return hostNames.toArray(new String[hostNames.size()]); + } +} diff --git a/modules/common/cassandra-common-itests/src/main/java/org/rhq/cassandra/CassandraClusterManager.java b/modules/common/cassandra-common-itests/src/main/java/org/rhq/cassandra/CassandraClusterManager.java deleted file mode 100644 index 8d5099c..0000000 --- a/modules/common/cassandra-common-itests/src/main/java/org/rhq/cassandra/CassandraClusterManager.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * - * * RHQ Management Platform - * * Copyright (C) 2005-2012 Red Hat, Inc. - * * All rights reserved. - * * - * * This program is free software; you can redistribute it and/or modify - * * it under the terms of the GNU General Public License, version 2, as - * * published by the Free Software Foundation, and/or the GNU Lesser - * * General Public License, version 2.1, also as published by the Free - * * Software Foundation. - * * - * * This program is distributed in the hope that it will be useful, - * * but WITHOUT ANY WARRANTY; without even the implied warranty of - * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * * GNU General Public License and the GNU Lesser General Public License - * * for more details. - * * - * * You should have received a copy of the GNU General Public License - * * and the GNU Lesser General Public License along with this program; - * * if not, write to the Free Software Foundation, Inc., - * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -package org.rhq.cassandra; - -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.testng.IInvokedMethod; -import org.testng.IInvokedMethodListener; -import org.testng.ITestResult; - -import org.rhq.cassandra.schema.SchemaManager; -import org.rhq.core.util.stream.StreamUtil; - -/** - * @author John Sanda - */ -public class CassandraClusterManager implements IInvokedMethodListener { - - private final Log log = LogFactory.getLog(CassandraClusterManager.class); - - @Override - public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult testResult) { - Method method = invokedMethod.getTestMethod().getConstructorOrMethod().getMethod(); - if (method.isAnnotationPresent(DeployCluster.class)) { - try { - deployCluster(method.getAnnotation(DeployCluster.class)); - } catch (CassandraException e) { - log.warn("Failed to deploy cluster", e); - } - } - } - - @Override - public void afterInvocation(IInvokedMethod invokedMethod, ITestResult testResult) { - Method method = invokedMethod.getTestMethod().getConstructorOrMethod().getMethod(); - if (method.isAnnotationPresent(ShutdownCluster.class)) { - try { - Boolean skipShutdown = Boolean.valueOf( - System.getProperty("rhq.cassandra.cluster.skip-shutdown", "false")); - if (!skipShutdown) { - shutdownCluster(); - } - } catch (Exception e) { - log.warn("An error occurred while shutting down the cluster", e); - } - } - } - - private void deployCluster(DeployCluster annotation) throws CassandraException { - File basedir = new File("target"); - File clusterDir = new File(basedir, "cassandra"); - - int numNodes = annotation.numNodes(); - DeploymentOptions deploymentOptions = new DeploymentOptions(); - deploymentOptions.setClusterDir(clusterDir.getAbsolutePath()); - deploymentOptions.setNumNodes(numNodes); - deploymentOptions.setUsername(annotation.username()); - deploymentOptions.setPassword(annotation.password()); - try { - deploymentOptions.load(); - } catch (IOException e) { - throw new RuntimeException("Unable to load deployment options.", e); - } - - BootstrapDeployer deployer = new BootstrapDeployer(); - deployer.setDeploymentOptions(deploymentOptions); - - deployer.deploy(); - - ClusterInitService clusterInitService = new ClusterInitService(); - List<CassandraNode> cassandraHosts = getCassandraHosts(deployer.getCassandraHosts()); - - if (annotation.waitForClusterToStart()) { - clusterInitService.waitForClusterToStart(cassandraHosts); - } - - if (annotation.waitForSchemaAgreement()) { - // TODO do not hard code cluster name - // I am ok with hard coding the cluster name for now as it is only required - // by the Hector API, and it is to be determined whether or not we will continue - // using Hector. If we wind up directly using the underlying Thrift API, there - // is no cluster name argument. - // - // jsanda - clusterInitService.waitForSchemaAgreement("rhq", cassandraHosts); - } - - String[] hostNames = getHostNames(deployer.getCassandraHosts()); - SchemaManager schemaManager = new SchemaManager(annotation.username(), annotation.password(), hostNames); - if (!schemaManager.schemaExists()) { - schemaManager.createSchema(); - } - schemaManager.updateSchema(); - schemaManager.shutdown(); - } - - private void shutdownCluster() throws Exception { - File basedir = new File("target"); - File clusterDir = new File(basedir, "cassandra"); - killNode(new File(clusterDir, "node0")); - killNode(new File(clusterDir, "node1")); - } - - private void killNode(File nodeDir) throws Exception { - long pid = getPid(nodeDir); - CLibrary.kill((int) pid, 9); - } - - private long getPid(File nodeDir) throws IOException { - File binDir = new File(nodeDir, "bin"); - StringWriter writer = new StringWriter(); - StreamUtil.copy(new FileReader(new File(binDir, "cassandra.pid")), writer); - - return Long.parseLong(writer.getBuffer().toString()); - } - - private List<CassandraNode> getCassandraHosts(String hosts) { - List<CassandraNode> cassandraHosts = new ArrayList<CassandraNode>(); - - for (String s : hosts.split(",")) { - String[] params = s.split(":"); - cassandraHosts.add(new CassandraNode(params[0], Integer.parseInt(params[1]))); - - } - return cassandraHosts; - } - - private String[] getHostNames(String hosts) { - List<String> hostNames = new ArrayList<String>(); - for (String s : hosts.split(",")) { - String[] params = s.split(":"); - hostNames.add(params[0]); - } - return hostNames.toArray(new String[hostNames.size()]); - } -} diff --git a/modules/common/cassandra-common-itests/src/test/java/org/rhq/cassandra/common/BootstrapDeployerTest.java b/modules/common/cassandra-common-itests/src/test/java/org/rhq/cassandra/common/BootstrapDeployerTest.java index d9b1658..8cef4e7 100644 --- a/modules/common/cassandra-common-itests/src/test/java/org/rhq/cassandra/common/BootstrapDeployerTest.java +++ b/modules/common/cassandra-common-itests/src/test/java/org/rhq/cassandra/common/BootstrapDeployerTest.java @@ -28,14 +28,14 @@ package org.rhq.cassandra.common; import org.testng.annotations.Listeners; import org.testng.annotations.Test;
-import org.rhq.cassandra.CassandraClusterManager; +import org.rhq.cassandra.CCMTestNGListener; import org.rhq.cassandra.CassandraException; import org.rhq.cassandra.ShutdownCluster;
/** * @author John Sanda */ -@Listeners({CassandraClusterManager.class}) +@Listeners({CCMTestNGListener.class}) public class BootstrapDeployerTest {
@Test diff --git a/modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/CassandraIntegrationTest.java b/modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/CassandraIntegrationTest.java index 92a1607..b3850c6 100644 --- a/modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/CassandraIntegrationTest.java +++ b/modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/CassandraIntegrationTest.java @@ -34,14 +34,14 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Listeners;
-import org.rhq.cassandra.CassandraClusterManager; +import org.rhq.cassandra.CCMTestNGListener; import org.rhq.cassandra.DeployCluster; import org.rhq.cassandra.ShutdownCluster;
/** * @author John Sanda */ -@Listeners({CassandraClusterManager.class}) +@Listeners({CCMTestNGListener.class}) public class CassandraIntegrationTest {
protected Session session;
commit 4e667becd157b757782637ca40626f8ab5ac8618 Author: John Sanda jsanda@redhat.com Date: Thu Dec 20 10:52:44 2012 -0500
adding support for updating rhq-server.properties when rhq-ccm runs
This commit adds logic to update rhq-server.properties with rhq.cassandra.cluster.seeds which is needed by the installer in order to install the schema.
diff --git a/modules/common/cassandra-common/src/main/java/org/rhq/cassandra/BootstrapDeployer.java b/modules/common/cassandra-common/src/main/java/org/rhq/cassandra/BootstrapDeployer.java index 36e19af..0bda778 100644 --- a/modules/common/cassandra-common/src/main/java/org/rhq/cassandra/BootstrapDeployer.java +++ b/modules/common/cassandra-common/src/main/java/org/rhq/cassandra/BootstrapDeployer.java @@ -34,11 +34,15 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; +import java.util.ArrayList; import java.util.Date; import java.util.HashSet; +import java.util.List; import java.util.Properties; import java.util.Set;
+import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransportException;
@@ -49,6 +53,8 @@ import org.rhq.core.system.ProcessExecution; import org.rhq.core.system.ProcessExecutionResults; import org.rhq.core.system.SystemInfo; import org.rhq.core.system.SystemInfoFactory; +import org.rhq.core.util.PropertiesFileUpdate; +import org.rhq.core.util.StringUtil; import org.rhq.core.util.ZipUtil; import org.rhq.core.util.file.FileUtil; import org.rhq.core.util.stream.StreamUtil; @@ -58,6 +64,8 @@ import org.rhq.core.util.stream.StreamUtil; */ public class BootstrapDeployer {
+ private final Log log = LogFactory.getLog(BootstrapDeployer.class); + private DeploymentOptions deploymentOptions;
public void setDeploymentOptions(DeploymentOptions deploymentOptions) { @@ -130,10 +138,9 @@ public class BootstrapDeployer {
doLocalDeploy(props, bundleDir); startNode(nodeBasedir); -// if (i == 0) { -// waitForNodeToStart(10, address); -// updateSchema(nodeBasedir, address, 9160); -// } + if (i == 0) { + waitForNodeToStart(10, address); + } } FileUtil.writeFile(new ByteArrayInputStream(new byte[] {0}), installedMarker); } catch (IOException e) { @@ -150,6 +157,7 @@ public class BootstrapDeployer { }
public static void main(String[] args) { + long start = System.currentTimeMillis(); BootstrapDeployer deployer = new BootstrapDeployer();
DeploymentOptions deploymentOptions = new DeploymentOptions(); @@ -162,9 +170,41 @@ public class BootstrapDeployer { deployer.setDeploymentOptions(deploymentOptions); try { deployer.deploy(); + PropertiesFileUpdate serverPropertiesUpdater = getServerProperties(); + + String[] hostNames = getHostNames(deployer.getCassandraHosts()); + serverPropertiesUpdater.update("rhq.cassandra.cluster.seeds", StringUtil.arrayToString(hostNames)); + + long end = System.currentTimeMillis(); + deployer.log.info("Finished installing embedded cluster in " + (end - start) + " ms"); } catch (CassandraException e) { throw new RuntimeException("A deployment error occurred.", e); + } catch (IOException e) { + throw new RuntimeException("An error occurred while trying to update RHQ server properties", e); + } + } + + private static PropertiesFileUpdate getServerProperties() { + String sysprop = System.getProperty("rhq.server.properties-file"); + if (sysprop == null) { + throw new RuntimeException("The required system property [rhq.server.properties] is not defined."); + } + + File file = new File(sysprop); + if (!(file.exists() && file.isFile())) { + throw new RuntimeException("System property [" + sysprop + "] points to in invalid file."); + } + + return new PropertiesFileUpdate(file.getAbsolutePath()); + } + + private static String[] getHostNames(String hosts) { + List<String> hostNames = new ArrayList<String>(); + for (String s : hosts.split(",")) { + String[] params = s.split(":"); + hostNames.add(params[0]); } + return hostNames.toArray(new String[hostNames.size()]); }
private boolean isClusterInstalled() { diff --git a/modules/common/cassandra-common/src/main/resources/logging.properties b/modules/common/cassandra-common/src/main/resources/logging.properties new file mode 100644 index 0000000..3a4f2b6 --- /dev/null +++ b/modules/common/cassandra-common/src/main/resources/logging.properties @@ -0,0 +1,27 @@ +# Additional logger names to configure (root logger is always configured) +loggers=org.rhq + +# Root logger level +logger.level=${rhq.ccm.loglevel:DEBUG} +# Root logger handlers +logger.handlers=FILE,CONSOLE + +# Console handler configuration +handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandler +handler.CONSOLE.properties=autoFlush +handler.CONSOLE.level=${rhq.ccm.loglevel:DEBUG} +handler.CONSOLE.autoFlush=true +handler.CONSOLE.formatter=PATTERN + +# File handler configuration +handler.FILE=org.jboss.logmanager.handlers.FileHandler +handler.FILE.level=${rhq.ccm.loglevel:DEBUG} +handler.FILE.properties=autoFlush,fileName +handler.FILE.autoFlush=true +handler.FILE.fileName=${rhq.ccm.logdir:.}/rhq-ccm.log +handler.FILE.formatter=PATTERN + +# Formatter pattern configuration +formatter.PATTERN=org.jboss.logmanager.formatters.PatternFormatter +formatter.PATTERN.properties=pattern +formatter.PATTERN.pattern=%d{HH:mm:ss,SSS} %-5p [%c] %s%E%n diff --git a/modules/common/cassandra-common/src/main/resources/module/main/module.xml b/modules/common/cassandra-common/src/main/resources/module/main/module.xml index f06ab14..2b842ed 100644 --- a/modules/common/cassandra-common/src/main/resources/module/main/module.xml +++ b/modules/common/cassandra-common/src/main/resources/module/main/module.xml @@ -29,5 +29,7 @@ <module name="org.apache.commons.logging"/> <module name="org.apache.log4j"/> <module name="javax.api"/> + <module name="org.jboss.logmanager" services="import"/> + <module name="org.jboss.logging"/> </dependencies> </module> \ No newline at end of file
rhq-commits@lists.fedorahosted.org