From daafc8a0b849ffdf851c6a618684656925f1df76 Mon Sep 17 00:00:00 2001 From: zhtttylz Date: Sat, 27 Apr 2024 20:36:11 +0800 Subject: [PATCH] HDFS-17367. Add PercentUsed for Different StorageTypes in JMX (#6735) Contributed by Hualong Zhang. Signed-off-by: Shilun Fan --- .../src/site/markdown/Metrics.md | 9 +++++++ .../blockmanagement/StorageTypeStats.java | 19 ++++++++++++++ .../blockmanagement/TestBlockStatsMXBean.java | 26 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/hadoop-common-project/hadoop-common/src/site/markdown/Metrics.md b/hadoop-common-project/hadoop-common/src/site/markdown/Metrics.md index b75523b08a..aaead83710 100644 --- a/hadoop-common-project/hadoop-common/src/site/markdown/Metrics.md +++ b/hadoop-common-project/hadoop-common/src/site/markdown/Metrics.md @@ -326,6 +326,15 @@ Each metrics record contains tags such as HAState and Hostname as additional inf | `FSN(Read/Write)LockOverallNanosAvgTime` | Average time of holding the lock by all operations in nanoseconds | | `PendingSPSPaths` | The number of paths to be processed by storage policy satisfier | +BlockManager +------------- + +The metrics present statistics from the BlockManager's perspective. + +| Name | Description | +|:---- |:--------------------------------------------------------------------------------------------------------------------------------| +| `StorageTypeStats` | key represents different StorageTypes, and value represents the detailed storage information corresponding to each StorageType. | + JournalNode ----------- diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/StorageTypeStats.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/StorageTypeStats.java index fecbe6a812..83f18bf780 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/StorageTypeStats.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/StorageTypeStats.java @@ -24,6 +24,7 @@ import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.fs.StorageType; +import org.apache.hadoop.hdfs.DFSUtilClient; /** * Statistics per StorageType. @@ -107,6 +108,24 @@ public long getBlockPoolUsed() { return blockPoolUsed; } + public float getPercentUsed() { + long used = getCapacityUsed(); + long total = getCapacityTotal(); + return DFSUtilClient.getPercentUsed(used, total); + } + + public float getPercentBlockPoolUsed() { + long poolUsed = getBlockPoolUsed(); + long total = getCapacityTotal(); + return DFSUtilClient.getPercentUsed(poolUsed, total); + } + + public float getPercentRemaining() { + long remaining = getCapacityRemaining(); + long total = getCapacityTotal(); + return DFSUtilClient.getPercentUsed(remaining, total); + } + public int getNodesInService() { return nodesInService; } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockStatsMXBean.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockStatsMXBean.java index 076a640808..5753de7c83 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockStatsMXBean.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockStatsMXBean.java @@ -291,4 +291,30 @@ public void testStorageTypeLoad() throws Exception { 5000); IOUtils.closeStreams(hotSpFileStream, coldSpFileStream); } + + @Test + public void testStorageTypePercentJMX() throws Exception { + URL baseUrl = new URL(cluster.getHttpUri(0)); + String result = readOutput(new URL(baseUrl, "/jmx")); + + Map stat = (Map) JSON.parse(result); + Object[] beans = (Object[]) stat.get("beans"); + Map blockStats = null; + for (Object bean : beans) { + Map map = (Map) bean; + if (map.get("name").equals("Hadoop:service=NameNode,name=BlockStats")) { + blockStats = map; + } + } + assertNotNull(blockStats); + Object[] storageTypeStatsList = + (Object[]) blockStats.get("StorageTypeStats"); + assertNotNull(storageTypeStatsList); + Map entry = (Map) storageTypeStatsList[0]; + Map storageTypeStats = (Map) entry.get("value"); + + assertTrue(storageTypeStats.containsKey("percentUsed")); + assertTrue(storageTypeStats.containsKey("percentBlockPoolUsed")); + assertTrue(storageTypeStats.containsKey("percentRemaining")); + } }