1 | package org.apache.hadoop.hdfs.server.namenode; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | |
---|
5 | import org.apache.hadoop.conf.Configuration; |
---|
6 | import org.apache.hadoop.hdfs.MiniDFSCluster; |
---|
7 | import org.apache.hadoop.hdfs.protocol.Block; |
---|
8 | import org.apache.hadoop.hdfs.server.common.GenerationStamp; |
---|
9 | import org.apache.hadoop.hdfs.server.protocol.BlockCommand; |
---|
10 | import org.apache.hadoop.hdfs.server.protocol.DatanodeCommand; |
---|
11 | import org.apache.hadoop.hdfs.server.protocol.DatanodeProtocol; |
---|
12 | import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration; |
---|
13 | |
---|
14 | import junit.framework.TestCase; |
---|
15 | |
---|
16 | /** |
---|
17 | * Test if FSNamesystem handles heartbeat right |
---|
18 | */ |
---|
19 | public class TestHeartbeatHandling extends TestCase { |
---|
20 | /** |
---|
21 | * Test if {@link FSNamesystem#handleHeartbeat(DatanodeRegistration, long, long, long, int, int)} |
---|
22 | * can pick up replication and/or invalidate requests and |
---|
23 | * observes the max limit |
---|
24 | */ |
---|
25 | public void testHeartbeat() throws Exception { |
---|
26 | final Configuration conf = new Configuration(); |
---|
27 | final MiniDFSCluster cluster = new MiniDFSCluster(conf, 1, true, null); |
---|
28 | try { |
---|
29 | cluster.waitActive(); |
---|
30 | final FSNamesystem namesystem = cluster.getNameNode().getNamesystem(); |
---|
31 | final DatanodeRegistration nodeReg = cluster.getDataNodes().get(0).dnRegistration; |
---|
32 | DatanodeDescriptor dd = namesystem.getDatanode(nodeReg); |
---|
33 | |
---|
34 | final int REMAINING_BLOCKS = 1; |
---|
35 | final int MAX_REPLICATE_LIMIT = conf.getInt("dfs.max-repl-streams", 2); |
---|
36 | final int MAX_INVALIDATE_LIMIT = FSNamesystem.BLOCK_INVALIDATE_CHUNK; |
---|
37 | final int MAX_INVALIDATE_BLOCKS = 2*MAX_INVALIDATE_LIMIT+REMAINING_BLOCKS; |
---|
38 | final int MAX_REPLICATE_BLOCKS = 2*MAX_REPLICATE_LIMIT+REMAINING_BLOCKS; |
---|
39 | final DatanodeDescriptor[] ONE_TARGET = new DatanodeDescriptor[1]; |
---|
40 | |
---|
41 | synchronized (namesystem.heartbeats) { |
---|
42 | for (int i=0; i<MAX_REPLICATE_BLOCKS; i++) { |
---|
43 | dd.addBlockToBeReplicated( |
---|
44 | new Block(i, 0, GenerationStamp.FIRST_VALID_STAMP), ONE_TARGET); |
---|
45 | } |
---|
46 | DatanodeCommand[] cmds = namesystem.handleHeartbeat( |
---|
47 | nodeReg, dd.getCapacity(), dd.getDfsUsed(), dd.getRemaining(), 0, 0); |
---|
48 | assertEquals(1, cmds.length); |
---|
49 | assertEquals(DatanodeProtocol.DNA_TRANSFER, cmds[0].getAction()); |
---|
50 | assertEquals(MAX_REPLICATE_LIMIT, ((BlockCommand)cmds[0]).getBlocks().length); |
---|
51 | |
---|
52 | ArrayList<Block> blockList = new ArrayList<Block>(MAX_INVALIDATE_BLOCKS); |
---|
53 | for (int i=0; i<MAX_INVALIDATE_BLOCKS; i++) { |
---|
54 | blockList.add(new Block(i, 0, GenerationStamp.FIRST_VALID_STAMP)); |
---|
55 | } |
---|
56 | dd.addBlocksToBeInvalidated(blockList); |
---|
57 | |
---|
58 | cmds = namesystem.handleHeartbeat( |
---|
59 | nodeReg, dd.getCapacity(), dd.getDfsUsed(), dd.getRemaining(), 0, 0); |
---|
60 | assertEquals(2, cmds.length); |
---|
61 | assertEquals(DatanodeProtocol.DNA_TRANSFER, cmds[0].getAction()); |
---|
62 | assertEquals(MAX_REPLICATE_LIMIT, ((BlockCommand)cmds[0]).getBlocks().length); |
---|
63 | assertEquals(DatanodeProtocol.DNA_INVALIDATE, cmds[1].getAction()); |
---|
64 | assertEquals(MAX_INVALIDATE_LIMIT, ((BlockCommand)cmds[1]).getBlocks().length); |
---|
65 | |
---|
66 | cmds = namesystem.handleHeartbeat( |
---|
67 | nodeReg, dd.getCapacity(), dd.getDfsUsed(), dd.getRemaining(), 0, 0); |
---|
68 | assertEquals(2, cmds.length); |
---|
69 | assertEquals(DatanodeProtocol.DNA_TRANSFER, cmds[0].getAction()); |
---|
70 | assertEquals(REMAINING_BLOCKS, ((BlockCommand)cmds[0]).getBlocks().length); |
---|
71 | assertEquals(DatanodeProtocol.DNA_INVALIDATE, cmds[1].getAction()); |
---|
72 | assertEquals(MAX_INVALIDATE_LIMIT, ((BlockCommand)cmds[1]).getBlocks().length); |
---|
73 | |
---|
74 | cmds = namesystem.handleHeartbeat( |
---|
75 | nodeReg, dd.getCapacity(), dd.getDfsUsed(), dd.getRemaining(), 0, 0); |
---|
76 | assertEquals(1, cmds.length); |
---|
77 | assertEquals(DatanodeProtocol.DNA_INVALIDATE, cmds[0].getAction()); |
---|
78 | assertEquals(REMAINING_BLOCKS, ((BlockCommand)cmds[0]).getBlocks().length); |
---|
79 | |
---|
80 | cmds = namesystem.handleHeartbeat( |
---|
81 | nodeReg, dd.getCapacity(), dd.getDfsUsed(), dd.getRemaining(), 0, 0); |
---|
82 | assertEquals(null, cmds); |
---|
83 | } |
---|
84 | } finally { |
---|
85 | cluster.shutdown(); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|