Browse Source

HDFS-919. Add tests for getBlockLocations and totalLoad metrics. Contributed by Ravi Phulari.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.20@906805 13f79535-47bb-0310-9956-ffa450edef68
Konstantin Boudnik 15 năm trước cách đây
mục cha
commit
af0c31d53b

+ 3 - 0
CHANGES.txt

@@ -12,6 +12,9 @@ Release 0.20.2 - Unreleased
     HDFS-919. Create test to validate the BlocksVerified metric (Gary Murry
     via cos)
 
+    HDFS-907. Add tests for getBlockLocations and totalLoad metrics.
+    (Ravi Phulari via cos)
+
   BUG FIXES
 
     MAPREDUCE-112. Add counters for reduce input, output records to the new API.

+ 95 - 0
src/test/org/apache/hadoop/hdfs/server/namenode/metrics/TestNameNodeMetrics.java

@@ -17,23 +17,30 @@
  */
 package org.apache.hadoop.hdfs.server.namenode.metrics;
 
+import java.io.DataInputStream;
 import java.io.IOException;
 import java.util.Random;
 
 import junit.framework.TestCase;
 
 import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.hdfs.server.namenode.NameNode;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.hdfs.DFSTestUtil;
 import org.apache.hadoop.hdfs.DistributedFileSystem;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
 import org.apache.hadoop.hdfs.protocol.LocatedBlock;
 import org.apache.hadoop.hdfs.server.namenode.FSNamesystem;
+import org.mortbay.log.Log;
 
 /**
  * Test for metrics published by the Namenode
  */
 public class TestNameNodeMetrics extends TestCase {
+  // Number of datanodes in the cluster
+  private static final int DATANODE_COUNT = 3; 
+	
   private static final Configuration CONF = new Configuration();
   static {
     CONF.setLong("dfs.block.size", 100);
@@ -47,6 +54,8 @@ public class TestNameNodeMetrics extends TestCase {
   private DistributedFileSystem fs;
   private Random rand = new Random();
   private FSNamesystem namesystem;
+  private NameNodeMetrics nnMetrics;
+  private NameNode nn;
 
   @Override
   protected void setUp() throws Exception {
@@ -55,6 +64,8 @@ public class TestNameNodeMetrics extends TestCase {
     namesystem = cluster.getNameNode().getNamesystem();
     fs = (DistributedFileSystem) cluster.getFileSystem();
     metrics = namesystem.getFSNamesystemMetrics();
+    nn = cluster.getNameNode();
+    nnMetrics = nn.getNameNodeMetrics();
   }
   
   @Override
@@ -75,6 +86,22 @@ public class TestNameNodeMetrics extends TestCase {
     metrics.doUpdates(null);
   }
 
+  private void updateNNMetrics() throws Exception {
+    //Wait for nnmetrics update
+    Thread.sleep(1000);
+    nnMetrics.doUpdates(null);
+  }
+ 
+  private void readFile(FileSystem fileSys,String path) throws IOException {
+    //Just read file so that getNumBlockLocations are incremented
+    Path name = new Path(path);
+    DataInputStream stm = fileSys.open(name);
+    byte [] buffer = new byte[4];
+    int bytesRead =  stm.read(buffer,0,4);
+    stm.close();
+  }
+
+
   /** Test metrics associated with addition of a file */
   public void testFileAdd() throws Exception {
     // Add files with 100 blocks
@@ -148,4 +175,72 @@ public class TestNameNodeMetrics extends TestCase {
     updateMetrics();
     assertEquals(0, metrics.underReplicatedBlocks.get());
   }
+
+ /**
+   * Test numGetBlockLocations metric   
+   * 
+   * Test initiates and performs file operations (create,read,close,open file )
+   * which results in metrics changes. These metrics changes are updated and 
+   * tested for correctness.
+   * 
+   *  create file operation does not increment numGetBlockLocation
+   *  one read file operation increments numGetBlockLocation by 1
+   *    
+   * @throws IOException in case of an error
+   */
+  public void testGetBlockLocationMetric() throws Exception{
+    final String METHOD_NAME = "TestGetBlockLocationMetric";
+    Log.info("Running test "+METHOD_NAME);
+  
+    String file1_path = "/tmp/filePath";
+
+    // When cluster starts first time there are no file  (read,create,open)
+    // operations so metric numGetBlockLocations should be 0.
+    // Verify that numGetBlockLocations for current interval 
+    // and previous interval are 0
+    assertEquals("numGetBlockLocations for previous interval is incorrect",
+    0,nnMetrics.numGetBlockLocations.getPreviousIntervalValue());
+    assertEquals("numGetBlockLocations for current interval is incorrect",
+    0,nnMetrics.numGetBlockLocations.getCurrentIntervalValue());
+
+    //Perform create file operation
+    createFile(file1_path,100,(short)2);
+    // Update NameNode metrics
+    updateNNMetrics();
+  
+    //Create file does not change numGetBlockLocations metric
+    //expect numGetBlockLocations = 0 for previous and current interval 
+    assertEquals("numGetBlockLocations for previous interval is incorrect",
+    0,nnMetrics.numGetBlockLocations.getPreviousIntervalValue());
+    // Verify numGetBlockLocations for current interval is 0
+    assertEquals("numGetBlockLocations for current interval is incorrect",
+    0,nnMetrics.numGetBlockLocations.getCurrentIntervalValue());
+  
+    // Open and read file operation increments numGetBlockLocations
+    // Perform read file operation on earlier created file
+    readFile(fs, file1_path);
+    // Update NameNode metrics
+    updateNNMetrics();
+    // Verify read file operation has incremented numGetBlockLocations by 1
+    assertEquals("numGetBlockLocations for previous interval is incorrect",
+    1,nnMetrics.numGetBlockLocations.getPreviousIntervalValue());
+    // Verify numGetBlockLocations for current interval is 0
+    assertEquals("numGetBlockLocations for current interval is incorrect",
+    0,nnMetrics.numGetBlockLocations.getCurrentIntervalValue());
+
+    // opening and reading file  twice will increment numGetBlockLocations by 2
+    readFile(fs, file1_path);
+    readFile(fs, file1_path);
+    updateNNMetrics();
+    assertEquals("numGetBlockLocations for previous interval is incorrect",
+    2,nnMetrics.numGetBlockLocations.getPreviousIntervalValue());
+    // Verify numGetBlockLocations for current interval is 0
+    assertEquals("numGetBlockLocations for current interval is incorrect",
+    0,nnMetrics.numGetBlockLocations.getCurrentIntervalValue());
+  
+    // Verify total load metrics, total load = Data Node started.
+    updateMetrics();
+    assertEquals("Metrics TotalLoad is incorrect"
+    ,DATANODE_COUNT,metrics.totalLoad.get());
+  }
 }