001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.metrics2.lib;
020
021import java.util.concurrent.atomic.AtomicReference;
022import javax.management.ObjectName;
023
024import org.apache.hadoop.classification.InterfaceAudience;
025import org.apache.hadoop.classification.InterfaceStability;
026import org.apache.hadoop.metrics2.MetricsException;
027import org.apache.hadoop.metrics2.MetricsSystem;
028import org.apache.hadoop.metrics2.impl.MetricsSystemImpl;
029
030/**
031 * The default metrics system singleton
032 */
033@InterfaceAudience.Public
034@InterfaceStability.Evolving
035public enum DefaultMetricsSystem {
036  INSTANCE; // the singleton
037
038  private AtomicReference<MetricsSystem> impl =
039      new AtomicReference<MetricsSystem>(new MetricsSystemImpl());
040  volatile boolean miniClusterMode = false;
041  transient final UniqueNames mBeanNames = new UniqueNames();
042  transient final UniqueNames sourceNames = new UniqueNames();
043
044  /**
045   * Convenience method to initialize the metrics system
046   * @param prefix  for the metrics system configuration
047   * @return the metrics system instance
048   */
049  public static MetricsSystem initialize(String prefix) {
050    return INSTANCE.init(prefix);
051  }
052
053  MetricsSystem init(String prefix) {
054    return impl.get().init(prefix);
055  }
056
057  /**
058   * @return the metrics system object
059   */
060  public static MetricsSystem instance() {
061    return INSTANCE.getImpl();
062  }
063
064  /**
065   * Shutdown the metrics system
066   */
067  public static void shutdown() {
068    INSTANCE.shutdownInstance();
069  }
070
071  void shutdownInstance() {
072    boolean last = impl.get().shutdown();
073    if (last) synchronized(this) {
074      mBeanNames.map.clear();
075      sourceNames.map.clear();
076    }
077  }
078
079  @InterfaceAudience.Private
080  public static MetricsSystem setInstance(MetricsSystem ms) {
081    return INSTANCE.setImpl(ms);
082  }
083
084  MetricsSystem setImpl(MetricsSystem ms) {
085    return impl.getAndSet(ms);
086  }
087
088  MetricsSystem getImpl() { return impl.get(); }
089
090  @InterfaceAudience.Private
091  public static void setMiniClusterMode(boolean choice) {
092    INSTANCE.miniClusterMode = choice;
093  }
094
095  @InterfaceAudience.Private
096  public static boolean inMiniClusterMode() {
097    return INSTANCE.miniClusterMode;
098  }
099
100  @InterfaceAudience.Private
101  public static ObjectName newMBeanName(String name) {
102    return INSTANCE.newObjectName(name);
103  }
104
105  @InterfaceAudience.Private
106  public static void removeMBeanName(ObjectName name) {
107    INSTANCE.removeObjectName(name.toString());
108  }
109
110  @InterfaceAudience.Private
111  public static void removeSourceName(String name) {
112    INSTANCE.removeSource(name);
113  }
114
115  @InterfaceAudience.Private
116  public static String sourceName(String name, boolean dupOK) {
117    return INSTANCE.newSourceName(name, dupOK);
118  }
119
120  synchronized ObjectName newObjectName(String name) {
121    try {
122      if (mBeanNames.map.containsKey(name) && !miniClusterMode) {
123        throw new MetricsException(name +" already exists!");
124      }
125      return new ObjectName(mBeanNames.uniqueName(name));
126    } catch (Exception e) {
127      throw new MetricsException(e);
128    }
129  }
130
131  synchronized void removeObjectName(String name) {
132    mBeanNames.map.remove(name);
133  }
134
135  synchronized void removeSource(String name) {
136    sourceNames.map.remove(name);
137  }
138
139  synchronized String newSourceName(String name, boolean dupOK) {
140    if (sourceNames.map.containsKey(name)) {
141      if (dupOK) {
142        return name;
143      } else if (!miniClusterMode) {
144        throw new MetricsException("Metrics source "+ name +" already exists!");
145      }
146    }
147    return sourceNames.uniqueName(name);
148  }
149}