Search in sources :

Example 1 with KubernetesException

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

the class KubernetesCheckpointIDCounter method getAndIncrement.

@Override
public long getAndIncrement() throws Exception {
    final AtomicLong current = new AtomicLong();
    final boolean updated = kubeClient.checkAndUpdateConfigMap(configMapName, configMap -> {
        if (isValidOperation(configMap)) {
            final long currentValue = getCurrentCounter(configMap);
            current.set(currentValue);
            configMap.getData().put(CHECKPOINT_COUNTER_KEY, String.valueOf(currentValue + 1));
            return Optional.of(configMap);
        }
        return Optional.empty();
    }).get();
    if (updated) {
        return current.get();
    } else {
        throw new KubernetesException("Failed to update ConfigMap " + configMapName + " since current KubernetesCheckpointIDCounter does not have the leadership.");
    }
}
Also used : Logger(org.slf4j.Logger) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) CheckpointIDCounter(org.apache.flink.runtime.checkpoint.CheckpointIDCounter) LoggerFactory(org.slf4j.LoggerFactory) JobStatus(org.apache.flink.api.common.JobStatus) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) Optional(java.util.Optional) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) CHECKPOINT_COUNTER_KEY(org.apache.flink.kubernetes.utils.Constants.CHECKPOINT_COUNTER_KEY) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) Nullable(javax.annotation.Nullable) AtomicLong(java.util.concurrent.atomic.AtomicLong) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)

Example 2 with KubernetesException

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

the class KubernetesLeaderElectionDriver method writeLeaderInformation.

@Override
public void writeLeaderInformation(LeaderInformation leaderInformation) {
    assert (running);
    final UUID confirmedLeaderSessionID = leaderInformation.getLeaderSessionID();
    final String confirmedLeaderAddress = leaderInformation.getLeaderAddress();
    try {
        kubeClient.checkAndUpdateConfigMap(configMapName, configMap -> {
            if (KubernetesLeaderElector.hasLeadership(configMap, lockIdentity)) {
                // Get the updated ConfigMap with new leader information
                if (confirmedLeaderAddress == null) {
                    configMap.getData().remove(LEADER_ADDRESS_KEY);
                } else {
                    configMap.getData().put(LEADER_ADDRESS_KEY, confirmedLeaderAddress);
                }
                if (confirmedLeaderSessionID == null) {
                    configMap.getData().remove(LEADER_SESSION_ID_KEY);
                } else {
                    configMap.getData().put(LEADER_SESSION_ID_KEY, confirmedLeaderSessionID.toString());
                }
                configMap.getLabels().putAll(configMapLabels);
                return Optional.of(configMap);
            }
            return Optional.empty();
        }).get();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Successfully wrote leader information: Leader={}, session ID={}.", confirmedLeaderAddress, confirmedLeaderSessionID);
        }
    } catch (Exception e) {
        fatalErrorHandler.onFatalError(new KubernetesException("Could not write leader information since ConfigMap " + configMapName + " does not exist.", e));
    }
}
Also used : LEADER_ADDRESS_KEY(org.apache.flink.kubernetes.utils.Constants.LEADER_ADDRESS_KEY) LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY(org.apache.flink.kubernetes.utils.Constants.LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY) KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) LoggerFactory(org.slf4j.LoggerFactory) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) Map(java.util.Map) FatalErrorHandler(org.apache.flink.runtime.rpc.FatalErrorHandler) KubernetesUtils.checkConfigMaps(org.apache.flink.kubernetes.utils.KubernetesUtils.checkConfigMaps) LeaderElectionEventHandler(org.apache.flink.runtime.leaderelection.LeaderElectionEventHandler) LeaderElectionException(org.apache.flink.runtime.leaderelection.LeaderElectionException) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) ExecutorService(java.util.concurrent.ExecutorService) KubernetesUtils(org.apache.flink.kubernetes.utils.KubernetesUtils) Logger(org.slf4j.Logger) KubernetesUtils.getLeaderInformationFromConfigMap(org.apache.flink.kubernetes.utils.KubernetesUtils.getLeaderInformationFromConfigMap) LeaderElectionDriver(org.apache.flink.runtime.leaderelection.LeaderElectionDriver) UUID(java.util.UUID) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation) Watch(org.apache.flink.kubernetes.kubeclient.KubernetesSharedWatcher.Watch) List(java.util.List) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) Optional(java.util.Optional) LEADER_SESSION_ID_KEY(org.apache.flink.kubernetes.utils.Constants.LEADER_SESSION_ID_KEY) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) UUID(java.util.UUID) LeaderElectionException(org.apache.flink.runtime.leaderelection.LeaderElectionException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)

