{"record":{"id":"af9fa10f7ed9f5b4","repo":"nathanmarz/storm","slug":"state-spout-has-already-been-declared-for-id-id","errorCode":null,"errorMessage":"State spout has already been declared for id ${id}","messagePattern":"State spout has already been declared for id (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"storm-core/src/jvm/backtype/storm/topology/TopologyBuilder.java","lineNumber":218,"sourceCode":"    public void setStateSpout(String id, IRichStateSpout stateSpout) {\n        setStateSpout(id, stateSpout, null);\n    }\n\n    public void setStateSpout(String id, IRichStateSpout stateSpout, Number parallelism_hint) {\n        validateUnusedId(id);\n        // TODO: finish\n    }\n\n\n    private void validateUnusedId(String id) {\n        if(_bolts.containsKey(id)) {\n            throw new IllegalArgumentException(\"Bolt has already been declared for id \" + id);\n        }\n        if(_spouts.containsKey(id)) {\n            throw new IllegalArgumentException(\"Spout has already been declared for id \" + id);\n        }\n        if(_stateSpouts.containsKey(id)) {\n            throw new IllegalArgumentException(\"State spout has already been declared for id \" + id);\n        }\n    }\n\n    private ComponentCommon getComponentCommon(String id, IComponent component) {\n        ComponentCommon ret = new ComponentCommon(_commons.get(id));\n        \n        OutputFieldsGetter getter = new OutputFieldsGetter();\n        component.declareOutputFields(getter);\n        ret.set_streams(getter.getFieldsDeclaration());\n        return ret;        \n    }\n    \n    private void initCommon(String id, IComponent component, Number parallelism) {\n        ComponentCommon common = new ComponentCommon();\n        common.set_inputs(new HashMap<GlobalStreamId, Grouping>());\n        if(parallelism!=null) common.set_parallelism_hint(parallelism.intValue());\n        Map conf = component.getComponentConfiguration();\n        if(conf!=null) common.set_json_conf(JSONValue.toJSONString(conf));","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/nathanmarz/storm/blob/cdb116e942666973bc4eaa0df098d5bab82739e7/storm-core/src/jvm/backtype/storm/topology/TopologyBuilder.java#L200-L236","documentation":"TopologyBuilder.validateUnusedId throws IllegalArgumentException when a component id collides with an already-registered state spout (_stateSpouts). Ids must be unique across all component kinds in the topology.","triggerScenarios":"Declaring a state spout (or bolt/spout) with an id already present in _stateSpouts via setStateSpout; loops re-declaring state spouts with fixed ids.","commonSituations":"State spout id copied from a bolt id; transactional topologies declaring state spouts and spouts under the same id; builder reuse after a prior declaration.","solutions":["Use a distinct id for the state spout","Ensure generated ids are namespaced per component kind (e.g. \"state-\" + name)","Use a fresh TopologyBuilder for each build","Check for duplicate setStateSpout calls with the same id"],"exampleFix":"// before\nbuilder.setStateSpout(\"tx\", new MyStateSpout());\nbuilder.setSpout(\"tx\", new TransactionalSpout()); // throws\n// after\nbuilder.setStateSpout(\"tx-state\", new MyStateSpout());\nbuilder.setSpout(\"tx\", new TransactionalSpout());","handlingStrategy":"validation","validationCode":"Set<String> used = new HashSet<>();\nvoid safeStateSpout(TopologyBuilder b, String id, IStateSpout ss) {\n    if (!used.add(id))\n        throw new IllegalStateException(\"duplicate component id: \" + id);\n    b.setStateSpout(id, ss);\n}","typeGuard":null,"tryCatchPattern":"try {\n    builder.setStateSpout(id, stateSpout);\n} catch (IllegalArgumentException e) {\n    if (!e.getMessage().contains(\"already been declared\")) throw e;\n    // handle duplicate id\n}","preventionTips":["Namespace state spout ids distinctly from bolts/spouts","Check the whole topology (bolts + spouts + state spouts) for id uniqueness before build","Never reuse a populated TopologyBuilder"],"tags":["topology","duplicate-id","storm"],"backgroundTag":"invalid-argument-value","analyzedSha":"cdb116e942666973bc4eaa0df098d5bab82739e7","analyzedAt":"2026-09-12T14:30:00.714Z","contentChangedAt":"2026-09-12T14:30:00.714Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}