Search in sources :

Example 11 with KubernetesConfigMap

use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap in project flink by apache.

the class KubernetesLeaderElectionDriverTest method testLeaderConfigMapModifiedExternallyShouldBeCorrected.

@Test
public void testLeaderConfigMapModifiedExternallyShouldBeCorrected() throws Exception {
    new Context() {

        {
            runTest(() -> {
                leaderCallbackGrantLeadership();
                final FlinkKubeClient.WatchCallbackHandler<KubernetesConfigMap> callbackHandler = getLeaderElectionConfigMapCallback();
                // Update ConfigMap with wrong data
                final KubernetesConfigMap updatedConfigMap = getLeaderConfigMap();
                final UUID leaderSessionId = UUID.fromString(updatedConfigMap.getData().get(LEADER_SESSION_ID_KEY));
                final LeaderInformation faultyLeader = LeaderInformation.known(UUID.randomUUID(), "faultyLeaderAddress");
                updatedConfigMap.getData().put(LEADER_ADDRESS_KEY, faultyLeader.getLeaderAddress());
                updatedConfigMap.getData().put(LEADER_SESSION_ID_KEY, faultyLeader.getLeaderSessionID().toString());
                callbackHandler.onModified(Collections.singletonList(updatedConfigMap));
                // The leader should be corrected
                assertThat(getLeaderConfigMap().getData().get(LEADER_ADDRESS_KEY), is(LEADER_ADDRESS));
                assertThat(getLeaderConfigMap().getData().get(LEADER_SESSION_ID_KEY), is(leaderSessionId.toString()));
            });
        }
    };
}
Also used : KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) UUID(java.util.UUID) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Test(org.junit.Test)

Example 12 with KubernetesConfigMap

use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap in project flink by apache.

the class Fabric8FlinkKubeClient method attemptCheckAndUpdateConfigMap.

private CompletableFuture<Boolean> attemptCheckAndUpdateConfigMap(String configMapName, Function<KubernetesConfigMap, Optional<KubernetesConfigMap>> updateFunction) {
    return CompletableFuture.supplyAsync(() -> {
        final KubernetesConfigMap configMap = getConfigMap(configMapName).orElseThrow(() -> new CompletionException(new KubernetesException("Cannot retry checkAndUpdateConfigMap with configMap " + configMapName + " because it does not exist.")));
        final Optional<KubernetesConfigMap> maybeUpdate = updateFunction.apply(configMap);
        if (maybeUpdate.isPresent()) {
            try {
                internalClient.configMaps().withName(configMapName).lockResourceVersion(maybeUpdate.get().getResourceVersion()).replace(maybeUpdate.get().getInternalResource());
                return true;
            } catch (Throwable throwable) {
                LOG.debug("Failed to update ConfigMap {} with data {}. Trying again.", configMap.getName(), configMap.getData());
                // handling here
                throw new CompletionException(new PossibleInconsistentStateException(throwable));
            }
        }
        return false;
    }, kubeClientExecutorService);
}
Also used : KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) CompletionException(java.util.concurrent.CompletionException) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)

Example 13 with KubernetesConfigMap

use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap in project flink by apache.

the class KubernetesMultipleComponentLeaderElectionDriver method hasLeadership.

@Override
public boolean hasLeadership() {
    Preconditions.checkState(running.get());
    final Optional<KubernetesConfigMap> optionalConfigMap = kubeClient.getConfigMap(configMapName);
    if (optionalConfigMap.isPresent()) {
        return KubernetesLeaderElector.hasLeadership(optionalConfigMap.get(), lockIdentity);
    } else {
        fatalErrorHandler.onFatalError(new KubernetesException(String.format("ConfigMap %s does not exist. This indicates that somebody has interfered with Flink's operation.", configMapName)));
        return false;
    }
}
Also used : KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)

Aggregations

KubernetesConfigMap (org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap)13 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 PossibleInconsistentStateException (org.apache.flink.runtime.persistence.PossibleInconsistentStateException)4 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 CompletionException (java.util.concurrent.CompletionException)3 ExecutionException (java.util.concurrent.ExecutionException)3 KubernetesConfigOptions (org.apache.flink.kubernetes.configuration.KubernetesConfigOptions)3 KubernetesException (org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 OwnerReference (io.fabric8.kubernetes.api.model.OwnerReference)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 Service (io.fabric8.kubernetes.api.model.Service)2