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

The following examples show how to use org.apache.curator.framework.recipes.leader.LeaderSelector. 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: CuratorLeaderElectionManager.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void stop() {
    stopped = true;

    for (final Map.Entry<String, LeaderRole> entry : leaderRoles.entrySet()) {
        final LeaderRole role = entry.getValue();
        final LeaderSelector selector = role.getLeaderSelector();

        try {
            selector.close();
        } catch (final Exception e) {
            logger.warn("Failed to close Leader Selector for {}", entry.getKey(), e);
        }
    }

    leaderRoles.clear();

    if (curatorClient != null) {
        curatorClient.close();
        curatorClient = null;
    }

    logger.info("{} stopped and closed", this);
}
 
Example #2
Source File: CuratorLeaderElectionManager.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void unregister(final String roleName) {
    registeredRoles.remove(roleName);

    final LeaderRole leaderRole = leaderRoles.remove(roleName);
    if (leaderRole == null) {
        logger.info("Cannot unregister Leader Election Role '{}' becuase that role is not registered", roleName);
        return;
    }

    final LeaderSelector leaderSelector = leaderRole.getLeaderSelector();
    if (leaderSelector == null) {
        logger.info("Cannot unregister Leader Election Role '{}' becuase that role is not registered", roleName);
        return;
    }

    leaderSelector.close();
    logger.info("This node is no longer registered to be elected as the Leader for Role '{}'", roleName);
}
 
Example #3
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 #4
Source File: LeaderInitiator.java    From spring-cloud-cluster with Apache License 2.0 6 votes vote down vote up
/**
 * Start the registration of the {@link #candidate} for leader election.
 */
@Override
public synchronized void start() {
	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;
	}
}
 
Example #5
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 #6
Source File: CuratorLeaderElectionManager.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void unregister(final String roleName) {
    registeredRoles.remove(roleName);

    final LeaderRole leaderRole = leaderRoles.remove(roleName);
    if (leaderRole == null) {
        logger.info("Cannot unregister Leader Election Role '{}' becuase that role is not registered", roleName);
        return;
    }

    final LeaderSelector leaderSelector = leaderRole.getLeaderSelector();
    if (leaderSelector == null) {
        logger.info("Cannot unregister Leader Election Role '{}' becuase that role is not registered", roleName);
        return;
    }

    leaderSelector.close();
    logger.info("This node is no longer registered to be elected as the Leader for Role '{}'", roleName);
}
 
Example #7
Source File: CuratorLeaderElectionManager.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void stop() {
    stopped = true;

    for (final Map.Entry<String, LeaderRole> entry : leaderRoles.entrySet()) {
        final LeaderRole role = entry.getValue();
        final LeaderSelector selector = role.getLeaderSelector();

        try {
            selector.close();
        } catch (final Exception e) {
            logger.warn("Failed to close Leader Selector for {}", entry.getKey(), e);
        }
    }

    leaderRoles.clear();

    if (curatorClient != null) {
        curatorClient.close();
        curatorClient = null;
    }

    logger.info("{} stopped and closed", this);
}
 
Example #8
Source File: CuratorLeaderSelector.java    From kylin with Apache License 2.0 5 votes vote down vote up
CuratorLeaderSelector(CuratorFramework client, String path, String name, JobEngineConfig jobEngineConfig) {
    this.name = name;
    this.leaderSelector = new LeaderSelector(client, path, this);
    this.leaderSelector.setId(name);
    this.leaderSelector.autoRequeue();
    this.jobEngineConfig = jobEngineConfig;
    this.defaultScheduler = DefaultScheduler.getInstance();
}
 
Example #9
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 #10
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 #11
Source File: MasterSelector.java    From javabase with Apache License 2.0 5 votes vote down vote up
public MasterSelector(String name, CuratorFramework client, String path) {
    this.name=name;
    //leaderSelector 多个客户端监听同一个节点
    this.leaderSelector=new LeaderSelector(client, path, this);
    //保证在此实例释放领导权之后还可能获得领导权
    leaderSelector.autoRequeue();
    //启动
    leaderSelector.start();
}
 
Example #12
Source File: SegmentContainerMonitor.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Monitor to manage pravega host addition and removal in the cluster.
 *
 * @param hostStore             The store to read and write the host container mapping data.
 * @param client                The curator client for coordination.
 * @param balancer              The host to segment container balancer implementation.
 * @param minRebalanceInterval  The minimum interval between any two rebalance operations in seconds.
 *                              0 indicates there can be no waits between retries.
 */
