{"record":{"id":"72ed02e5800b5630","repo":"apache/flink","slug":"the-accumulator-name-already-exists-and-canno","errorCode":null,"errorMessage":"The accumulator '${name}' already exists and cannot be added.","messagePattern":"The accumulator '(.+?)' already exists and cannot be added\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/functions/util/AbstractRuntimeUDFContext.java","lineNumber":137,"sourceCode":"    public LongCounter getLongCounter(String name) {\n        return (LongCounter) getAccumulator(name, LongCounter.class);\n    }\n\n    @Override\n    public Histogram getHistogram(String name) {\n        return (Histogram) getAccumulator(name, Histogram.class);\n    }\n\n    @Override\n    public DoubleCounter getDoubleCounter(String name) {\n        return (DoubleCounter) getAccumulator(name, DoubleCounter.class);\n    }\n\n    @Override\n    public <V, A extends Serializable> void addAccumulator(\n            String name, Accumulator<V, A> accumulator) {\n        if (accumulators.containsKey(name)) {\n            throw new UnsupportedOperationException(\n                    \"The accumulator '\" + name + \"' already exists and cannot be added.\");\n        }\n        accumulators.put(name, accumulator);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    @Override\n    public <V, A extends Serializable> Accumulator<V, A> getAccumulator(String name) {\n        return (Accumulator<V, A>) accumulators.get(name);\n    }\n\n    @Override\n    public ClassLoader getUserCodeClassLoader() {\n        return this.userCodeClassLoader.asClassLoader();\n    }\n\n    @Override\n    public void registerUserCodeClassLoaderReleaseHookIfAbsent(","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/functions/util/AbstractRuntimeUDFContext.java#L119-L155","documentation":"Thrown by AbstractRuntimeUDFContext.addAccumulator() when an accumulator with the given name already exists in the accumulators map. Accumulator names must be unique within a single task's RuntimeContext; attempting to register a second accumulator under an already-used name is rejected.","triggerScenarios":"A RichFunction calls getRuntimeContext().addAccumulator(name, accumulator) more than once with the same name string, or two functions in the same operator chain share the same accumulator name and both register it.","commonSituations":"A function is reused across multiple operators in a chain and registers accumulators with hardcoded names. Multiple instances of the same RichFunction class in one task slot. Accumulator name collisions caused by copying function code without changing the name string.","solutions":["Use unique accumulator names, e.g. prefix with a task-specific or function-instance-specific identifier.","Before adding, check getRuntimeContext().getAccumulator(name) != null and reuse or skip instead of re-adding.","Register accumulators in open() (called once) rather than in per-record methods like map()/flatMap() where they might be called repeatedly."],"exampleFix":"// before — registers on every record, throws on second\npublic String map(String value) {\n    getRuntimeContext().addAccumulator(\"count\", new LongCounter());\n    ...\n}\n\n// after — register once in open()\nprivate LongCounter counter;\n\n@Override\npublic void open(OpenContext ctx) {\n    counter = new LongCounter();\n    getRuntimeContext().addAccumulator(\"count\", counter);\n}\n\npublic String map(String value) {\n    counter.add(1);\n    ...\n}","handlingStrategy":"validation","validationCode":"// Check before adding\nRuntimeContext ctx = getRuntimeContext();\nif (ctx.getAccumulator(\"myCounter\") == null) {\n    ctx.addAccumulator(\"myCounter\", new LongCounter());\n}\n// Best practice: do this once in open()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Register accumulators in open() (called once per task), not in per-record methods.","Use unique names, optionally namespaced by task or function instance.","Check getAccumulator(name) for null before calling addAccumulator() if registration may be repeated."],"tags":["accumulator","runtime-context","duplicate","user-error"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}