// get datanode info
DatanodeInfo[] dinfo = namenode.getDatanodeReport(DatanodeReportType.ALL);
for (int i = 0; i < dinfo.length; i++) {
DatanodeDescriptor desc = cluster.getNameNode().getNamesystem()
.getDatanode(dinfo[i]);
assertEquals(numFiles, desc.numBlocks());
}
// save block ids
HashSet<Long> blocks = new HashSet<Long>();
DatanodeDescriptor dd = cluster.getNameNode().getNamesystem()
.getDatanode(dinfo[0]);
for (Iterator<BlockInfo> iter = dd.getBlockIterator(); iter.hasNext();) {
blocks.add(iter.next().getBlockId());
}
// restart datanodes and remove it from namesystem
for (int i = 0; i < dinfo.length; i++) {
namenode.getNamesystem().removeDatanode(dinfo[i]);
DatanodeDescriptor desc = cluster.getNameNode().getNamesystem()
.getDatanode(dinfo[i]);
assertEquals(0, desc.numBlocks());
}
h.clearEvents();
cluster.restartDataNodes();
long start = System.currentTimeMillis();
while (!h.processedEvents
.containsKey(InjectionEvent.FSNAMESYSTEM_BLOCKREPORT_COMPLETED)
&& !(numNodes == h.processedEvents
.get(InjectionEvent.FSNAMESYSTEM_BLOCKREPORT_COMPLETED))
&& System.currentTimeMillis() - start < 30000) {
DFSTestUtil.waitNMilliSecond(100);
}
// blocks are back
// we need to get the report again (re-registration)
dinfo = namenode.getDatanodeReport(DatanodeReportType.ALL);
for (int i = 0; i < dinfo.length; i++) {
DatanodeDescriptor desc = cluster.getNameNode().getNamesystem()
.getDatanode(dinfo[i]);
// block number matches
assertEquals(numFiles, desc.numBlocks());
// blocks for this datanode
HashSet<Long> blocksAfter = new HashSet<Long>();
for (Iterator<BlockInfo> iter = desc.getBlockIterator(); iter.hasNext();) {
BlockInfo bi = iter.next();
blocksAfter.add(bi.getBlockId());
// datanode must be listed as a location for this block
assertTrue(0 <= bi.findDatanode(desc));
}