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();
}
}
}
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();
}
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations