zhisheng17/flink-learning · error · NotExistException

Could not find {key} in ConfigMap {configMapName}

Error message

Could not find {key} in ConfigMap {configMapName}

What it means

KubernetesStateHandleStore.getAndLock looks up the given key in the named ConfigMap and throws getKeyNotExistException (a KeyNotFoundException) when the ConfigMap exists but does not contain the requested state-handle key. In Flink's HA setup this means the job's state handle entry is missing from Kubernetes storage. Callers treat it as 'no state stored yet' for that key.

Source

Thrown at flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/highavailability/KubernetesStateHandleStore.java:240

	 * @param key Key in ConfigMap
	 *
	 * @return The retrieved state handle from the specified ConfigMap and key
	 *
	 * @throws IOException if the method failed to deserialize the stored state handle
	 * @throws NotExistException when the name does not exist
	 * @throws Exception if get state handle from ConfigMap failed
	 */
	@Override
	public RetrievableStateHandle<T> getAndLock(String key) throws Exception {
		checkNotNull(key, "Key in ConfigMap.");

		final Optional<KubernetesConfigMap> optional = kubeClient.getConfigMap(configMapName);
		if (optional.isPresent()) {
			final KubernetesConfigMap configMap = optional.get();
			if (configMap.getData().containsKey(key)) {
				return deserializeObject(configMap.getData().get(key));
			} else {
				throw getKeyNotExistException(key);
			}
		} else {
			throw getConfigMapNotExistException();
		}
	}

	/**
	 * Gets all available state handles from Kubernetes.
	 *
	 * @return All state handles from ConfigMap.
	 */
	@Override
	public List<Tuple2<RetrievableStateHandle<T>, String>> getAllAndLock() {

		return kubeClient.getConfigMap(configMapName)
			.map(
				configMap -> {
					final List<Tuple2<RetrievableStateHandle<T>, String>> stateHandles = new ArrayList<>();

View on GitHub (pinned to d731cee761)

Solutions

  1. Verify the key name and HA storage path configuration (high-availability.storageDir / cluster-id) match what was originally written
  2. Check the ConfigMap contents (kubectl get configmap <name> -o yaml) to confirm which keys exist
  3. If state was genuinely lost, restart the job as a fresh start rather than HA recovery
  4. Restore the ConfigMap from backup or recreate the state handles before retrying

Example fix

// before
Optional<CompletedCheckpoint> checkpoint = stateHandleStore.getAndLock(configMapName, "job-state-handle");
// after
Optional<KubernetesConfigMap> cm = kubeClient.getConfigMap(configMapName);
if (cm.isPresent() && cm.get().getData().containsKey("job-state-handle")) {
    Optional<CompletedCheckpoint> checkpoint = stateHandleStore.getAndLock(configMapName, "job-state-handle");
} else {
    // fall back to fresh start or restore from alternative storage
}
Defensive patterns

Strategy: validation

Validate before calling

KubernetesConfigMap cm = kubeClient.getConfigMap(configMapName).orElse(null);
if (cm == null) { /* ConfigMap missing: handle 56 case */ }
if (cm != null && !cm.getData().containsKey(key)) {
    LOG.warn("Key {} absent from ConfigMap {}", key, configMapName);
    return Optional.empty(); // skip lock/recovery
}

Try / catch

try {
    return stateHandleStore.getAndLock(configMapName, key);
} catch (KeyNotFoundException e) {
    LOG.warn("No stored state for key {} in {}", key, configMapName);
    return Optional.empty(); // fall back to fresh start
}

Prevention

When it happens

Trigger: Calling getAndLock(configMapName, key) where kubeClient.getConfigMap(configMapName) returns a present ConfigMap but configMap.getData().containsKey(key) is false — the key was never written, was removed, or the wrong key name is requested.

Common situations: HA recovery pointing at a fresh/empty ConfigMap after a namespace was recreated, mismatched job/HA storage path keys between config and what was actually stored, or an external actor pruned keys from the ConfigMap.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of zhisheng17/flink-learning@d731cee761 (2026-09-06). Data as JSON: /api/errors/4bcac6dc05d10522. Report an issue: GitHub.