You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2011/01/29 21:20:31 UTC
svn commit: r1065098 - in
/cassandra/trunk/test/distributed/org/apache/cassandra:
CassandraServiceController.java MutationTest.java TestBase.java
Author: jbellis
Date: Sat Jan 29 20:20:31 2011
New Revision: 1065098
URL: http://svn.apache.org/viewvc?rev=1065098&view=rev
Log:
refactor TestBase
patcy by stuhood; reviewed by jbellis for CASSANDRA-2005
Modified:
cassandra/trunk/test/distributed/org/apache/cassandra/CassandraServiceController.java
cassandra/trunk/test/distributed/org/apache/cassandra/MutationTest.java
cassandra/trunk/test/distributed/org/apache/cassandra/TestBase.java
Modified: cassandra/trunk/test/distributed/org/apache/cassandra/CassandraServiceController.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/distributed/org/apache/cassandra/CassandraServiceController.java?rev=1065098&r1=1065097&r2=1065098&view=diff
==============================================================================
--- cassandra/trunk/test/distributed/org/apache/cassandra/CassandraServiceController.java (original)
+++ cassandra/trunk/test/distributed/org/apache/cassandra/CassandraServiceController.java Sat Jan 29 20:20:31 2011
@@ -197,15 +197,15 @@ public class CassandraServiceController
try
{
LOG.info("Shutting down cluster...");
- if (service != null)
- service.destroyCluster(clusterSpec);
if (tarball != null)
BlobUtils.deleteBlob(config, clusterSpec, tarball);
+ if (service != null)
+ service.destroyCluster(clusterSpec);
running = false;
}
catch (Exception e)
{
- LOG.error(String.format("Error shutting down cluster: %s", e));
+ LOG.error("Error shutting down cluster.", e);
}
}
Modified: cassandra/trunk/test/distributed/org/apache/cassandra/MutationTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/distributed/org/apache/cassandra/MutationTest.java?rev=1065098&r1=1065097&r2=1065098&view=diff
==============================================================================
--- cassandra/trunk/test/distributed/org/apache/cassandra/MutationTest.java (original)
+++ cassandra/trunk/test/distributed/org/apache/cassandra/MutationTest.java Sat Jan 29 20:20:31 2011
@@ -18,12 +18,6 @@
package org.apache.cassandra;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.IOException;
-import java.io.Writer;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.*;
@@ -32,12 +26,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.cassandra.thrift.*;
-import org.apache.cassandra.tools.NodeProbe;
import org.apache.cassandra.utils.WrappedRunnable;
import org.apache.thrift.TException;
-import org.apache.cassandra.client.*;
-import org.apache.cassandra.dht.RandomPartitioner;
-import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.CassandraServiceController.Failure;
@@ -184,193 +174,4 @@ public class MutationTest extends TestBa
failure.resolve();
}
}
-
- protected void insert(Cassandra.Client client, ByteBuffer key, String cf, String name, String value, long timestamp, ConsistencyLevel cl)
- throws InvalidRequestException, UnavailableException, TimedOutException, TException
- {
- Column col = new Column(
- ByteBuffer.wrap(name.getBytes()),
- ByteBuffer.wrap(value.getBytes()),
- timestamp
- );
- client.insert(key, new ColumnParent(cf), col, cl);
- }
-
- protected Column getColumn(Cassandra.Client client, ByteBuffer key, String cf, String col, ConsistencyLevel cl)
- throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException
- {
- ColumnPath cpath = new ColumnPath(cf);
- cpath.setColumn(col.getBytes());
- return client.get(key, cpath, cl).column;
- }
-
- protected class Get extends RetryingAction
- {
- public Get(Cassandra.Client client, String cf, ByteBuffer key)
- {
- super(client, cf, key);
- }
-
- public void tryPerformAction(ConsistencyLevel cl) throws Exception
- {
- assertColumnEqual(name, value, timestamp, getColumn(client, key, cf, name, cl));
- }
- }
-
- protected class Insert extends RetryingAction
- {
- public Insert(Cassandra.Client client, String cf, ByteBuffer key)
- {
- super(client, cf, key);
- }
-
- public void tryPerformAction(ConsistencyLevel cl) throws Exception
- {
- insert(client, key, cf, name, value, timestamp, cl);
- }
- }
-
- /** Performs an action repeatedly until timeout, success or failure. */
- protected abstract class RetryingAction
- {
- protected Cassandra.Client client;
- protected String cf;
- protected ByteBuffer key;
- protected String name;
- protected String value;
- protected long timestamp;
-
- private Set<Class<Exception>> expected = new HashSet<Class<Exception>>();
- private long timeout = StorageService.RING_DELAY;
-
- public RetryingAction(Cassandra.Client client, String cf, ByteBuffer key)
- {
- this.client = client;
- this.cf = cf;
- this.key = key;
- this.timestamp = 0;
- }
-
- public RetryingAction name(String name)
- {
- this.name = name; return this;
- }
-
- /** The value to expect for the return column, or null to expect the column to be missing. */
- public RetryingAction value(String value)
- {
- this.value = value; return this;
- }
-
- /** The total time to allow before failing. */
- public RetryingAction timeout(long timeout)
- {
- this.timeout = timeout; return this;
- }
-
- /** The expected timestamp of the returned column. */
- public RetryingAction timestamp(long timestamp)
- {
- this.timestamp = timestamp; return this;
- }
-
- /** The exception classes that indicate success. */
- public RetryingAction expecting(Class... tempExceptions)
- {
- this.expected.clear();
- for (Class exclass : tempExceptions)
- expected.add((Class<Exception>)exclass);
- return this;
- }
-
- public void perform(ConsistencyLevel cl) throws AssertionError
- {
- long deadline = System.currentTimeMillis() + timeout;
- int attempts = 0;
- String template = "%s for " + this + " after %d attempt(s) with %d ms to spare.";
- Exception e = null;
- while(deadline > System.currentTimeMillis())
- {
- try
- {
- attempts++;
- tryPerformAction(cl);
- logger.info(String.format(template, "Succeeded", attempts, deadline - System.currentTimeMillis()));
- return;
- }
- catch (Exception ex)
- {
- e = ex;
- if (!expected.contains(ex.getClass()))
- continue;
- logger.info(String.format(template, "Caught expected exception: " + e, attempts, deadline - System.currentTimeMillis()));
- return;
- }
- }
- String err = String.format(template, "Caught unexpected: " + e, attempts, deadline - System.currentTimeMillis());
- logger.error(err);
- throw new AssertionError(err);
- }
-
- public String toString()
- {
- return this.getClass() + "(" + key + "," + name + ")";
- }
-
- protected abstract void tryPerformAction(ConsistencyLevel cl) throws Exception;
- }
-
- protected List<ColumnOrSuperColumn> get_slice(Cassandra.Client client, ByteBuffer key, String cf, ConsistencyLevel cl)
- throws InvalidRequestException, UnavailableException, TimedOutException, TException
- {
- SlicePredicate sp = new SlicePredicate();
- sp.setSlice_range(
- new SliceRange(
- ByteBuffer.wrap(new byte[0]),
- ByteBuffer.wrap(new byte[0]),
- false,
- 1000
- )
- );
- return client.get_slice(key, new ColumnParent(cf), sp, cl);
- }
-
- protected void assertColumnEqual(String name, String value, long timestamp, Column col)
- {
- assertEquals(ByteBuffer.wrap(name.getBytes()), col.name);
- assertEquals(ByteBuffer.wrap(value.getBytes()), col.value);
- assertEquals(timestamp, col.timestamp);
- }
-
- protected List<InetAddress> endpointsForKey(InetAddress seed, ByteBuffer key, String keyspace)
- throws IOException
- {
- RingCache ring = new RingCache(keyspace, new RandomPartitioner(), seed.getHostAddress(), 9160);
- List<InetAddress> privateendpoints = ring.getEndpoint(key);
- List<InetAddress> endpoints = new ArrayList<InetAddress>();
- for (InetAddress endpoint : privateendpoints)
- {
- endpoints.add(controller.getPublicHost(endpoint));
- }
- return endpoints;
- }
-
- protected InetAddress nonEndpointForKey(InetAddress seed, ByteBuffer key, String keyspace)
- throws IOException
- {
- List<InetAddress> endpoints = endpointsForKey(seed, key, keyspace);
- for (InetAddress host : controller.getHosts())
- {
- if (!endpoints.contains(host))
- {
- return host;
- }
- }
- return null;
- }
-
- protected ByteBuffer newKey()
- {
- return ByteBuffer.wrap(String.format("test.key.%d", System.currentTimeMillis()).getBytes());
- }
}
Modified: cassandra/trunk/test/distributed/org/apache/cassandra/TestBase.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/distributed/org/apache/cassandra/TestBase.java?rev=1065098&r1=1065097&r2=1065098&view=diff
==============================================================================
--- cassandra/trunk/test/distributed/org/apache/cassandra/TestBase.java (original)
+++ cassandra/trunk/test/distributed/org/apache/cassandra/TestBase.java Sat Jan 29 20:20:31 2011
@@ -18,17 +18,19 @@
package org.apache.cassandra;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
+import java.io.*;
import java.net.InetAddress;
-import java.util.LinkedList;
-import java.util.List;
+import java.nio.ByteBuffer;
+import java.util.*;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.apache.thrift.TException;
+import org.apache.cassandra.client.*;
+import org.apache.cassandra.dht.RandomPartitioner;
+import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.thrift.*;
import org.junit.AfterClass;
@@ -40,75 +42,290 @@ import static junit.framework.Assert.ass
public abstract class TestBase
{
+ private static final Logger logger = LoggerFactory.getLogger(TestBase.class);
+
protected static CassandraServiceController controller =
CassandraServiceController.getInstance();
- protected static void addKeyspace(String name, int rf) throws Exception
+ static class KeyspaceCreation
{
- List<CfDef> cfDefList = new LinkedList<CfDef>();
+ private String name;
+ private int rf;
+ private CfDef cfdef;
+ public KeyspaceCreation(String name)
+ {
+ this.name = name;
+ cfdef = new CfDef(name, "Standard1");
+ cfdef.setComparator_type("BytesType");
+ cfdef.setKey_cache_size(10000);
+ cfdef.setRow_cache_size(1000);
+ cfdef.setRow_cache_save_period_in_seconds(0);
+ cfdef.setKey_cache_save_period_in_seconds(3600);
+ cfdef.setMemtable_flush_after_mins(59);
+ cfdef.setMemtable_throughput_in_mb(255);
+ cfdef.setMemtable_operations_in_millions(0.29);
+ }
- CfDef standard1 = new CfDef(name, "Standard1");
- standard1.setComparator_type("BytesType");
- standard1.setKey_cache_size(10000);
- standard1.setRow_cache_size(1000);
- standard1.setRow_cache_save_period_in_seconds(0);
- standard1.setKey_cache_save_period_in_seconds(3600);
- standard1.setMemtable_flush_after_mins(59);
- standard1.setMemtable_throughput_in_mb(255);
- standard1.setMemtable_operations_in_millions(0.29);
- cfDefList.add(standard1);
-
- List<InetAddress> hosts = controller.getHosts();
- Cassandra.Client client = controller.createClient(hosts.get(0));
-
- client.system_add_keyspace(
- new KsDef(
- name,
- "org.apache.cassandra.locator.SimpleStrategy",
- rf,
- cfDefList));
+ public KeyspaceCreation validator(String validator)
+ {
+ cfdef.setDefault_validation_class(validator);
+ return this;
+ }
- // poll, until KS added
- for (InetAddress host : hosts)
+ public KeyspaceCreation rf(int rf)
{
- try
+ this.rf = rf;
+ return this;
+ }
+
+ public void create() throws Exception
+ {
+ List<InetAddress> hosts = controller.getHosts();
+ Cassandra.Client client = controller.createClient(hosts.get(0));
+
+ client.system_add_keyspace(
+ new KsDef(
+ name,
+ "org.apache.cassandra.locator.SimpleStrategy",
+ rf,
+ Arrays.asList(cfdef)));
+
+ // poll, until KS added
+ for (InetAddress host : hosts)
{
- client = controller.createClient(host);
- poll:
- while (true)
+ try
{
- List<KsDef> ksDefList = client.describe_keyspaces();
- for (KsDef ks : ksDefList)
+ client = controller.createClient(host);
+ poll:
+ while (true)
{
- if (ks.name.equals(name))
+ List<KsDef> ksDefList = client.describe_keyspaces();
+ for (KsDef ks : ksDefList)
+ {
+ if (ks.name.equals(name))
+ break poll;
+ }
+
+ try
+ {
+ Thread.sleep(1000);
+ }
+ catch (InterruptedException e)
+ {
break poll;
- }
-
- try
- {
- Thread.sleep(1000);
- }
- catch (InterruptedException e)
- {
- break poll;
+ }
}
}
- }
- catch (TException te)
- {
- continue;
+ catch (TException te)
+ {
+ continue;
+ }
}
}
}
+ protected static KeyspaceCreation keyspace(String name)
+ {
+ return new KeyspaceCreation(name);
+ }
+
+ protected static void addKeyspace(String name, int rf) throws Exception
+ {
+ keyspace(name).rf(rf).create();
+ }
+
@BeforeClass
public static void setUp() throws Exception
{
controller.ensureClusterRunning();
}
- protected static String createTemporaryKey()
+ protected ByteBuffer newKey()
{
- return String.format("test.key.%d", System.currentTimeMillis());
+ return ByteBuffer.wrap(String.format("test.key.%d", System.currentTimeMillis()).getBytes());
+ }
+
+ protected void insert(Cassandra.Client client, ByteBuffer key, String cf, String name, String value, long timestamp, ConsistencyLevel cl)
+ throws InvalidRequestException, UnavailableException, TimedOutException, TException
+ {
+ Column col = new Column(
+ ByteBuffer.wrap(name.getBytes()),
+ ByteBuffer.wrap(value.getBytes()),
+ timestamp
+ );
+ client.insert(key, new ColumnParent(cf), col, cl);
+ }
+
+ protected Column getColumn(Cassandra.Client client, ByteBuffer key, String cf, String col, ConsistencyLevel cl)
+ throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException
+ {
+ ColumnPath cpath = new ColumnPath(cf);
+ cpath.setColumn(col.getBytes());
+ return client.get(key, cpath, cl).column;
+ }
+
+ protected class Get extends RetryingAction<String>
+ {
+ public Get(Cassandra.Client client, String cf, ByteBuffer key)
+ {
+ super(client, cf, key);
+ }
+
+ public void tryPerformAction(ConsistencyLevel cl) throws Exception
+ {
+ assertColumnEqual(name, value, timestamp, getColumn(client, key, cf, name, cl));
+ }
+ }
+
+ protected class Insert extends RetryingAction<String>
+ {
+ public Insert(Cassandra.Client client, String cf, ByteBuffer key)
+ {
+ super(client, cf, key);
+ }
+
+ public void tryPerformAction(ConsistencyLevel cl) throws Exception
+ {
+ insert(client, key, cf, name, value, timestamp, cl);
+ }
+ }
+
+ /** Performs an action repeatedly until timeout, success or failure. */
+ protected abstract class RetryingAction<T>
+ {
+ protected Cassandra.Client client;
+ protected String cf;
+ protected ByteBuffer key;
+ protected String name;
+ protected T value;
+ protected long timestamp;
+
+ private Set<Class<Exception>> expected = new HashSet<Class<Exception>>();
+ private long timeout = StorageService.RING_DELAY;
+
+ public RetryingAction(Cassandra.Client client, String cf, ByteBuffer key)
+ {
+ this.client = client;
+ this.cf = cf;
+ this.key = key;
+ this.timestamp = 0;
+ }
+
+ public RetryingAction name(String name)
+ {
+ this.name = name; return this;
+ }
+
+ /** A parameterized value for the action. */
+ public RetryingAction value(T value)
+ {
+ this.value = value; return this;
+ }
+
+ /** The total time to allow before failing. */
+ public RetryingAction timeout(long timeout)
+ {
+ this.timeout = timeout; return this;
+ }
+
+ /** The expected timestamp of the returned column. */
+ public RetryingAction timestamp(long timestamp)
+ {
+ this.timestamp = timestamp; return this;
+ }
+
+ /** The exception classes that indicate success. */
+ public RetryingAction expecting(Class... tempExceptions)
+ {
+ this.expected.clear();
+ for (Class exclass : tempExceptions)
+ expected.add((Class<Exception>)exclass);
+ return this;
+ }
+
+ public void perform(ConsistencyLevel cl) throws AssertionError
+ {
+ long deadline = System.currentTimeMillis() + timeout;
+ int attempts = 0;
+ String template = "%s for " + this + " after %d attempt(s) with %d ms to spare.";
+ Exception e = null;
+ while(deadline > System.currentTimeMillis())
+ {
+ try
+ {
+ attempts++;
+ tryPerformAction(cl);
+ logger.info(String.format(template, "Succeeded", attempts, deadline - System.currentTimeMillis()));
+ return;
+ }
+ catch (Exception ex)
+ {
+ e = ex;
+ if (!expected.contains(ex.getClass()))
+ continue;
+ logger.info(String.format(template, "Caught expected exception: " + e, attempts, deadline - System.currentTimeMillis()));
+ return;
+ }
+ }
+ String err = String.format(template, "Caught unexpected: " + e, attempts, deadline - System.currentTimeMillis());
+ logger.error(err, e);
+ throw new AssertionError(err);
+ }
+
+ public String toString()
+ {
+ return this.getClass().getSimpleName() + "(" + key + "," + name + ")";
+ }
+
+ protected abstract void tryPerformAction(ConsistencyLevel cl) throws Exception;
+ }
+
+ protected List<ColumnOrSuperColumn> get_slice(Cassandra.Client client, ByteBuffer key, String cf, ConsistencyLevel cl)
+ throws InvalidRequestException, UnavailableException, TimedOutException, TException
+ {
+ SlicePredicate sp = new SlicePredicate();
+ sp.setSlice_range(
+ new SliceRange(
+ ByteBuffer.wrap(new byte[0]),
+ ByteBuffer.wrap(new byte[0]),
+ false,
+ 1000
+ )
+ );
+ return client.get_slice(key, new ColumnParent(cf), sp, cl);
+ }
+
+ protected void assertColumnEqual(String name, String value, long timestamp, Column col)
+ {
+ assertEquals(ByteBuffer.wrap(name.getBytes()), col.name);
+ assertEquals(ByteBuffer.wrap(value.getBytes()), col.value);
+ assertEquals(timestamp, col.timestamp);
+ }
+
+ protected List<InetAddress> endpointsForKey(InetAddress seed, ByteBuffer key, String keyspace)
+ throws IOException
+ {
+ RingCache ring = new RingCache(keyspace, new RandomPartitioner(), seed.getHostAddress(), 9160);
+ List<InetAddress> privateendpoints = ring.getEndpoint(key);
+ List<InetAddress> endpoints = new ArrayList<InetAddress>();
+ for (InetAddress endpoint : privateendpoints)
+ {
+ endpoints.add(controller.getPublicHost(endpoint));
+ }
+ return endpoints;
+ }
+
+ protected InetAddress nonEndpointForKey(InetAddress seed, ByteBuffer key, String keyspace)
+ throws IOException
+ {
+ List<InetAddress> endpoints = endpointsForKey(seed, key, keyspace);
+ for (InetAddress host : controller.getHosts())
+ {
+ if (!endpoints.contains(host))
+ {
+ return host;
+ }
+ }
+ return null;
}
}