Search in sources :

Example 1 with FlinkKubeClient

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

use of org.apache.flink.kubernetes.kubeclient.FlinkKubeClient in project flink by apache.

the class KubernetesStateHandleStoreTest method testReplaceFailedWithPossiblyInconsistentState.

@Test
public void testReplaceFailedWithPossiblyInconsistentState() throws Exception {
    final PossibleInconsistentStateException updateException = new PossibleInconsistentStateException();
    new Context() {

        {
            runTest(() -> {
                leaderCallbackGrantLeadership();
                final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new KubernetesStateHandleStore<>(flinkKubeClient, LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
                store.addAndLock(key, state);
                final FlinkKubeClient anotherFlinkKubeClient = createFlinkKubeClientBuilder().setCheckAndUpdateConfigMapFunction((configMapName, function) -> FutureUtils.completedExceptionally(updateException)).build();
                final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> anotherStore = new KubernetesStateHandleStore<>(anotherFlinkKubeClient, LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
                final StringResourceVersion resourceVersion = anotherStore.exists(key);
                assertThat(resourceVersion.isExisting(), is(true));
                try {
                    anotherStore.replace(key, resourceVersion, new TestingLongStateHandleHelper.LongStateHandle(23456L));
                    fail("An exception having a PossibleInconsistentStateException as its cause should have been thrown.");
                } catch (Exception ex) {
                    assertThat(ex, is(updateException));
                }
                assertThat(anotherStore.getAllAndLock().size(), is(1));
                // The state does not change
                assertThat(anotherStore.getAndLock(key).retrieveState(), is(state));
                assertThat(TestingLongStateHandleHelper.getGlobalStorageSize(), is(2));
                // no state was discarded
                assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(0), is(0));
                assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(1), is(0));
            });
        }
    };
}
Also used : Arrays(java.util.Arrays) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) Predicate(java.util.function.Predicate) FlinkMatchers(org.apache.flink.core.testutils.FlinkMatchers) Matchers.not(org.hamcrest.Matchers.not) Test(org.junit.Test) Collectors(java.util.stream.Collectors) StringResourceVersion(org.apache.flink.runtime.persistence.StringResourceVersion) List(java.util.List) JobID(org.apache.flink.api.common.JobID) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) FunctionUtils(org.apache.flink.util.function.FunctionUtils) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) StateHandleStore(org.apache.flink.runtime.persistence.StateHandleStore) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Matchers.is(org.hamcrest.Matchers.is) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) Comparator(java.util.Comparator) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) Before(org.junit.Before) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) StringResourceVersion(org.apache.flink.runtime.persistence.StringResourceVersion) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) Test(org.junit.Test)

Example 3 with FlinkKubeClient

use of org.apache.flink.kubernetes.kubeclient.FlinkKubeClient 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 4 with FlinkKubeClient

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

Example 5 with FlinkKubeClient

use of org.apache.flink.kubernetes.kubeclient.FlinkKubeClient in project flink by apache.

the class KubernetesStateHandleStoreTest method testReplaceFailedAndDiscardState.

@Test
public void testReplaceFailedAndDiscardState() throws Exception {
    final FlinkRuntimeException updateException = new FlinkRuntimeException("Failed to update");
    new Context() {

        {
            runTest(() -> {
                leaderCallbackGrantLeadership();
                final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> store = new KubernetesStateHandleStore<>(flinkKubeClient, LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
                store.addAndLock(key, state);
                final FlinkKubeClient anotherFlinkKubeClient = createFlinkKubeClientBuilder().setCheckAndUpdateConfigMapFunction((configMapName, function) -> {
                    throw updateException;
                }).build();
                final KubernetesStateHandleStore<TestingLongStateHandleHelper.LongStateHandle> anotherStore = new KubernetesStateHandleStore<>(anotherFlinkKubeClient, LEADER_CONFIGMAP_NAME, longStateStorage, filter, LOCK_IDENTITY);
                final TestingLongStateHandleHelper.LongStateHandle newState = new TestingLongStateHandleHelper.LongStateHandle(23456L);
                final StringResourceVersion resourceVersion = anotherStore.exists(key);
                assertThat(resourceVersion.isExisting(), is(true));
                try {
                    anotherStore.replace(key, resourceVersion, newState);
                    fail("We should get an exception when kube client failed to update.");
                } catch (Exception ex) {
                    assertThat(ex, FlinkMatchers.containsCause(updateException));
                }
                assertThat(anotherStore.getAllAndLock().size(), is(1));
                // The state do not change
                assertThat(anotherStore.getAndLock(key).retrieveState(), is(state));
                assertThat(TestingLongStateHandleHelper.getGlobalStorageSize(), is(2));
                assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(0), is(0));
                assertThat(TestingLongStateHandleHelper.getDiscardCallCountForStateHandleByIndex(1), is(1));
            });
        }
    };
}
Also used : Arrays(java.util.Arrays) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) Predicate(java.util.function.Predicate) FlinkMatchers(org.apache.flink.core.testutils.FlinkMatchers) Matchers.not(org.hamcrest.Matchers.not) Test(org.junit.Test) Collectors(java.util.stream.Collectors) StringResourceVersion(org.apache.flink.runtime.persistence.StringResourceVersion) List(java.util.List) JobID(org.apache.flink.api.common.JobID) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) FunctionUtils(org.apache.flink.util.function.FunctionUtils) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) StateHandleStore(org.apache.flink.runtime.persistence.StateHandleStore) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) Matchers.is(org.hamcrest.Matchers.is) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) Comparator(java.util.Comparator) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) Before(org.junit.Before) TestingLongStateHandleHelper(org.apache.flink.runtime.persistence.TestingLongStateHandleHelper) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) StringResourceVersion(org.apache.flink.runtime.persistence.StringResourceVersion) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) Test(org.junit.Test)

Aggregations

FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)7 Test (org.junit.Test)6 Configuration (org.apache.flink.configuration.Configuration)4 KubernetesLeaderElector (org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector)4 TestingLongStateHandleHelper (org.apache.flink.runtime.persistence.TestingLongStateHandleHelper)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.is (org.hamcrest.Matchers.is)4 Arrays (java.util.Arrays)3 Comparator (java.util.Comparator)3 List (java.util.List)3 Predicate (java.util.function.Predicate)3 Collectors (java.util.stream.Collectors)3 JobID (org.apache.flink.api.common.JobID)3 FlinkMatchers (org.apache.flink.core.testutils.FlinkMatchers)3 KubernetesLeaderElectionConfiguration (org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration)3 PossibleInconsistentStateException (org.apache.flink.runtime.persistence.PossibleInconsistentStateException)3 StateHandleStore (org.apache.flink.runtime.persistence.StateHandleStore)3 StringResourceVersion (org.apache.flink.runtime.persistence.StringResourceVersion)3 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)3 FutureUtils (org.apache.flink.util.concurrent.FutureUtils)3