Simplify TestCluster

TestCluster now doesn't use any reference counting anymore and
testcluster names are based on creation time to prevent confilcts if
builds hang.
This commit is contained in:
Simon Willnauer 2013-06-10 12:04:32 +02:00
parent 11d08ac436
commit b9feaa9999
3 changed files with 18 additions and 56 deletions

View File

@ -124,9 +124,8 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase {
@AfterClass @AfterClass
protected static void afterClass() { protected static void afterClass() {
TestCluster toRelease = cluster;
cluster = null; cluster = null;
ClusterManager.releaseCluster(toRelease); ClusterManager.releaseCluster();
} }
public static Client client() { public static Client client() {

View File

@ -18,41 +18,21 @@
*/ */
package org.elasticsearch.test.integration; package org.elasticsearch.test.integration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ClusterManager { public class ClusterManager {
private static final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
private static TestCluster cluster; private static TestCluster cluster;
private static int generation = 0;
public synchronized static TestCluster accquireCluster() { public synchronized static TestCluster accquireCluster() {
if (cluster == null) { if (cluster == null) {
cluster = new TestCluster(generation++); cluster = new TestCluster();
} }
TestCluster c = cluster; cluster.reset();
if (!c.tryAccquire()) { return cluster;
c = new TestCluster(generation++);
boolean tryAccquire = c.tryAccquire();
assert tryAccquire;
cluster = c;
}
c.reset();
return c;
} }
public static synchronized void releaseCluster(final TestCluster toRelease) { public static synchronized void releaseCluster() {
toRelease.decrementReference(); // doNothing
// TODO find a better way
// service.schedule(new Runnable() {
// @Override
// public void run() {
// toRelease.close();
// }
// }, 3, TimeUnit.MINUTES);
} }
} }

View File

@ -35,7 +35,6 @@ import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
@ -69,15 +68,15 @@ public class TestCluster {
* "action.auto_create_index" * "action.auto_create_index"
* "node.local" * "node.local"
*/ */
protected final ESLogger logger = Loggers.getLogger(getClass()); protected final ESLogger logger = Loggers.getLogger(getClass());
private Map<String, NodeAndClient> nodes = newHashMap(); private Map<String, NodeAndClient> nodes = newHashMap();
private final AtomicInteger refCount = new AtomicInteger(1);
private final String clusterName; private final String clusterName;
private final AtomicBoolean open = new AtomicBoolean(true);
private final Settings defaultSettings; private final Settings defaultSettings;
@ -85,8 +84,8 @@ public class TestCluster {
private NodeAndClient clientNode; private NodeAndClient clientNode;
public TestCluster(int generation) { public TestCluster() {
this("simple-test-cluster-" + NetworkUtils.getLocalAddress().getHostName() + "_gen_" + generation, ImmutableSettings.settingsBuilder().build()); this("simple-test-cluster-" + NetworkUtils.getLocalAddress().getHostName() + "_" + System.currentTimeMillis(), ImmutableSettings.settingsBuilder().build());
} }
private TestCluster(String clusterName, Settings defaultSettings) { private TestCluster(String clusterName, Settings defaultSettings) {
@ -103,25 +102,11 @@ public class TestCluster {
this.defaultSettings = ImmutableSettings.settingsBuilder().put(defaultSettings).put("cluster.name", clusterName).build(); this.defaultSettings = ImmutableSettings.settingsBuilder().put(defaultSettings).put("cluster.name", clusterName).build();
} }
boolean tryAccquire() {
int refs = this.refCount.get();
while (refs > 0) {
if (this.refCount.compareAndSet(refs, refs + 1)) {
return true;
}
}
return false;
}
private void ensureOpen() { private void ensureOpen() {
if (this.refCount.get() == 0) { if (!open.get()) {
throw new RuntimeException("Cluster is already closed"); throw new RuntimeException("Cluster is already closed");
} }
assert this.refCount.get() >= 0;
}
int decrementReference() {
return this.refCount.decrementAndGet();
} }
public Node getOneNode() { public Node getOneNode() {
@ -201,15 +186,13 @@ public class TestCluster {
public void close() { public void close() {
ensureOpen(); ensureOpen();
while (this.refCount.get() == 1) { if (this.open.compareAndSet(true, false)) {
if (this.refCount.compareAndSet(1, 0)) { IOUtils.closeWhileHandlingException(nodes.values());
IOUtils.closeWhileHandlingException(nodes.values()); nodes.clear();
nodes.clear(); if (clientNode != null) {
IOUtils.closeWhileHandlingException(clientNode);
} }
} }
if (clientNode != null) {
IOUtils.closeWhileHandlingException(clientNode);
}
} }
public ImmutableSet<ClusterBlock> waitForNoBlocks(TimeValue timeout, Node node) throws InterruptedException { public ImmutableSet<ClusterBlock> waitForNoBlocks(TimeValue timeout, Node node) throws InterruptedException {