use of org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler 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.TestingLeaderRetrievalEventHandler in project flink by apache.
the class KubernetesLeaderElectionAndRetrievalITCase method testLeaderElectionAndRetrieval.
@Test
public void testLeaderElectionAndRetrieval() throws Exception {
final String configMapName = LEADER_CONFIGMAP_NAME + System.currentTimeMillis();
KubernetesLeaderElectionDriver leaderElectionDriver = null;
KubernetesLeaderRetrievalDriver leaderRetrievalDriver = null;
final FlinkKubeClient flinkKubeClient = kubernetesResource.getFlinkKubeClient();
final Configuration configuration = kubernetesResource.getConfiguration();
final String clusterId = configuration.getString(KubernetesConfigOptions.CLUSTER_ID);
final KubernetesConfigMapSharedWatcher configMapSharedWatcher = flinkKubeClient.createConfigMapSharedWatcher(KubernetesUtils.getConfigMapLabels(clusterId, LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY));
final ExecutorService watchExecutorService = Executors.newCachedThreadPool();
final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
try {
leaderElectionDriver = new KubernetesLeaderElectionDriver(flinkKubeClient, configMapSharedWatcher, watchExecutorService, new KubernetesLeaderElectionConfiguration(configMapName, UUID.randomUUID().toString(), configuration), electionEventHandler, electionEventHandler::handleError);
electionEventHandler.init(leaderElectionDriver);
final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
leaderRetrievalDriver = new KubernetesLeaderRetrievalDriver(flinkKubeClient, configMapSharedWatcher, watchExecutorService, configMapName, retrievalEventHandler, KubernetesUtils::getLeaderInformationFromConfigMap, retrievalEventHandler::handleError);
electionEventHandler.waitForLeader(TIMEOUT);
// Check the new leader is confirmed
final LeaderInformation confirmedLeaderInformation = electionEventHandler.getConfirmedLeaderInformation();
assertThat(confirmedLeaderInformation.getLeaderAddress(), is(LEADER_ADDRESS));
// Check the leader retrieval driver should be notified the 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();
}
flinkKubeClient.deleteConfigMap(configMapName).get();
configMapSharedWatcher.close();
ExecutorUtils.gracefulShutdown(5, TimeUnit.SECONDS, watchExecutorService);
}
}
use of org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler 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.TestingLeaderRetrievalEventHandler 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();
}
}
}
use of org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler in project flink by apache.
the class ZooKeeperLeaderElectionTest method testNotLeaderShouldNotCleanUpTheLeaderInformation.
@Test
public void testNotLeaderShouldNotCleanUpTheLeaderInformation() throws Exception {
final TestingLeaderElectionEventHandler electionEventHandler = new TestingLeaderElectionEventHandler(LEADER_ADDRESS);
final TestingLeaderRetrievalEventHandler retrievalEventHandler = new TestingLeaderRetrievalEventHandler();
ZooKeeperLeaderElectionDriver leaderElectionDriver = null;
ZooKeeperLeaderRetrievalDriver leaderRetrievalDriver = null;
try {
leaderElectionDriver = createAndInitLeaderElectionDriver(curatorFrameworkWrapper.asCuratorFramework(), electionEventHandler);
electionEventHandler.waitForLeader(timeout);
final LeaderInformation confirmedLeaderInformation = electionEventHandler.getConfirmedLeaderInformation();
assertThat(confirmedLeaderInformation.getLeaderAddress(), is(LEADER_ADDRESS));
// Leader is revoked
leaderElectionDriver.notLeader();
electionEventHandler.waitForRevokeLeader(timeout);
assertThat(electionEventHandler.getConfirmedLeaderInformation(), is(LeaderInformation.empty()));
// The data on ZooKeeper it not be cleared
leaderRetrievalDriver = ZooKeeperUtils.createLeaderRetrievalDriverFactory(curatorFrameworkWrapper.asCuratorFramework()).createLeaderRetrievalDriver(retrievalEventHandler, retrievalEventHandler::handleError);
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();
}
}
}
Aggregations