/** Test that the {@link PlacementMonitor.BlockAndDatanodeResolver} works correctly. */ @Test public void testBlockAndDatanodeResolver() throws Exception { setupCluster(); try { Path src = new Path("/dir/file"); Path parity = new Path("/raid/dir/file"); DFSTestUtil.createFile(fs, src, 20, (short) 2, 0L); DFSTestUtil.createFile(fs, parity, 11, (short) 2, 0L); DFSTestUtil.waitReplication(fs, src, (short) 2); DFSTestUtil.waitReplication(fs, parity, (short) 2); LocatedBlocks srcLbs, parityLbs; List<BlockInfo> srcInfos, parityInfos; srcLbs = namenode.getBlockLocations(src.toString(), 4, 10); srcInfos = placementMonitor.getBlockInfos(fs, src, 4, 10); parityLbs = namenode.getBlockLocations(parity.toString(), 3, 7); parityInfos = placementMonitor.getBlockInfos(fs, parity, 3, 7); Assert.assertEquals(10, srcLbs.getLocatedBlocks().size()); Assert.assertEquals(7, parityLbs.getLocatedBlocks().size()); Assert.assertEquals(10, srcInfos.size()); Assert.assertEquals(7, parityInfos.size()); BlockAndDatanodeResolver resolver = new BlockAndDatanodeResolver(src, fs, parity, fs); for (int i = 0; i < srcInfos.size(); ++i) { LocatedBlock lb = resolver.getLocatedBlock(srcInfos.get(i)); Assert.assertEquals(srcLbs.get(i).getBlock(), lb.getBlock()); for (String nodeName : srcInfos.get(i).getNames()) { DatanodeInfo node = resolver.getDatanodeInfo(nodeName); Assert.assertEquals(node.getName(), nodeName); } } for (int i = 0; i < parityInfos.size(); ++i) { LocatedBlock lb = resolver.getLocatedBlock(parityInfos.get(i)); Assert.assertEquals(parityLbs.get(i).getBlock(), lb.getBlock()); for (String nodeName : parityInfos.get(i).getNames()) { DatanodeInfo node = resolver.getDatanodeInfo(nodeName); Assert.assertEquals(node.getName(), nodeName); } } } finally { if (cluster != null) { cluster.shutdown(); } if (placementMonitor != null) { placementMonitor.stop(); } } }
private static DatanodeInfo chooseDatanode( final NameNode namenode, final String path, final HttpOpParam.Op op, final long openOffset) throws IOException { if (op == GetOpParam.Op.OPEN || op == GetOpParam.Op.GETFILECHECKSUM || op == PostOpParam.Op.APPEND) { final HdfsFileStatus status = namenode.getFileInfo(path); final long len = status.getLen(); if (op == GetOpParam.Op.OPEN && (openOffset < 0L || openOffset >= len)) { throw new IOException( "Offset=" + openOffset + " out of the range [0, " + len + "); " + op + ", path=" + path); } if (len > 0) { final long offset = op == GetOpParam.Op.OPEN ? openOffset : len - 1; final LocatedBlocks locations = namenode.getBlockLocations(path, offset, 1); final int count = locations.locatedBlockCount(); if (count > 0) { return JspHelper.bestNode(locations.get(0)); } } } return namenode.getNamesystem().getRandomDatanode(); }
/** * Test that {@link PlacementMonitor} moves block correctly * * @throws Exception */ @Test public void testMoveBlock() throws Exception { setupCluster(); try { Path path = new Path("/dir/file"); DFSTestUtil.createFile(fs, path, 1, (short) 1, 0L); DFSTestUtil.waitReplication(fs, path, (short) 1); FileStatus status = fs.getFileStatus(path); LocatedBlocks blocks = namenode.getBlockLocations(path.toString(), 0, status.getLen()); Assert.assertEquals(1, blocks.getLocatedBlocks().size()); LocatedBlock block = blocks.getLocatedBlocks().get(0); Assert.assertEquals(1, block.getLocations().length); DatanodeInfo source = block.getLocations()[0]; Set<DatanodeInfo> excluded = new HashSet<DatanodeInfo>(); for (DatanodeInfo d : datanodes) { excluded.add(d); } excluded.remove(source); DatanodeInfo target = excluded.iterator().next(); excluded.add(source); excluded.remove(target); BlockMover.BlockMoveAction action = blockMover.new BlockMoveAction(block, source, excluded, 1); LOG.info("Start moving block from " + source + " to " + target); action.run(); LOG.info("Done moving block"); boolean blockMoved = false; for (int i = 0; i < 100; ++i) { blocks = namenode.getBlockLocations(path.toString(), 0, status.getLen()); block = blocks.getLocatedBlocks().get(0); if (block.getLocations().length == 1 && block.getLocations()[0].equals((target))) { blockMoved = true; break; } Thread.sleep(100L); } Assert.assertTrue(blockMoved); } finally { if (cluster != null) { cluster.shutdown(); } if (placementMonitor != null) { placementMonitor.stop(); } } }