浏览代码

svn merge -c 1548161 FIXES: HDFS-5514. FSNamesystem's fsLock should allow custom implementation (daryn)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1548168 13f79535-47bb-0310-9956-ffa450edef68
Daryn Sharp 11 年之前
父节点
当前提交
c90426e4d7

+ 2 - 0
hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt

@@ -28,6 +28,8 @@ Release 2.4.0 - UNRELEASED
     HDFS-5444. Choose default web UI based on browser capabilities. (Haohui Mai
     HDFS-5444. Choose default web UI based on browser capabilities. (Haohui Mai
     via jing9)
     via jing9)
 
 
+    HDFS-5514. FSNamesystem's fsLock should allow custom implementation (daryn)
+
   IMPROVEMENTS
   IMPROVEMENTS
 
 
     HDFS-5267. Remove volatile from LightWeightHashSet. (Junping Du via llu)
     HDFS-5267. Remove volatile from LightWeightHashSet. (Junping Du via llu)

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

@@ -446,7 +446,7 @@ public class FSNamesystem implements Namesystem, FSClusterStats,
   private final long accessTimePrecision;
   private final long accessTimePrecision;
 
 
   /** Lock to protect FSNamesystem. */
   /** Lock to protect FSNamesystem. */
-  private ReentrantReadWriteLock fsLock;
+  private FSNamesystemLock fsLock;
 
 
   /**
   /**
    * Used when this NN is in standby state to read from the shared edit log.
    * Used when this NN is in standby state to read from the shared edit log.
@@ -629,7 +629,7 @@ public class FSNamesystem implements Namesystem, FSClusterStats,
       throws IOException {
       throws IOException {
     boolean fair = conf.getBoolean("dfs.namenode.fslock.fair", true);
     boolean fair = conf.getBoolean("dfs.namenode.fslock.fair", true);
     LOG.info("fsLock is fair:" + fair);
     LOG.info("fsLock is fair:" + fair);
-    fsLock = new ReentrantReadWriteLock(fair);
+    fsLock = new FSNamesystemLock(fair);
     try {
     try {
       resourceRecheckInterval = conf.getLong(
       resourceRecheckInterval = conf.getLong(
           DFS_NAMENODE_RESOURCE_CHECK_INTERVAL_KEY,
           DFS_NAMENODE_RESOURCE_CHECK_INTERVAL_KEY,
@@ -6724,12 +6724,12 @@ public class FSNamesystem implements Namesystem, FSClusterStats,
   
   
   @VisibleForTesting
   @VisibleForTesting
   void setFsLockForTests(ReentrantReadWriteLock lock) {
   void setFsLockForTests(ReentrantReadWriteLock lock) {
-    this.fsLock = lock;
+    this.fsLock.coarseLock = lock;
   }
   }
   
   
   @VisibleForTesting
   @VisibleForTesting
   ReentrantReadWriteLock getFsLockForTests() {
   ReentrantReadWriteLock getFsLockForTests() {
-    return fsLock;
+    return fsLock.coarseLock;
   }
   }
 
 
   @VisibleForTesting
   @VisibleForTesting

+ 61 - 0
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystemLock.java

@@ -0,0 +1,61 @@
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdfs.server.namenode;
+
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * Mimics a ReentrantReadWriteLock so more sophisticated locking capabilities
+ * are possible.
+ */
+class FSNamesystemLock implements ReadWriteLock {
+  @VisibleForTesting
+  protected ReentrantReadWriteLock coarseLock;
+  
+  FSNamesystemLock(boolean fair) {
+    this.coarseLock = new ReentrantReadWriteLock(fair);
+  }
+  
+  @Override
+  public Lock readLock() {
+    return coarseLock.readLock();
+  }
+  
+  @Override
+  public Lock writeLock() {
+    return coarseLock.writeLock();
+  }
+  
+  public int getReadHoldCount() {
+    return coarseLock.getReadHoldCount();
+  }
+  
+  public int getWriteHoldCount() {
+    return coarseLock.getWriteHoldCount();
+  }
+  
+  public boolean isWriteLockedByCurrentThread() {
+    return coarseLock.isWriteLockedByCurrentThread();
+  }
+}

+ 36 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestFSNamesystem.java

@@ -158,4 +158,40 @@ public class TestFSNamesystem {
     fsNamesystem = new FSNamesystem(conf, fsImage);
     fsNamesystem = new FSNamesystem(conf, fsImage);
     assertFalse(fsNamesystem.getFsLockForTests().isFair());
     assertFalse(fsNamesystem.getFsLockForTests().isFair());
   }  
   }  
+  
+  @Test
+  public void testFSNamesystemLockCompatibility() {
+    FSNamesystemLock rwLock = new FSNamesystemLock(true);
+
+    assertEquals(0, rwLock.getReadHoldCount());
+    rwLock.readLock().lock();
+    assertEquals(1, rwLock.getReadHoldCount());
+
+    rwLock.readLock().lock();
+    assertEquals(2, rwLock.getReadHoldCount());
+
+    rwLock.readLock().unlock();
+    assertEquals(1, rwLock.getReadHoldCount());
+
+    rwLock.readLock().unlock();
+    assertEquals(0, rwLock.getReadHoldCount());
+
+    assertFalse(rwLock.isWriteLockedByCurrentThread());
+    assertEquals(0, rwLock.getWriteHoldCount());
+    rwLock.writeLock().lock();
+    assertTrue(rwLock.isWriteLockedByCurrentThread());
+    assertEquals(1, rwLock.getWriteHoldCount());
+    
+    rwLock.writeLock().lock();
+    assertTrue(rwLock.isWriteLockedByCurrentThread());
+    assertEquals(2, rwLock.getWriteHoldCount());
+
+    rwLock.writeLock().unlock();
+    assertTrue(rwLock.isWriteLockedByCurrentThread());
+    assertEquals(1, rwLock.getWriteHoldCount());
+
+    rwLock.writeLock().unlock();
+    assertFalse(rwLock.isWriteLockedByCurrentThread());
+    assertEquals(0, rwLock.getWriteHoldCount());
+  }
 }
 }