Search in sources :

Example 1 with KubernetesLeaderElector

use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector 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)

Aggregations

UUID (java.util.UUID)1 Configuration (org.apache.flink.configuration.Configuration)1 KubernetesResource (org.apache.flink.kubernetes.KubernetesResource)1 KubernetesLeaderElectionConfiguration (org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration)1 FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)1 FlinkKubeClientFactory (org.apache.flink.kubernetes.kubeclient.FlinkKubeClientFactory)1 KubernetesLeaderElector (org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector)1 TestingLeaderCallbackHandler (org.apache.flink.kubernetes.kubeclient.resources.TestingLeaderCallbackHandler)1 TestingLongStateHandleHelper (org.apache.flink.runtime.persistence.TestingLongStateHandleHelper)1 TestLogger (org.apache.flink.util.TestLogger)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)1 ClassRule (org.junit.ClassRule)1 Test (org.junit.Test)1