Search in sources :

Example 1 with DefaultLeaderRetrievalService

use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.

the class KubernetesMultipleComponentLeaderElectionDriverTest method testPublishLeaderInformation.

@Test
public void testPublishLeaderInformation() throws Exception {
    new TestFixture() {

        {
            runTest(() -> {
                leaderCallbackGrantLeadership();
                leaderElectionListener.await(LeaderElectionEvent.IsLeaderEvent.class);
                final LeaderInformation leaderInformation = LeaderInformation.known(UUID.randomUUID(), "localhost");
                final String componentId = "componentId";
                final DefaultLeaderRetrievalService leaderRetrievalService = new DefaultLeaderRetrievalService(new KubernetesMultipleComponentLeaderRetrievalDriverFactory(getFlinkKubeClient(), getConfigMapSharedWatcher(), testExecutorExtension.getExecutor(), LEADER_CONFIGMAP_NAME, componentId));
                final TestingListener leaderRetrievalListener = new TestingListener();
                leaderRetrievalService.start(leaderRetrievalListener);
                leaderElectionDriver.publishLeaderInformation(componentId, leaderInformation);
                notifyLeaderRetrievalWatchOnModifiedConfigMap();
                leaderRetrievalListener.waitForNewLeader(10_000L);
                assertThat(leaderRetrievalListener.getLeader()).isEqualTo(leaderInformation);
            });
        }
    };
}
Also used : TestingListener(org.apache.flink.runtime.leaderelection.TestingListener) LeaderElectionEvent(org.apache.flink.runtime.leaderelection.LeaderElectionEvent) DefaultLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultLeaderRetrievalService

use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.

the class ZooKeeperLeaderElectionTest method testZooKeeperReelectionWithReplacement.

/**
 * Tests the repeated reelection of {@link LeaderContender} once the current leader dies.
 * Furthermore, it tests that new LeaderElectionServices can be started later on and that they
 * successfully register at ZooKeeper and take part in the leader election.
 */
