{"record":{"id":"641859fdb072c591","repo":"zhisheng17/flink-learning","slug":"could-not-find-in-configmap","errorCode":null,"errorMessage":"Could not find {} in ConfigMap {}","messagePattern":"Could not find (.+?) in ConfigMap (.+?)","errorType":"exception","errorClass":"NotExistException","httpStatus":null,"severity":"error","filePath":"flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/highavailability/KubernetesStateHandleStore.java","lineNumber":179,"sourceCode":"\t\tcheckNotNull(state, \"State.\");\n\n\t\tfinal RetrievableStateHandle<T> oldStateHandle = getAndLock(key);\n\n\t\tfinal RetrievableStateHandle<T> newStateHandle = storage.store(state);\n\n\t\tboolean success = false;\n\n\t\ttry {\n\t\t\tfinal byte[] serializedStoreHandle = InstantiationUtil.serializeObject(newStateHandle);\n\t\t\tsuccess = kubeClient.checkAndUpdateConfigMap(\n\t\t\t\tconfigMapName,\n\t\t\t\tc -> {\n\t\t\t\t\tif (KubernetesLeaderElector.hasLeadership(c, lockIdentity)) {\n\t\t\t\t\t\t// Check the existence\n\t\t\t\t\t\tif (c.getData().containsKey(key)) {\n\t\t\t\t\t\t\tc.getData().put(key, encodeStateHandle(serializedStoreHandle));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthrow new CompletionException(getKeyNotExistException(key));\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn Optional.of(c);\n\t\t\t\t\t}\n\t\t\t\t\treturn Optional.empty();\n\t\t\t\t}).get();\n\t\t} catch (Exception ex) {\n\t\t\tthrow ExceptionUtils.findThrowable(ex, NotExistException.class).orElseThrow(() -> ex);\n\t\t} finally {\n\t\t\tif (success) {\n\t\t\t\toldStateHandle.discardState();\n\t\t\t} else {\n\t\t\t\tnewStateHandle.discardState();\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Returns the resource version of the ConfigMap.","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/zhisheng17/flink-learning/blob/d731cee7618021be56d132cc925102ffff8d75e6/flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/highavailability/KubernetesStateHandleStore.java#L161-L197","documentation":"KubernetesStateHandleStore.replace() throws this when the key to replace does not exist in the ConfigMap ( getKeyNotExistException(key) ), mapped via a CompletionException. replace() only updates existing keys — it is not an upsert.","triggerScenarios":"replace(key, stateHandle) called while holding leadership but the key is absent from the ConfigMap — e.g. the state handle was already removed, the ConfigMap was recreated empty, or replace raced with a concurrent remove.","commonSituations":"Updating a checkpoint/coordinator state handle after the job was cleaned up; two nodes racing where one removes the key before the other's replace; using replace on a fresh ConfigMap that only addAndLock may seed.","solutions":["Use addAndLock instead of replace when the key may not exist yet.","Check existence first (check(key)) and branch between add and replace.","If replace races with removal, re-check leadership/job state — the key disappearing may be legitimate cleanup.","Verify the ConfigMap was not recreated (e.g. by re-deploying the HA setup) losing prior keys."],"exampleFix":"// before: replace on possibly-missing key\nstateHandleStore.replace(key, newHandle);\n// after: add-or-replace\nif (stateHandleStore.check(key).isPresent()) {\n    stateHandleStore.replace(key, newHandle);\n} else {\n    stateHandleStore.addAndLock(key, newHandle);\n}","handlingStrategy":"validation","validationCode":"// verify key exists before replace\nif (!stateHandleStore.check(key).isPresent()) {\n    throw new IllegalStateException(\"Cannot replace missing key \" + key);\n}\nstateHandleStore.replace(key, handle);","typeGuard":null,"tryCatchPattern":"try {\n    stateHandleStore.replace(key, handle);\n} catch (Exception e) {\n    if (e.getCause() != null && e.getCause().getMessage().contains(\"Could not find\")) {\n        stateHandleStore.addAndLock(key, handle); // add-or-replace fallback\n    }\n}","preventionTips":["Prefer addAndLock for first writes; replace only for updates.","Serialize replace/remove for the same key under one leader.","Recreate HA ConfigMaps carefully — keys are lost on recreation.","Use check(key) before destructive flows."],"tags":["kubernetes","configmap","missing-key","high-availability"],"backgroundTag":"record-not-found","analyzedSha":"d731cee7618021be56d132cc925102ffff8d75e6","analyzedAt":"2026-09-06T05:35:08.496Z","contentChangedAt":"2026-09-06T05:35:08.496Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}