Example 3 with KubernetesException

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

the class KubernetesStateHandleStore method releaseAndTryRemoveAll.

/**
 * Remove all the state handle keys in the ConfigMap and discard the states.
 *
 * @throws Exception when removing the keys or discarding the state failed
 */
@Override
public void releaseAndTryRemoveAll() throws Exception {
    final List<RetrievableStateHandle<T>> validStateHandles = new ArrayList<>();
    kubeClient.checkAndUpdateConfigMap(configMapName, c -> {
        if (isValidOperation(c)) {
            final Map<String, String> updateData = new HashMap<>(c.getData());
            c.getData().entrySet().stream().filter(entry -> configMapKeyFilter.test(entry.getKey())).forEach(entry -> {
                try {
                    validStateHandles.add(deserializeObject(entry.getValue()));
                    updateData.remove(entry.getKey());
                } catch (IOException e) {
                    LOG.warn("ConfigMap {} contained corrupted data. Ignoring the key {}.", configMapName, entry.getKey());
                }
            });
            c.getData().clear();
            c.getData().putAll(updateData);
            return Optional.of(c);
        }
        return Optional.empty();
    }).whenComplete((succeed, ignore) -> {
        if (succeed) {
            Exception exception = null;
            for (RetrievableStateHandle<T> stateHandle : validStateHandles) {
                try {
                    stateHandle.discardState();
                } catch (Exception e) {
                    exception = ExceptionUtils.firstOrSuppressed(e, exception);
                }
            }
            if (exception != null) {
                throw new CompletionException(new KubernetesException("Could not properly remove all state handles.", exception));
            }
        }
    }).get();
}
Also used : Tuple2(org.apache.flink.api.java.tuple.Tuple2) LoggerFactory(org.slf4j.LoggerFactory) StateHandleStoreUtils.deserialize(org.apache.flink.runtime.util.StateHandleStoreUtils.deserialize) ExceptionUtils(org.apache.flink.util.ExceptionUtils) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringResourceVersion(org.apache.flink.runtime.persistence.StringResourceVersion) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) ArrayList(java.util.ArrayList) Map(java.util.Map) StateHandleStore(org.apache.flink.runtime.persistence.StateHandleStore) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) Nullable(javax.annotation.Nullable) RetrievableStateHandle(org.apache.flink.runtime.state.RetrievableStateHandle) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Base64(java.util.Base64) List(java.util.List) StateHandleStoreUtils.serializeOrDiscard(org.apache.flink.runtime.util.StateHandleStoreUtils.serializeOrDiscard) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) Optional(java.util.Optional) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) RetrievableStateStorageHelper(org.apache.flink.runtime.persistence.RetrievableStateStorageHelper) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) Collections(java.util.Collections) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) HashMap(java.util.HashMap) RetrievableStateHandle(org.apache.flink.runtime.state.RetrievableStateHandle) CompletionException(java.util.concurrent.CompletionException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)

Example 4 with KubernetesException

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

the class KubernetesTestFixture method createFlinkKubeClientBuilder.

