Search in sources :

Example 1 with LeaderRetrievalDriver

use of org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver 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 LeaderRetrievalDriver

use of org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver 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 3 with LeaderRetrievalDriver

use of org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver 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)

Aggregations

LeaderRetrievalDriver (org.apache.flink.runtime.leaderretrieval.LeaderRetrievalDriver)3 TestingLeaderRetrievalEventHandler (org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler)3 ZooKeeperLeaderRetrievalDriver (org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver)3 Test (org.junit.Test)3 CuratorFrameworkWithUnhandledErrorListener (org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 TimeoutException (java.util.concurrent.TimeoutException)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