{"record":{"id":"c2f59530a48bdb91","repo":"apache/pulsar","slug":"key-s-doesn-t-exist","errorCode":null,"errorMessage":"key '%s' doesn't exist.","messagePattern":"key '(.+?)' doesn't exist\\.","errorType":"http","errorClass":"RestException","httpStatus":404,"severity":"warning","filePath":"pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/ComponentImpl.java","lineNumber":1276,"sourceCode":"            log.error().attr(\"tenant\", tenant).attr(\"namespace\", namespace).attr(\"componentName\", functionName)\n\n                    .attr(\"key\", key).exception(e).log(\"Invalid getFunctionState request @ / / / /\");\n            throw new RestException(Status.BAD_REQUEST, e.getMessage());\n        }\n\n        FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();\n        if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {\n            log.warn().attr(\"tenant\", tenant).attr(\"namespace\", namespace).attr(\"componentName\", functionName)\n\n                    .log(\"getFunctionState does not exist @ / / /\");\n            throw new RestException(Status.NOT_FOUND, String.format(\"'%s' is not found\", functionName));\n        }\n\n        try {\n            DefaultStateStore store = worker().getStateStoreProvider().getStateStore(tenant, namespace, functionName);\n            StateValue value = store.getStateValue(key);\n            if (value == null) {\n                throw new RestException(Status.NOT_FOUND, \"key '\" + key + \"' doesn't exist.\");\n            }\n            byte[] data = value.getValue();\n            if (data == null) {\n                throw new RestException(Status.NOT_FOUND, \"key '\" + key + \"' doesn't exist.\");\n            }\n\n            ByteBuffer buf = ByteBuffer.wrap(data);\n\n            Long number = null;\n            if (buf.remaining() == Long.BYTES) {\n                number = buf.getLong();\n            }\n            if (Boolean.TRUE.equals(value.getIsNumber())) {\n                return new FunctionState(key, null, null, number, value.getVersion());\n            }\n\n            if (Utf8.isWellFormed(data)) {\n                return new FunctionState(key, new String(data, UTF_8), null, number, value.getVersion());","sourceCodeStart":1258,"sourceCodeEnd":1294,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/ComponentImpl.java#L1258-L1294","documentation":"The state store returned no StateValue for the requested key, so the worker throws a 404 RestException \"key '<key>' doesn't exist.\". The function exists and the state store is reachable; only the key lookup came back empty.","triggerScenarios":"GET .../functions/{tenant}/{namespace}/{functionName}/state/{key} where store.getStateValue(key) returns null — the key was never written, has expired/been deleted, or the query targets the wrong function's store.","commonSituations":"Reading a counter before the function ever incremented it; key-name case mismatch (state keys are case-sensitive); querying a different function instance's key; bookkeeper table-service data loss or store re-created.","solutions":["Verify the exact key spelling/case used when the function stored the value.","Confirm the function actually ran and put the key (check function logs / trigger it first).","Query the correct tenant/namespace/function that owns the key.","If data should exist, inspect the BookKeeper table service / state storage for the namespace to check for data loss."],"exampleFix":"// before\nGET .../state/Counter  -> key 'Counter' doesn't exist.\n\n// after (matching stored key)\nGET .../state/counter","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const state = await getState(tenant, ns, fn, key);\n} catch (e) {\n  if (e.status === 404 && e.message.includes(\"doesn't exist\")) {\n    // key absent: treat as default/zero or wait until the function writes it\n    return defaultValue;\n  }\n  throw e;\n}","preventionTips":["Treat 'key doesn't exist' as a normal first-read condition (initialize defaults).","Match key case exactly; state keys are case-sensitive.","Ensure the function has actually run (e.g. trigger it) before reading its state."],"tags":["rest-api","not-found","state-store","functions"],"backgroundTag":"state-key-not-found","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}