public void testNodeCount() throws Exception {
// start a mini dfs cluster of 2 nodes
final Configuration conf = new Configuration();
final short REPLICATION_FACTOR = (short)2;
final MiniDFSCluster cluster =
new MiniDFSCluster(conf, REPLICATION_FACTOR, true, null);
try {
final FSNamesystem namesystem = cluster.getNameNode().namesystem;
final FileSystem fs = cluster.getFileSystem();
// populate the cluster with a one block file
final Path FILE_PATH = new Path("/testfile");
DFSTestUtil.createFile(fs, FILE_PATH, 1L, REPLICATION_FACTOR, 1L);
DFSTestUtil.waitReplication(fs, FILE_PATH, REPLICATION_FACTOR);
Block block = DFSTestUtil.getFirstBlock(fs, FILE_PATH);
// keep a copy of all datanode descriptor
DatanodeDescriptor[] datanodes = (DatanodeDescriptor[])
namesystem.heartbeats.toArray(new DatanodeDescriptor[REPLICATION_FACTOR]);
// start two new nodes
cluster.startDataNodes(conf, 2, true, null, null);
cluster.waitActive(false);
LOG.info("Bringing down first DN");
// bring down first datanode
DatanodeDescriptor datanode = datanodes[0];
DataNodeProperties dnprop = cluster.stopDataNode(datanode.getName());
// make sure that NN detects that the datanode is down
synchronized (namesystem.heartbeats) {
datanode.setLastUpdate(0); // mark it dead
namesystem.heartbeatCheck();
}
LOG.info("Waiting for block to be replicated");
// the block will be replicated
DFSTestUtil.waitReplication(fs, FILE_PATH, REPLICATION_FACTOR);
LOG.info("Restarting first datanode");
// restart the first datanode
cluster.restartDataNode(dnprop);
cluster.waitActive(false);
LOG.info("Waiting for excess replicas to be detected");
// check if excessive replica is detected
waitForExcessReplicasToChangeTo(namesystem, block, 1);
LOG.info("Finding a non-excess node");
// find out a non-excess node
Iterator<DatanodeDescriptor> iter = namesystem.blocksMap.nodeIterator(block);
DatanodeDescriptor nonExcessDN = null;
while (iter.hasNext()) {
DatanodeDescriptor dn = iter.next();
Collection<Block> blocks = namesystem.excessReplicateMap.get(dn.getStorageID());
if (blocks == null || !blocks.contains(block) ) {
nonExcessDN = dn;
break;
}
}
assertTrue(nonExcessDN!=null);
LOG.info("Stopping non-excess node: " + nonExcessDN);
// bring down non excessive datanode
dnprop = cluster.stopDataNode(nonExcessDN.getName());
// make sure that NN detects that the datanode is down
synchronized (namesystem.heartbeats) {
nonExcessDN.setLastUpdate(0); // mark it dead
namesystem.heartbeatCheck();
}
LOG.info("Waiting for live replicas to hit repl factor");
// The block should be replicated
NumberReplicas num;
do {
num = namesystem.countNodes(block);
} while (num.liveReplicas() != REPLICATION_FACTOR);
LOG.info("Restarting first DN");
// restart the first datanode
cluster.restartDataNode(dnprop);
cluster.waitActive(false);
Thread.sleep(3000);
LOG.info("Waiting for excess replicas to be detected");
// check if excessive replica is detected
waitForExcessReplicasToChangeTo(namesystem, block, 2);
} finally {
cluster.shutdown();
}
}