HDFS-14449. Expose total number of DT in JMX for Namenode. Contributed by Fengnan Li.

This commit is contained in:
Inigo Goiri 2019-07-30 13:45:27 -07:00
parent e68d8446c4
commit 7849bdcf70
4 changed files with 41 additions and 1 deletions

View File

@ -838,6 +838,11 @@ public class NamenodeBeanMetrics
return null; return null;
} }
@Override
public long getCurrentTokensCount() {
return 0;
}
private Router getRouter() throws IOException { private Router getRouter() throws IOException {
if (this.router == null) { if (this.router == null) {
throw new IOException("Router is not initialized"); throw new IOException("Router is not initialized");

View File

@ -4423,6 +4423,13 @@ public class FSNamesystem implements Namesystem, FSNamesystemMBean,
return dir.ezManager.getNumEncryptionZones(); 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. * Returns the length of the wait Queue for the FSNameSystemLock.
* *

View File

@ -242,4 +242,10 @@ public interface FSNamesystemMBean {
* @return Number of ENTERING_MAINTENANCE data nodes * @return Number of ENTERING_MAINTENANCE data nodes
*/ */
int getNumEnteringMaintenanceDataNodes(); int getNumEnteringMaintenanceDataNodes();
/**
* Get the current number of delegation tokens in memory.
* @return number of DTs
*/
long getCurrentTokensCount();
} }

View File

@ -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.assertFalse;
import static org.junit.Assert.assertTrue; 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.DelegationTokenIdentifier;
import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenSecretManager; import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenSecretManager;
import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.StartupOption; 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.NameNode;
import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter; import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter;
import org.apache.hadoop.hdfs.server.namenode.web.resources.NamenodeWebHdfsMethods; import org.apache.hadoop.hdfs.server.namenode.web.resources.NamenodeWebHdfsMethods;
@ -153,7 +155,27 @@ public class TestDelegationToken {
// PASS // PASS
} }
} }
@Test
public void testDelegationTokenMetrics() throws Exception {
FSNamesystem namesystem = cluster.getNamesystem();
// should start with no token
assertEquals(0, namesystem.getCurrentTokensCount());
// get token
Token<DelegationTokenIdentifier> 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 @Test
public void testAddDelegationTokensDFSApi() throws Exception { public void testAddDelegationTokensDFSApi() throws Exception {
UserGroupInformation ugi = UserGroupInformation.createRemoteUser("JobTracker"); UserGroupInformation ugi = UserGroupInformation.createRemoteUser("JobTracker");