소스 검색

HADOOP-856. Fix HDFS's fsck command to not report non-existant filesystems as healthy. Contributed by Milind.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@500394 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 년 전
부모
커밋
6b01f2d7d3
3개의 변경된 파일65개의 추가작업 그리고 19개의 파일을 삭제
  1. 3 0
      CHANGES.txt
  2. 26 16
      src/java/org/apache/hadoop/dfs/NamenodeFsck.java
  3. 36 3
      src/test/org/apache/hadoop/dfs/TestFsck.java

+ 3 - 0
CHANGES.txt

@@ -79,6 +79,9 @@ Trunk (unreleased changes)
 24. HADOOP-936.  More metric renamings, as in HADOOP-890.
     (Nigel Daley via cutting)
 
+25. HADOOP-856.  Fix HDFS's fsck command to not report that
+    non-existent filesystems are healthy.  (Milind Bhandarkar via cutting)
+
 
 Release 0.10.1 - 2007-01-10
 

+ 26 - 16
src/java/org/apache/hadoop/dfs/NamenodeFsck.java

@@ -84,7 +84,6 @@ public class NamenodeFsck {
   private String path = "/";
   
   private Configuration conf;
-  private HttpServletResponse response;
   private PrintWriter out;
   
   /**
@@ -101,7 +100,6 @@ public class NamenodeFsck {
       HttpServletResponse response) throws IOException {
     this.conf = conf;
     this.nn = nn;
-    this.response = response;
     this.out = response.getWriter();
     for (Iterator<String> it = pmap.keySet().iterator(); it.hasNext();) {
       String key = it.next();
@@ -127,12 +125,14 @@ public class NamenodeFsck {
         for (int i = 0; i < files.length; i++) {
           check(files[i], res);
         }
-      }
-      out.println(res);
-      if (res.isHealthy()) {
-        out.println("\n\nThe filesystem under path '" + path + "' is HEALTHY");
-      }  else {
-        out.println("\n\nThe filesystem under path '" + path + "' is CORRUPT");
+        out.println(res);
+        if (res.isHealthy()) {
+          out.println("\n\nThe filesystem under path '" + path + "' is HEALTHY");
+        }  else {
+          out.println("\n\nThe filesystem under path '" + path + "' is CORRUPT");
+        }
+      } else {
+        out.println("\n\nPath '" + path + "' does not exist.");
       }
     } finally {
       out.close();
@@ -141,8 +141,9 @@ public class NamenodeFsck {
   
   private void check(DFSFileInfo file, FsckResult res) throws IOException {
     if (file.isDir()) {
-      if (showFiles)
+      if (showFiles) {
         out.println(file.getPath() + " <dir>");
+      }
       res.totalDirs++;
       DFSFileInfo[] files = nn.getListing(file.getPath());
       for (int i = 0; i < files.length; i++) {
@@ -160,7 +161,7 @@ public class NamenodeFsck {
     }  else {
       out.print('.');
       out.flush();
-      if (res.totalFiles % 100 == 0)        out.println();
+      if (res.totalFiles % 100 == 0) { out.println(); }
     }
     int missing = 0;
     long missize = 0;
@@ -170,8 +171,12 @@ public class NamenodeFsck {
       long id = block.getBlockId();
       DatanodeInfo[] locs = blocks[i].getLocations();
       short targetFileReplication = file.getReplication();
-      if (locs.length > targetFileReplication) res.overReplicatedBlocks += (locs.length - targetFileReplication);
-      if (locs.length < targetFileReplication && locs.length > 0) res.underReplicatedBlocks += (targetFileReplication - locs.length);
+      if (locs.length > targetFileReplication) {
+        res.overReplicatedBlocks += (locs.length - targetFileReplication);
+      }
+      if (locs.length < targetFileReplication && locs.length > 0) {
+        res.underReplicatedBlocks += (targetFileReplication - locs.length);
+      }
       report.append(i + ". " + id + " len=" + block.getNumBytes());
       if (locs == null || locs.length == 0) {
         report.append(" MISSING!");
@@ -183,7 +188,7 @@ public class NamenodeFsck {
         if (showLocations) {
           StringBuffer sb = new StringBuffer("[");
           for (int j = 0; j < locs.length; j++) {
-            if (j > 0) sb.append(", ");
+            if (j > 0) { sb.append(", "); }
             sb.append(locs[j]);
           }
           sb.append(']');
@@ -193,8 +198,9 @@ public class NamenodeFsck {
       report.append('\n');
     }
     if (missing > 0) {
-      if (!showFiles)
+      if (!showFiles) {
         out.println("\nMISSING " + missing + " blocks of total size " + missize + " B");
+      }
       res.corruptFiles++;
       switch(fixing) {
         case FIXING_NONE:
@@ -209,8 +215,12 @@ public class NamenodeFsck {
     if (showFiles) {
       if (missing > 0) {
         out.println(" MISSING " + missing + " blocks of total size " + missize + " B");
-      }  else        out.println(" OK");
-      if (showBlocks)        out.println(report.toString());
+      }  else {
+        out.println(" OK");
+      }
+      if (showBlocks) {
+        out.println(report.toString());
+      }
     }
   }
   

+ 36 - 3
src/test/org/apache/hadoop/dfs/TestFsck.java

@@ -18,14 +18,13 @@
 
 package org.apache.hadoop.dfs;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.PrintStream;
 import java.util.Random;
 import junit.framework.*;
 import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.dfs.MiniDFSCluster;
 import org.apache.hadoop.fs.*;
-import org.apache.hadoop.util.CopyFiles;
-
 
 /**
  * A JUnit test for doing fsck
@@ -143,7 +142,41 @@ public class TestFsck extends TestCase {
       namenode = conf.get("fs.default.name", "local");
       if (!"local".equals(namenode)) {
         MyFile[] files = createFiles(namenode, "/srcdat");
+        PrintStream oldOut = System.out;
+        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
+        PrintStream newOut = new PrintStream(bStream, true);
+        System.setOut(newOut);
         assertEquals(0, new DFSck().doMain(conf, new String[] {"/"}));
+        System.setOut(oldOut);
+        String outStr = bStream.toString();
+        assertTrue(-1 != outStr.indexOf("HEALTHY"));
+        System.out.println(outStr);
+        deldir(namenode, "/srcdat");
+      }
+    } finally {
+      if (cluster != null) { cluster.shutdown(); }
+    }
+  }
+  
+  /** do fsck on non-existent path*/
+  public void testFsckNonExistent() throws Exception {
+    String namenode = null;
+    MiniDFSCluster cluster = null;
+    try {
+      Configuration conf = new Configuration();
+      cluster = new MiniDFSCluster(65314, conf, 4, false);
+      namenode = conf.get("fs.default.name", "local");
+      if (!"local".equals(namenode)) {
+        MyFile[] files = createFiles(namenode, "/srcdat");
+        PrintStream oldOut = System.out;
+        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
+        PrintStream newOut = new PrintStream(bStream, true);
+        System.setOut(newOut);
+        assertEquals(0, new DFSck().doMain(conf, new String[] {"/non-existent"}));
+        System.setOut(oldOut);
+        String outStr = bStream.toString();
+        assertEquals(-1, outStr.indexOf("HEALTHY"));
+        System.out.println(outStr);
         deldir(namenode, "/srcdat");
       }
     } finally {