1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.metrics2.lib; 20 21 import java.util.concurrent.atomic.AtomicReference; 22 import javax.management.ObjectName; 23 24 import org.apache.hadoop.classification.InterfaceAudience; 25 import org.apache.hadoop.classification.InterfaceStability; 26 import org.apache.hadoop.metrics2.MetricsException; 27 import org.apache.hadoop.metrics2.MetricsSystem; 28 import org.apache.hadoop.metrics2.impl.MetricsSystemImpl; 29 30 /** 31 * The default metrics system singleton 32 */ 33 @InterfaceAudience.Public 34 @InterfaceStability.Evolving 35 public enum DefaultMetricsSystem { 36 INSTANCE; // the singleton 37 38 private AtomicReference<MetricsSystem> impl = 39 new AtomicReference<MetricsSystem>(new MetricsSystemImpl()); 40 volatile boolean miniClusterMode = false; 41 transient final UniqueNames mBeanNames = new UniqueNames(); 42 transient final UniqueNames sourceNames = new UniqueNames(); 43 44 /** 45 * Convenience method to initialize the metrics system 46 * @param prefix for the metrics system configuration 47 * @return the metrics system instance 48 */ initialize(String prefix)49 public static MetricsSystem initialize(String prefix) { 50 return INSTANCE.init(prefix); 51 } 52 init(String prefix)53 MetricsSystem init(String prefix) { 54 return impl.get().init(prefix); 55 } 56 57 /** 58 * @return the metrics system object 59 */ instance()60 public static MetricsSystem instance() { 61 return INSTANCE.getImpl(); 62 } 63 64 /** 65 * Shutdown the metrics system 66 */ shutdown()67 public static void shutdown() { 68 INSTANCE.shutdownInstance(); 69 } 70 shutdownInstance()71 void shutdownInstance() { 72 boolean last = impl.get().shutdown(); 73 if (last) synchronized(this) { 74 mBeanNames.map.clear(); 75 sourceNames.map.clear(); 76 } 77 } 78 79 @InterfaceAudience.Private setInstance(MetricsSystem ms)80 public static MetricsSystem setInstance(MetricsSystem ms) { 81 return INSTANCE.setImpl(ms); 82 } 83 setImpl(MetricsSystem ms)84 MetricsSystem setImpl(MetricsSystem ms) { 85 return impl.getAndSet(ms); 86 } 87 getImpl()88 MetricsSystem getImpl() { return impl.get(); } 89 90 @InterfaceAudience.Private setMiniClusterMode(boolean choice)91 public static void setMiniClusterMode(boolean choice) { 92 INSTANCE.miniClusterMode = choice; 93 } 94 95 @InterfaceAudience.Private inMiniClusterMode()96 public static boolean inMiniClusterMode() { 97 return INSTANCE.miniClusterMode; 98 } 99 100 @InterfaceAudience.Private newMBeanName(String name)101 public static ObjectName newMBeanName(String name) { 102 return INSTANCE.newObjectName(name); 103 } 104 105 @InterfaceAudience.Private removeMBeanName(ObjectName name)106 public static void removeMBeanName(ObjectName name) { 107 INSTANCE.removeObjectName(name.toString()); 108 } 109 110 @InterfaceAudience.Private sourceName(String name, boolean dupOK)111 public static String sourceName(String name, boolean dupOK) { 112 return INSTANCE.newSourceName(name, dupOK); 113 } 114 newObjectName(String name)115 synchronized ObjectName newObjectName(String name) { 116 try { 117 if (mBeanNames.map.containsKey(name) && !miniClusterMode) { 118 throw new MetricsException(name +" already exists!"); 119 } 120 return new ObjectName(mBeanNames.uniqueName(name)); 121 } catch (Exception e) { 122 throw new MetricsException(e); 123 } 124 } 125 removeObjectName(String name)126 synchronized void removeObjectName(String name) { 127 mBeanNames.map.remove(name); 128 } 129 newSourceName(String name, boolean dupOK)130 synchronized String newSourceName(String name, boolean dupOK) { 131 if (sourceNames.map.containsKey(name)) { 132 if (dupOK) { 133 return name; 134 } else if (!miniClusterMode) { 135 throw new MetricsException("Metrics source "+ name +" already exists!"); 136 } 137 } 138 return sourceNames.uniqueName(name); 139 } 140 } 141