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

The following examples show how to use org.apache.curator.framework.recipes.leader.LeaderSelector. These examples are extracted from open source projects. 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
@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 2
@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 3
Source Project: xian   Source File: ZkLeaderElection.java    License: 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 4
Source Project: spring-cloud-cluster   Source File: LeaderInitiator.java    License: 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 Project: workflow   Source File: SchedulerSelector.java    License: 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 6
Source Project: nifi   Source File: CuratorLeaderElectionManager.java    License: 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 Project: nifi   Source File: CuratorLeaderElectionManager.java    License: 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 Project: binlake   Source File: ZkLeaderSelector.java    License: 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 9
Source Project: binlake   Source File: ZkLeaderSelector.java    License: 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 10
Source Project: DDMQ   Source File: MasterElection.java    License: 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 11
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 12
Source Project: kylin-on-parquet-v2   Source File: CuratorLeaderSelector.java    License: 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 13
Source Project: xian   Source File: ZkLeaderElection.java    License: 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 14
Source Project: xian   Source File: ExampleClient.java    License: 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 15
Source Project: DDMQ   Source File: MasterElection.java    License: 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 16
Source Project: PoseidonX   Source File: LeaderSelecter.java    License: 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 17
Source Project: javabase   Source File: MasterSelector.java    License: 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 18
Source Project: javabase   Source File: ClientSelector.java    License: 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 19
Source Project: pravega   Source File: SegmentContainerMonitor.java    License: 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 20
Source Project: kylin   Source File: ReplicaSetLeaderSelector.java    License: 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 21
Source Project: kylin   Source File: CuratorLeaderSelector.java    License: 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 22
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 23
Source Project: curator   Source File: ExampleClient.java    License: 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 24
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 25
Source Project: binlake   Source File: ZkLeaderSelector.java    License: Apache License 2.0 4 votes vote down vote up
public LeaderSelector getLeaderSelector() {
    return leaderSelector;
}
 
Example 26
Source Project: kylin-on-parquet-v2   Source File: Coordinator.java    License: Apache License 2.0 4 votes vote down vote up
public CoordinatorLeaderSelector() {
    String path = StreamingUtils.COORDINATOR_LEAD;
    leaderSelector = new LeaderSelector(zkClient, path, this);
    leaderSelector.autoRequeue();
}
 
Example 27
Source Project: kylin-on-parquet-v2   Source File: StreamingCoordinator.java    License: Apache License 2.0 4 votes vote down vote up
public CoordinatorLeaderSelector() {
    String path = StreamingUtils.COORDINATOR_LEAD;
    leaderSelector = new LeaderSelector(zkClient, path, this);
    leaderSelector.autoRequeue();
}
 
Example 28
public LeaderRole(final LeaderSelector leaderSelector, final ElectionListener electionListener, final boolean participant) {
    this.leaderSelector = leaderSelector;
    this.electionListener = electionListener;
    this.participant = participant;
}
 
Example 29
public LeaderSelector getLeaderSelector() {
    return leaderSelector;
}
 
Example 30
Source Project: xian   Source File: ZkLeaderElection.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isLeader() {
    LeaderSelector leaderSelector = singleton.leaderSelector;
    if (leaderSelector != null) {
        return leaderSelector.hasLeadership();
    }
    LOG.warn("当前节点根本就没有参与选举,你为何要检查它是否是leader呢?", new Throwable());
    return false;
}