Search in sources :

Example 1 with ZooKeeperLeaderRetrievalService

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

the class ZooKeeperLeaderElectionTest method testMultipleLeaders.

/**
	 * 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 testMultipleLeaders() throws Exception {
    final String FAULTY_CONTENDER_URL = "faultyContender";
    final String leaderPath = "/leader";
    Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    configuration.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    configuration.setString(ConfigConstants.HA_ZOOKEEPER_LEADER_PATH, leaderPath);
    ZooKeeperLeaderElectionService leaderElectionService = null;
    ZooKeeperLeaderRetrievalService leaderRetrievalService = null;
    ZooKeeperLeaderRetrievalService leaderRetrievalService2 = null;
    TestingListener listener = new TestingListener();
    TestingListener listener2 = new TestingListener();
    TestingContender contender;
    try {
        leaderElectionService = ZooKeeperUtils.createLeaderElectionService(configuration);
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        leaderRetrievalService2 = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        contender = new TestingContender(TEST_URL, leaderElectionService);
        leaderElectionService.start(contender);
        leaderRetrievalService.start(listener);
        listener.waitForNewLeader(timeout.toMillis());
        assertEquals(listener.getLeaderSessionID(), contender.getLeaderSessionID());
        assertEquals(TEST_URL, listener.getAddress());
        CuratorFramework client = ZooKeeperUtils.startCuratorFramework(configuration);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeUTF(FAULTY_CONTENDER_URL);
        oos.writeObject(null);
        oos.close();
        // overwrite the current leader address, the leader should notice that and correct it
        boolean dataWritten = false;
        while (!dataWritten) {
            client.delete().forPath(leaderPath);
            try {
                client.create().forPath(leaderPath, baos.toByteArray());
                dataWritten = true;
            } catch (KeeperException.NodeExistsException e) {
            // this can happen if the leader election service was faster
            }
        }
        leaderRetrievalService2.start(listener2);
        listener2.waitForNewLeader(timeout.toMillis());
        if (FAULTY_CONTENDER_URL.equals(listener2.getAddress())) {
            listener2.waitForNewLeader(timeout.toMillis());
        }
        assertEquals(listener2.getLeaderSessionID(), contender.getLeaderSessionID());
        assertEquals(listener2.getAddress(), contender.getAddress());
    } finally {
        if (leaderElectionService != null) {
            leaderElectionService.stop();
        }
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
        if (leaderRetrievalService2 != null) {
            leaderRetrievalService2.stop();
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Configuration(org.apache.flink.configuration.Configuration) ZooKeeperLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 Configuration (org.apache.flink.configuration.Configuration)1 ZooKeeperLeaderRetrievalService (org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService)1 KeeperException (org.apache.zookeeper.KeeperException)1 Test (org.junit.Test)1