TestingFlinkKubeClient.Builder createFlinkKubeClientBuilder() {
    return TestingFlinkKubeClient.builder().setCreateConfigMapFunction(configMap -> {
        configMapStore.put(configMap.getName(), configMap);
        return CompletableFuture.completedFuture(null);
    }).setGetConfigMapFunction(configMapName -> Optional.ofNullable(configMapStore.get(configMapName))).setCheckAndUpdateConfigMapFunction((configMapName, updateFunction) -> {
        final KubernetesConfigMap configMap = configMapStore.get(configMapName);
        if (configMap != null) {
            try {
                final boolean updated = updateFunction.apply(configMap).map(updateConfigMap -> {
                    configMapStore.put(configMap.getName(), updateConfigMap);
                    return true;
                }).orElse(false);
                return CompletableFuture.completedFuture(updated);
            } catch (Throwable throwable) {
                throw new CompletionException(throwable);
            }
        }
        throw new CompletionException(new KubernetesException("ConfigMap " + configMapName + " does not exist."));
    }).setDeleteConfigMapFunction(name -> {
        configMapStore.remove(name);
        return FutureUtils.completedVoidFuture();
    }).setDeleteConfigMapByLabelFunction(labels -> {
        if (deleteConfigMapByLabelsFuture.isDone()) {
            return FutureUtils.completedExceptionally(new KubernetesException("ConfigMap with labels " + labels + " has already be deleted."));
        } else {
            deleteConfigMapByLabelsFuture.complete(labels);
            return FutureUtils.completedVoidFuture();
        }
    }).setCloseConsumer(closeKubeClientFuture::complete).setCreateLeaderElectorFunction((leaderConfig, callbackHandler) -> {
        leaderCallbackHandlerFuture.complete(callbackHandler);
        return new TestingFlinkKubeClient.TestingKubernetesLeaderElector(leaderConfig, callbackHandler);
    }).setCreateConfigMapSharedWatcherFunction((labels) -> {
        final TestingFlinkKubeClient.TestingKubernetesConfigMapSharedWatcher watcher = new TestingFlinkKubeClient.TestingKubernetesConfigMapSharedWatcher(labels);
        watcher.setWatchFunction((ignore, handler) -> {
            final CompletableFuture<FlinkKubeClient.WatchCallbackHandler<KubernetesConfigMap>> future = CompletableFuture.completedFuture(handler);
            configMapCallbackFutures.add(future);
            final TestingFlinkKubeClient.MockKubernetesWatch watch = new TestingFlinkKubeClient.MockKubernetesWatch();
            configMapWatches.add(watch);
            return watch;
        });
        return watcher;
    });
}
Also used : KubernetesUtils(org.apache.flink.kubernetes.utils.KubernetesUtils) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) KubernetesConfigOptions(org.apache.flink.kubernetes.configuration.KubernetesConfigOptions) LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY(org.apache.flink.kubernetes.utils.Constants.LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY) LEADER_ANNOTATION_KEY(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector.LEADER_ANNOTATION_KEY) KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) Configuration(org.apache.flink.configuration.Configuration) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) Map(java.util.Map) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) TestingFlinkKubeClient(org.apache.flink.kubernetes.kubeclient.TestingFlinkKubeClient) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) TestingFlinkKubeClient(org.apache.flink.kubernetes.kubeclient.TestingFlinkKubeClient) CompletionException(java.util.concurrent.CompletionException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)

Example 5 with KubernetesException

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

Aggregations

KubernetesConfigMap (org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap)6 KubernetesException (org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)6 Optional (java.util.Optional)4 FlinkKubeClient (org.apache.flink.kubernetes.kubeclient.FlinkKubeClient)4 KubernetesLeaderElector (org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector)4 List (java.util.List)3 Map (java.util.Map)3 CompletionException (java.util.concurrent.CompletionException)3 Preconditions.checkNotNull (org.apache.flink.util.Preconditions.checkNotNull)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Nullable (javax.annotation.Nullable)2 KubernetesConfigMapSharedWatcher (org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher)2 LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY (org.apache.flink.kubernetes.utils.Constants.LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY)2 KubernetesUtils (org.apache.flink.kubernetes.utils.KubernetesUtils)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Base64 (java.util.Base64)1