Search in sources :

Example 21 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector 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 22 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector 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)

Example 23 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project spring-integration by spring-projects.

the class LeaderInitiator method start.

/**
 * Start the registration of the {@link #candidate} for leader election.
 */
@Override
public void start() {
    synchronized (this.lifecycleMonitor) {
        if (!this.running) {
            if (this.client.getState() != CuratorFrameworkState.STARTED) {
                // we want to do curator start here because it needs to
                // be started before leader selector and it gets a little
                // complicated to control ordering via beans so that
                // curator is fully started.
                this.client.start();
            }
            this.leaderSelector = new LeaderSelector(this.client, buildLeaderPath(), new LeaderListener());
            this.leaderSelector.setId(this.candidate.getId());
            this.leaderSelector.autoRequeue();
            this.leaderSelector.start();
            this.running = true;
            logger.debug("Started LeaderInitiator");
        }
    }
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

Example 24 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project coprhd-controller by CoprHD.

the class LeaderSelectionTest method leaderSelectionTest.

/**
 * Simulates multiple clients accessing persistent lock API simultaneously.
 *
 * @throws Exception
 */
@Test
public void leaderSelectionTest() throws Exception {
    logger.info("*** Leader Seleciton Test start");
    ExecutorService clients = Executors.newFixedThreadPool(NUMCLIENTS);
    for (int i = 0; i < NUMCLIENTS; i++) {
        final int count = i;
        clients.submit(new Runnable() {

            @Override
            public void run() {
                String leaderName = LATCH_NAME + '_' + (count + 1);
                LeaderSelector leader = null;
                try {
                    TestProcessor processor = new TestProcessor(leaderName);
                    leader = connectClient().getLeaderSelector(LATCH_PATH, processor);
                } catch (Exception e) {
                    logger.info(": {} leaderSelectionTest could not get coordinator client", e);
                    Assert.assertNull(e);
                    return;
                }
                logger.info(": ### Initialized LeaderSelector {} ###", leaderName);
                leader.start();
                leader.requeue();
                synchronized (leaders) {
                    leaders.add(leader);
                }
            }
        });
    }
    synchronized (leaderMonitor) {
        while (leaderMonitor.size() < NUMCLIENTS * NUMRUN) {
            leaderMonitor.wait();
        }
    }
    synchronized (leaders) {
        for (int i = 0; i < NUMCLIENTS; i++) {
            leaders.get(i).close();
        }
    }
    synchronized (leaderMonitor) {
        for (int i = 0; i < NUMCLIENTS; i++) {
            for (int j = 0; j < NUMRUN; j++) {
                logger.info("Leadership : " + leaderMonitor.get(i * NUMRUN + j));
            }
            String first = leaderMonitor.get(0).substring(0, 17);
            for (int j = 1; j < NUMRUN; j++) {
                Assert.assertTrue(leaderMonitor.get(j).startsWith(first));
            }
        }
    }
    logger.info("*** LeaderSelector end");
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) Test(org.junit.Test)

Aggregations

LeaderSelector (org.apache.curator.framework.recipes.leader.LeaderSelector)24 CuratorFramework (org.apache.curator.framework.CuratorFramework)7 LeaderSelectorListener (org.apache.curator.framework.recipes.leader.LeaderSelectorListener)6 IOException (java.io.IOException)5 ConnectionState (org.apache.curator.framework.state.ConnectionState)4 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)4 CancelLeadershipException (org.apache.curator.framework.recipes.leader.CancelLeadershipException)3 LeaderSelectorListenerAdapter (org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter)3 Test (org.junit.Test)3 Timing (org.apache.curator.test.Timing)2 KeeperException (org.apache.zookeeper.KeeperException)2 Test (org.testng.annotations.Test)2 BaseTest (com.baeldung.apache.curator.BaseTest)1 DrUtil (com.emc.storageos.coordinator.client.service.DrUtil)1 LeaderSelectorListenerImpl (com.emc.storageos.coordinator.client.service.impl.LeaderSelectorListenerImpl)1 ReaperLeaderSelectorListener (com.emc.storageos.coordinator.client.service.impl.ReaperLeaderSelectorListener)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1