Search in sources :

Example 6 with CuratorFrameworkWithUnhandledErrorListener

use of org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener 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 7 with CuratorFrameworkWithUnhandledErrorListener

use of org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener in project flink by apache.

the class ZooKeeperLeaderElectionConnectionHandlingTest method runTestWithZooKeeperConnectionProblem.

private void runTestWithZooKeeperConnectionProblem(Configuration configuration, BiConsumerWithException<TestingConnectionStateListener, TestingContender, Exception> validationLogic, Problem problem) throws Exception {
    CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper = ZooKeeperUtils.startCuratorFramework(configuration, fatalErrorHandlerResource.getFatalErrorHandler());
    CuratorFramework client = curatorFrameworkWrapper.asCuratorFramework();
    LeaderElectionDriverFactory leaderElectionDriverFactory = new ZooKeeperLeaderElectionDriverFactory(client, PATH);
    DefaultLeaderElectionService leaderElectionService = new DefaultLeaderElectionService(leaderElectionDriverFactory);
    try {
        final TestingConnectionStateListener connectionStateListener = new TestingConnectionStateListener();
        client.getConnectionStateListenable().addListener(connectionStateListener);
        final TestingContender contender = new TestingContender();
        leaderElectionService.start(contender);
        contender.awaitGrantLeadership();
        switch(problem) {
            case SUSPENDED_CONNECTION:
                zooKeeperResource.restart();
                break;
            case LOST_CONNECTION:
                zooKeeperResource.stop();
                break;
            default:
                throw new IllegalArgumentException(String.format("Unknown problem type %s.", problem));
        }
        validationLogic.accept(connectionStateListener, contender);
    } finally {
        leaderElectionService.stop();
        curatorFrameworkWrapper.close();
        if (problem == Problem.LOST_CONNECTION) {
            // in case of lost connections we accept that some unhandled error can occur
            fatalErrorHandlerResource.getFatalErrorHandler().clearError();
        }
    }
}
Also used : CuratorFramework(org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener)

Example 8 with CuratorFrameworkWithUnhandledErrorListener

use of org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener in project flink by apache.

the class ZooKeeperMultipleComponentLeaderElectionDriverTest method testLeaderElectionWithMultipleDrivers.

