Search in sources :

Example 1 with KubernetesLeaderElectionConfiguration

use of org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration in project flink by apache.

the class KubernetesStateHandleStoreITCase method testMultipleKubernetesStateHandleStores.

@Test
public void testMultipleKubernetesStateHandleStores() throws Exception {
    final Configuration configuration = kubernetesResource.getConfiguration();
    final String leaderConfigMapName = LEADER_CONFIGMAP_NAME + System.currentTimeMillis();
    final int leaderNum = 3;
    final KubernetesLeaderElector[] leaderElectors = new KubernetesLeaderElector[leaderNum];
    final FlinkKubeClient[] kubeClients = new FlinkKubeClient[leaderNum];
    final TestingLeaderCallbackHandler[] leaderCallbackHandlers = new TestingLeaderCallbackHandler[leaderNum];
    @SuppressWarnings("unchecked") final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle>[] stateHandleStores = new KubernetesStateHandleStore[leaderNum];
    try {
        for (int i = 0; i < leaderNum; i++) {
            final String lockIdentity = UUID.randomUUID().toString();
            kubeClients[i] = kubeClientFactory.fromConfiguration(configuration, "testing");
            leaderCallbackHandlers[i] = new TestingLeaderCallbackHandler(lockIdentity);
            leaderElectors[i] = kubeClients[i].createLeaderElector(new KubernetesLeaderElectionConfiguration(leaderConfigMapName, lockIdentity, configuration), leaderCallbackHandlers[i]);
            stateHandleStores[i] = new KubernetesStateHandleStore<>(kubeClients[i], leaderConfigMapName, new TestingLongStateHandleHelper(), (ignore) -> true, lockIdentity);
            leaderElectors[i].run();
        }
        // Wait for the leader
        final String lockIdentity = TestingLeaderCallbackHandler.waitUntilNewLeaderAppears(TIMEOUT);
        Long expectedState = null;
        for (int i = 0; i < leaderNum; i++) {
            // leader
            if (leaderCallbackHandlers[i].getLockIdentity().equals(lockIdentity)) {
                expectedState = (long) i;
            }
            stateHandleStores[i].addAndLock(KEY, new TestingLongStateHandleHelper.LongStateHandle(i));
        }
        // Only the leader could add successfully
        assertThat(expectedState, is(notNullValue()));
        assertThat(stateHandleStores[0].getAllAndLock().size(), is(1));
        assertThat(stateHandleStores[0].getAllAndLock().get(0).f0.retrieveState().getValue(), is(expectedState));
        assertThat(stateHandleStores[0].getAllAndLock().get(0).f1, is(KEY));
    } finally {
        TestingLongStateHandleHelper.clearGlobalState();
        // Cleanup the resources
        for (int i = 0; i < leaderNum; i++) {
            if (leaderElectors[i] != null) {
                leaderElectors[i].stop();
            }
            if (kubeClients[i] != null) {
                kubeClients[i].close();
            }
        }
        kubernetesResource.getFlinkKubeClient().deleteConfigMap(leaderConfigMapName).get();
    }
}
Also used : Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Configuration(org.apache.flink.configuration.Configuration) Test(org.junit.Test) UUID(java.util.UUID) TestingLeaderCallbackHandler(org.apache.flink.kubernetes.kubeclient.resources.TestingLeaderCallbackHandler) FlinkKubeClientFactory(org.apache.flink.kubernetes.kubeclient.FlinkKubeClientFactory) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestLogger(org.apache.flink.util.TestLogger) KubernetesResource(org.apache.flink.kubernetes.KubernetesResource) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) ClassRule(org.junit.ClassRule) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) Configuration(org.apache.flink.configuration.Configuration) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestingLeaderCallbackHandler(org.apache.flink.kubernetes.kubeclient.resources.TestingLeaderCallbackHandler) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) Test(org.junit.Test)

Example 2 with KubernetesLeaderElectionConfiguration

use of org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration in project flink by apache.

the class KubernetesLeaderElectorITCase method testMultipleKubernetesLeaderElectors.

