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();
}
}
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();
}
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);
}
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();
}
}
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启动完毕.");
}
}
Aggregations