Browse Source

HDFS-14374. Expose total number of delegation tokens in AbstractDelegationTokenSecretManager. Contributed by CR Hota.

Inigo Goiri 6 years ago
parent
commit
fb1c549139

+ 8 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/delegation/AbstractDelegationTokenSecretManager.java

@@ -145,7 +145,14 @@ extends AbstractDelegationTokenIdentifier>
     setDelegationTokenSeqNum(0);
     setDelegationTokenSeqNum(0);
     currentTokens.clear();
     currentTokens.clear();
   }
   }
-  
+
+  /**
+   * Total count of active delegation tokens.
+   */
+  public long getCurrentTokensSize() {
+    return currentTokens.size();
+  }
+
   /** 
   /** 
    * Add a previously used master key to cache (when NN restarts), 
    * Add a previously used master key to cache (when NN restarts), 
    * should be called before activate(). 
    * should be called before activate(). 

+ 23 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/TestDelegationToken.java

@@ -259,6 +259,29 @@ public class TestDelegationToken {
                  ugi.getRealUser().getAuthenticationMethod());
                  ugi.getRealUser().getAuthenticationMethod());
   }
   }
 
 
+  @Test
+  public void testDelegationTokenCount() throws Exception {
+    final TestDelegationTokenSecretManager dtSecretManager =
+        new TestDelegationTokenSecretManager(24*60*60*1000,
+            3*1000, 1*1000, 3600000);
+    try {
+      dtSecretManager.startThreads();
+      Assert.assertEquals(dtSecretManager.getCurrentTokensSize(), 0);
+      final Token<TestDelegationTokenIdentifier> token1 =
+          generateDelegationToken(dtSecretManager, "SomeUser", "JobTracker");
+      Assert.assertEquals(dtSecretManager.getCurrentTokensSize(), 1);
+      final Token<TestDelegationTokenIdentifier> token2 =
+          generateDelegationToken(dtSecretManager, "SomeUser", "JobTracker");
+      Assert.assertEquals(dtSecretManager.getCurrentTokensSize(), 2);
+      dtSecretManager.cancelToken(token1, "JobTracker");
+      Assert.assertEquals(dtSecretManager.getCurrentTokensSize(), 1);
+      dtSecretManager.cancelToken(token2, "JobTracker");
+      Assert.assertEquals(dtSecretManager.getCurrentTokensSize(), 0);
+    } finally {
+      dtSecretManager.stopThreads();
+    }
+  }
+
   @Test
   @Test
   public void testDelegationTokenSecretManager() throws Exception {
   public void testDelegationTokenSecretManager() throws Exception {
     final TestDelegationTokenSecretManager dtSecretManager = 
     final TestDelegationTokenSecretManager dtSecretManager =