Search in sources :

Example 16 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project BRFS by zhangnianli.

the class CuratorLeaderSelectorClient method removeAllSelector.

/**
 * 概述:移除path下的所有leader监听器
 * @param path 监听的path
 * @user <a href=mailto:weizheng@bonree.com>魏征</a>
 */
public void removeAllSelector(String path) {
    List<LeaderSelector> leaderSelectors = selectorMap.get(path);
    if (leaderSelectors != null) {
        for (LeaderSelector selector : leaderSelectors) {
            selector.close();
        }
        leaderSelectors.clear();
        selectorMap.remove(path);
    }
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

Example 17 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project BRFS by zhangnianli.

the class CuratorLeaderSelectorClient method removeSelectorById.

/**
 * 概述:不能跨client移除,只能移除本client中的selector
 * @param path
 * @param id
 * @user <a href=mailto:weizheng@bonree.com>魏征</a>
 */
public void removeSelectorById(String path, String id) {
    List<LeaderSelector> leaderSelectors = selectorMap.get(path);
    if (leaderSelectors != null) {
        Iterator<LeaderSelector> it = leaderSelectors.iterator();
        while (it.hasNext()) {
            LeaderSelector selector = it.next();
            if (StringUtils.equals(selector.getId(), id)) {
                selector.close();
                it.remove();
            }
        }
        if (leaderSelectors.size() == 0) {
            selectorMap.remove(path);
        }
    }
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

Example 18 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project nifi by apache.

the class CuratorLeaderElectionManager method unregister.

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

Example 19 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project nifi by apache.

the class CuratorLeaderElectionManager method stop.

@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);
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) HashMap(java.util.HashMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException)

Example 20 with LeaderSelector

use of org.apache.curator.framework.recipes.leader.LeaderSelector in project coprhd-controller by CoprHD.

the class BackupScheduler method startLeaderSelector.

/**
 * Called when initializing Spring bean, make sure only one node(leader node) performs backup job
 */
public void startLeaderSelector() throws InterruptedException {
    while (!coordinator.getCoordinatorClient().isConnected()) {
        log.info("waiting for connecting to zookeeper");
        try {
            Thread.sleep(BackupConstants.BACKUP_WAINT_BEFORE_RETRY_ZK_CONN);
        } catch (InterruptedException e) {
            log.warn("Exception while sleeping,ignore", e);
            throw e;
        }
    }
    singletonInstance = this;
    if (drUtil.isStandby()) {
        log.info("Current site is standby, disable BackupScheduler");
        return;
    }
    this.cfg = new SchedulerConfig(coordinator, this.encryptionProvider, this.dbClient);
    LeaderSelector leaderSelector = coordinator.getCoordinatorClient().getLeaderSelector(coordinator.getCoordinatorClient().getSiteId(), BackupConstants.BACKUP_LEADER_PATH, new BackupLeaderSelectorListener());
    leaderSelector.autoRequeue();
    leaderSelector.start();
}
Also used : LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector)

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