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
030import com.google.common.annotations.VisibleForTesting;
031
032/**
033 * The default metrics system singleton. This class is used by all the daemon
034 * processes(such as NameNode, DataNode, JobTracker etc.). During daemon process
035 * initialization the processes call {@link DefaultMetricsSystem#init(String)}
036 * to initialize the {@link MetricsSystem}.
037 */
038@InterfaceAudience.Public
039@InterfaceStability.Evolving
040public enum DefaultMetricsSystem {
041  INSTANCE; // the singleton
042
043  private AtomicReference<MetricsSystem> impl =
044      new AtomicReference<MetricsSystem>(new MetricsSystemImpl());
045  
046  @VisibleForTesting
047  volatile boolean miniClusterMode = false;
048  
049  transient final UniqueNames mBeanNames = new UniqueNames();
050  transient final UniqueNames sourceNames = new UniqueNames();
051
052  /**
053   * Convenience method to initialize the metrics system
054   * @param prefix  for the metrics system configuration
055   * @return the metrics system instance
056   */
057  public static MetricsSystem initialize(String prefix) {
058    return INSTANCE.init(prefix);
059  }
060
061  MetricsSystem init(String prefix) {
062    return impl.get().init(prefix);
063  }
064
065  /**
066   * @return the metrics system object
067   */
068  public static MetricsSystem instance() {
069    return INSTANCE.getImpl();
070  }
071
072  /**
073   * Shutdown the metrics system
074   */
075  public static void shutdown() {
076    INSTANCE.shutdownInstance();
077  }
078
079  void shutdownInstance() {
080    boolean last = impl.get().shutdown();
081    if (last) synchronized(this) {
082      mBeanNames.map.clear();
083      sourceNames.map.clear();
084    }
085  }
086
087  @InterfaceAudience.Private
088  public static MetricsSystem setInstance(MetricsSystem ms) {
089    return INSTANCE.setImpl(ms);
090  }
091
092  MetricsSystem setImpl(MetricsSystem ms) {
093    return impl.getAndSet(ms);
094  }
095
096  MetricsSystem getImpl() { return impl.get(); }
097
098  @VisibleForTesting
099  public static void setMiniClusterMode(boolean choice) {
100    INSTANCE.miniClusterMode = choice;
101  }
102
103  @VisibleForTesting
104  public static boolean inMiniClusterMode() {
105    return INSTANCE.miniClusterMode;
106  }
107
108  @InterfaceAudience.Private
109  public static ObjectName newMBeanName(String name) {
110    return INSTANCE.newObjectName(name);
111  }
112
113  @InterfaceAudience.Private
114  public static void removeMBeanName(ObjectName name) {
115    INSTANCE.removeObjectName(name.toString());
116  }
117
118  @InterfaceAudience.Private
119  public static String sourceName(String name, boolean dupOK) {
120    return INSTANCE.newSourceName(name, dupOK);
121  }
122
123  synchronized ObjectName newObjectName(String name) {
124    try {
125      if (mBeanNames.map.containsKey(name) && !miniClusterMode) {
126        throw new MetricsException(name +" already exists!");
127      }
128      return new ObjectName(mBeanNames.uniqueName(name));
129    } catch (Exception e) {
130      throw new MetricsException(e);
131    }
132  }
133
134  synchronized void removeObjectName(String name) {
135    mBeanNames.map.remove(name);
136  }
137
138  synchronized String newSourceName(String name, boolean dupOK) {
139    if (sourceNames.map.containsKey(name)) {
140      if (dupOK) {
141        return name;
142      } else if (!miniClusterMode) {
143        throw new MetricsException("Metrics source "+ name +" already exists!");
144      }
145    }
146    return sourceNames.uniqueName(name);
147  }
148}