Search in sources :

Example 1 with LeaderLatch

use of org.apache.curator.framework.recipes.leader.LeaderLatch in project hadoop by apache.

the class CuratorBasedElectorService method initAndStartLeaderLatch.

private void initAndStartLeaderLatch() throws Exception {
    leaderLatch = new LeaderLatch(curator, latchPath, rmId);
    leaderLatch.addListener(this);
    leaderLatch.start();
}
Also used : LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch)

Example 2 with LeaderLatch

use of org.apache.curator.framework.recipes.leader.LeaderLatch in project storm by apache.

the class LeaderElectorImp method addToLeaderLockQueue.

@Override
public void addToLeaderLockQueue() throws Exception {
    // if this latch is already closed, we need to create new instance.
    if (LeaderLatch.State.CLOSED.equals(leaderLatch.get().getState())) {
        leaderLatch.set(new LeaderLatch(zk, leaderlockPath));
        leaderLatchListener.set(Zookeeper.leaderLatchListenerImpl(conf, zk, blobStore, leaderLatch.get()));
        LOG.info("LeaderLatch was in closed state. Resetted the leaderLatch and listeners.");
    }
    // Only if the latch is not already started we invoke start
    if (LeaderLatch.State.LATENT.equals(leaderLatch.get().getState())) {
        leaderLatch.get().addListener(leaderLatchListener.get());
        leaderLatch.get().start();
        LOG.info("Queued up for leader lock.");
    } else {
        LOG.info("Node already in queue for leader lock.");
    }
}
Also used : LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch)

Example 3 with LeaderLatch

use of org.apache.curator.framework.recipes.leader.LeaderLatch in project storm by apache.

the class Zookeeper method zkLeaderElectorImpl.

protected ILeaderElector zkLeaderElectorImpl(Map conf, BlobStore blobStore) throws UnknownHostException {
    List<String> servers = (List<String>) conf.get(Config.STORM_ZOOKEEPER_SERVERS);
    Object port = conf.get(Config.STORM_ZOOKEEPER_PORT);
    CuratorFramework zk = mkClientImpl(conf, servers, port, "", conf);
    String leaderLockPath = conf.get(Config.STORM_ZOOKEEPER_ROOT) + "/leader-lock";
    String id = NimbusInfo.fromConf(conf).toHostPortString();
    AtomicReference<LeaderLatch> leaderLatchAtomicReference = new AtomicReference<>(new LeaderLatch(zk, leaderLockPath, id));
    AtomicReference<LeaderLatchListener> leaderLatchListenerAtomicReference = new AtomicReference<>(leaderLatchListenerImpl(conf, zk, blobStore, leaderLatchAtomicReference.get()));
    return new LeaderElectorImp(conf, servers, zk, leaderLockPath, id, leaderLatchAtomicReference, leaderLatchListenerAtomicReference, blobStore);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) LeaderLatchListener(org.apache.curator.framework.recipes.leader.LeaderLatchListener)

Example 4 with LeaderLatch

use of org.apache.curator.framework.recipes.leader.LeaderLatch in project elastic-job by dangdangdotcom.

the class JobNodeStorage method executeInLeader.

/**
     * 在主节点执行操作.
     * 
     * @param latchNode 分布式锁使用的作业节点名称
     * @param callback 执行操作的回调
     */
public void executeInLeader(final String latchNode, final LeaderExecutionCallback callback) {
    try (LeaderLatch latch = new LeaderLatch(getClient(), jobNodePath.getFullPath(latchNode))) {
        latch.start();
        latch.await();
        callback.execute();
    //CHECKSTYLE:OFF
    } catch (final Exception ex) {
        //CHECKSTYLE:ON
        handleException(ex);
    }
}
Also used : LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) JobSystemException(com.dangdang.ddframe.job.exception.JobSystemException)

Example 5 with LeaderLatch

use of org.apache.curator.framework.recipes.leader.LeaderLatch in project incubator-atlas by apache.

the class ActiveInstanceElectorServiceTest method testRegisteredHandlersAreNotifiedOfPassiveWhenInstanceIsPassive.

@Test
public void testRegisteredHandlersAreNotifiedOfPassiveWhenInstanceIsPassive() throws AtlasException {
    when(configuration.containsKey(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getBoolean(HAConfiguration.ATLAS_SERVER_HA_ENABLED_KEY)).thenReturn(true);
    when(configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS)).thenReturn(new String[] { "id1" });
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX + "id1")).thenReturn("127.0.0.1:21000");
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    LeaderLatch leaderLatch = mock(LeaderLatch.class);
    when(curatorFactory.leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(leaderLatch);
    Set<ActiveStateChangeHandler> changeHandlers = new HashSet<>();
    final ActiveStateChangeHandler handler1 = mock(ActiveStateChangeHandler.class);
    final ActiveStateChangeHandler handler2 = mock(ActiveStateChangeHandler.class);
    changeHandlers.add(handler1);
    changeHandlers.add(handler2);
    ActiveInstanceElectorService activeInstanceElectorService = new ActiveInstanceElectorService(configuration, changeHandlers, curatorFactory, activeInstanceState, serviceState);
    activeInstanceElectorService.start();
    activeInstanceElectorService.notLeader();
    verify(handler1).instanceIsPassive();
    verify(handler2).instanceIsPassive();
}
Also used : LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) HashSet(java.util.HashSet) ActiveStateChangeHandler(org.apache.atlas.listener.ActiveStateChangeHandler) Test(org.testng.annotations.Test)

Aggregations

LeaderLatch (org.apache.curator.framework.recipes.leader.LeaderLatch)16 ActiveStateChangeHandler (org.apache.atlas.listener.ActiveStateChangeHandler)10 Test (org.testng.annotations.Test)10 HashSet (java.util.HashSet)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 LeaderLatchListener (org.apache.curator.framework.recipes.leader.LeaderLatchListener)2 InOrder (org.mockito.InOrder)2 JobSystemException (com.dangdang.ddframe.job.exception.JobSystemException)1 IOException (java.io.IOException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 Participant (org.apache.curator.framework.recipes.leader.Participant)1