PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestDiskError.java

https://github.com/RS1999ent/hadoop-hdfs
Java | 197 lines | 124 code | 21 blank | 52 comment | 8 complexity | ffd5f69ab0e19f7a7d769661f2492543 MD5 | raw file
  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.datanode;
  19. import java.io.DataOutputStream;
  20. import java.io.File;
  21. import java.net.InetSocketAddress;
  22. import java.net.Socket;
  23. import org.apache.hadoop.conf.Configuration;
  24. import org.apache.hadoop.fs.FileSystem;
  25. import org.apache.hadoop.fs.Path;
  26. import org.apache.hadoop.fs.permission.FsPermission;
  27. import org.apache.hadoop.hdfs.DFSTestUtil;
  28. import org.apache.hadoop.hdfs.MiniDFSCluster;
  29. import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
  30. import org.apache.hadoop.hdfs.protocol.LocatedBlock;
  31. import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
  32. import org.apache.hadoop.hdfs.protocol.DataTransferProtocol.BlockConstructionStage;
  33. import org.apache.hadoop.hdfs.protocol.DataTransferProtocol.Sender;
  34. import org.apache.hadoop.hdfs.security.token.block.BlockTokenSecretManager;
  35. import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter;
  36. import org.apache.hadoop.hdfs.HdfsConfiguration;
  37. import org.apache.hadoop.hdfs.DFSConfigKeys;
  38. import org.junit.Test;
  39. import org.junit.Before;
  40. import org.junit.After;
  41. import static org.junit.Assert.*;
  42. /**
  43. * Test that datanodes can correctly handle errors during block read/write.
  44. */
  45. public class TestDiskError {
  46. private FileSystem fs;
  47. private MiniDFSCluster cluster;
  48. private Configuration conf;
  49. @Before
  50. public void setUp() throws Exception {
  51. conf = new HdfsConfiguration();
  52. conf.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 512L);
  53. cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
  54. cluster.waitActive();
  55. fs = cluster.getFileSystem();
  56. }
  57. @After
  58. public void tearDown() throws Exception {
  59. cluster.shutdown();
  60. }
  61. /**
  62. * Test to check that a DN goes down when all its volumes have failed.
  63. */
  64. @Test
  65. public void testShutdown() throws Exception {
  66. if (System.getProperty("os.name").startsWith("Windows")) {
  67. /**
  68. * This test depends on OS not allowing file creations on a directory
  69. * that does not have write permissions for the user. Apparently it is
  70. * not the case on Windows (at least under Cygwin), and possibly AIX.
  71. * This is disabled on Windows.
  72. */
  73. return;
  74. }
  75. // Bring up two more datanodes
  76. cluster.startDataNodes(conf, 2, true, null, null);
  77. cluster.waitActive();
  78. final int dnIndex = 0;
  79. String bpid = cluster.getNamesystem().getBlockPoolId();
  80. File storageDir = MiniDFSCluster.getStorageDir(dnIndex, 0);
  81. File dir1 = MiniDFSCluster.getRbwDir(storageDir, bpid);
  82. storageDir = MiniDFSCluster.getStorageDir(dnIndex, 1);
  83. File dir2 = MiniDFSCluster.getRbwDir(storageDir, bpid);
  84. try {
  85. // make the data directory of the first datanode to be readonly
  86. assertTrue("Couldn't chmod local vol", dir1.setReadOnly());
  87. assertTrue("Couldn't chmod local vol", dir2.setReadOnly());
  88. // create files and make sure that first datanode will be down
  89. DataNode dn = cluster.getDataNodes().get(dnIndex);
  90. for (int i=0; dn.isDatanodeUp(); i++) {
  91. Path fileName = new Path("/test.txt"+i);
  92. DFSTestUtil.createFile(fs, fileName, 1024, (short)2, 1L);
  93. DFSTestUtil.waitReplication(fs, fileName, (short)2);
  94. fs.delete(fileName, true);
  95. }
  96. } finally {
  97. // restore its old permission
  98. dir1.setWritable(true);
  99. dir2.setWritable(true);
  100. }
  101. }
  102. /**
  103. * Test that when there is a failure replicating a block the temporary
  104. * and meta files are cleaned up and subsequent replication succeeds.
  105. */
  106. @Test
  107. public void testReplicationError() throws Exception {
  108. // create a file of replication factor of 1
  109. final Path fileName = new Path("/test.txt");
  110. final int fileLen = 1;
  111. DFSTestUtil.createFile(fs, fileName, 1, (short)1, 1L);
  112. DFSTestUtil.waitReplication(fs, fileName, (short)1);
  113. // get the block belonged to the created file
  114. LocatedBlocks blocks = NameNodeAdapter.getBlockLocations(
  115. cluster.getNameNode(), fileName.toString(), 0, (long)fileLen);
  116. assertEquals("Should only find 1 block", blocks.locatedBlockCount(), 1);
  117. LocatedBlock block = blocks.get(0);
  118. // bring up a second datanode
  119. cluster.startDataNodes(conf, 1, true, null, null);
  120. cluster.waitActive();
  121. final int sndNode = 1;
  122. DataNode datanode = cluster.getDataNodes().get(sndNode);
  123. // replicate the block to the second datanode
  124. InetSocketAddress target = datanode.getSelfAddr();
  125. Socket s = new Socket(target.getAddress(), target.getPort());
  126. // write the header.
  127. DataOutputStream out = new DataOutputStream(s.getOutputStream());
  128. Sender.opWriteBlock(out, block.getBlock(), 1,
  129. BlockConstructionStage.PIPELINE_SETUP_CREATE,
  130. 0L, 0L, 0L, "", null, new DatanodeInfo[0],
  131. BlockTokenSecretManager.DUMMY_TOKEN);
  132. // write check header
  133. out.writeByte( 1 );
  134. out.writeInt( 512 );
  135. out.flush();
  136. // close the connection before sending the content of the block
  137. out.close();
  138. // the temporary block & meta files should be deleted
  139. String bpid = cluster.getNamesystem().getBlockPoolId();
  140. File storageDir = MiniDFSCluster.getStorageDir(sndNode, 0);
  141. File dir1 = MiniDFSCluster.getRbwDir(storageDir, bpid);
  142. storageDir = MiniDFSCluster.getStorageDir(sndNode, 1);
  143. File dir2 = MiniDFSCluster.getRbwDir(storageDir, bpid);
  144. while (dir1.listFiles().length != 0 || dir2.listFiles().length != 0) {
  145. Thread.sleep(100);
  146. }
  147. // then increase the file's replication factor
  148. fs.setReplication(fileName, (short)2);
  149. // replication should succeed
  150. DFSTestUtil.waitReplication(fs, fileName, (short)1);
  151. // clean up the file
  152. fs.delete(fileName, false);
  153. }
  154. /**
  155. * Check that the permissions of the local DN directories are as expected.
  156. */
  157. @Test
  158. public void testLocalDirs() throws Exception {
  159. Configuration conf = new Configuration();
  160. final String permStr = conf.get(
  161. DFSConfigKeys.DFS_DATANODE_DATA_DIR_PERMISSION_KEY);
  162. FsPermission expected = new FsPermission(permStr);
  163. // Check permissions on directories in 'dfs.datanode.data.dir'
  164. FileSystem localFS = FileSystem.getLocal(conf);
  165. for (DataNode dn : cluster.getDataNodes()) {
  166. String[] dataDirs =
  167. dn.getConf().getStrings(DFSConfigKeys.DFS_DATANODE_DATA_DIR_KEY);
  168. for (String dir : dataDirs) {
  169. Path dataDir = new Path(dir);
  170. FsPermission actual = localFS.getFileStatus(dataDir).getPermission();
  171. assertEquals("Permission for dir: " + dataDir + ", is " + actual +
  172. ", while expected is " + expected, expected, actual);
  173. }
  174. }
  175. }
  176. }