1 | package org.apache.hadoop.hdfs.server.namenode; |
---|
2 | |
---|
3 | import org.apache.hadoop.conf.Configuration; |
---|
4 | import org.apache.hadoop.fs.FileSystem; |
---|
5 | import org.apache.hadoop.fs.FsShell; |
---|
6 | import org.apache.hadoop.fs.Path; |
---|
7 | import org.apache.hadoop.hdfs.DFSTestUtil; |
---|
8 | import org.apache.hadoop.hdfs.MiniDFSCluster; |
---|
9 | import org.apache.hadoop.hdfs.protocol.Block; |
---|
10 | |
---|
11 | import junit.framework.TestCase; |
---|
12 | |
---|
13 | public class TestUnderReplicatedBlocks extends TestCase { |
---|
14 | public void testSetrepIncWithUnderReplicatedBlocks() throws Exception { |
---|
15 | Configuration conf = new Configuration(); |
---|
16 | final short REPLICATION_FACTOR = 2; |
---|
17 | final String FILE_NAME = "/testFile"; |
---|
18 | final Path FILE_PATH = new Path(FILE_NAME); |
---|
19 | MiniDFSCluster cluster = new MiniDFSCluster(conf, REPLICATION_FACTOR+1, true, null); |
---|
20 | try { |
---|
21 | // create a file with one block with a replication factor of 2 |
---|
22 | final FileSystem fs = cluster.getFileSystem(); |
---|
23 | DFSTestUtil.createFile(fs, FILE_PATH, 1L, REPLICATION_FACTOR, 1L); |
---|
24 | DFSTestUtil.waitReplication(fs, FILE_PATH, REPLICATION_FACTOR); |
---|
25 | |
---|
26 | // remove one replica from the blocksMap so block becomes under-replicated |
---|
27 | // but the block does not get put into the under-replicated blocks queue |
---|
28 | FSNamesystem namesystem = cluster.getNameNode().namesystem; |
---|
29 | Block b = DFSTestUtil.getFirstBlock(fs, FILE_PATH); |
---|
30 | DatanodeDescriptor dn = namesystem.blocksMap.nodeIterator(b).next(); |
---|
31 | namesystem.addToInvalidates(b, dn); |
---|
32 | namesystem.blocksMap.removeNode(b, dn); |
---|
33 | |
---|
34 | // increment this file's replication factor |
---|
35 | FsShell shell = new FsShell(conf); |
---|
36 | assertEquals(0, shell.run(new String[]{ |
---|
37 | "-setrep", "-w", Integer.toString(1+REPLICATION_FACTOR), FILE_NAME})); |
---|
38 | } finally { |
---|
39 | cluster.shutdown(); |
---|
40 | } |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | } |
---|