use of org.apache.curator.framework.recipes.leader.LeaderSelector in project coprhd-controller by CoprHD.
the class ControllerServiceImpl method loadCustomConfigDefaults.
private void loadCustomConfigDefaults() {
LeaderSelectorListenerImpl executor = new LeaderSelectorListenerImpl() {
@Override
protected void startLeadership() throws Exception {
customConfigHandler.loadSystemCustomConfigs();
}
@Override
protected void stopLeadership() {
}
};
LeaderSelector service = _coordinator.getLeaderSelector(CUSTOM_CONFIG_PATH, executor);
service.autoRequeue();
service.start();
}
use of org.apache.curator.framework.recipes.leader.LeaderSelector in project coprhd-controller by CoprHD.
the class DisasterRecoveryService method startLeaderSelector.
private void startLeaderSelector() {
LeaderSelector leaderSelector = coordinator.getLeaderSelector(coordinator.getSiteId(), Constants.FAILBACK_DETECT_LEADER, new FailbackLeaderSelectorListener());
leaderSelector.autoRequeue();
leaderSelector.start();
}
use of org.apache.curator.framework.recipes.leader.LeaderSelector 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.LeaderSelector in project nifi by apache.
the class CuratorLeaderElectionManager method register.
@Override
public synchronized void register(final String roleName, final LeaderElectionStateChangeListener listener, final String participantId) {
logger.debug("{} Registering new Leader Selector for role {}", this, roleName);
// If we already have a Leader Role registered and either the Leader Role is participating in election,
// or the given participant id == null (don't want to participant in election) then we're done.
final LeaderRole currentRole = leaderRoles.get(roleName);
if (currentRole != null && (currentRole.isParticipant() || participantId == null)) {
logger.info("{} Attempted to register Leader Election for role '{}' but this role is already registered", this, roleName);
return;
}
final String leaderPath = getElectionPath(roleName);
try {
PathUtils.validatePath(leaderPath);
} catch (final IllegalArgumentException e) {
throw new IllegalStateException("Cannot register leader election for role '" + roleName + "' because this is not a valid role name");
}
registeredRoles.put(roleName, new RegisteredRole(participantId, listener));
final boolean isParticipant = participantId != null && !participantId.trim().isEmpty();
if (!isStopped()) {
final ElectionListener electionListener = new ElectionListener(roleName, listener);
final LeaderSelector leaderSelector = new LeaderSelector(curatorClient, leaderPath, leaderElectionMonitorEngine, electionListener);
if (isParticipant) {
leaderSelector.autoRequeue();
leaderSelector.setId(participantId);
leaderSelector.start();
}
final LeaderRole leaderRole = new LeaderRole(leaderSelector, electionListener, isParticipant);
leaderRoles.put(roleName, leaderRole);
}
if (isParticipant) {
logger.info("{} Registered new Leader Selector for role {}; this node is an active participant in the election.", this, roleName);
} else {
logger.info("{} Registered new Leader Selector for role {}; this node is a silent observer in the election.", this, roleName);
}
}
use of org.apache.curator.framework.recipes.leader.LeaderSelector 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();
}
Aggregations