浏览代码

HADOOP-7529. Fix lock cycles in metrics system. (llu)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1157187 13f79535-47bb-0310-9956-ffa450edef68
Luke Lu 13 年之前
父节点
当前提交
7d612e9325

+ 2 - 0
hadoop-common/CHANGES.txt

@@ -493,6 +493,8 @@ Trunk (unreleased changes)
 
     HADOOP-6622. Token should not print the password in toString. (eli)
 
+    HADOOP-7529. Fix lock cycles in metrics system. (llu)
+
 Release 0.22.0 - Unreleased
 
   INCOMPATIBLE CHANGES

+ 11 - 10
hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java

@@ -18,6 +18,7 @@
 
 package org.apache.hadoop.metrics2.lib;
 
+import java.util.concurrent.atomic.AtomicReference;
 import javax.management.ObjectName;
 
 import org.apache.hadoop.classification.InterfaceAudience;
@@ -34,7 +35,8 @@ import org.apache.hadoop.metrics2.impl.MetricsSystemImpl;
 public enum DefaultMetricsSystem {
   INSTANCE; // the singleton
 
-  private MetricsSystem impl = new MetricsSystemImpl();
+  private AtomicReference<MetricsSystem> impl =
+      new AtomicReference<MetricsSystem>(new MetricsSystemImpl());
   volatile boolean miniClusterMode = false;
   final UniqueNames mBeanNames = new UniqueNames();
   final UniqueNames sourceNames = new UniqueNames();
@@ -48,8 +50,8 @@ public enum DefaultMetricsSystem {
     return INSTANCE.init(prefix);
   }
 
-  synchronized MetricsSystem init(String prefix) {
-    return impl.init(prefix);
+  MetricsSystem init(String prefix) {
+    return impl.get().init(prefix);
   }
 
   /**
@@ -66,8 +68,9 @@ public enum DefaultMetricsSystem {
     INSTANCE.shutdownInstance();
   }
 
-  synchronized void shutdownInstance() {
-    if (impl.shutdown()) {
+  void shutdownInstance() {
+    boolean last = impl.get().shutdown();
+    if (last) synchronized(this) {
       mBeanNames.map.clear();
       sourceNames.map.clear();
     }
@@ -78,13 +81,11 @@ public enum DefaultMetricsSystem {
     return INSTANCE.setImpl(ms);
   }
 
-  synchronized MetricsSystem setImpl(MetricsSystem ms) {
-    MetricsSystem old = impl;
-    impl = ms;
-    return old;
+  MetricsSystem setImpl(MetricsSystem ms) {
+    return impl.getAndSet(ms);
   }
 
-  synchronized MetricsSystem getImpl() { return impl; }
+  MetricsSystem getImpl() { return impl.get(); }
 
   @InterfaceAudience.Private
   public static void setMiniClusterMode(boolean choice) {