use of org.apache.flink.runtime.leaderelection.TestingLeaderElectionEventHandler 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);
}
}
Aggregations