public SegmentContainerMonitor(HostControllerStore hostStore, CuratorFramework client, ContainerBalancer balancer,
        int minRebalanceInterval) {
    Preconditions.checkNotNull(hostStore, "hostStore");
    Preconditions.checkNotNull(client, "client");
    Preconditions.checkNotNull(balancer, "balancer");

    leaderZKPath = ZKPaths.makePath("cluster", "faulthandlerleader");

    segmentMonitorLeader = new SegmentMonitorLeader(hostStore, balancer, minRebalanceInterval);
    leaderSelector = new LeaderSelector(client, leaderZKPath, segmentMonitorLeader);

    //Listen for any zookeeper connectivity error and relinquish leadership.
    client.getConnectionStateListenable().addListener(
            (curatorClient, newState) -> {
                switch (newState) {
                    case LOST:
                        log.warn("Connection to zookeeper lost, attempting to interrrupt the leader thread");
                        leaderSelector.interruptLeadership();
                        break;
                    case SUSPENDED:
                        if (leaderSelector.hasLeadership()) {
                            log.info("Zookeeper session suspended, pausing the segment monitor");
                            segmentMonitorLeader.suspend();
                        }
                        break;
                    case RECONNECTED:
                        if (leaderSelector.hasLeadership()) {
                            log.info("Zookeeper session reconnected, resume the segment monitor");
                            segmentMonitorLeader.resume();
                        }
                        break;
                    //$CASES-OMITTED$
                    default:
                        log.debug("Connection state to zookeeper updated: " + newState.toString());
                }
            }
    );
}
 
Example #13
Source File: ReplicaSetLeaderSelector.java    From kylin with Apache License 2.0 5 votes vote down vote up
public ReplicaSetLeaderSelector(CuratorFramework client, Node currNode, int replicaSetID) {
    this.node = currNode;
    this.replicaSetID = replicaSetID;
    String path = StreamingUtils.REPLICASETS_LEADER_ELECT + "/" + replicaSetID;
    leaderSelector = new LeaderSelector(client, path, this);
    leaderSelector.autoRequeue();
    leaderChangeListeners = Lists.newArrayList();
}
 
Example #14
Source File: ClientSelector.java    From javabase with Apache License 2.0 5 votes vote down vote up
public ClientSelector(String name,CuratorFramework client, String path) {
    this.name=name;
    //leaderSelector 多个客户端监听同一个节点
    this.leaderSelector=new LeaderSelector(client, path, this);
    //保证在此实例释放领导权之后还可能获得领导权
    leaderSelector.autoRequeue();
    //启动
    leaderSelector.start();
}
 
Example #15
Source File: TransactionCommandDispatcher.java    From ByteJTA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void afterSingletonsInstantiated() {
	String basePath = String.format("%s/%s", this.workDirectory, this.getApplication());
	try {
		this.createPersistentPathIfNecessary(basePath);
	} catch (Exception ex) {
		throw new IllegalStateException(ex);
	}

	String masterPath = String.format("%s/master", basePath);
	this.leadSelector = new LeaderSelector(this.curatorFramework, masterPath, this);
	this.leadSelector.autoRequeue();
	this.leadSelector.start();
}
 
Example #16
Source File: ExampleClient.java    From curator with Apache License 2.0 5 votes vote down vote up
public ExampleClient(CuratorFramework client, String path, String name)
{
    this.name = name;

    // create a leader selector using the given path for management
    // all participants in a given leader selection must use the same path
    // ExampleClient here is also a LeaderSelectorListener but this isn't required
    leaderSelector = new LeaderSelector(client, path, this);

    // for most cases you will want your instance to requeue when it relinquishes leadership
    leaderSelector.autoRequeue();
}
 
Example #17
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 #18
Source File: ReplicaSetLeaderSelector.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public ReplicaSetLeaderSelector(CuratorFramework client, Node currNode, int replicaSetID) {
    this.node = currNode;
    this.replicaSetID = replicaSetID;
    String path = StreamingUtils.REPLICASETS_LEADER_ELECT + "/" + replicaSetID;
    leaderSelector = new LeaderSelector(client, path, this);
    leaderSelector.autoRequeue();
    leaderChangeListeners = Lists.newArrayList();
}
 
Example #19
Source File: ZkLeaderSelector.java    From binlake with Apache License 2.0 5 votes vote down vote up
public ZkLeaderSelector(String path,
                        String key,
                        ZKConfig zkConfig,
                        ServerConfig sc,
                        HttpConfig hc,
                        IWorkInitializer workIni) {
    this.workIni = workIni;

    this.client = CuratorFrameworkFactory.newClient(
            zkConfig.getServers(),
            new RetryNTimes(zkConfig.getRetryTimes(), zkConfig.getSleepMsBetweenRetries())
    );
    this.client.start();

    this.leaderSelector = new LeaderSelector(client, path, this);
    this.leaderSelector.autoRequeue();

    this.serverConf = sc;
    this.httpConf = hc;
    this.path = path;
    this.key = key;
    this.host = sc.getHost();

    this.zkConf = zkConfig;

    this.binlogInfoPath = path + ConstUtils.ZK_DYNAMIC_PATH;
    this.counterPath = path + ConstUtils.ZK_COUNTER_PATH;
    this.terminalPath = path + ConstUtils.ZK_TERMINAL_PATH;
    this.candidatePath = path + ConstUtils.ZK_CANDIDATE_PATH;
    this.leaderPath = path + ConstUtils.ZK_LEADER_PATH;
    this.errorPath = path + ConstUtils.ZK_ERROR_PATH;
    this.alarmPath = path + ConstUtils.ZK_ALARM_PATH;
}
 
