Search in sources :

Example 11 with LeaderLatch

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

the class ActiveInstanceElectorServiceTest method testRegisteredHandlersAreNotifiedOfPassiveWhenStateUpdateFails.

@Test
public void testRegisteredHandlersAreNotifiedOfPassiveWhenStateUpdateFails() throws Exception {
    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);
    doThrow(new AtlasBaseException()).when(activeInstanceState).update("id1");
    ActiveInstanceElectorService activeInstanceElectorService = new ActiveInstanceElectorService(configuration, changeHandlers, curatorFactory, activeInstanceState, serviceState);
    activeInstanceElectorService.start();
    activeInstanceElectorService.isLeader();
    verify(handler1).instanceIsPassive();
    verify(handler2).instanceIsPassive();
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) HashSet(java.util.HashSet) ActiveStateChangeHandler(org.apache.atlas.listener.ActiveStateChangeHandler) Test(org.testng.annotations.Test)

Example 12 with LeaderLatch

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

the class ActiveInstanceElectorServiceTest method testCuratorFactoryIsClosedOnStop.

@Test
public void testCuratorFactoryIsClosedOnStop() 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);
    ActiveInstanceElectorService activeInstanceElectorService = new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory, activeInstanceState, serviceState);
    activeInstanceElectorService.start();
    activeInstanceElectorService.stop();
    verify(curatorFactory).close();
}
Also used : LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) ActiveStateChangeHandler(org.apache.atlas.listener.ActiveStateChangeHandler) Test(org.testng.annotations.Test)

Example 13 with LeaderLatch

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

the class ActiveInstanceElectorServiceTest method testLeaderElectionIsJoinedOnStart.

@Test
public void testLeaderElectionIsJoinedOnStart() throws Exception {
    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);
    ActiveInstanceElectorService activeInstanceElectorService = new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory, activeInstanceState, serviceState);
    activeInstanceElectorService.start();
    verify(leaderLatch).start();
}
Also used : LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) ActiveStateChangeHandler(org.apache.atlas.listener.ActiveStateChangeHandler) Test(org.testng.annotations.Test)

Example 14 with LeaderLatch

use of org.apache.curator.framework.recipes.leader.LeaderLatch in project druid by druid-io.

the class DruidCoordinator method createNewLeaderLatch.

private LeaderLatch createNewLeaderLatch() {
    final LeaderLatch newLeaderLatch = new LeaderLatch(curator, ZKPaths.makePath(zkPaths.getCoordinatorPath(), COORDINATOR_OWNER_NODE), self.getHostAndPort());
    newLeaderLatch.addListener(new LeaderLatchListener() {

        @Override
        public void isLeader() {
            DruidCoordinator.this.becomeLeader();
        }

        @Override
        public void notLeader() {
            DruidCoordinator.this.stopBeingLeader();
        }
    }, Execs.singleThreaded("CoordinatorLeader-%s"));
    return leaderLatch.getAndSet(newLeaderLatch);
}
Also used : LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) LeaderLatchListener(org.apache.curator.framework.recipes.leader.LeaderLatchListener)

Example 15 with LeaderLatch

use of org.apache.curator.framework.recipes.leader.LeaderLatch in project druid by druid-io.

the class DruidCoordinator method getCurrentLeader.

public String getCurrentLeader() {
    try {
        final LeaderLatch latch = leaderLatch.get();
        if (latch == null) {
            return null;
        }
        Participant participant = latch.getLeader();
        if (participant.isLeader()) {
            return participant.getId();
        }
        return null;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Participant(org.apache.curator.framework.recipes.leader.Participant) LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) IOException(java.io.IOException)

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