Search in sources :

Example 36 with Timing

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

the class TestLeaderSelector method testLeaderNodeDeleteOnInterrupt.

@Test
public void testLeaderNodeDeleteOnInterrupt() throws Exception {
    Timing timing = new Timing();
    LeaderSelector selector = null;
    CuratorFramework client = null;
    try {
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        final CountDownLatch reconnectedLatch = new CountDownLatch(1);
        ConnectionStateListener connectionStateListener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.RECONNECTED) {
                    reconnectedLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(connectionStateListener);
        client.start();
        final BlockingQueue<Thread> queue = new ArrayBlockingQueue<Thread>(1);
        LeaderSelectorListener listener = new LeaderSelectorListener() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                queue.add(Thread.currentThread());
                try {
                    Thread.currentThread().join();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        selector = new LeaderSelector(client, "/leader", listener);
        selector.start();
        Thread leaderThread = queue.take();
        server.stop();
        leaderThread.interrupt();
        server.restart();
        Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
        timing.sleepABit();
        Assert.assertEquals(client.getChildren().forPath("/leader").size(), 0);
    } finally {
        CloseableUtils.closeQuietly(selector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) 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 37 with Timing

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

the class TestLeaderSelector method testKillServerThenCloseShouldElectNewLeader.

/**
 * This is similar to TestLeaderSelector.testKillSessionThenCloseShouldElectNewLeader
 * The differences are:
 * it restarts the TestingServer instead of killing the session
 * it uses autoRequeue instead of explicitly calling requeue
 */
@Test
public void testKillServerThenCloseShouldElectNewLeader() throws Exception {
    final Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        final Semaphore semaphore = new Semaphore(0);
        final CountDownLatch interruptedLatch = new CountDownLatch(1);
        final AtomicInteger leaderCount = new AtomicInteger(0);
        LeaderSelectorListener listener = new LeaderSelectorListenerAdapter() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                leaderCount.incrementAndGet();
                try {
                    semaphore.release();
                    try {
                        Thread.currentThread().join();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        interruptedLatch.countDown();
                    }
                } finally {
                    leaderCount.decrementAndGet();
                }
            }
        };
        LeaderSelector leaderSelector1 = new LeaderSelector(client, PATH_NAME, listener);
        LeaderSelector leaderSelector2 = new LeaderSelector(client, PATH_NAME, listener);
        boolean leaderSelector1Closed = false;
        boolean leaderSelector2Closed = false;
        leaderSelector1.autoRequeue();
        leaderSelector2.autoRequeue();
        leaderSelector1.start();
        leaderSelector2.start();
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        int port = server.getPort();
        server.stop();
        timing.sleepABit();
        server = new TestingServer(port);
        Assert.assertTrue(timing.awaitLatch(interruptedLatch));
        timing.sleepABit();
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        Assert.assertEquals(leaderCount.get(), 1);
        if (leaderSelector1.hasLeadership()) {
            leaderSelector1.close();
            leaderSelector1Closed = true;
        } else if (leaderSelector2.hasLeadership()) {
            leaderSelector2.close();
            leaderSelector2Closed = true;
        } else {
            fail("No leaderselector has leadership!");
        }
        // Verify that the other leader took over leadership.
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        Assert.assertEquals(leaderCount.get(), 1);
        if (!leaderSelector1Closed) {
            leaderSelector1.close();
        }
        if (!leaderSelector2Closed) {
            leaderSelector2.close();
        }
    } finally {
        client.close();
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) RetryOneTime(org.apache.curator.retry.RetryOneTime) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 38 with Timing

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

the class TestLeaderSelector method testInterruptLeadership.

@Test
public void testInterruptLeadership() throws Exception {
    LeaderSelector selector = null;
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        final CountDownLatch isLeaderLatch = new CountDownLatch(1);
        final CountDownLatch losingLeaderLatch = new CountDownLatch(1);
        LeaderSelectorListener listener = new LeaderSelectorListener() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                isLeaderLatch.countDown();
                try {
                    Thread.currentThread().join();
                } finally {
                    losingLeaderLatch.countDown();
                }
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        selector = new LeaderSelector(client, "/leader", listener);
        selector.start();
        Assert.assertTrue(timing.awaitLatch(isLeaderLatch));
        selector.interruptLeadership();
        Assert.assertTrue(timing.awaitLatch(losingLeaderLatch));
    } finally {
        CloseableUtils.closeQuietly(selector);
        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) Test(org.testng.annotations.Test)

Example 39 with Timing

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

the class TestLeaderSelector method testKillSessionThenCloseShouldElectNewLeader.

@Test
public void testKillSessionThenCloseShouldElectNewLeader() throws Exception {
    final Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        final Semaphore semaphore = new Semaphore(0);
        final CountDownLatch interruptedLatch = new CountDownLatch(1);
        final AtomicInteger leaderCount = new AtomicInteger(0);
        LeaderSelectorListener listener = new LeaderSelectorListenerAdapter() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                leaderCount.incrementAndGet();
                try {
                    semaphore.release();
                    try {
                        Thread.currentThread().join();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        interruptedLatch.countDown();
                    }
                } finally {
                    leaderCount.decrementAndGet();
                }
            }
        };
        LeaderSelector leaderSelector1 = new LeaderSelector(client, PATH_NAME, listener);
        LeaderSelector leaderSelector2 = new LeaderSelector(client, PATH_NAME, listener);
        boolean leaderSelector1Closed = false;
        boolean leaderSelector2Closed = false;
        leaderSelector1.start();
        leaderSelector2.start();
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        Assert.assertTrue(timing.awaitLatch(interruptedLatch));
        timing.sleepABit();
        boolean requeued1 = leaderSelector1.requeue();
        boolean requeued2 = leaderSelector2.requeue();
        Assert.assertTrue(requeued1);
        Assert.assertTrue(requeued2);
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        Assert.assertEquals(leaderCount.get(), 1);
        if (leaderSelector1.hasLeadership()) {
            leaderSelector1.close();
            leaderSelector1Closed = true;
        } else if (leaderSelector2.hasLeadership()) {
            leaderSelector2.close();
            leaderSelector2Closed = true;
        } else {
            fail("No leaderselector has leadership!");
        }
        // Verify that the other leader took over leadership.
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        Assert.assertEquals(leaderCount.get(), 1);
        if (!leaderSelector1Closed) {
            leaderSelector1.close();
        }
        if (!leaderSelector2Closed) {
            leaderSelector2.close();
        }
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 40 with Timing

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

the class TestLeaderSelectorWithExecutor method test.

@Test
public void test() throws Exception {
    Timing timing = new Timing();
    LeaderSelector leaderSelector = null;
    CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(new ExponentialBackoffRetry(100, 3)).connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).build();
    try {
        client.start();
        MyLeaderSelectorListener listener = new MyLeaderSelectorListener();
        ExecutorService executorPool = Executors.newFixedThreadPool(20);
        leaderSelector = new LeaderSelector(client, "/test", threadFactory, executorPool, listener);
        leaderSelector.autoRequeue();
        leaderSelector.start();
        timing.sleepABit();
        Assert.assertEquals(listener.getLeaderCount(), 1);
    } finally {
        CloseableUtils.closeQuietly(leaderSelector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorService(java.util.concurrent.ExecutorService) 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