Author: gdusbabek
Date: Fri Feb 12 18:25:52 2010
New Revision: 909549
URL: http://svn.apache.org/viewvc?rev=909549&view=rev
Log:
clean up data directories + unit test for CassandraService. Patch by Ran
Tavory, reviewed by Gary Dusbabek. CASSANDRA-782
Added:
incubator/cassandra/trunk/contrib/javautils/
incubator/cassandra/trunk/contrib/javautils/README
incubator/cassandra/trunk/contrib/javautils/build.xml
incubator/cassandra/trunk/contrib/javautils/src/
incubator/cassandra/trunk/contrib/javautils/src/main/
incubator/cassandra/trunk/contrib/javautils/src/main/java/
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java
incubator/cassandra/trunk/contrib/javautils/src/test/
incubator/cassandra/trunk/contrib/javautils/src/test/java/
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java
Added: incubator/cassandra/trunk/contrib/javautils/README
URL:
http://svn.apache.org/viewvc/incubator/cassandra/trunk/contrib/javautils/README?rev=909549&view=auto
==============================================================================
--- incubator/cassandra/trunk/contrib/javautils/README (added)
+++ incubator/cassandra/trunk/contrib/javautils/README Fri Feb 12 18:25:52 2010
@@ -0,0 +1,8 @@
+Java utilities for cassandra clients
+
+BUILD and test:
+
+1. ant the top-level cassandra project
+2. cd here
+3. ant test
+
Added: incubator/cassandra/trunk/contrib/javautils/build.xml
URL:
http://svn.apache.org/viewvc/incubator/cassandra/trunk/contrib/javautils/build.xml?rev=909549&view=auto
==============================================================================
--- incubator/cassandra/trunk/contrib/javautils/build.xml (added)
+++ incubator/cassandra/trunk/contrib/javautils/build.xml Fri Feb 12 18:25:52
2010
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+~ 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.
+-->
+<project basedir="." default="build" name="apache-cassandra-javautils">
+ <property name="cassandra.dir" value="../.." />
+ <property name="cassandra.lib" value="${cassandra.dir}/lib" />
+ <property name="cassandra.classes" value="${cassandra.dir}/build/classes" />
+ <property name="build.src" value="${basedir}/src/main/java" />
+ <property name="test.src" value="${basedir}/src/test/java" />
+ <property name="test.reports" value="${basedir}/reports" />
+ <property name="build.out" value="${basedir}/build" />
+ <property name="build.classes" value="${build.out}/classes" />
+ <property name="final.name" value="cassandra-javautils" />
+
+ <path id="classpath">
+ <path>
+ <fileset dir="${cassandra.lib}">
+ <include name="**/*.jar" />
+ </fileset>
+ <pathelement location="${cassandra.classes}" />
+ </path>
+ </path>
+ <path id="test.classpath">
+ <path refid="classpath"/>
+ <path>
+ <pathelement location="${build.classes}" />
+ </path>
+ </path>
+
+ <target name="init">
+ <mkdir dir="${build.classes}" />
+ </target>
+
+ <target depends="init" name="build">
+ <javac destdir="${build.classes}">
+ <src path="${build.src}" />
+ <classpath refid="classpath"/>
+ </javac>
+ </target>
+
+ <target name="jar" depends="build">
+ <mkdir dir="${build.classes}/META-INF" />
+ <jar jarfile="${build.out}/${final.name}.jar" basedir="${build.classes}" />
+ </target>
+
+ <target name="build-tests" depends="build" description="Builds the test
files">
+ <javac debug="true" srcdir="${test.src}" destdir="${build.classes}">
+ <classpath refid="test.classpath"/>
+ </javac>
+ </target>
+
+ <target name="test" depends="build-tests">
+ <delete dir="${test.reports}" failonerror="no"/>
+ <mkdir dir="${test.reports}"/>
+ <junit printsummary="yes" dir="${basedir}" timeout="600000" fork="yes"
includeantruntime="yes">
+ <classpath refid="test.classpath"/>
+ <formatter type="plain"/>
+ <batchtest todir="${test.reports}">
+ <fileset dir="${test.src}">
+ <include name="**/*Test*.java"/>
+ </fileset>
+ </batchtest>
+ </junit>
+ </target>
+
+ <target name="clean">
+ <delete dir="${build.out}" />
+ <delete dir="${test.reports}" />
+ </target>
+</project>
+
Added:
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java
URL:
http://svn.apache.org/viewvc/incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java?rev=909549&view=auto
==============================================================================
---
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java
(added)
+++
incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java
Fri Feb 12 18:25:52 2010
@@ -0,0 +1,101 @@
+/*
+* 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.cassandra.contrib.utils.service;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.util.FileUtils;
+
+/**
+ * A cleanup utility that wipes the cassandra data directories.
+ *
+ * @author Ran Tavory ([email protected])
+ *
+ */
+public class CassandraServiceDataCleaner {
+
+ /**
+ * Creates all data dir if they don't exist and cleans them
+ * @throws IOException
+ */
+ public void prepare() throws IOException {
+ makeDirsIfNotExist();
+ cleanupDataDirectories();
+ }
+
+ /**
+ * Deletes all data from cassandra data directories, including the commit
log.
+ * @throws IOException in case of permissions error etc.
+ */
+ public void cleanupDataDirectories() throws IOException {
+ for (String s: getDataDirs()) {
+ cleanDir(s);
+ }
+ }
+ /**
+ * Creates the data diurectories, if they didn't exist.
+ * @throws IOException if directories cannot be created (permissions etc).
+ */
+ public void makeDirsIfNotExist() throws IOException {
+ for (String s: getDataDirs()) {
+ mkdir(s);
+ }
+ }
+
+ /**
+ * Collects all data dirs and returns a set of String paths on the file
system.
+ *
+ * @return
+ */
+ private Set<String> getDataDirs() {
+ Set<String> dirs = new HashSet<String>();
+ for (String s : DatabaseDescriptor.getAllDataFileLocations()) {
+ dirs.add(s);
+ }
+ dirs.add(DatabaseDescriptor.getLogFileLocation());
+ return dirs;
+ }
+ /**
+ * Creates a directory
+ *
+ * @param dir
+ * @throws IOException
+ */
+ private void mkdir(String dir) throws IOException {
+ FileUtils.createDirectory(dir);
+ }
+
+ /**
+ * Removes all directory content from file the system
+ *
+ * @param dir
+ * @throws IOException
+ */
+ private void cleanDir(String dir) throws IOException {
+ File dirFile = new File(dir);
+ if (dirFile.exists() && dirFile.isDirectory()) {
+ FileUtils.delete(dirFile.listFiles());
+ }
+ }
+}
Added:
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java
URL:
http://svn.apache.org/viewvc/incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java?rev=909549&view=auto
==============================================================================
---
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java
(added)
+++
incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java
Fri Feb 12 18:25:52 2010
@@ -0,0 +1,119 @@
+/*
+* 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.cassandra.contrib.utils.service;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.cassandra.service.EmbeddedCassandraService;
+import org.apache.cassandra.thrift.Cassandra;
+import org.apache.cassandra.thrift.ColumnOrSuperColumn;
+import org.apache.cassandra.thrift.ColumnPath;
+import org.apache.cassandra.thrift.ConsistencyLevel;
+import org.apache.cassandra.thrift.InvalidRequestException;
+import org.apache.cassandra.thrift.NotFoundException;
+import org.apache.cassandra.thrift.TimedOutException;
+import org.apache.cassandra.thrift.UnavailableException;
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Example how to use an embedded and a data cleaner.
+ *
+ * @author Ran Tavory ([email protected])
+ *
+ */
+public class CassandraServiceTest {
+
+ private static EmbeddedCassandraService cassandra;
+
+ /**
+ * Set embedded cassandra up and spawn it in a new thread.
+ *
+ * @throws TTransportException
+ * @throws IOException
+ * @throws InterruptedException
+ */
+ @BeforeClass
+ public static void setup() throws TTransportException, IOException,
+ InterruptedException {
+ // Tell cassandra where the configuration files are.
+ // Use the test configuration file.
+ System.setProperty("storage-config", "../../test/conf");
+
+ CassandraServiceDataCleaner cleaner = new
CassandraServiceDataCleaner();
+ cleaner.prepare();
+ cassandra = new EmbeddedCassandraService();
+ cassandra.init();
+ Thread t = new Thread(cassandra);
+ t.setDaemon(true);
+ t.start();
+ }
+
+
+ @Test
+ public void testInProcessCassandraServer()
+ throws UnsupportedEncodingException, InvalidRequestException,
+ UnavailableException, TimedOutException, TException,
+ NotFoundException {
+ Cassandra.Client client = getClient();
+
+ String key_user_id = "1";
+
+ long timestamp = System.currentTimeMillis();
+ ColumnPath cp = new ColumnPath("Standard1");
+ cp.setColumn("name".getBytes("utf-8"));
+
+ // insert
+ client.insert("Keyspace1", key_user_id, cp, "Ran".getBytes("UTF-8"),
+ timestamp, ConsistencyLevel.ONE);
+
+ // read
+ ColumnOrSuperColumn got = client.get("Keyspace1", key_user_id, cp,
+ ConsistencyLevel.ONE);
+
+ // assert
+ assertNotNull("Got a null ColumnOrSuperColumn", got);
+ assertEquals("Ran", new String(got.getColumn().getValue(), "utf-8"));
+ }
+
+ /**
+ * Gets a connection to the localhost client
+ *
+ * @return
+ * @throws TTransportException
+ */
+ private Cassandra.Client getClient() throws TTransportException {
+ TTransport tr = new TSocket("localhost", 9170);
+ TProtocol proto = new TBinaryProtocol(tr);
+ Cassandra.Client client = new Cassandra.Client(proto);
+ tr.open();
+ return client;
+ }
+}