PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/frameworks/hadoop-0.20.2/src/test/org/apache/hadoop/hdfs/server/namenode/TestFsck.java

http://github.com/mesos/mesos
Java | 343 lines | 269 code | 26 blank | 48 comment | 36 complexity | 4fcf2eb82e5f7d1f1612eaa3242eaa7b MD5 | raw file
Possible License(s): JSON, Apache-2.0, BSD-3-Clause, BSD-2-Clause
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hdfs.server.namenode;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.PrintStream;
  21. import java.net.InetSocketAddress;
  22. import java.io.File;
  23. import java.io.RandomAccessFile;
  24. import java.lang.Exception;
  25. import java.io.IOException;
  26. import java.nio.channels.FileChannel;
  27. import java.util.Random;
  28. import junit.framework.TestCase;
  29. import org.apache.commons.logging.impl.Log4JLogger;
  30. import org.apache.log4j.Level;
  31. import org.apache.hadoop.conf.Configuration;
  32. import org.apache.hadoop.fs.FileSystem;
  33. import org.apache.hadoop.fs.Path;
  34. import org.apache.hadoop.fs.FSDataOutputStream;
  35. import org.apache.hadoop.util.ToolRunner;
  36. import org.apache.hadoop.hdfs.DFSClient;
  37. import org.apache.hadoop.hdfs.DFSTestUtil;
  38. import org.apache.hadoop.hdfs.MiniDFSCluster;
  39. import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
  40. import org.apache.hadoop.hdfs.tools.DFSck;
  41. import org.apache.hadoop.io.IOUtils;
  42. /**
  43. * A JUnit test for doing fsck
  44. */
  45. public class TestFsck extends TestCase {
  46. static String runFsck(Configuration conf, int expectedErrCode,
  47. boolean checkErrorCode,String... path)
  48. throws Exception {
  49. PrintStream oldOut = System.out;
  50. ByteArrayOutputStream bStream = new ByteArrayOutputStream();
  51. PrintStream newOut = new PrintStream(bStream, true);
  52. System.setOut(newOut);
  53. ((Log4JLogger)PermissionChecker.LOG).getLogger().setLevel(Level.ALL);
  54. int errCode = ToolRunner.run(new DFSck(conf), path);
  55. if (checkErrorCode)
  56. assertEquals(expectedErrCode, errCode);
  57. ((Log4JLogger)PermissionChecker.LOG).getLogger().setLevel(Level.INFO);
  58. System.setOut(oldOut);
  59. return bStream.toString();
  60. }
  61. /** do fsck */
  62. public void testFsck() throws Exception {
  63. DFSTestUtil util = new DFSTestUtil("TestFsck", 20, 3, 8*1024);
  64. MiniDFSCluster cluster = null;
  65. FileSystem fs = null;
  66. try {
  67. Configuration conf = new Configuration();
  68. conf.setLong("dfs.blockreport.intervalMsec", 10000L);
  69. cluster = new MiniDFSCluster(conf, 4, true, null);
  70. fs = cluster.getFileSystem();
  71. util.createFiles(fs, "/srcdat");
  72. util.waitReplication(fs, "/srcdat", (short)3);
  73. String outStr = runFsck(conf, 0, true, "/");
  74. assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS));
  75. System.out.println(outStr);
  76. if (fs != null) {try{fs.close();} catch(Exception e){}}
  77. cluster.shutdown();
  78. // restart the cluster; bring up namenode but not the data nodes
  79. cluster = new MiniDFSCluster(conf, 0, false, null);
  80. outStr = runFsck(conf, 1, true, "/");
  81. // expect the result is corrupt
  82. assertTrue(outStr.contains(NamenodeFsck.CORRUPT_STATUS));
  83. System.out.println(outStr);
  84. // bring up data nodes & cleanup cluster
  85. cluster.startDataNodes(conf, 4, true, null, null);
  86. cluster.waitActive();
  87. cluster.waitClusterUp();
  88. fs = cluster.getFileSystem();
  89. util.cleanup(fs, "/srcdat");
  90. } finally {
  91. if (fs != null) {try{fs.close();} catch(Exception e){}}
  92. if (cluster != null) { cluster.shutdown(); }
  93. }
  94. }
  95. public void testFsckNonExistent() throws Exception {
  96. DFSTestUtil util = new DFSTestUtil("TestFsck", 20, 3, 8*1024);
  97. MiniDFSCluster cluster = null;
  98. FileSystem fs = null;
  99. try {
  100. Configuration conf = new Configuration();
  101. conf.setLong("dfs.blockreport.intervalMsec", 10000L);
  102. cluster = new MiniDFSCluster(conf, 4, true, null);
  103. fs = cluster.getFileSystem();
  104. util.createFiles(fs, "/srcdat");
  105. util.waitReplication(fs, "/srcdat", (short)3);
  106. String outStr = runFsck(conf, 0, true, "/non-existent");
  107. assertEquals(-1, outStr.indexOf(NamenodeFsck.HEALTHY_STATUS));
  108. System.out.println(outStr);
  109. util.cleanup(fs, "/srcdat");
  110. } finally {
  111. if (fs != null) {try{fs.close();} catch(Exception e){}}
  112. if (cluster != null) { cluster.shutdown(); }
  113. }
  114. }
  115. public void testFsckMove() throws Exception {
  116. DFSTestUtil util = new DFSTestUtil("TestFsck", 5, 3, 8*1024);
  117. MiniDFSCluster cluster = null;
  118. FileSystem fs = null;
  119. try {
  120. Configuration conf = new Configuration();
  121. conf.setLong("dfs.blockreport.intervalMsec", 10000L);
  122. cluster = new MiniDFSCluster(conf, 4, true, null);
  123. String topDir = "/srcdat";
  124. fs = cluster.getFileSystem();
  125. cluster.waitActive();
  126. util.createFiles(fs, topDir);
  127. util.waitReplication(fs, topDir, (short)3);
  128. String outStr = runFsck(conf, 0, true, "/");
  129. assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS));
  130. // Corrupt a block by deleting it
  131. String[] fileNames = util.getFileNames(topDir);
  132. DFSClient dfsClient = new DFSClient(new InetSocketAddress("localhost",
  133. cluster.getNameNodePort()), conf);
  134. String block = dfsClient.namenode.
  135. getBlockLocations(fileNames[0], 0, Long.MAX_VALUE).
  136. get(0).getBlock().getBlockName();
  137. File baseDir = new File(System.getProperty("test.build.data",
  138. "build/test/data"),"dfs/data");
  139. for (int i=0; i<8; i++) {
  140. File blockFile = new File(baseDir, "data" +(i+1)+ "/current/" + block);
  141. if(blockFile.exists()) {
  142. assertTrue(blockFile.delete());
  143. }
  144. }
  145. // We excpect the filesystem to be corrupted
  146. outStr = runFsck(conf, 1, false, "/");
  147. while (!outStr.contains(NamenodeFsck.CORRUPT_STATUS)) {
  148. try {
  149. Thread.sleep(100);
  150. } catch (InterruptedException ignore) {
  151. }
  152. outStr = runFsck(conf, 1, false, "/");
  153. }
  154. // Fix the filesystem by moving corrupted files to lost+found
  155. outStr = runFsck(conf, 1, true, "/", "-move");
  156. assertTrue(outStr.contains(NamenodeFsck.CORRUPT_STATUS));
  157. // Check to make sure we have healthy filesystem
  158. outStr = runFsck(conf, 0, true, "/");
  159. assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS));
  160. util.cleanup(fs, topDir);
  161. if (fs != null) {try{fs.close();} catch(Exception e){}}
  162. cluster.shutdown();
  163. } finally {
  164. if (fs != null) {try{fs.close();} catch(Exception e){}}
  165. if (cluster != null) { cluster.shutdown(); }
  166. }
  167. }
  168. public void testFsckOpenFiles() throws Exception {
  169. DFSTestUtil util = new DFSTestUtil("TestFsck", 4, 3, 8*1024);
  170. MiniDFSCluster cluster = null;
  171. FileSystem fs = null;
  172. try {
  173. Configuration conf = new Configuration();
  174. conf.setLong("dfs.blockreport.intervalMsec", 10000L);
  175. cluster = new MiniDFSCluster(conf, 4, true, null);
  176. String topDir = "/srcdat";
  177. String randomString = "HADOOP ";
  178. fs = cluster.getFileSystem();
  179. cluster.waitActive();
  180. util.createFiles(fs, topDir);
  181. util.waitReplication(fs, topDir, (short)3);
  182. String outStr = runFsck(conf, 0, true, "/");
  183. assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS));
  184. // Open a file for writing and do not close for now
  185. Path openFile = new Path(topDir + "/openFile");
  186. FSDataOutputStream out = fs.create(openFile);
  187. int writeCount = 0;
  188. while (writeCount != 100) {
  189. out.write(randomString.getBytes());
  190. writeCount++;
  191. }
  192. // We expect the filesystem to be HEALTHY and show one open file
  193. outStr = runFsck(conf, 0, true, topDir);
  194. System.out.println(outStr);
  195. assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS));
  196. assertFalse(outStr.contains("OPENFORWRITE"));
  197. // Use -openforwrite option to list open files
  198. outStr = runFsck(conf, 0, true, topDir, "-openforwrite");
  199. System.out.println(outStr);
  200. assertTrue(outStr.contains("OPENFORWRITE"));
  201. assertTrue(outStr.contains("openFile"));
  202. // Close the file
  203. out.close();
  204. // Now, fsck should show HEALTHY fs and should not show any open files
  205. outStr = runFsck(conf, 0, true, topDir);
  206. System.out.println(outStr);
  207. assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS));
  208. assertFalse(outStr.contains("OPENFORWRITE"));
  209. util.cleanup(fs, topDir);
  210. if (fs != null) {try{fs.close();} catch(Exception e){}}
  211. cluster.shutdown();
  212. } finally {
  213. if (fs != null) {try{fs.close();} catch(Exception e){}}
  214. if (cluster != null) { cluster.shutdown(); }
  215. }
  216. }
  217. public void testCorruptBlock() throws Exception {
  218. Configuration conf = new Configuration();
  219. conf.setLong("dfs.blockreport.intervalMsec", 1000);
  220. FileSystem fs = null;
  221. DFSClient dfsClient = null;
  222. LocatedBlocks blocks = null;
  223. int replicaCount = 0;
  224. Random random = new Random();
  225. String outStr = null;
  226. MiniDFSCluster cluster = null;
  227. try {
  228. cluster = new MiniDFSCluster(conf, 3, true, null);
  229. cluster.waitActive();
  230. fs = cluster.getFileSystem();
  231. Path file1 = new Path("/testCorruptBlock");
  232. DFSTestUtil.createFile(fs, file1, 1024, (short)3, 0);
  233. // Wait until file replication has completed
  234. DFSTestUtil.waitReplication(fs, file1, (short)3);
  235. String block = DFSTestUtil.getFirstBlock(fs, file1).getBlockName();
  236. // Make sure filesystem is in healthy state
  237. outStr = runFsck(conf, 0, true, "/");
  238. System.out.println(outStr);
  239. assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS));
  240. // corrupt replicas
  241. File baseDir = new File(System.getProperty("test.build.data",
  242. "build/test/data"),"dfs/data");
  243. for (int i=0; i < 6; i++) {
  244. File blockFile = new File(baseDir, "data" + (i+1) + "/current/" +
  245. block);
  246. if (blockFile.exists()) {
  247. RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw");
  248. FileChannel channel = raFile.getChannel();
  249. String badString = "BADBAD";
  250. int rand = random.nextInt((int)channel.size()/2);
  251. raFile.seek(rand);
  252. raFile.write(badString.getBytes());
  253. raFile.close();
  254. }
  255. }
  256. // Read the file to trigger reportBadBlocks
  257. try {
  258. IOUtils.copyBytes(fs.open(file1), new IOUtils.NullOutputStream(), conf,
  259. true);
  260. } catch (IOException ie) {
  261. // Ignore exception
  262. }
  263. dfsClient = new DFSClient(new InetSocketAddress("localhost",
  264. cluster.getNameNodePort()), conf);
  265. blocks = dfsClient.namenode.
  266. getBlockLocations(file1.toString(), 0, Long.MAX_VALUE);
  267. replicaCount = blocks.get(0).getLocations().length;
  268. while (replicaCount != 3) {
  269. try {
  270. Thread.sleep(100);
  271. } catch (InterruptedException ignore) {
  272. }
  273. blocks = dfsClient.namenode.
  274. getBlockLocations(file1.toString(), 0, Long.MAX_VALUE);
  275. replicaCount = blocks.get(0).getLocations().length;
  276. }
  277. assertTrue (blocks.get(0).isCorrupt());
  278. // Check if fsck reports the same
  279. outStr = runFsck(conf, 1, true, "/");
  280. System.out.println(outStr);
  281. assertTrue(outStr.contains(NamenodeFsck.CORRUPT_STATUS));
  282. assertTrue(outStr.contains("testCorruptBlock"));
  283. } finally {
  284. if (cluster != null) {cluster.shutdown();}
  285. }
  286. }
  287. /** Test if fsck can return -1 in case of failure
  288. *
  289. * @throws Exception
  290. */
  291. public void testFsckError() throws Exception {
  292. MiniDFSCluster cluster = null;
  293. try {
  294. // bring up a one-node cluster
  295. Configuration conf = new Configuration();
  296. cluster = new MiniDFSCluster(conf, 1, true, null);
  297. String fileName = "/test.txt";
  298. Path filePath = new Path(fileName);
  299. FileSystem fs = cluster.getFileSystem();
  300. // create a one-block file
  301. DFSTestUtil.createFile(fs, filePath, 1L, (short)1, 1L);
  302. DFSTestUtil.waitReplication(fs, filePath, (short)1);
  303. // intentionally corrupt NN data structure
  304. INodeFile node = (INodeFile)cluster.getNameNode().namesystem.dir.rootDir.getNode(fileName);
  305. assertEquals(node.blocks.length, 1);
  306. node.blocks[0].setNumBytes(-1L); // set the block length to be negative
  307. // run fsck and expect a failure with -1 as the error code
  308. String outStr = runFsck(conf, -1, true, fileName);
  309. System.out.println(outStr);
  310. assertTrue(outStr.contains(NamenodeFsck.FAILURE_STATUS));
  311. // clean up file system
  312. fs.delete(filePath, true);
  313. } finally {
  314. if (cluster != null) {cluster.shutdown();}
  315. }
  316. }
  317. }