apache/hadoop · error · MetricsException

{} already exists!

Error message

{} already exists!

What it means

DefaultMetricsSystem keeps a JVM-wide map of registered JMX object names. newObjectName(name) throws MetricsException('<name> already exists!') when the name is already mapped and miniClusterMode is off; when duplicates are tolerated, uniqueName() appends a numeric suffix instead.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java:131

  @InterfaceAudience.Private
  public static void removeMBeanName(ObjectName name) {
    INSTANCE.removeObjectName(name.toString());
  }

  @InterfaceAudience.Private
  public static void removeSourceName(String name) {
    INSTANCE.removeSource(name);
  }

  @InterfaceAudience.Private
  public static String sourceName(String name, boolean dupOK) {
    return INSTANCE.newSourceName(name, dupOK);
  }

  synchronized ObjectName newObjectName(String name) {
    try {
      if (mBeanNames.map.containsKey(name) && !miniClusterMode) {
        throw new MetricsException(name +" already exists!");
      }
      return new ObjectName(mBeanNames.uniqueName(name));
    } catch (Exception e) {
      throw new MetricsException(e);
    }
  }

  synchronized void removeObjectName(String name) {
    mBeanNames.map.remove(name);
  }

  synchronized void removeSource(String name) {
    sourceNames.map.remove(name);
  }

  synchronized String newSourceName(String name, boolean dupOK) {
    if (sourceNames.map.containsKey(name)) {
      if (dupOK) {

View on GitHub (pinned to 2add963021)

Solutions

  1. Unregister the old source before re-registering: metricsSystem.unregisterSource(name) / DefaultMetricsSystem.removeSourceName(name) plus removeObjectName(name)
  2. Make the source name unique per instance (append an instance id)
  3. In test JVMs, call DefaultMetricsSystem.setMiniClusterMode(true) so duplicates get unique suffixed names

Example fix

// before
metricsSystem.register("MySource", "My source", myImpl);  // 2nd registration throws

// after
metricsSystem.unregisterSource("MySource");  // or DefaultMetricsSystem.removeSourceName("MySource")
metricsSystem.register("MySource", "My source", myImpl);
Defensive patterns

Strategy: try-catch

Try / catch

try {
  metricsSystem.register(name, desc, source);
} catch (MetricsException e) {
  if (e.getMessage().contains("already exists")) {
    metricsSystem.unregisterSource(name);  // stale registration from a previous lifecycle
    metricsSystem.register(name, desc, source);
  } else throw e;
}

Prevention

When it happens

Trigger: Registering a metrics source whose MBean name collides with one already registered in the same JVM: MetricsSystem.register(...) twice with the same name, two components choosing the same source name, or re-registering after re-init without unregistering first.

Common situations: Unit tests and mini-clusters that repeatedly initialize metrics; multiple instances of a component in one process; redeploy code that rebuilds the metrics system without shutting down the old one.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/3a904995bc8c30f1. Report an issue: GitHub.