Search in sources :

Example 31 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestLeaderLatch method testCreateDeleteRace.

@Test
public void testCreateDeleteRace() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath(PATH_NAME);
        LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
        latch.debugResetWaitLatch = new CountDownLatch(1);
        latch.start();
        latch.close();
        timing.sleepABit();
        latch.debugResetWaitLatch.countDown();
        timing.sleepABit();
        Assert.assertEquals(client.getChildren().forPath(PATH_NAME).size(), 0);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 32 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestLeaderLatch method testLostConnection.

@Test
public void testLostConnection() throws Exception {
    final int PARTICIPANT_QTY = 10;
    List<LeaderLatch> latches = Lists.newArrayList();
    final Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    countDownLatch.countDown();
                }
            }
        });
        for (int i = 0; i < PARTICIPANT_QTY; ++i) {
            LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
            latch.start();
            latches.add(latch);
        }
        waitForALeader(latches, timing);
        server.stop();
        Assert.assertTrue(timing.awaitLatch(countDownLatch));
        timing.forWaiting().sleepABit();
        Assert.assertEquals(getLeaders(latches).size(), 0);
        server.restart();
        // should reconnect
        Assert.assertEquals(waitForALeader(latches, timing).size(), 1);
    } finally {
        for (LeaderLatch latch : latches) {
            CloseableUtils.closeQuietly(latch);
        }
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 33 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestLeaderLatch method testResetRace.

@Test
public void testResetRace() throws Exception {
    Timing timing = new Timing();
    LeaderLatch latch = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        latch = new LeaderLatch(client, PATH_NAME);
        latch.debugResetWaitLatch = new CountDownLatch(1);
        // will call reset()
        latch.start();
        // should not result in two nodes
        latch.reset();
        timing.sleepABit();
        latch.debugResetWaitLatch.countDown();
        timing.sleepABit();
        Assert.assertEquals(client.getChildren().forPath(PATH_NAME).size(), 1);
    } finally {
        CloseableUtils.closeQuietly(latch);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 34 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestLeaderLatch method testCorrectWatching.

@Test
public void testCorrectWatching() throws Exception {
    final int PARTICIPANT_QTY = 10;
    final int PARTICIPANT_ID = 2;
    List<LeaderLatch> latches = Lists.newArrayList();
    final Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        for (int i = 0; i < PARTICIPANT_QTY; ++i) {
            LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
            latch.start();
            latches.add(latch);
        }
        waitForALeader(latches, timing);
        // we need to close a Participant that doesn't be actual leader (first Participant) nor the last
        latches.get(PARTICIPANT_ID).close();
        // As the previous algorithm assumed that if the watched node is deleted gets the leadership
        // we need to ensure that the PARTICIPANT_ID-1 is not getting (wrongly) elected as leader.
        Assert.assertTrue(!latches.get(PARTICIPANT_ID - 1).hasLeadership());
    } finally {
        // removes the already closed participant
        latches.remove(PARTICIPANT_ID);
        for (LeaderLatch latch : latches) {
            CloseableUtils.closeQuietly(latch);
        }
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 35 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestLeaderLatch method testCallbackNotifyLeader.

@Test
public void testCallbackNotifyLeader() throws Exception {
    final int PARTICIPANT_QTY = 10;
    final int SILENT_QTY = 3;
    final CountDownLatch timesSquare = new CountDownLatch(PARTICIPANT_QTY);
    final AtomicLong masterCounter = new AtomicLong(0);
    final AtomicLong notLeaderCounter = new AtomicLong(0);
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    ExecutorService exec = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("callbackNotifyLeader-%s").build());
    List<LeaderLatch> latches = Lists.newArrayList();
    for (int i = 0; i < PARTICIPANT_QTY; ++i) {
        LeaderLatch.CloseMode closeMode = i < SILENT_QTY ? LeaderLatch.CloseMode.SILENT : LeaderLatch.CloseMode.NOTIFY_LEADER;
        final LeaderLatch latch = new LeaderLatch(client, PATH_NAME, "", closeMode);
        latch.addListener(new LeaderLatchListener() {

            boolean beenLeader = false;

            @Override
            public void isLeader() {
                if (!beenLeader) {
                    masterCounter.incrementAndGet();
                    beenLeader = true;
                    try {
                        latch.reset();
                    } catch (Exception e) {
                        throw Throwables.propagate(e);
                    }
                } else {
                    masterCounter.incrementAndGet();
                    CloseableUtils.closeQuietly(latch);
                    timesSquare.countDown();
                }
            }

            @Override
            public void notLeader() {
                notLeaderCounter.incrementAndGet();
            }
        }, exec);
        latches.add(latch);
    }
    try {
        client.start();
        for (LeaderLatch latch : latches) {
            latch.start();
        }
        timesSquare.await();
        Assert.assertEquals(masterCounter.get(), PARTICIPANT_QTY * 2);
        Assert.assertEquals(notLeaderCounter.get(), PARTICIPANT_QTY * 2 - SILENT_QTY);
        for (LeaderLatch latch : latches) {
            Assert.assertEquals(latch.getState(), LeaderLatch.State.CLOSED);
        }
    } finally {
        for (LeaderLatch latch : latches) {
            if (latch.getState() != LeaderLatch.State.CLOSED) {
                CloseableUtils.closeQuietly(latch);
            }
        }
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Aggregations

Timing (org.apache.curator.test.Timing)166 CuratorFramework (org.apache.curator.framework.CuratorFramework)147 Test (org.testng.annotations.Test)138 RetryOneTime (org.apache.curator.retry.RetryOneTime)129 CountDownLatch (java.util.concurrent.CountDownLatch)56 ConnectionState (org.apache.curator.framework.state.ConnectionState)39 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)29 KeeperException (org.apache.zookeeper.KeeperException)28 ExecutorService (java.util.concurrent.ExecutorService)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Semaphore (java.util.concurrent.Semaphore)18 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)16 TestingServer (org.apache.curator.test.TestingServer)15 Stat (org.apache.zookeeper.data.Stat)12 TestingCluster (org.apache.curator.test.TestingCluster)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 InstanceSpec (org.apache.curator.test.InstanceSpec)9 Test (org.junit.Test)9