@Test
public void testLeaderElectionWithMultipleDrivers() throws Exception {
    final CuratorFrameworkWithUnhandledErrorListener curatorFramework = startCuratorFramework();
    try {
        Set<ElectionDriver> electionDrivers = Stream.generate(() -> createLeaderElectionDriver(curatorFramework.asCuratorFramework())).limit(3).collect(Collectors.toSet());
        while (!electionDrivers.isEmpty()) {
            final CompletableFuture<Object> anyLeader = CompletableFuture.anyOf(electionDrivers.stream().map(ElectionDriver::getLeadershipFuture).toArray(CompletableFuture[]::new));
            // wait for any leader
            anyLeader.join();
            final Map<Boolean, Set<ElectionDriver>> leaderAndRest = electionDrivers.stream().collect(Collectors.partitioningBy(ElectionDriver::hasLeadership, Collectors.toSet()));
            assertThat(leaderAndRest.get(true)).hasSize(1);
            Iterables.getOnlyElement(leaderAndRest.get(true)).close();
            electionDrivers = leaderAndRest.get(false);
        }
    } finally {
        curatorFramework.close();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Set(java.util.Set) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) Test(org.junit.jupiter.api.Test)

Example 9 with CuratorFrameworkWithUnhandledErrorListener

use of org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener in project flink by apache.

the class ZooKeeperJobGraphStoreWatcherTest method testJobGraphAddedAndRemovedShouldNotifyGraphStoreListener.

@Test
public void testJobGraphAddedAndRemovedShouldNotifyGraphStoreListener() throws Exception {
    try (final CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper = ZooKeeperUtils.startCuratorFramework(configuration, NoOpFatalErrorHandler.INSTANCE)) {
        final CuratorFramework client = curatorFrameworkWrapper.asCuratorFramework();
        final JobGraphStoreWatcher jobGraphStoreWatcher = createAndStartJobGraphStoreWatcher(client);
        final ZooKeeperStateHandleStore<JobGraph> stateHandleStore = createStateHandleStore(client);
        final JobGraph jobGraph = JobGraphTestUtils.emptyJobGraph();
        final JobID jobID = jobGraph.getJobID();
        stateHandleStore.addAndLock("/" + jobID, jobGraph);
        CommonTestUtils.waitUntilCondition(() -> testingJobGraphListener.getAddedJobGraphs().size() > 0, Deadline.fromNow(TIMEOUT));
        assertThat(testingJobGraphListener.getAddedJobGraphs(), contains(jobID));
        stateHandleStore.releaseAndTryRemove("/" + jobID);
        CommonTestUtils.waitUntilCondition(() -> testingJobGraphListener.getRemovedJobGraphs().size() > 0, Deadline.fromNow(TIMEOUT));
        assertThat(testingJobGraphListener.getRemovedJobGraphs(), contains(jobID));
        jobGraphStoreWatcher.stop();
    }
}
Also used : CuratorFramework(org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 10 with CuratorFrameworkWithUnhandledErrorListener

use of org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener in project flink by apache.

the class ZooKeeperCompletedCheckpointStoreTest method testDiscardingSubsumedCheckpoints.

/**
 * Tests that subsumed checkpoints are discarded.
 */
@Test
public void testDiscardingSubsumedCheckpoints() throws Exception {
    final SharedStateRegistry sharedStateRegistry = new SharedStateRegistryImpl();
    final Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, zooKeeperResource.getConnectString());
    final CuratorFrameworkWithUnhandledErrorListener curatorFrameworkWrapper = ZooKeeperUtils.startCuratorFramework(configuration, NoOpFatalErrorHandler.INSTANCE);
    final CompletedCheckpointStore checkpointStore = createZooKeeperCheckpointStore(curatorFrameworkWrapper.asCuratorFramework());
    try {
        final CompletedCheckpointStoreTest.TestCompletedCheckpoint checkpoint1 = CompletedCheckpointStoreTest.createCheckpoint(0, sharedStateRegistry);
        checkpointStore.addCheckpointAndSubsumeOldestOne(checkpoint1, new CheckpointsCleaner(), () -> {
        });
        assertThat(checkpointStore.getAllCheckpoints(), Matchers.contains(checkpoint1));
        final CompletedCheckpointStoreTest.TestCompletedCheckpoint checkpoint2 = CompletedCheckpointStoreTest.createCheckpoint(1, sharedStateRegistry);
        checkpointStore.addCheckpointAndSubsumeOldestOne(checkpoint2, new CheckpointsCleaner(), () -> {
        });
        final List<CompletedCheckpoint> allCheckpoints = checkpointStore.getAllCheckpoints();
        assertThat(allCheckpoints, Matchers.contains(checkpoint2));
        assertThat(allCheckpoints, Matchers.not(Matchers.contains(checkpoint1)));
        // verify that the subsumed checkpoint is discarded
        CompletedCheckpointStoreTest.verifyCheckpointDiscarded(checkpoint1);
    } finally {
        curatorFrameworkWrapper.close();
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) SharedStateRegistryImpl(org.apache.flink.runtime.state.SharedStateRegistryImpl) CuratorFrameworkWithUnhandledErrorListener(org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener) SharedStateRegistry(org.apache.flink.runtime.state.SharedStateRegistry) Test(org.junit.Test)

Aggregations

CuratorFrameworkWithUnhandledErrorListener (org.apache.flink.runtime.highavailability.zookeeper.CuratorFrameworkWithUnhandledErrorListener)18 Test (org.junit.Test)11 Configuration (org.apache.flink.configuration.Configuration)8 CuratorFramework (org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework)6 JobID (org.apache.flink.api.common.JobID)3 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)3 ZooKeeperLeaderRetrievalDriver (org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalDriver)3 KeeperException (org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException)3 Mockito.anyString (org.mockito.Mockito.anyString)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ObjectOutputStream (java.io.ObjectOutputStream)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeoutException (java.util.concurrent.TimeoutException)2 Nonnull (javax.annotation.Nonnull)2 CuratorFrameworkFactory (org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFrameworkFactory)2 ACLProvider (org.apache.flink.shaded.curator5.org.apache.curator.framework.api.ACLProvider)2 ExponentialBackoffRetry (org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry)2 ACL (org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.ACL)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1