org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter Java Examples

The following examples show how to use org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZkLeaderElection.java    From xian with Apache License 2.0 6 votes vote down vote up
/**
 * 启动主节点选举
 */
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);
        leaderElection.leaderSelector.autoRequeue(); //not required, but this is behavior that you will probably expect
        leaderElection.leaderSelector.start();
        singleton = leaderElection;
        LOG.info("ZkLeaderElection启动完毕.");
    }
}
 
Example #2
Source File: SchedulerSelector.java    From workflow with Apache License 2.0 6 votes vote down vote up
public SchedulerSelector(WorkflowManagerImpl workflowManager, QueueFactory queueFactory, AutoCleanerHolder autoCleanerHolder)
{
    this.workflowManager = workflowManager;
    this.queueFactory = queueFactory;
    this.autoCleanerHolder = autoCleanerHolder;

    LeaderSelectorListener listener = new LeaderSelectorListenerAdapter()
    {
        @Override
        public void takeLeadership(CuratorFramework client) throws Exception
        {
            SchedulerSelector.this.takeLeadership();
        }
    };
    leaderSelector = new LeaderSelector(workflowManager.getCurator(), ZooKeeperConstants.getSchedulerLeaderPath(), listener);
    leaderSelector.autoRequeue();
}
 
Example #3
Source File: MasterElection.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static void election(final CountDownLatch cdl) {
    final CuratorFramework client = ZkUtils.getCuratorClient();
    final LeaderSelector selector = new LeaderSelector(client, Constants.MASTER_PATH, new LeaderSelectorListenerAdapter() {
        @Override
        public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
            SWITCH_LOGGER.info("take master leadership");

            long seekTimestamp = MetaService.getSeekTimestamp();
            long zkSeekTimestamp = MetaService.getZkSeekTimestamp();
            final long sleepMs = 200;
            long sleepCount = 0;
            // 如果zk上的数据丢失了, 则zkSeekTimestamp为0, 此时chronos则被block住
            while (seekTimestamp < zkSeekTimestamp && zkSeekTimestamp > 0) {
                SWITCH_LOGGER.info("sleep {}ms to wait seekTimestamp:{} to catch up with zkSeekTimestamp:{}",
                        sleepMs, seekTimestamp, zkSeekTimestamp);
                TimeUnit.MILLISECONDS.sleep(sleepMs);
                seekTimestamp = MetaService.getSeekTimestamp();
                zkSeekTimestamp = MetaService.getZkSeekTimestamp();
                sleepCount++;
            }

            state = ServerState.MASTERING;
            SWITCH_LOGGER.info("change server state to {}, totalSleepMs:{}ms", state, sleepCount * sleepMs);
            cdl.await();
            state = ServerState.BACKUPING;
            SWITCH_LOGGER.info("release master leadership");
        }
    });
    selector.autoRequeue();
    selector.start();
}
 
Example #4
Source File: MasterElection.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static void election(final CountDownLatch cdl) {
    final CuratorFramework client = ZkUtils.getCuratorClient();
    final LeaderSelector selector = new LeaderSelector(client, Constants.MASTER_PATH, new LeaderSelectorListenerAdapter() {
        @Override
        public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
            SWITCH_LOGGER.info("take master leadership");

            long seekTimestamp = MetaService.getSeekTimestamp();
            long zkSeekTimestamp = MetaService.getZkSeekTimestamp();
            final long sleepMs = 200;
            long sleepCount = 0;
            // 如果zk上的数据丢失了, 则zkSeekTimestamp为0, 此时chronos则被block住
            while (seekTimestamp < zkSeekTimestamp && zkSeekTimestamp > 0) {
                SWITCH_LOGGER.info("sleep {}ms to wait seekTimestamp:{} to catch up with zkSeekTimestamp:{}",
                        sleepMs, seekTimestamp, zkSeekTimestamp);
                TimeUnit.MILLISECONDS.sleep(sleepMs);
                seekTimestamp = MetaService.getSeekTimestamp();
                zkSeekTimestamp = MetaService.getZkSeekTimestamp();
                sleepCount++;
            }

            state = ServerState.MASTERING;
            SWITCH_LOGGER.info("change server state to {}, totalSleepMs:{}ms", state, sleepCount * sleepMs);
            cdl.await();
            state = ServerState.BACKUPING;
            SWITCH_LOGGER.info("release master leadership");
        }
    });
    selector.autoRequeue();
    selector.start();
}