Search in sources :

Example 1 with LeaderContender

use of org.apache.flink.runtime.leaderelection.LeaderContender in project flink by apache.

the class SingleLeaderElectionServiceTest method testShutdown.

@Test
public void testShutdown() throws Exception {
    final UUID uuid = UUID.randomUUID();
    final SingleLeaderElectionService service = new SingleLeaderElectionService(executor, uuid);
    // create a leader contender and let it grab leadership
    final LeaderContender contender = mockContender(service);
    service.start(contender);
    verify(contender, times(1)).grantLeadership(uuid);
    // some leader listeners
    final LeaderRetrievalListener listener1 = mock(LeaderRetrievalListener.class);
    final LeaderRetrievalListener listener2 = mock(LeaderRetrievalListener.class);
    LeaderRetrievalService listenerService1 = service.createLeaderRetrievalService();
    LeaderRetrievalService listenerService2 = service.createLeaderRetrievalService();
    listenerService1.start(listener1);
    listenerService2.start(listener2);
    // one listener stops
    listenerService1.stop();
    // shut down the service
    service.shutdown();
    // the leader contender and running listener should get error notifications
    verify(contender, times(1)).handleError(any(Exception.class));
    verify(listener2, times(1)).handleError(any(Exception.class));
    // the stopped listener gets no notification
    verify(listener1, times(0)).handleError(any(Exception.class));
    // should not be possible to start again after shutdown
    try {
        service.start(contender);
        fail("should fail with an exception");
    } catch (IllegalStateException e) {
    // expected
    }
    // no additional leadership grant
    verify(contender, times(1)).grantLeadership(any(UUID.class));
}
Also used : LeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService) LeaderContender(org.apache.flink.runtime.leaderelection.LeaderContender) LeaderRetrievalListener(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalListener) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with LeaderContender

use of org.apache.flink.runtime.leaderelection.LeaderContender in project flink by apache.

the class SingleLeaderElectionServiceTest method mockContender.

private static LeaderContender mockContender(final LeaderElectionService service, final String address) {
    LeaderContender mockContender = mock(LeaderContender.class);
    when(mockContender.getAddress()).thenReturn(address);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            final UUID uuid = (UUID) invocation.getArguments()[0];
            service.confirmLeaderSessionID(uuid);
            return null;
        }
    }).when(mockContender).grantLeadership(any(UUID.class));
    return mockContender;
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) LeaderContender(org.apache.flink.runtime.leaderelection.LeaderContender) UUID(java.util.UUID)

Example 3 with LeaderContender

use of org.apache.flink.runtime.leaderelection.LeaderContender in project flink by apache.

the class SingleLeaderElectionServiceTest method testStartStopAssignLeadership.

// ------------------------------------------------------------------------
@Test
public void testStartStopAssignLeadership() throws Exception {
    final UUID uuid = UUID.randomUUID();
    final SingleLeaderElectionService service = new SingleLeaderElectionService(executor, uuid);
    final LeaderContender contender = mockContender(service);
    final LeaderContender otherContender = mockContender(service);
    service.start(contender);
    verify(contender, times(1)).grantLeadership(uuid);
    service.stop();
    verify(contender, times(1)).revokeLeadership();
    // start with a new contender - the old contender must not gain another leadership
    service.start(otherContender);
    verify(otherContender, times(1)).grantLeadership(uuid);
    verify(contender, times(1)).grantLeadership(uuid);
    verify(contender, times(1)).revokeLeadership();
}
Also used : LeaderContender(org.apache.flink.runtime.leaderelection.LeaderContender) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with LeaderContender

use of org.apache.flink.runtime.leaderelection.LeaderContender in project flink by apache.

the class SingleLeaderElectionServiceTest method testStopBeforeConfirmingLeadership.

@Test
public void testStopBeforeConfirmingLeadership() throws Exception {
    final UUID uuid = UUID.randomUUID();
    final SingleLeaderElectionService service = new SingleLeaderElectionService(executor, uuid);
    final LeaderContender contender = mock(LeaderContender.class);
    service.start(contender);
    verify(contender, times(1)).grantLeadership(uuid);
    service.stop();
    // because the leadership was never confirmed, there is no "revoke" call
    verifyNoMoreInteractions(contender);
}
Also used : LeaderContender(org.apache.flink.runtime.leaderelection.LeaderContender) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with LeaderContender

use of org.apache.flink.runtime.leaderelection.LeaderContender in project flink by apache.

the class SingleLeaderElectionServiceTest method testStartOnlyOnce.

@Test
public void testStartOnlyOnce() throws Exception {
    final UUID uuid = UUID.randomUUID();
    final SingleLeaderElectionService service = new SingleLeaderElectionService(executor, uuid);
    final LeaderContender contender = mock(LeaderContender.class);
    final LeaderContender otherContender = mock(LeaderContender.class);
    service.start(contender);
    verify(contender, times(1)).grantLeadership(uuid);
    // should not be possible to start again this with another contender
    try {
        service.start(otherContender);
        fail("should fail with an exception");
    } catch (IllegalStateException e) {
    // expected
    }
    // should not be possible to start this again with the same contender
    try {
        service.start(contender);
        fail("should fail with an exception");
    } catch (IllegalStateException e) {
    // expected
    }
}
Also used : LeaderContender(org.apache.flink.runtime.leaderelection.LeaderContender) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

UUID (java.util.UUID)8 LeaderContender (org.apache.flink.runtime.leaderelection.LeaderContender)8 Test (org.junit.Test)6 LeaderRetrievalListener (org.apache.flink.runtime.leaderretrieval.LeaderRetrievalListener)2 LeaderRetrievalService (org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Configuration (org.apache.flink.configuration.Configuration)1 LeaderElectionService (org.apache.flink.runtime.leaderelection.LeaderElectionService)1