{"record":{"id":"ff618026f7bca893","repo":"apache/hadoop","slug":"metrics-source-already-exists","errorCode":null,"errorMessage":"Metrics source {} already exists!","messagePattern":"Metrics source (.+?) already exists!","errorType":"exception","errorClass":"MetricsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java","lineNumber":152,"sourceCode":"    } catch (Exception e) {\n      throw new MetricsException(e);\n    }\n  }\n\n  synchronized void removeObjectName(String name) {\n    mBeanNames.map.remove(name);\n  }\n\n  synchronized void removeSource(String name) {\n    sourceNames.map.remove(name);\n  }\n\n  synchronized String newSourceName(String name, boolean dupOK) {\n    if (sourceNames.map.containsKey(name)) {\n      if (dupOK) {\n        return name;\n      } else if (!miniClusterMode) {\n        throw new MetricsException(\"Metrics source \"+ name +\" already exists!\");\n      }\n    }\n    return sourceNames.uniqueName(name);\n  }\n}\n","sourceCodeStart":134,"sourceCodeEnd":158,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java#L134-L158","documentation":"DefaultMetricsSystem.newSourceName(name, dupOK), reached via sourceName(name, dupOK), throws MetricsException('Metrics source <name> already exists!') when the name is already in sourceNames, dupOK is false, and miniClusterMode is off. Passing dupOK=true tolerates exact duplicates instead of throwing.","triggerScenarios":"Creating a metrics source/record name that already exists in this JVM with dupOK=false — e.g., initializing metrics twice for the same component name without removing the old one.","commonSituations":"Repeated init in unit tests; two subsystems that both chose the same source name; component restart inside a long-lived process.","solutions":["Pass dupOK=true where exact duplicate names are acceptable (e.g., per-attempt records)","Remove the previous name via DefaultMetricsSystem.removeSourceName(name) before re-creating it","Enable DefaultMetricsSystem.setMiniClusterMode(true) in tests so unique suffixed names are generated"],"exampleFix":"// before\nString name = DefaultMetricsSystem.sourceName(\"MyRecord\", false);  // throws if exists\n\n// after\nString name = DefaultMetricsSystem.sourceName(\"MyRecord\", true);  // dup OK","handlingStrategy":"validation","validationCode":"boolean duplicateOk = lifecycleAllowsReuse;  // per-attempt records: true\nString name = DefaultMetricsSystem.sourceName(\"MyRecord\", duplicateOk);","typeGuard":null,"tryCatchPattern":"try {\n  String name = DefaultMetricsSystem.sourceName(\"MyRecord\", false);\n} catch (MetricsException e) {\n  DefaultMetricsSystem.removeSourceName(\"MyRecord\");  // clear stale name and retry once\n}","preventionTips":["Pass dupOK=true when exact duplicate record names are semantically acceptable","Remove source names on teardown of components that create them","In tests, set miniClusterMode so duplicate names get unique suffixes instead of throwing"],"tags":["metrics2","hadoop","duplicate-registration","lifecycle"],"backgroundTag":"duplicate-metrics-source-registration","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}