Search in sources :

Example 1 with TestingLeaderElectionEventHandler

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);
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestingLeaderElectionEventHandler(org.apache.flink.runtime.leaderelection.TestingLeaderElectionEventHandler) KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) ExecutorService(java.util.concurrent.ExecutorService) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestingLeaderRetrievalEventHandler(org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Test(org.junit.Test)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)1 Configuration (org.apache.flink.configuration.Configuration)1 KubernetesLeaderElectionConfiguration (org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration)1 FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)1 KubernetesConfigMapSharedWatcher (org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher)1 LeaderInformation (org.apache.flink.runtime.leaderelection.LeaderInformation)1 TestingLeaderElectionEventHandler (org.apache.flink.runtime.leaderelection.TestingLeaderElectionEventHandler)1 TestingLeaderRetrievalEventHandler (org.apache.flink.runtime.leaderretrieval.TestingLeaderRetrievalEventHandler)1 Test (org.junit.Test)1