Search in sources :

Example 1 with LeaderSelectorListener

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

the class CuratorLeaderElectionManager method determineLeaderExternal.

/**
 * Use a new Curator client to determine which node is the elected leader for the given role.
 *
 * @param roleName the name of the role
 * @return the id of the elected leader, or <code>null</code> if no leader has been selected or if unable to determine
 *         the leader from ZooKeeper
 */
private String determineLeaderExternal(final String roleName) {
    final CuratorFramework client = createClient();
    try {
        final LeaderSelectorListener electionListener = new LeaderSelectorListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
            }
        };
        final String electionPath = getElectionPath(roleName);
        // Note that we intentionally do not auto-requeue here, and we do not start the selector. We do not
        // want to join the leader election. We simply want to observe.
        final LeaderSelector selector = new LeaderSelector(client, electionPath, electionListener);
        try {
            final Participant leader = selector.getLeader();
            return leader == null ? null : leader.getId();
        } catch (final KeeperException.NoNodeException nne) {
            // If there is no ZNode, then there is no elected leader.
            return null;
        } catch (final Exception e) {
            logger.warn("Unable to determine the Elected Leader for role '{}' due to {}; assuming no leader has been elected", roleName, e.toString());
            if (logger.isDebugEnabled()) {
                logger.warn("", e);
            }
            return null;
        }
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Participant(org.apache.curator.framework.recipes.leader.Participant) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) ConnectionState(org.apache.curator.framework.state.ConnectionState) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with LeaderSelectorListener

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

the class CoordinatorImpl method startMutexReaper.

/**
 * Reaper mutex dirs generated from InterProcessMutex
 */
private void startMutexReaper() {
    Thread childReaperThread = new Thread(new Runnable() {

        public void run() {
            try {
                // from LoggingBean, needs to wait for CoordinatorSvc started.
                while (!_coordinatorClient.isConnected()) {
                    _log.info("Waiting for connection to cluster ...");
                    Thread.sleep(3 * 1000);
                }
                _log.info("Connected to cluster");
                DrUtil drUtil = new DrUtil(_coordinatorClient);
                if (drUtil.isStandby()) {
                    _log.info("Skip mutex reapter on standby site");
                    return;
                }
                /**
                 * Reaper empty dirs under /mutex in zookeeper
                 * It leverages curator Reaper and ChildReaper to remove empty sub dirs.
                 * It leverages LeaderSelector to assure only one reaper running at the same time.
                 * Note: Please use autoRequeue() to requeue for competing leader automatically
                 * while connection broken and reconnected. The requeue() has a bug in curator
                 * 1.3.4 and should not be used.
                 */
                LeaderSelectorListener listener = new ReaperLeaderSelectorListener(ZkPath.MUTEX.toString());
                String _leaderRelativePath = "mutexReaper";
                LeaderSelector leaderSel = _coordinatorClient.getLeaderSelector(_leaderRelativePath, listener);
                leaderSel.autoRequeue();
                leaderSel.start();
            } catch (Exception e) {
                _log.warn("reaper task threw", e);
            }
        }
    }, "reaper thread");
    childReaperThread.start();
}
Also used : ReaperLeaderSelectorListener(com.emc.storageos.coordinator.client.service.impl.ReaperLeaderSelectorListener) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) ReaperLeaderSelectorListener(com.emc.storageos.coordinator.client.service.impl.ReaperLeaderSelectorListener) DrUtil(com.emc.storageos.coordinator.client.service.DrUtil) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) IOException(java.io.IOException) JMException(javax.management.JMException)

Example 3 with LeaderSelectorListener

use of org.apache.curator.framework.recipes.leader.LeaderSelectorListener in project BRFS by zhangnianli.

the class LeaderTest method main.

public static void main(String[] args) throws InterruptedException, IOException {
    String path = "/brfs/wz/leader";
    LeaderSelectorListener l1 = new MyLeaderListener("listener1");
    LeaderSelectorListener l2 = new MyLeaderListener("listener2");
    CuratorLeaderSelectorClient leaderSelector = CuratorLeaderSelectorClient.getLeaderSelectorInstance("192.168.101.86:2181");
    leaderSelector.addSelector(path, l1);
    leaderSelector.addSelector(path, l2);
    // Thread.sleep(Long.MAX_VALUE);
    Thread.sleep(10000);
    System.out.println(leaderSelector.getSelectorListeners(path));
    Thread.sleep(30000);
    leaderSelector.removeAllSelector(path);
    Thread.sleep(Long.MAX_VALUE);
}
Also used : CuratorLeaderSelectorClient(com.bonree.brfs.common.zookeeper.curator.leader.CuratorLeaderSelectorClient) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener)

Example 4 with LeaderSelectorListener

use of org.apache.curator.framework.recipes.leader.LeaderSelectorListener in project tutorials by eugenp.

the class RecipesManualTest method givenRunningZookeeper_whenUsingLeaderElection_thenNoErrors.

@Test
public void givenRunningZookeeper_whenUsingLeaderElection_thenNoErrors() {
    try (CuratorFramework client = newClient()) {
        client.start();
        LeaderSelector leaderSelector = new LeaderSelector(client, "/mutex/select/leader/for/job/A", new LeaderSelectorListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
            // I'm the leader of the job A !
            }
        });
        leaderSelector.start();
        // Wait until the job A is done among all the members
        leaderSelector.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) ConnectionState(org.apache.curator.framework.state.ConnectionState) BaseTest(com.baeldung.apache.curator.BaseTest) Test(org.junit.Test)

Example 5 with LeaderSelectorListener

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

the class ZkLeaderElection method start.

/**
 * 启动主节点选举
 */
public static void start() {
    synchronized (lock) {
        if (singleton != null)
            return;
        ZkLeaderElection leaderElection = new ZkLeaderElection();
        LeaderSelectorListener listener = new LeaderSelectorListenerAdapter() {

            public void takeLeadership(CuratorFramework client) throws InterruptedException {
                LOG.info("被提升为主节点!");
                try {
                    Thread.sleep(Long.MAX_VALUE);
                } catch (InterruptedException e) {
                    LOG.info("节点主动断开 or  主动让出主节点身份?反正此处是预期的打断!不需要打印堆栈");
                } finally {
                    LOG.info("主节点降级为普通节点!");
                }
            }
        };
        leaderElection.leaderSelector = new LeaderSelector(ZkConnection.client, ZkPathManager.getMyNodeBasePath(), listener);
        // not required, but this is behavior that you will probably expect
        leaderElection.leaderSelector.autoRequeue();
        leaderElection.leaderSelector.start();
        singleton = leaderElection;
        LOG.info("ZkLeaderElection启动完毕.");
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) LeaderSelectorListenerAdapter(org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter)

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