Search in sources :

Example 1 with LeaderSelector

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();
}
Also used : LeaderSelectorListenerImpl(com.emc.storageos.coordinator.client.service.impl.LeaderSelectorListenerImpl) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

Example 2 with LeaderSelector

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();
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

Example 3 with LeaderSelector

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();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Participant(org.apache.curator.framework.recipes.leader.Participant) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) ConnectionState(org.apache.curator.framework.state.ConnectionState) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException)

Example 4 with LeaderSelector

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);
    }
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

Example 5 with LeaderSelector

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();
}
Also used : ReaperLeaderSelectorListener(com.emc.storageos.coordinator.client.service.impl.ReaperLeaderSelectorListener) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) ReaperLeaderSelectorListener(com.emc.storageos.coordinator.client.service.impl.ReaperLeaderSelectorListener) DrUtil(com.emc.storageos.coordinator.client.service.DrUtil) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) IOException(java.io.IOException) JMException(javax.management.JMException)

Aggregations

LeaderSelector (org.apache.curator.framework.recipes.leader.LeaderSelector)24 CuratorFramework (org.apache.curator.framework.CuratorFramework)7 LeaderSelectorListener (org.apache.curator.framework.recipes.leader.LeaderSelectorListener)6 IOException (java.io.IOException)5 ConnectionState (org.apache.curator.framework.state.ConnectionState)4 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)4 CancelLeadershipException (org.apache.curator.framework.recipes.leader.CancelLeadershipException)3 LeaderSelectorListenerAdapter (org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter)3 Test (org.junit.Test)3 Timing (org.apache.curator.test.Timing)2 KeeperException (org.apache.zookeeper.KeeperException)2 Test (org.testng.annotations.Test)2 BaseTest (com.baeldung.apache.curator.BaseTest)1 DrUtil (com.emc.storageos.coordinator.client.service.DrUtil)1 LeaderSelectorListenerImpl (com.emc.storageos.coordinator.client.service.impl.LeaderSelectorListenerImpl)1 ReaperLeaderSelectorListener (com.emc.storageos.coordinator.client.service.impl.ReaperLeaderSelectorListener)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1