Author: cos
Date: Fri Feb 5 03:27:05 2010
New Revision: 906794
URL: http://svn.apache.org/viewvc?rev=906794&view=rev
Log:
HDFS-907. Add tests for getBlockLocations and totalLoad metrics. Contributed by
Ravi Phulari.
Modified:
hadoop/hdfs/trunk/CHANGES.txt
hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/metrics/TestNameNodeMetrics.java
Modified: hadoop/hdfs/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/hadoop/hdfs/trunk/CHANGES.txt?rev=906794&r1=906793&r2=906794&view=diff
==============================================================================
--- hadoop/hdfs/trunk/CHANGES.txt (original)
+++ hadoop/hdfs/trunk/CHANGES.txt Fri Feb 5 03:27:05 2010
@@ -678,6 +678,9 @@
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
Modified:
hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/metrics/TestNameNodeMetrics.java
URL:
http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/metrics/TestNameNodeMetrics.java?rev=906794&r1=906793&r2=906794&view=diff
==============================================================================
---
hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/metrics/TestNameNodeMetrics.java
(original)
+++
hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/metrics/TestNameNodeMetrics.java
Fri Feb 5 03:27:05 2010
@@ -17,12 +17,14 @@
*/
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.fs.Path;
import org.apache.hadoop.hdfs.DFSTestUtil;
import org.apache.hadoop.hdfs.DistributedFileSystem;
@@ -30,9 +32,11 @@
import org.apache.hadoop.hdfs.protocol.LocatedBlock;
import org.apache.hadoop.hdfs.server.namenode.BlockManager;
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.DFSConfigKeys;
import org.apache.hadoop.hdfs.HdfsConfiguration;
+import org.mortbay.log.Log;
/**
* Test for metrics published by the Namenode
@@ -59,6 +63,8 @@
private DistributedFileSystem fs;
private Random rand = new Random();
private FSNamesystem namesystem;
+ private NameNodeMetrics nnMetrics;
+ private NameNode nn;
private static Path getTestPath(String fileName) {
return new Path(TEST_ROOT_DIR_PATH, fileName);
@@ -71,6 +77,8 @@
namesystem = cluster.getNamesystem();
fs = (DistributedFileSystem) cluster.getFileSystem();
metrics = namesystem.getFSNamesystemMetrics();
+ nn = cluster.getNameNode();
+ nnMetrics = nn.getNameNodeMetrics();
}
@Override
@@ -90,6 +98,20 @@
metrics.doUpdates(null);
}
+ private void updateNNMetrics() throws Exception {
+ //Wait for nnmetrics update
+ Thread.sleep(1000);
+ nnMetrics.doUpdates(null);
+ }
+
+ private void readFile(FileSystem fileSys,Path name) throws IOException {
+ //Just read file so that getNumBlockLocations are incremented
+ 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
@@ -174,4 +196,72 @@
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);
+
+ Path file1_Path = new Path(TEST_ROOT_DIR_PATH, "file1.dat");
+
+ // 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());
+ }
}