Search in sources :

Example 6 with LeaderSelectorListener

use of org.apache.curator.framework.recipes.leader.LeaderSelectorListener in project xian by happyyangyuan.

the class TestResetConnectionWithBackgroundFailure method testConnectionStateListener.

@Test
public void testConnectionStateListener() throws Exception {
    server.stop();
    LeaderSelector selector = null;
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        timing.sleepABit();
        LeaderSelectorListener listenerLeader = new LeaderSelectorListenerAdapter() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                Thread.currentThread().join();
            }
        };
        selector = new LeaderSelector(client, "/leader", listenerLeader);
        selector.autoRequeue();
        selector.start();
        final BlockingQueue<ConnectionState> listenerSequence = Queues.newLinkedBlockingQueue();
        ConnectionStateListener listener1 = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                listenerSequence.add(newState);
            }
        };
        Timing forWaiting = timing.forWaiting();
        client.getConnectionStateListenable().addListener(listener1);
        log.debug("Starting ZK server");
        server.restart();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
        log.debug("Stopping ZK server");
        server.stop();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
        log.debug("Starting ZK server");
        server.restart();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
        log.debug("Stopping ZK server");
        server.close();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
    } finally {
        CloseableUtils.closeQuietly(selector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) Timing(org.apache.curator.test.Timing) LeaderSelectorListenerAdapter(org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 7 with LeaderSelectorListener

use of org.apache.curator.framework.recipes.leader.LeaderSelectorListener in project xian by happyyangyuan.

the class TestReaper method testUsingManualLeader.

@Test
public void testUsingManualLeader() throws Exception {
    final Timing timing = new Timing();
    final CuratorFramework client = makeClient(timing, null);
    final CountDownLatch latch = new CountDownLatch(1);
    LeaderSelectorListener listener = new LeaderSelectorListener() {

        @Override
        public void takeLeadership(CuratorFramework client) throws Exception {
            Reaper reaper = new Reaper(client, 1);
            try {
                reaper.addPath("/one/two/three", Reaper.Mode.REAP_UNTIL_DELETE);
                reaper.start();
                timing.sleepABit();
                latch.countDown();
            } finally {
                CloseableUtils.closeQuietly(reaper);
            }
        }

        @Override
        public void stateChanged(CuratorFramework client, ConnectionState newState) {
        }
    };
    LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/one/two/three");
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
        selector.start();
        timing.awaitLatch(latch);
        Assert.assertNull(client.checkExists().forPath("/one/two/three"));
    } finally {
        CloseableUtils.closeQuietly(selector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

LeaderSelectorListener (org.apache.curator.framework.recipes.leader.LeaderSelectorListener)7 LeaderSelector (org.apache.curator.framework.recipes.leader.LeaderSelector)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 ConnectionState (org.apache.curator.framework.state.ConnectionState)4 LeaderSelectorListenerAdapter (org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter)2 Timing (org.apache.curator.test.Timing)2 Test (org.testng.annotations.Test)2 BaseTest (com.baeldung.apache.curator.BaseTest)1 CuratorLeaderSelectorClient (com.bonree.brfs.common.zookeeper.curator.leader.CuratorLeaderSelectorClient)1 DrUtil (com.emc.storageos.coordinator.client.service.DrUtil)1 ReaperLeaderSelectorListener (com.emc.storageos.coordinator.client.service.impl.ReaperLeaderSelectorListener)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 JMException (javax.management.JMException)1 Participant (org.apache.curator.framework.recipes.leader.Participant)1 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)1 RetryOneTime (org.apache.curator.retry.RetryOneTime)1 KeeperException (org.apache.zookeeper.KeeperException)1 Test (org.junit.Test)1