@Test
public void testZooKeeperReelectionWithReplacement() throws Exception {
    int num = 3;
    int numTries = 30;
    DefaultLeaderElectionService[] leaderElectionService = new DefaultLeaderElectionService[num];
    TestingContender[] contenders = new TestingContender[num];
    DefaultLeaderRetrievalService leaderRetrievalService = null;
    TestingListener listener = new TestingListener();
    try {
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(curatorFrameworkWrapper.asCuratorFramework());
        leaderRetrievalService.start(listener);
        for (int i = 0; i < num; i++) {
            leaderElectionService[i] = ZooKeeperUtils.createLeaderElectionService(curatorFrameworkWrapper.asCuratorFramework());
            contenders[i] = new TestingContender(LEADER_ADDRESS + "_" + i + "_0", leaderElectionService[i]);
            leaderElectionService[i].start(contenders[i]);
        }
        String pattern = LEADER_ADDRESS + "_" + "(\\d+)" + "_" + "(\\d+)";
        Pattern regex = Pattern.compile(pattern);
        for (int i = 0; i < numTries; i++) {
            listener.waitForNewLeader(timeout);
            String address = listener.getAddress();
            Matcher m = regex.matcher(address);
            if (m.find()) {
                int index = Integer.parseInt(m.group(1));
                int lastTry = Integer.parseInt(m.group(2));
                assertEquals(listener.getLeaderSessionID(), contenders[index].getLeaderSessionID());
                // stop leader election service = revoke leadership
                leaderElectionService[index].stop();
                // create new leader election service which takes part in the leader election
                leaderElectionService[index] = ZooKeeperUtils.createLeaderElectionService(curatorFrameworkWrapper.asCuratorFramework());
                contenders[index] = new TestingContender(LEADER_ADDRESS + "_" + index + "_" + (lastTry + 1), leaderElectionService[index]);
                leaderElectionService[index].start(contenders[index]);
            } else {
                throw new Exception("Did not find the leader's index.");
            }
        }
    } finally {
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
        for (DefaultLeaderElectionService electionService : leaderElectionService) {
            if (electionService != null) {
                electionService.stop();
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Mockito.anyString(org.mockito.Mockito.anyString) DefaultLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with DefaultLeaderRetrievalService

use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.

the class ZooKeeperLeaderElectionTest method testZooKeeperReelection.

/**
 * Tests repeatedly the reelection of still available LeaderContender. After a contender has
 * been elected as the leader, it is removed. This forces the DefaultLeaderElectionService to
 * elect a new leader.
 */
@Test
public void testZooKeeperReelection() throws Exception {
    Deadline deadline = Deadline.fromNow(Duration.ofMinutes(5L));
    int num = 10;
    DefaultLeaderElectionService[] leaderElectionService = new DefaultLeaderElectionService[num];
    TestingContender[] contenders = new TestingContender[num];
    DefaultLeaderRetrievalService leaderRetrievalService = null;
    TestingListener listener = new TestingListener();
    try {
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(curatorFrameworkWrapper.asCuratorFramework());
        LOG.debug("Start leader retrieval service for the TestingListener.");
        leaderRetrievalService.start(listener);
        for (int i = 0; i < num; i++) {
            leaderElectionService[i] = ZooKeeperUtils.createLeaderElectionService(curatorFrameworkWrapper.asCuratorFramework());
            contenders[i] = new TestingContender(createAddress(i), leaderElectionService[i]);
            LOG.debug("Start leader election service for contender #{}.", i);
            leaderElectionService[i].start(contenders[i]);
        }
        String pattern = LEADER_ADDRESS + "_" + "(\\d+)";
        Pattern regex = Pattern.compile(pattern);
        int numberSeenLeaders = 0;
        while (deadline.hasTimeLeft() && numberSeenLeaders < num) {
            LOG.debug("Wait for new leader #{}.", numberSeenLeaders);
            String address = listener.waitForNewLeader(deadline.timeLeft().toMillis());
            Matcher m = regex.matcher(address);
            if (m.find()) {
                int index = Integer.parseInt(m.group(1));
                TestingContender contender = contenders[index];
                // check that the retrieval service has retrieved the correct leader
                if (address.equals(createAddress(index)) && listener.getLeaderSessionID().equals(contender.getLeaderSessionID())) {
                    // kill the election service of the leader
                    LOG.debug("Stop leader election service of contender #{}.", numberSeenLeaders);
                    leaderElectionService[index].stop();
                    leaderElectionService[index] = null;
                    numberSeenLeaders++;
                }
            } else {
                fail("Did not find the leader's index.");
            }
        }
        assertFalse("Did not complete the leader reelection in time.", deadline.isOverdue());
        assertEquals(num, numberSeenLeaders);
    } finally {
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
        for (DefaultLeaderElectionService electionService : leaderElectionService) {
            if (electionService != null) {
                electionService.stop();
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Deadline(org.apache.flink.api.common.time.Deadline) Mockito.anyString(org.mockito.Mockito.anyString) DefaultLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService) Test(org.junit.Test)

Example 4 with DefaultLeaderRetrievalService

use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.

the class ZooKeeperMultipleComponentLeaderElectionDriverTest method testPublishEmptyLeaderInformation.

@Test
public void testPublishEmptyLeaderInformation() throws Exception {
    new Context() {

        {
            runTest(() -> {
                leaderElectionListener.await(LeaderElectionEvent.IsLeaderEvent.class);
                final String componentId = "retrieved-component";
                final DefaultLeaderRetrievalService defaultLeaderRetrievalService = new DefaultLeaderRetrievalService(new ZooKeeperLeaderRetrievalDriverFactory(curatorFramework.asCuratorFramework(), componentId, ZooKeeperLeaderRetrievalDriver.LeaderInformationClearancePolicy.ON_LOST_CONNECTION));
                final TestingListener leaderRetrievalListener = new TestingListener();
                defaultLeaderRetrievalService.start(leaderRetrievalListener);
                leaderElectionDriver.publishLeaderInformation(componentId, LeaderInformation.known(UUID.randomUUID(), "foobar"));
                leaderRetrievalListener.waitForNewLeader(10_000L);
                leaderElectionDriver.publishLeaderInformation(componentId, LeaderInformation.empty());
                leaderRetrievalListener.waitForEmptyLeaderInformation(10_000L);
                assertThat(leaderRetrievalListener.getLeader()).isEqualTo(LeaderInformation.empty());
            });
        }
    };
}
Also used : ZooKeeperLeaderRetrievalDriverFactory(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriverFactory) DefaultLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultLeaderRetrievalService

use of org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService in project flink by apache.

the class ZooKeeperMultipleComponentLeaderElectionDriverTest method testPublishLeaderInformation.

@Test
public void testPublishLeaderInformation() throws Exception {
    new Context() {

        {
            runTest(() -> {
                leaderElectionListener.await(LeaderElectionEvent.IsLeaderEvent.class);
                final String componentId = "retrieved-component";
                final DefaultLeaderRetrievalService defaultLeaderRetrievalService = new DefaultLeaderRetrievalService(new ZooKeeperLeaderRetrievalDriverFactory(curatorFramework.asCuratorFramework(), componentId, ZooKeeperLeaderRetrievalDriver.LeaderInformationClearancePolicy.ON_LOST_CONNECTION));
                final TestingListener leaderRetrievalListener = new TestingListener();
                defaultLeaderRetrievalService.start(leaderRetrievalListener);
                final LeaderInformation leaderInformation = LeaderInformation.known(UUID.randomUUID(), "foobar");
                leaderElectionDriver.publishLeaderInformation(componentId, leaderInformation);
                leaderRetrievalListener.waitForNewLeader(10_000L);
                assertThat(leaderRetrievalListener.getLeader()).isEqualTo(leaderInformation);
            });
        }
    };
}
Also used : ZooKeeperLeaderRetrievalDriverFactory(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriverFactory) DefaultLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultLeaderRetrievalService (org.apache.flink.runtime.leaderretrieval.DefaultLeaderRetrievalService)6 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Deadline (org.apache.flink.api.common.time.Deadline)2 ZooKeeperLeaderRetrievalDriverFactory (org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriverFactory)2 Mockito.anyString (org.mockito.Mockito.anyString)2 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeoutException (java.util.concurrent.TimeoutException)1 Configuration (org.apache.flink.configuration.Configuration)1 DispatcherGateway (org.apache.flink.runtime.dispatcher.DispatcherGateway)1 CuratorFrameworkWithUnhandledErrorListener (org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener)1 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)1 JobResult (org.apache.flink.runtime.jobmaster.JobResult)1 LeaderElectionEvent (org.apache.flink.runtime.leaderelection.LeaderElectionEvent)1 LeaderInformation (org.apache.flink.runtime.leaderelection.LeaderInformation)1 TestingListener (org.apache.flink.runtime.leaderelection.TestingListener)1 TestingMiniCluster (org.apache.flink.runtime.minicluster.TestingMiniCluster)1