Search in sources :

Example 1 with TestingLeaderRetrievalEventHandler

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

the class ZooKeeperLeaderElectionTest method testLeaderShouldBeCorrectedWhenOverwritten.

/**
 * Tests that the current leader is notified when his leader connection information in ZooKeeper
 * are overwritten. The leader must re-establish the correct leader connection information in
 * ZooKeeper.
 */
@Test
public void testLeaderShouldBeCorrectedWhenOverwritten() throws Exception {
    final String faultyContenderUrl = "faultyContender";
    final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
    final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
    ZooKeeperLeaderElectionDriver leaderElectionDriver = null;
    LeaderRetrievalDriver leaderRetrievalDriver = null;
    CuratorFrameworkWithUnhandledErrorListener anotherCuratorFrameworkWrapper = null;
    try {
        leaderElectionDriver = createAndInitLeaderElectionDriver(curatorFrameworkWrapper.asCuratorFramework(), electionEventHandler);
        electionEventHandler.waitForLeader(timeout);
        final LeaderInformation confirmedLeaderInformation = electionEventHandler.getConfirmedLeaderInformation();
        assertThat(confirmedLeaderInformation.getLeaderAddress(), is(LEADER_ADDRESS));
        anotherCuratorFrameworkWrapper = ZooKeeperUtils.startCuratorFramework(configuration, NoOpFatalErrorHandler.INSTANCE);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeUTF(faultyContenderUrl);
        oos.writeObject(UUID.randomUUID());
        oos.close();
        // overwrite the current leader address, the leader should notice that
        boolean dataWritten = false;
        final String connectionInformationPath = leaderElectionDriver.getConnectionInformationPath();
        while (!dataWritten) {
            anotherCuratorFrameworkWrapper.asCuratorFramework().delete().forPath(connectionInformationPath);
            try {
                anotherCuratorFrameworkWrapper.asCuratorFramework().create().forPath(connectionInformationPath, baos.toByteArray());
                dataWritten = true;
            } catch (KeeperException.NodeExistsException e) {
            // this can happen if the leader election service was faster
            }
        }
        // The faulty leader should be corrected on ZooKeeper
        leaderRetrievalDriver = ZooKeeperUtils.createLeaderRetrievalDriverFactory(curatorFrameworkWrapper.asCuratorFramework()).createLeaderRetrievalDriver(retrievalEventHandler, retrievalEventHandler::handleError);
        if (retrievalEventHandler.waitForNewLeader(timeout).equals(faultyContenderUrl)) {
            retrievalEventHandler.waitForNewLeader(timeout);
        }
        assertThat(retrievalEventHandler.getLeaderSessionID(), is(confirmedLeaderInformation.getLeaderSessionID()));
        assertThat(retrievalEventHandler.getAddress(), is(confirmedLeaderInformation.getLeaderAddress()));
    } finally {
        electionEventHandler.close();
        if (leaderElectionDriver != null) {
            leaderElectionDriver.close();
        }
        if (leaderRetrievalDriver != null) {
            leaderRetrievalDriver.close();
        }
        if (anotherCuratorFrameworkWrapper != null) {
            anotherCuratorFrameworkWrapper.close();
        }
    }
}
Also used : ZooKeeperLeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver) LeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) Mockito.anyString(org.mockito.Mockito.anyString) TestingLeaderRetrievalEventHandler(org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) KeeperException(org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 2 with TestingLeaderRetrievalEventHandler

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

the class KubernetesLeaderElectionAndRetrievalITCase method testLeaderElectionAndRetrieval.

@Test
public void testLeaderElectionAndRetrieval() throws Exception {
    final String configMapName = LEADER_CONFIGMAP_NAME + System.currentTimeMillis();
    KubernetesLeaderElectionDriver leaderElectionDriver = null;
    KubernetesLeaderRetrievalDriver leaderRetrievalDriver = null;
    final FlinkKubeClient flinkKubeClient = kubernetesResource.getFlinkKubeClient();
    final Configuration configuration = kubernetesResource.getConfiguration();
    final String clusterId = configuration.getString(KubernetesConfigOptions.CLUSTER_ID);
    final KubernetesConfigMapSharedWatcher configMapSharedWatcher = flinkKubeClient.createConfigMapSharedWatcher(KubernetesUtils.getConfigMapLabels(clusterId, LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY));
    final ExecutorService watchExecutorService = Executors.newCachedThreadPool();
    final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
    try {
        leaderElectionDriver = new KubernetesLeaderElectionDriver(flinkKubeClient, configMapSharedWatcher, watchExecutorService, new KubernetesLeaderElectionConfiguration(configMapName, UUID.randomUUID().toString(), configuration), electionEventHandler, electionEventHandler::handleError);
        electionEventHandler.init(leaderElectionDriver);
        final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
        leaderRetrievalDriver = new KubernetesLeaderRetrievalDriver(flinkKubeClient, configMapSharedWatcher, watchExecutorService, configMapName, retrievalEventHandler, KubernetesUtils::getLeaderInformationFromConfigMap, retrievalEventHandler::handleError);
        electionEventHandler.waitForLeader(TIMEOUT);
        // Check the new leader is confirmed
        final LeaderInformation confirmedLeaderInformation = electionEventHandler.getConfirmedLeaderInformation();
        assertThat(confirmedLeaderInformation.getLeaderAddress(), is(LEADER_ADDRESS));
        // Check the leader retrieval driver should be notified the leader address
        retrievalEventHandler.waitForNewLeader(TIMEOUT);
        assertThat(retrievalEventHandler.getLeaderSessionID(), is(confirmedLeaderInformation.getLeaderSessionID()));
        assertThat(retrievalEventHandler.getAddress(), is(confirmedLeaderInformation.getLeaderAddress()));
    } finally {
        electionEventHandler.close();
        if (leaderElectionDriver != null) {
            leaderElectionDriver.close();
        }
        if (leaderRetrievalDriver != null) {
            leaderRetrievalDriver.close();
        }
        flinkKubeClient.deleteConfigMap(configMapName).get();
        configMapSharedWatcher.close();
        ExecutorUtils.gracefulShutdown(5, TimeUnit.SECONDS, watchExecutorService);
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestingLeaderElectionEventHandler(org.apache.flink.runtime.leaderelection.TestingLeaderElectionEventHandler) KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) ExecutorService(java.util.concurrent.ExecutorService) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestingLeaderRetrievalEventHandler(org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Test(org.junit.Test)

Example 3 with TestingLeaderRetrievalEventHandler

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

the class ZooKeeperLeaderElectionTest method testZooKeeperLeaderElectionRetrieval.

/**
 * Tests that the ZooKeeperLeaderElection/RetrievalService return both the correct URL.
 */
@Test
public void testZooKeeperLeaderElectionRetrieval() throws Exception {
    final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
    final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
    LeaderElectionDriver leaderElectionDriver = null;
    LeaderRetrievalDriver leaderRetrievalDriver = null;
    try {
        leaderElectionDriver = createAndInitLeaderElectionDriver(curatorFrameworkWrapper.asCuratorFramework(), electionEventHandler);
        leaderRetrievalDriver = ZooKeeperUtils.createLeaderRetrievalDriverFactory(curatorFrameworkWrapper.asCuratorFramework()).createLeaderRetrievalDriver(retrievalEventHandler, retrievalEventHandler::handleError);
        electionEventHandler.waitForLeader(timeout);
        final LeaderInformation confirmedLeaderInformation = electionEventHandler.getConfirmedLeaderInformation();
        assertThat(confirmedLeaderInformation.getLeaderAddress(), is(LEADER_ADDRESS));
        retrievalEventHandler.waitForNewLeader(timeout);
        assertThat(retrievalEventHandler.getLeaderSessionID(), is(confirmedLeaderInformation.getLeaderSessionID()));
        assertThat(retrievalEventHandler.getAddress(), is(confirmedLeaderInformation.getLeaderAddress()));
    } finally {
        electionEventHandler.close();
        if (leaderElectionDriver != null) {
            leaderElectionDriver.close();
        }
        if (leaderRetrievalDriver != null) {
            leaderRetrievalDriver.close();
        }
    }
}
Also used : ZooKeeperLeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver) LeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver) TestingLeaderRetrievalEventHandler(org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler) Test(org.junit.Test)

Example 4 with TestingLeaderRetrievalEventHandler

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

the class ZooKeeperLeaderElectionTest method testEphemeralZooKeeperNodes.

/**
 * Tests that there is no information left in the ZooKeeper cluster after the ZooKeeper client
 * has terminated. In other words, checks that the ZooKeeperLeaderElection service uses
 * ephemeral nodes.
 */
@Test
public void testEphemeralZooKeeperNodes() throws Exception {
    ZooKeeperLeaderElectionDriver leaderElectionDriver = null;
    LeaderRetrievalDriver leaderRetrievalDriver = null;
    final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
    final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
    CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper = null;
    CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper2 = null;
    NodeCache cache = null;
    try {
        curatorFrameworkWrapper = ZooKeeperUtils.startCuratorFramework(configuration, testingFatalErrorHandlerResource.getFatalErrorHandler());
        curatorFrameworkWrapper2 = ZooKeeperUtils.startCuratorFramework(configuration, testingFatalErrorHandlerResource.getFatalErrorHandler());
        leaderElectionDriver = createAndInitLeaderElectionDriver(curatorFrameworkWrapper.asCuratorFramework(), electionEventHandler);
        leaderRetrievalDriver = ZooKeeperUtils.createLeaderRetrievalDriverFactory(curatorFrameworkWrapper2.asCuratorFramework()).createLeaderRetrievalDriver(retrievalEventHandler, retrievalEventHandler::handleError);
        cache = new NodeCache(curatorFrameworkWrapper2.asCuratorFramework(), leaderElectionDriver.getConnectionInformationPath());
        ExistsCacheListener existsListener = new ExistsCacheListener(cache);
        DeletedCacheListener deletedCacheListener = new DeletedCacheListener(cache);
        cache.getListenable().addListener(existsListener);
        cache.start();
        electionEventHandler.waitForLeader(timeout);
        retrievalEventHandler.waitForNewLeader(timeout);
        Future<Boolean> existsFuture = existsListener.nodeExists();
        existsFuture.get(timeout, TimeUnit.MILLISECONDS);
        cache.getListenable().addListener(deletedCacheListener);
        leaderElectionDriver.close();
        // now stop the underlying client
        curatorFrameworkWrapper.close();
        Future<Boolean> deletedFuture = deletedCacheListener.nodeDeleted();
        // make sure that the leader node has been deleted
        deletedFuture.get(timeout, TimeUnit.MILLISECONDS);
        try {
            retrievalEventHandler.waitForNewLeader(1000L);
            fail("TimeoutException was expected because there is no leader registered and " + "thus there shouldn't be any leader information in ZooKeeper.");
        } catch (TimeoutException e) {
        // that was expected
        }
    } finally {
        electionEventHandler.close();
        if (leaderRetrievalDriver != null) {
            leaderRetrievalDriver.close();
        }
        if (cache != null) {
            cache.close();
        }
        if (curatorFrameworkWrapper2 != null) {
            curatorFrameworkWrapper2.close();
        }
    }
}
Also used : ZooKeeperLeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver) LeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver) NodeCache(org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.NodeCache) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) TestingLeaderRetrievalEventHandler(org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 5 with TestingLeaderRetrievalEventHandler

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

the class ZooKeeperLeaderElectionTest method testNotLeaderShouldNotCleanUpTheLeaderInformation.

@Test
public void testNotLeaderShouldNotCleanUpTheLeaderInformation() throws Exception {
    final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
    final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
    ZooKeeperLeaderElectionDriver leaderElectionDriver = null;
    ZooKeeperLeaderRetrievalDriver leaderRetrievalDriver = null;
    try {
        leaderElectionDriver = createAndInitLeaderElectionDriver(curatorFrameworkWrapper.asCuratorFramework(), electionEventHandler);
        electionEventHandler.waitForLeader(timeout);
        final LeaderInformation confirmedLeaderInformation = electionEventHandler.getConfirmedLeaderInformation();
        assertThat(confirmedLeaderInformation.getLeaderAddress(), is(LEADER_ADDRESS));
        // Leader is revoked
        leaderElectionDriver.notLeader();
        electionEventHandler.waitForRevokeLeader(timeout);
        assertThat(electionEventHandler.getConfirmedLeaderInformation(), is(LeaderInformation.empty()));
        // The data on ZooKeeper it not be cleared
        leaderRetrievalDriver = ZooKeeperUtils.createLeaderRetrievalDriverFactory(curatorFrameworkWrapper.asCuratorFramework()).createLeaderRetrievalDriver(retrievalEventHandler, retrievalEventHandler::handleError);
        retrievalEventHandler.waitForNewLeader(timeout);
        assertThat(retrievalEventHandler.getLeaderSessionID(), is(confirmedLeaderInformation.getLeaderSessionID()));
        assertThat(retrievalEventHandler.getAddress(), is(confirmedLeaderInformation.getLeaderAddress()));
    } finally {
        electionEventHandler.close();
        if (leaderElectionDriver != null) {
            leaderElectionDriver.close();
        }
        if (leaderRetrievalDriver != null) {
            leaderRetrievalDriver.close();
        }
    }
}
Also used : ZooKeeperLeaderRetrievalDriver(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver) TestingLeaderRetrievalEventHandler(org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler) Test(org.junit.Test)

Aggregations

TestingLeaderRetrievalEventHandler (org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler)5 Test (org.junit.Test)5 ZooKeeperLeaderRetrievalDriver (org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver)4 LeaderRetrievalDriver (org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver)3 CuratorFrameworkWithUnhandledErrorListener (org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 Configuration (org.apache.flink.configuration.Configuration)1 KubernetesLeaderElectionConfiguration (org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration)1 FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)1 KubernetesConfigMapSharedWatcher (org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher)1 LeaderInformation (org.apache.flink.runtime.leaderelection.LeaderInformation)1 TestingLeaderElectionEventHandler (org.apache.flink.runtime.leaderelection.TestingLeaderElectionEventHandler)1 NodeCache (org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.NodeCache)1 KeeperException (org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException)1 Mockito.anyString (org.mockito.Mockito.anyString)1