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