Search in sources :

Example 1 with ZkConnection

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);
        }
    }
}
Also used : Closeable(java.io.Closeable) ZkConnection(com.emc.storageos.coordinator.common.impl.ZkConnection) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Example 2 with ZkConnection

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);
}
Also used : ZkConnection(com.emc.storageos.coordinator.common.impl.ZkConnection) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Example 3 with ZkConnection

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);
}
Also used : ZkConnection(com.emc.storageos.coordinator.common.impl.ZkConnection) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Example 4 with ZkConnection

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;
}
Also used : ZkConnection(com.emc.storageos.coordinator.common.impl.ZkConnection)

Example 5 with ZkConnection

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;
}
Also used : CoordinatorClientImpl(com.emc.storageos.coordinator.client.service.impl.CoordinatorClientImpl) ZkConnection(com.emc.storageos.coordinator.common.impl.ZkConnection)

Aggregations

ZkConnection (com.emc.storageos.coordinator.common.impl.ZkConnection)20 URI (java.net.URI)12 CoordinatorClientInetAddressMap (com.emc.storageos.coordinator.client.service.impl.CoordinatorClientInetAddressMap)9 DualInetAddress (com.emc.storageos.coordinator.client.service.impl.DualInetAddress)8 ArrayList (java.util.ArrayList)8 CoordinatorClientImpl (com.emc.storageos.coordinator.client.service.impl.CoordinatorClientImpl)6 FileInputStream (java.io.FileInputStream)6 Before (org.junit.Before)6 Properties (java.util.Properties)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 Timing (org.apache.curator.test.Timing)5 Test (org.junit.Test)5 KeyCertificateAlgorithmValuesHolder (com.emc.storageos.security.keystore.impl.KeyCertificateAlgorithmValuesHolder)4 HashMap (java.util.HashMap)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 KeyCertificatePairGenerator (com.emc.storageos.security.keystore.impl.KeyCertificatePairGenerator)3 DbVersionInfo (com.emc.storageos.coordinator.client.model.DbVersionInfo)2 DistributedLoadKeyStoreParam (com.emc.storageos.security.keystore.impl.DistributedLoadKeyStoreParam)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 StubCoordinatorClientImpl (com.emc.sa.model.mock.StubCoordinatorClientImpl)1