Search in sources :

Example 11 with ActiveStateChangeHandler

use of org.apache.atlas.listener.ActiveStateChangeHandler 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 12 with ActiveStateChangeHandler

use of org.apache.atlas.listener.ActiveStateChangeHandler in project incubator-atlas by apache.

the class ActiveInstanceElectorServiceTest method testElectionIsRejoinedWhenStateUpdateFails.

@Test
public void testElectionIsRejoinedWhenStateUpdateFails() 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);
    doThrow(new AtlasBaseException()).when(activeInstanceState).update("id1");
    ActiveInstanceElectorService activeInstanceElectorService = new ActiveInstanceElectorService(configuration, new HashSet<ActiveStateChangeHandler>(), curatorFactory, activeInstanceState, serviceState);
    activeInstanceElectorService.start();
    activeInstanceElectorService.isLeader();
    InOrder inOrder = inOrder(leaderLatch, curatorFactory);
    inOrder.verify(leaderLatch).close();
    inOrder.verify(curatorFactory).leaderLatchInstance("id1", HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    inOrder.verify(leaderLatch).addListener(activeInstanceElectorService);
    inOrder.verify(leaderLatch).start();
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) InOrder(org.mockito.InOrder) LeaderLatch(org.apache.curator.framework.recipes.leader.LeaderLatch) ActiveStateChangeHandler(org.apache.atlas.listener.ActiveStateChangeHandler) Test(org.testng.annotations.Test)

Example 13 with ActiveStateChangeHandler

use of org.apache.atlas.listener.ActiveStateChangeHandler in project incubator-atlas by apache.

the class ActiveInstanceElectorService method isLeader.

/**
     * Call all registered {@link ActiveStateChangeHandler}s on being elected active.
     *
     * In addition, shared state information about this instance becoming active is updated
     * using {@link ActiveInstanceState}.
     */
@Override
public void isLeader() {
    LOG.warn("Server instance with server id {} is elected as leader", serverId);
    serviceState.becomingActive();
    try {
        for (ActiveStateChangeHandler handler : activeStateChangeHandlers) {
            handler.instanceIsActive();
        }
        activeInstanceState.update(serverId);
        serviceState.setActive();
    } catch (Exception e) {
        LOG.error("Got exception while activating", e);
        notLeader();
        rejoinElection();
    }
}
Also used : IOException(java.io.IOException) AtlasException(org.apache.atlas.AtlasException) ActiveStateChangeHandler(org.apache.atlas.listener.ActiveStateChangeHandler)

Example 14 with ActiveStateChangeHandler

use of org.apache.atlas.listener.ActiveStateChangeHandler in project incubator-atlas by apache.

the class ActiveInstanceElectorService method notLeader.

/**
     * Call all registered {@link ActiveStateChangeHandler}s on becoming passive instance.
     */
@Override
public void notLeader() {
    LOG.warn("Server instance with server id {} is removed as leader", serverId);
    serviceState.becomingPassive();
    for (ActiveStateChangeHandler handler : activeStateChangeHandlers) {
        try {
            handler.instanceIsPassive();
        } catch (AtlasException e) {
            LOG.error("Error while reacting to passive state.", e);
        }
    }
    serviceState.setPassive();
}
Also used : AtlasException(org.apache.atlas.AtlasException) ActiveStateChangeHandler(org.apache.atlas.listener.ActiveStateChangeHandler)

Aggregations

ActiveStateChangeHandler (org.apache.atlas.listener.ActiveStateChangeHandler)14 Test (org.testng.annotations.Test)12 LeaderLatch (org.apache.curator.framework.recipes.leader.LeaderLatch)10 InOrder (org.mockito.InOrder)4 HashSet (java.util.HashSet)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 AtlasException (org.apache.atlas.AtlasException)2 IOException (java.io.IOException)1