Java Code Examples for org.apache.curator.framework.recipes.leader.LeaderSelector#start()

The following examples show how to use org.apache.curator.framework.recipes.leader.LeaderSelector#start() . 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: 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 2
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 3
Source File: LeaderSelecter.java    From PoseidonX with Apache License 2.0 5 votes vote down vote up
/**
 * 执行选主
 */
private void electionLeader(){
    cfClient = ZKUtil.getClient(StreamContant.HDFS_HADOOP_ZOOKEEPER);
    if(cfClient == null){
        LOGGER.error("LeaderSelecter electionLeader failed so cfClient is null, HDFS_HADOOP_ZOOKEEPER=" + StreamContant.HDFS_HADOOP_ZOOKEEPER);
        return ;
    }
    leaderSelector = new LeaderSelector(cfClient, StreamContant.ZOOKEEPER_LEADER_DIR, this);
    leaderSelector.autoRequeue();
    leaderSelector.start();
}
 
Example 4
Source File: OlapServerMaster.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
private void leaderElection() {
    String ensemble = ZKConfig.getZKQuorumServersString(conf);
    CuratorFramework client = CuratorFrameworkFactory.newClient(ensemble, new ExponentialBackoffRetry(1000, 3));

    client.start();

    String leaderElectionPath = HConfiguration.getConfiguration().getSpliceRootPath()
            + HBaseConfiguration.OLAP_SERVER_PATH + HBaseConfiguration.OLAP_SERVER_LEADER_ELECTION_PATH
            + "/" + queueName;

    LeaderSelector leaderSelector = new LeaderSelector(client, leaderElectionPath, this);
    LOG.info("Starting leader election for OlapServer-"+queueName);
    leaderSelector.start();
}
 
Example 5
Source File: ZkClient.java    From xio with Apache License 2.0 4 votes vote down vote up
public void electLeader(String ELECTION_PATH, LeaderSelectorListener listener) {
  leaderSelector = new LeaderSelector(client, ELECTION_PATH, listener);

  leaderSelector.autoRequeue();
  leaderSelector.start();
}