From 6828337d855abe8564ece2266ace7cfc51ee16ec Mon Sep 17 00:00:00 2001 From: Daryn Sharp Date: Thu, 5 Dec 2013 15:28:12 +0000 Subject: [PATCH] HDFS-5514. FSNamesystem's fsLock should allow custom implementation (daryn) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1548161 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 2 ++ .../hdfs/server/namenode/FSNamesystem.java | 8 ++--- .../server/namenode/TestFSNamesystem.java | 36 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index d7ff8fb7a7..379b01af43 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -457,6 +457,8 @@ Release 2.4.0 - UNRELEASED HDFS-5444. Choose default web UI based on browser capabilities. (Haohui Mai via jing9) + HDFS-5514. FSNamesystem's fsLock should allow custom implementation (daryn) + IMPROVEMENTS HDFS-5267. Remove volatile from LightWeightHashSet. (Junping Du via llu) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java index 0cae09863b..6a74b49fa5 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java @@ -467,7 +467,7 @@ private void logAuditEvent(boolean succeeded, private final long accessTimePrecision; /** 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. @@ -650,7 +650,7 @@ public static FSNamesystem loadFromDisk(Configuration conf) throws IOException { boolean fair = conf.getBoolean("dfs.namenode.fslock.fair", true); LOG.info("fsLock is fair:" + fair); - fsLock = new ReentrantReadWriteLock(fair); + fsLock = new FSNamesystemLock(fair); try { resourceRecheckInterval = conf.getLong( DFS_NAMENODE_RESOURCE_CHECK_INTERVAL_KEY, @@ -6771,12 +6771,12 @@ public void setEditLogTailerForTests(EditLogTailer tailer) { @VisibleForTesting void setFsLockForTests(ReentrantReadWriteLock lock) { - this.fsLock = lock; + this.fsLock.coarseLock = lock; } @VisibleForTesting ReentrantReadWriteLock getFsLockForTests() { - return fsLock; + return fsLock.coarseLock; } @VisibleForTesting diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestFSNamesystem.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestFSNamesystem.java index a2bd172d5e..3af20a7303 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestFSNamesystem.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestFSNamesystem.java @@ -158,4 +158,40 @@ public void testFsLockFairness() throws IOException, InterruptedException{ fsNamesystem = new FSNamesystem(conf, fsImage); 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()); + } }