Browse Source

HDFS-10183. Prevent race condition during class initialization. Contributed by Pavel Avgustinov.

(cherry picked from commit f40969a141ec6aff254c41e4185cc61ea9e4e554)
Sangjin Lee 7 years ago
parent
commit
ac2bea815f

+ 4 - 4
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLogAsync.java

@@ -40,7 +40,7 @@ class FSEditLogAsync extends FSEditLog implements Runnable {
   // use separate mutex to avoid possible deadlock when stopping the thread.
   private final Object syncThreadLock = new Object();
   private Thread syncThread;
-  private static ThreadLocal<Edit> threadEdit = new ThreadLocal<Edit>();
+  private static final ThreadLocal<Edit> THREAD_EDIT = new ThreadLocal<Edit>();
 
   // requires concurrent access from caller threads and syncing thread.
   private final BlockingQueue<Edit> editPendingQ =
@@ -114,16 +114,16 @@ class FSEditLogAsync extends FSEditLog implements Runnable {
   @Override
   void logEdit(final FSEditLogOp op) {
     Edit edit = getEditInstance(op);
-    threadEdit.set(edit);
+    THREAD_EDIT.set(edit);
     enqueueEdit(edit);
   }
 
   @Override
   public void logSync() {
-    Edit edit = threadEdit.get();
+    Edit edit = THREAD_EDIT.get();
     if (edit != null) {
       // do NOT remove to avoid expunge & rehash penalties.
-      threadEdit.set(null);
+      THREAD_EDIT.set(null);
       if (LOG.isDebugEnabled()) {
         LOG.debug("logSync " + edit);
       }

+ 2 - 2
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLogOp.java

@@ -148,7 +148,7 @@ public abstract class FSEditLogOp {
   int rpcCallId;
 
   public static class OpInstanceCache {
-    private static ThreadLocal<OpInstanceCacheMap> cache =
+    private static final ThreadLocal<OpInstanceCacheMap> CACHE =
         new ThreadLocal<OpInstanceCacheMap>() {
       @Override
       protected OpInstanceCacheMap initialValue() {
@@ -179,7 +179,7 @@ public abstract class FSEditLogOp {
 
     @SuppressWarnings("unchecked")
     public <T extends FSEditLogOp> T get(FSEditLogOpCodes opCode) {
-      return useCache ? (T)cache.get().get(opCode) : (T)newInstance(opCode);
+      return useCache ? (T)CACHE.get().get(opCode) : (T)newInstance(opCode);
     }
 
     private static FSEditLogOp newInstance(FSEditLogOpCodes opCode) {