Prechádzať zdrojové kódy

HDFS-17367. Add PercentUsed for Different StorageTypes in JMX (#6735) Contributed by Hualong Zhang.

Signed-off-by: Shilun Fan <slfan1989@apache.org>
zhtttylz 1 rok pred
rodič
commit
daafc8a0b8

+ 9 - 0
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 |
 | `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 |
 | `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
 JournalNode
 -----------
 -----------
 
 

+ 19 - 0
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.VisibleForTesting;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.fs.StorageType;
 import org.apache.hadoop.fs.StorageType;
+import org.apache.hadoop.hdfs.DFSUtilClient;
 
 
 /**
 /**
  * Statistics per StorageType.
  * Statistics per StorageType.
@@ -107,6 +108,24 @@ public class StorageTypeStats {
     return blockPoolUsed;
     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() {
   public int getNodesInService() {
     return nodesInService;
     return nodesInService;
   }
   }

+ 26 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockStatsMXBean.java

@@ -291,4 +291,30 @@ public class TestBlockStatsMXBean {
             5000);
             5000);
     IOUtils.closeStreams(hotSpFileStream, coldSpFileStream);
     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<String, Object> stat = (Map<String, Object>) JSON.parse(result);
+    Object[] beans = (Object[]) stat.get("beans");
+    Map<String, Object> blockStats = null;
+    for (Object bean : beans) {
+      Map<String, Object> map = (Map<String, Object>) bean;
+      if (map.get("name").equals("Hadoop:service=NameNode,name=BlockStats")) {
+        blockStats = map;
+      }
+    }
+    assertNotNull(blockStats);
+    Object[] storageTypeStatsList =
+        (Object[]) blockStats.get("StorageTypeStats");
+    assertNotNull(storageTypeStatsList);
+    Map<String, Object> entry = (Map<String, Object>) storageTypeStatsList[0];
+    Map<String, Object> storageTypeStats = (Map<String, Object>) entry.get("value");
+
+    assertTrue(storageTypeStats.containsKey("percentUsed"));
+    assertTrue(storageTypeStats.containsKey("percentBlockPoolUsed"));
+    assertTrue(storageTypeStats.containsKey("percentRemaining"));
+  }
 }
 }