use of com.emc.storageos.coordinator.common.impl.ZkConnection in project coprhd-controller by CoprHD.
the class DistributedDoubleBarrierTest method testBasic.
/**
* Test case port from Apache Curator
*/
@Test
public void testBasic() throws Exception {
final Timing timing = new Timing();
final List<Closeable> closeables = Lists.newArrayList();
ZkConnection zkConnection = createConnection(10 * 1000);
CuratorFramework client = zkConnection.curator();
try {
closeables.add(client);
client.start();
final CountDownLatch postEnterLatch = new CountDownLatch(QTY);
final CountDownLatch postLeaveLatch = new CountDownLatch(QTY);
final AtomicInteger count = new AtomicInteger(0);
final AtomicInteger max = new AtomicInteger(0);
List<Future<Void>> futures = Lists.newArrayList();
ExecutorService service = Executors.newCachedThreadPool();
for (int i = 0; i < QTY; ++i) {
Future<Void> future = service.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testBasic", QTY);
Assert.assertTrue(barrier.enter(timing.seconds(), TimeUnit.SECONDS));
synchronized (DistributedDoubleBarrierTest.this) {
int thisCount = count.incrementAndGet();
if (thisCount > max.get()) {
max.set(thisCount);
}
}
postEnterLatch.countDown();
Assert.assertTrue(timing.awaitLatch(postEnterLatch));
Assert.assertEquals(count.get(), QTY);
Assert.assertTrue(barrier.leave(10, TimeUnit.SECONDS));
count.decrementAndGet();
postLeaveLatch.countDown();
Assert.assertTrue(timing.awaitLatch(postLeaveLatch));
return null;
}
});
futures.add(future);
}
for (Future<Void> f : futures) {
f.get();
}
Assert.assertEquals(count.get(), 0);
Assert.assertEquals(max.get(), QTY);
} finally {
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of com.emc.storageos.coordinator.common.impl.ZkConnection in project coprhd-controller by CoprHD.
the class DistributedDoubleBarrierTest method testMultiClient.
/**
* Test case port from Apache Curator
*/
@Test
public void testMultiClient() throws Exception {
final Timing timing = new Timing();
final CountDownLatch postEnterLatch = new CountDownLatch(QTY);
final CountDownLatch postLeaveLatch = new CountDownLatch(QTY);
final AtomicInteger count = new AtomicInteger(0);
final AtomicInteger max = new AtomicInteger(0);
List<Future<Void>> futures = Lists.newArrayList();
ExecutorService service = Executors.newCachedThreadPool();
for (int i = 0; i < QTY; ++i) {
Future<Void> future = service.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
ZkConnection zkConnection = createConnection(60 * 1000);
CuratorFramework client = zkConnection.curator();
try {
zkConnection.connect();
DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testMultiClient", QTY);
log.info("Entering with timeout {}", timing.seconds());
Assert.assertTrue("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
log.info("Entered");
synchronized (DistributedDoubleBarrierTest.this) {
int thisCount = count.incrementAndGet();
if (thisCount > max.get()) {
max.set(thisCount);
}
}
postEnterLatch.countDown();
Assert.assertTrue("postEnterLatch", timing.awaitLatch(postEnterLatch));
Assert.assertEquals("entered count", count.get(), QTY);
log.info("Leaving timeout {}", timing.seconds());
Assert.assertTrue("Return value of leave()", barrier.leave(timing.seconds(), TimeUnit.SECONDS));
log.info("Left");
count.decrementAndGet();
postLeaveLatch.countDown();
Assert.assertTrue("postLeaveLatch", timing.awaitLatch(postLeaveLatch));
} finally {
CloseableUtils.closeQuietly(client);
}
return null;
}
});
futures.add(future);
}
for (Future<Void> f : futures) {
f.get();
}
Assert.assertEquals(count.get(), 0);
Assert.assertEquals(max.get(), QTY);
}
use of com.emc.storageos.coordinator.common.impl.ZkConnection in project coprhd-controller by CoprHD.
the class DistributedDoubleBarrierTest method testEnterTimeout.
/**
* Test return value of enter() in case of timeout
*
* If not all members enter the barrier as expected, it should return false. Subsequent enter() should return false until
* it meets the barrier criteria
*/
@Test
public void testEnterTimeout() throws Exception {
final Timing timing = new Timing(2, TimeUnit.SECONDS);
final AtomicInteger count = new AtomicInteger(0);
final ExecutorService service = Executors.newCachedThreadPool();
final List<Future<Void>> futures = Lists.newArrayList();
for (int i = 1; i < QTY - 1; ++i) {
Future<Void> worker = service.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
ZkConnection zkConnection = createConnection(60 * 1000);
CuratorFramework client = zkConnection.curator();
try {
zkConnection.connect();
DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testEnterTimeout", QTY);
log.info("Entering with timeout {} seconds", timing.seconds());
Assert.assertFalse("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
count.incrementAndGet();
log.info("Entering again with timeout {} seconds", timing.seconds());
Assert.assertFalse("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
count.decrementAndGet();
} finally {
CloseableUtils.closeQuietly(client);
}
return null;
}
});
futures.add(worker);
}
for (Future<Void> f : futures) {
f.get();
}
Assert.assertEquals("enter/leave count", count.get(), 0);
futures.clear();
for (int i = 0; i < QTY; ++i) {
Future<Void> worker = service.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
ZkConnection zkConnection = createConnection(60 * 1000);
CuratorFramework client = zkConnection.curator();
try {
zkConnection.connect();
DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testEnterTimeout", QTY);
log.info("Entering with timeout {} seconds", timing.seconds());
Assert.assertTrue("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
count.incrementAndGet();
log.info("Leaving with timeout {} seconds", timing.seconds());
Assert.assertTrue("Return value of enter()", barrier.leave(timing.seconds(), TimeUnit.SECONDS));
count.decrementAndGet();
} finally {
CloseableUtils.closeQuietly(client);
}
return null;
}
});
futures.add(worker);
}
for (Future<Void> f : futures) {
f.get();
}
Assert.assertEquals("enter/leave count", count.get(), 0);
}
use of com.emc.storageos.coordinator.common.impl.ZkConnection in project coprhd-controller by CoprHD.
the class CoordinatorTestBase method createConnection.
protected static ZkConnection createConnection(int timeoutMs) throws IOException {
ZkConnection conn = new ZkConnection();
conn.setServer(Arrays.asList(URI.create("coordinator://localhost:2181")));
conn.setTimeoutMs(timeoutMs);
conn.setSiteId("fake-site-id");
conn.setSiteIdFile("fake-site-id-file");
conn.build();
return conn;
}
use of com.emc.storageos.coordinator.common.impl.ZkConnection in project coprhd-controller by CoprHD.
the class CoordinatorTestBase method connectClient.
protected static CoordinatorClient connectClient(List<URI> server) throws Exception {
CoordinatorClientImpl client = new CoordinatorClientImpl();
ZkConnection conn = new ZkConnection();
conn.setServer(server);
conn.setTimeoutMs(10 * 1000);
System.out.println("Connecting with coordinator service...");
conn.build();
System.out.println("Connecting with coordinator service.");
client.setZkConnection(conn);
client.start();
return client;
}
Aggregations