You are viewing a plain text version of this content. The canonical link for it is here.
Posted to hdfs-commits@hadoop.apache.org by el...@apache.org on 2010/11/18 01:54:20 UTC

svn commit: r1036303 - in /hadoop/hdfs/trunk: CHANGES.txt src/test/hdfs/org/apache/hadoop/hdfs/TestAbandonBlock.java

Author: eli
Date: Thu Nov 18 00:54:20 2010
New Revision: 1036303

URL: http://svn.apache.org/viewvc?rev=1036303&view=rev
Log:
HDFS-1507. TestAbandonBlock should abandon a block. Contributed by Eli Collins

Modified:
    hadoop/hdfs/trunk/CHANGES.txt
    hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/TestAbandonBlock.java

Modified: hadoop/hdfs/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/CHANGES.txt?rev=1036303&r1=1036302&r2=1036303&view=diff
==============================================================================
--- hadoop/hdfs/trunk/CHANGES.txt (original)
+++ hadoop/hdfs/trunk/CHANGES.txt Thu Nov 18 00:54:20 2010
@@ -384,6 +384,8 @@ Release 0.22.0 - Unreleased
     HDFS-874. TestHDFSFileContextMainOperations fails on weirdly 
     configured DNS hosts. (Todd Lipcon via eli)
 
+    HDFS-1507. TestAbandonBlock should abandon a block. (eli)
+
 Release 0.21.1 - Unreleased
 
   IMPROVEMENTS

Modified: hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/TestAbandonBlock.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/TestAbandonBlock.java?rev=1036303&r1=1036302&r2=1036303&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/TestAbandonBlock.java (original)
+++ hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/hdfs/TestAbandonBlock.java Thu Nov 18 00:54:20 2010
@@ -25,47 +25,55 @@ import org.apache.hadoop.conf.Configurat
 import org.apache.hadoop.fs.*;
 import org.apache.hadoop.hdfs.protocol.LocatedBlock;
 import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
-import org.apache.hadoop.hdfs.server.namenode.NameNode;
-import org.apache.hadoop.util.StringUtils;
 
-public class TestAbandonBlock extends junit.framework.TestCase {
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test abandoning blocks, which clients do on pipeline creation failure.
+ */
+public class TestAbandonBlock {
   public static final Log LOG = LogFactory.getLog(TestAbandonBlock.class);
   
   private static final Configuration CONF = new HdfsConfiguration();
   static final String FILE_NAME_PREFIX
       = "/" + TestAbandonBlock.class.getSimpleName() + "_"; 
+  private MiniDFSCluster cluster;
+  private FileSystem fs;
 
-  public void testAbandonBlock() throws IOException {
-    MiniDFSCluster cluster = new MiniDFSCluster.Builder(CONF).numDataNodes(2).build();
-    FileSystem fs = cluster.getFileSystem();
+  @Before
+  public void setUp() throws Exception {
+    cluster = new MiniDFSCluster.Builder(CONF).numDataNodes(2).build();
+    fs = cluster.getFileSystem();
+    cluster.waitActive();
+  }
 
+  @After
+  public void tearDown() throws Exception {
+    fs.close();
+    cluster.shutdown();
+  }
+
+  @Test
+  /** Abandon a block while creating a file */
+  public void testAbandonBlock() throws IOException {
     String src = FILE_NAME_PREFIX + "foo";
-    FSDataOutputStream fout = null;
-    try {
-      //start writing a a file but not close it
-      fout = fs.create(new Path(src), true, 4096, (short)1, 512L);
-      for(int i = 0; i < 1024; i++) {
-        fout.write(123);
-      }
-      fout.hflush();
-  
-      //try reading the block by someone
-      final DFSClient dfsclient = new DFSClient(NameNode.getAddress(CONF), CONF);
-      LocatedBlocks blocks = dfsclient.getNamenode().getBlockLocations(src, 0, 1);
-      LocatedBlock b = blocks.get(0); 
-      try {
-        dfsclient.getNamenode().abandonBlock(b.getBlock(), src, "someone");
-        //previous line should throw an exception.
-        assertTrue(false);
-      }
-      catch(IOException ioe) {
-        LOG.info("GREAT! " + StringUtils.stringifyException(ioe));
-      }
-    }
-    finally {
-      try{fout.close();} catch(Exception e) {}
-      try{fs.close();} catch(Exception e) {}
-      try{cluster.shutdown();} catch(Exception e) {}
+
+    // Start writing a file but do not close it
+    FSDataOutputStream fout = fs.create(new Path(src), true, 4096, (short)1, 512L);
+    for (int i = 0; i < 1024; i++) {
+      fout.write(123);
     }
+    fout.hflush();
+
+    // Now abandon the last block
+    DFSClient dfsclient = ((DistributedFileSystem)fs).getClient();
+    LocatedBlocks blocks = dfsclient.getNamenode().getBlockLocations(src, 0, 1);
+    LocatedBlock b = blocks.getLastLocatedBlock();
+    dfsclient.getNamenode().abandonBlock(b.getBlock(), src, dfsclient.clientName);
+
+    // And close the file
+    fout.close();
   }
 }