Example #20
Source File: ZkLeaderSelector.java    From binlake with Apache License 2.0 5 votes vote down vote up
/**
 * 放弃leader 表明必须得停止dump
 * <p>
 * 停止dump 由外部调用 不是在这里调用 这里只是去除悬挂引用
 * <p>
 * 但是不关闭leader selector
 */
public void abandonLeaderShip() {
    LogUtils.debug.debug("abandonLeaderShip");
    LeaderSelector leaderSelector = this.leaderSelector;

    if (leaderSelector != null) {
        leaderSelector.interruptLeadership();
    }
    this.work = null;
    this.metaInfo = null;
}
 
Example #21
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 #22
Source File: ExampleClient.java    From xian with Apache License 2.0 5 votes vote down vote up
public ExampleClient(CuratorFramework client, String path, String name)
{
    this.name = name;

    // create a leader selector using the given path for management
    // all participants in a given leader selection must use the same path
    // ExampleClient here is also a LeaderSelectorListener but this isn't required
    leaderSelector = new LeaderSelector(client, path, this);

    // for most cases you will want your instance to requeue when it relinquishes leadership
    leaderSelector.autoRequeue();
}
 
Example #23
Source File: CuratorLeaderSelector.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
CuratorLeaderSelector(CuratorFramework client, String path, String name, JobEngineConfig jobEngineConfig) {
    this.name = name;
    this.leaderSelector = new LeaderSelector(client, path, this);
    this.leaderSelector.setId(name);
    this.leaderSelector.autoRequeue();
    this.jobEngineConfig = jobEngineConfig;
    this.defaultScheduler = DefaultScheduler.getInstance();
}
 
Example #24
Source File: ZkLeaderElection.java    From xian with Apache License 2.0 5 votes vote down vote up
/**
 * 如果参与了选举,那么退出主节点选举
 *
 * @deprecated curator的选举算法有问题,在最后一个唯一节点,同时也是主节点退出选举时,它抛出java.lang.InterruptedException。
 * 所以请直接依赖zk断开连接的方式退出节点选举,而不是调用本方法来退出选举
 */
public static void stop() {
    synchronized (lock) {
        if (singleton == null) return;
        LeaderSelector leaderSelector = singleton.leaderSelector;
        if (leaderSelector == null) {
            return;
        }
        LOG.info("节点退出zk选举");
        leaderSelector.close();
        singleton = null;
        LOG.info("退出选举 完毕");
    }
}
 
Example #25
Source File: ExampleClient.java    From ZKRecipesByExample with Apache License 2.0 4 votes vote down vote up
public ExampleClient(CuratorFramework client, String path, String name) {
	this.name = name;
	leaderSelector = new LeaderSelector(client, path, this);
	leaderSelector.autoRequeue();
}
 
Example #26
Source File: CuratorLeaderElectionManager.java    From nifi with Apache License 2.0 4 votes vote down vote up
public LeaderSelector getLeaderSelector() {
    return leaderSelector;
}
 
Example #27
Source File: CuratorLeaderElectionManager.java    From nifi with Apache License 2.0 4 votes vote down vote up
public LeaderRole(final LeaderSelector leaderSelector, final ElectionListener electionListener, final boolean participant) {
    this.leaderSelector = leaderSelector;
    this.electionListener = electionListener;
    this.participant = participant;
}
 
Example #28
Source File: LeaderSelectorClient.java    From Juice with GNU General Public License v3.0 4 votes vote down vote up
public LeaderSelectorClient(SchedulerDriver schedulerDriver) {
    leaderSelector = new LeaderSelector(schedulerDriver.getCuratorUtils().getClient(), ZKLOCKS + HTTP_SEPERATOR + MESOS_FRAMEWORK_TAG, this);
    leaderSelector.autoRequeue();
    this.schedulerDriver = schedulerDriver;

}
 
Example #29
Source File: ZkLeaderSelector.java    From binlake with Apache License 2.0 4 votes vote down vote up
public LeaderSelector getLeaderSelector() {
    return leaderSelector;
}
 
Example #30
Source File: SchedulerSelector.java    From workflow with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
LeaderSelector getLeaderSelector()
{
    return leaderSelector;
}