diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/metrics/NamenodeBeanMetrics.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/metrics/NamenodeBeanMetrics.java index 6d26aa0945a..2e54765d442 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/metrics/NamenodeBeanMetrics.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/metrics/NamenodeBeanMetrics.java @@ -838,6 +838,11 @@ public class NamenodeBeanMetrics return null; } + @Override + public long getCurrentTokensCount() { + return 0; + } + private Router getRouter() throws IOException { if (this.router == null) { throw new IOException("Router is not initialized"); 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 70b65f3982c..36402af4df0 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 @@ -4423,6 +4423,13 @@ public class FSNamesystem implements Namesystem, FSNamesystemMBean, return dir.ezManager.getNumEncryptionZones(); } + @Override // FSNamesystemMBean + @Metric({ "CurrentTokensCount", "The number of delegation tokens"}) + public long getCurrentTokensCount() { + return dtSecretManager != null ? + dtSecretManager.getCurrentTokensSize() : -1; + } + /** * Returns the length of the wait Queue for the FSNameSystemLock. * diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/metrics/FSNamesystemMBean.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/metrics/FSNamesystemMBean.java index c25bafd48d7..7dbddc2d3ae 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/metrics/FSNamesystemMBean.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/metrics/FSNamesystemMBean.java @@ -242,4 +242,10 @@ public interface FSNamesystemMBean { * @return Number of ENTERING_MAINTENANCE data nodes */ int getNumEnteringMaintenanceDataNodes(); + + /** + * Get the current number of delegation tokens in memory. + * @return number of DTs + */ + long getCurrentTokensCount(); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/security/TestDelegationToken.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/security/TestDelegationToken.java index e50c397b9c6..077b10320fd 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/security/TestDelegationToken.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/security/TestDelegationToken.java @@ -20,6 +20,7 @@ package org.apache.hadoop.hdfs.security; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -41,6 +42,7 @@ import org.apache.hadoop.hdfs.MiniDFSCluster; import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier; import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenSecretManager; import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.StartupOption; +import org.apache.hadoop.hdfs.server.namenode.FSNamesystem; import org.apache.hadoop.hdfs.server.namenode.NameNode; import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter; import org.apache.hadoop.hdfs.server.namenode.web.resources.NamenodeWebHdfsMethods; @@ -153,7 +155,27 @@ public class TestDelegationToken { // PASS } } - + + @Test + public void testDelegationTokenMetrics() throws Exception { + FSNamesystem namesystem = cluster.getNamesystem(); + // should start with no token + assertEquals(0, namesystem.getCurrentTokensCount()); + + // get token + Token token = generateDelegationToken( + "SomeUser", "JobTracker"); + assertEquals(1, namesystem.getCurrentTokensCount()); + + // Renew token shouldn't change the count of tokens + dtSecretManager.renewToken(token, "JobTracker"); + assertEquals(1, namesystem.getCurrentTokensCount()); + + // Cancel token should remove the token from memory + dtSecretManager.cancelToken(token, "JobTracker"); + assertEquals(0, namesystem.getCurrentTokensCount()); + } + @Test public void testAddDelegationTokensDFSApi() throws Exception { UserGroupInformation ugi = UserGroupInformation.createRemoteUser("JobTracker");