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.");
}
}
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));
}
}
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();
}
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;
});
}
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);
}
Aggregations