@Test
public void testMultipleKubernetesLeaderElectors() throws Exception {
    final Configuration configuration = kubernetesResource.getConfiguration();
    final String leaderConfigMapName = LEADER_CONFIGMAP_NAME_PREFIX + System.currentTimeMillis();
    final int leaderNum = 3;
    final KubernetesLeaderElector[] leaderElectors = new KubernetesLeaderElector[leaderNum];
    // We use different Kubernetes clients for different leader electors.
    final FlinkKubeClient[] kubeClients = new FlinkKubeClient[leaderNum];
    final TestingLeaderCallbackHandler[] leaderCallbackHandlers = new TestingLeaderCallbackHandler[leaderNum];
    try {
        for (int i = 0; i < leaderNum; i++) {
            kubeClients[i] = kubeClientFactory.fromConfiguration(configuration, "testing");
            leaderCallbackHandlers[i] = new TestingLeaderCallbackHandler(UUID.randomUUID().toString());
            final KubernetesLeaderElectionConfiguration leaderConfig = new KubernetesLeaderElectionConfiguration(leaderConfigMapName, leaderCallbackHandlers[i].getLockIdentity(), configuration);
            leaderElectors[i] = kubeClients[i].createLeaderElector(leaderConfig, leaderCallbackHandlers[i]);
            // Start the leader electors to contend the leader
            leaderElectors[i].run();
        }
        // Wait for the first leader
        final String firstLockIdentity = TestingLeaderCallbackHandler.waitUntilNewLeaderAppears(TIMEOUT);
        for (int i = 0; i < leaderNum; i++) {
            if (leaderCallbackHandlers[i].getLockIdentity().equals(firstLockIdentity)) {
                // Check the callback isLeader is called.
                leaderCallbackHandlers[i].waitForNewLeader(TIMEOUT);
                assertThat(leaderCallbackHandlers[i].hasLeadership(), is(true));
                // Current leader died
                leaderElectors[i].stop();
                // Check the callback notLeader is called.
                leaderCallbackHandlers[i].waitForRevokeLeader(TIMEOUT);
                assertThat(leaderCallbackHandlers[i].hasLeadership(), is(false));
            } else {
                assertThat(leaderCallbackHandlers[i].hasLeadership(), is(false));
            }
        }
        // Another leader should be elected successfully and update the lock identity
        final String anotherLockIdentity = TestingLeaderCallbackHandler.waitUntilNewLeaderAppears(TIMEOUT);
        assertThat(anotherLockIdentity, is(not(firstLockIdentity)));
    } finally {
        // Cleanup the resources
        for (int i = 0; i < leaderNum; i++) {
            if (leaderElectors[i] != null) {
                leaderElectors[i].stop();
            }
            if (kubeClients[i] != null) {
                kubeClients[i].close();
            }
        }
        kubernetesResource.getFlinkKubeClient().deleteConfigMap(leaderConfigMapName).get();
    }
}
Also used : KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) Configuration(org.apache.flink.configuration.Configuration) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) Test(org.junit.Test)

Example 3 with KubernetesLeaderElectionConfiguration

use of org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration in project flink by apache.

the class KubernetesMultipleComponentLeaderElectionHaServices method getOrInitializeSingleLeaderElectionService.

private DefaultMultipleComponentLeaderElectionService getOrInitializeSingleLeaderElectionService() {
    synchronized (lock) {
        if (multipleComponentLeaderElectionService == null) {
            try {
                final KubernetesLeaderElectionConfiguration leaderElectionConfiguration = new KubernetesLeaderElectionConfiguration(getClusterConfigMap(), lockIdentity, configuration);
                multipleComponentLeaderElectionService = new DefaultMultipleComponentLeaderElectionService(fatalErrorHandler, new KubernetesMultipleComponentLeaderElectionDriverFactory(kubeClient, leaderElectionConfiguration, configMapSharedWatcher, watchExecutorService, fatalErrorHandler));
            } catch (Exception e) {
                throw new FlinkRuntimeException("Could not initialize the default single leader election service.", e);
            }
        }
        return multipleComponentLeaderElectionService;
    }
}
Also used : FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) DefaultMultipleComponentLeaderElectionService(org.apache.flink.runtime.leaderelection.DefaultMultipleComponentLeaderElectionService) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException)

Example 4 with KubernetesLeaderElectionConfiguration

use of org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration 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

KubernetesLeaderElectionConfiguration (org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration)4 Configuration (org.apache.flink.configuration.Configuration)3 FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)3 Test (org.junit.Test)3 IOException (java.io.IOException)1 UUID (java.util.UUID)1 ExecutorService (java.util.concurrent.ExecutorService)1 KubernetesResource (org.apache.flink.kubernetes.KubernetesResource)1 FlinkKubeClientFactory (org.apache.flink.kubernetes.kubeclient.FlinkKubeClientFactory)1 KubernetesConfigMapSharedWatcher (org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher)1 KubernetesLeaderElector (org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector)1 TestingLeaderCallbackHandler (org.apache.flink.kubernetes.kubeclient.resources.TestingLeaderCallbackHandler)1 DefaultMultipleComponentLeaderElectionService (org.apache.flink.runtime.leaderelection.DefaultMultipleComponentLeaderElectionService)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 TestingLongStateHandleHelper (org.apache.flink.runtime.persistence.TestingLongStateHandleHelper)1 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)1 TestLogger (org.apache.flink.util.TestLogger)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1