Browse Source

HDFS-16973. RBF: MountTableResolver cache size lookup should take read lock (#5533)

Viraj Jasani 2 years ago
parent
commit
422bf3b24c

+ 9 - 4
hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/MountTableResolver.java

@@ -678,11 +678,16 @@ public class MountTableResolver
    * @return Size of the cache.
    * @throws IOException If the cache is not initialized.
    */
-  protected long getCacheSize() throws IOException{
-    if (this.locationCache != null) {
-      return this.locationCache.size();
+  protected long getCacheSize() throws IOException {
+    this.readLock.lock();
+    try {
+      if (this.locationCache != null) {
+        return this.locationCache.size();
+      }
+      throw new IOException("localCache is null");
+    } finally {
+      this.readLock.unlock();
     }
-    throw new IOException("localCache is null");
   }
 
   @VisibleForTesting

+ 10 - 0
hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/resolver/TestMountTableResolver.java

@@ -552,6 +552,16 @@ public class TestMountTableResolver {
 
     assertEquals(100000, mountTable.getMountPoints("/").size());
     assertEquals(100000, mountTable.getMounts("/").size());
+    // test concurrency for mount table cache size when it gets updated frequently
+    for (int i = 0; i < 20; i++) {
+      mountTable.getDestinationForPath("/" + i);
+      if (i >= 10) {
+        assertEquals(TEST_MAX_CACHE_SIZE, mountTable.getCacheSize());
+      } else {
+        assertEquals(i + 1, mountTable.getCacheSize());
+      }
+    }
+    assertEquals(TEST_MAX_CACHE_SIZE, mountTable.getCacheSize());
 
     // Add 1000 entries in deep list
     mountTable.refreshEntries(emptyList);