public void removeSourceName(String name) {
    if (sourceNamesField == null || mapField == null) {
      return;
    }
    try {
      Object sourceNames = sourceNamesField.get(DefaultMetricsSystem.INSTANCE);
      HashMap map = (HashMap) mapField.get(sourceNames);
      synchronized (sourceNames) {
        map.remove(name);
      }
    } catch (Exception ex) {
      if (LOG.isTraceEnabled()) {
        LOG.trace(
          "Received exception while trying to access Hadoop Metrics classes via " + "reflection.",
          ex);
      }
    }
  }
Unfortunately Hadoop tries to be too-clever and permanently keeps track of all names registered
so far as a Source, thus preventing further re-registration of the source with the same name.
In case of dynamic metrics tied to region-lifecycles, this becomes a problem because we would
like to be able to re-register and remove with the same name. Otherwise, it is resource leak.
This ugly code manually removes the name from the UniqueNames map. TODO: May not be needed for
Hadoop versions after YARN-5190.
hbase-hadoop-compat/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystemHelper.java