|
@@ -42,24 +42,23 @@ import org.apache.hadoop.hdfs.protocol.Block;
|
|
|
import org.apache.hadoop.hdfs.protocol.ClientProtocol;
|
|
|
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
|
|
|
import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
|
|
|
-import org.apache.hadoop.hdfs.protocol.HdfsConstants.DatanodeReportType;
|
|
|
import org.apache.hadoop.hdfs.protocol.LocatedBlock;
|
|
|
+import org.apache.hadoop.hdfs.protocol.HdfsConstants.DatanodeReportType;
|
|
|
import org.apache.hadoop.hdfs.server.datanode.SimulatedFSDataset;
|
|
|
-import org.apache.hadoop.hdfs.server.namenode.NameNode;
|
|
|
|
|
|
/**
|
|
|
* This class tests if a balancer schedules tasks correctly.
|
|
|
*/
|
|
|
public class TestBalancer extends TestCase {
|
|
|
private static final Log LOG = LogFactory.getLog(
|
|
|
- "org.apache.hadoop.hdfs.TestReplication");
|
|
|
+ "org.apache.hadoop.hdfs.TestBalancer");
|
|
|
|
|
|
- final private static long CAPACITY = 500L;
|
|
|
- final private static String RACK0 = "/rack0";
|
|
|
- final private static String RACK1 = "/rack1";
|
|
|
- final private static String RACK2 = "/rack2";
|
|
|
- final static private String fileName = "/tmp.txt";
|
|
|
- final static private Path filePath = new Path(fileName);
|
|
|
+ final static long CAPACITY = 500L;
|
|
|
+ final static String RACK0 = "/rack0";
|
|
|
+ final static String RACK1 = "/rack1";
|
|
|
+ final static String RACK2 = "/rack2";
|
|
|
+ final private static String fileName = "/tmp.txt";
|
|
|
+ final static Path filePath = new Path(fileName);
|
|
|
private MiniDFSCluster cluster;
|
|
|
|
|
|
ClientProtocol client;
|
|
@@ -83,9 +82,10 @@ public class TestBalancer extends TestCase {
|
|
|
}
|
|
|
|
|
|
/* create a file with a length of <code>fileLen</code> */
|
|
|
- private void createFile(long fileLen, short replicationFactor)
|
|
|
+ static void createFile(MiniDFSCluster cluster, Path filePath, long fileLen,
|
|
|
+ short replicationFactor, int nnIndex)
|
|
|
throws IOException {
|
|
|
- FileSystem fs = cluster.getFileSystem();
|
|
|
+ FileSystem fs = cluster.getFileSystem(nnIndex);
|
|
|
DFSTestUtil.createFile(fs, filePath, fileLen,
|
|
|
replicationFactor, r.nextLong());
|
|
|
DFSTestUtil.waitReplication(fs, filePath, replicationFactor);
|
|
@@ -104,7 +104,7 @@ public class TestBalancer extends TestCase {
|
|
|
|
|
|
short replicationFactor = (short)(numNodes-1);
|
|
|
long fileLen = size/replicationFactor;
|
|
|
- createFile(fileLen, replicationFactor);
|
|
|
+ createFile(cluster , filePath, fileLen, replicationFactor, 0);
|
|
|
|
|
|
List<LocatedBlock> locatedBlocks = client.
|
|
|
getBlockLocations(fileName, 0, fileLen).getLocatedBlocks();
|
|
@@ -212,7 +212,8 @@ public class TestBalancer extends TestCase {
|
|
|
* @throws IOException - if getStats() fails
|
|
|
* @throws TimeoutException
|
|
|
*/
|
|
|
- private void waitForHeartBeat(long expectedUsedSpace, long expectedTotalSpace)
|
|
|
+ static void waitForHeartBeat(long expectedUsedSpace,
|
|
|
+ long expectedTotalSpace, ClientProtocol client, MiniDFSCluster cluster)
|
|
|
throws IOException, TimeoutException {
|
|
|
long timeout = TIMEOUT;
|
|
|
long failtime = (timeout <= 0L) ? Long.MAX_VALUE
|
|
@@ -249,7 +250,8 @@ public class TestBalancer extends TestCase {
|
|
|
* @throws IOException
|
|
|
* @throws TimeoutException
|
|
|
*/
|
|
|
- private void waitForBalancer(long totalUsedSpace, long totalCapacity)
|
|
|
+ static void waitForBalancer(long totalUsedSpace, long totalCapacity,
|
|
|
+ ClientProtocol client, MiniDFSCluster cluster)
|
|
|
throws IOException, TimeoutException {
|
|
|
long timeout = TIMEOUT;
|
|
|
long failtime = (timeout <= 0L) ? Long.MAX_VALUE
|
|
@@ -312,7 +314,8 @@ public class TestBalancer extends TestCase {
|
|
|
|
|
|
// fill up the cluster to be 30% full
|
|
|
long totalUsedSpace = totalCapacity*3/10;
|
|
|
- createFile(totalUsedSpace/numOfDatanodes, (short)numOfDatanodes);
|
|
|
+ createFile(cluster, filePath, totalUsedSpace / numOfDatanodes,
|
|
|
+ (short) numOfDatanodes, 0);
|
|
|
// start up an empty node with the same capacity and on the same rack
|
|
|
cluster.startDataNodes(conf, 1, true, null,
|
|
|
new String[]{newRack}, new long[]{newCapacity});
|
|
@@ -328,7 +331,7 @@ public class TestBalancer extends TestCase {
|
|
|
|
|
|
private void runBalancer(Configuration conf,
|
|
|
long totalUsedSpace, long totalCapacity) throws Exception {
|
|
|
- waitForHeartBeat(totalUsedSpace, totalCapacity);
|
|
|
+ waitForHeartBeat(totalUsedSpace, totalCapacity, client, cluster);
|
|
|
|
|
|
// start rebalancing
|
|
|
Map<String, Map<String, InetSocketAddress>> namenodes =
|
|
@@ -336,9 +339,9 @@ public class TestBalancer extends TestCase {
|
|
|
final int r = Balancer.run(namenodes, Balancer.Parameters.DEFALUT, conf);
|
|
|
assertEquals(Balancer.ReturnStatus.SUCCESS.code, r);
|
|
|
|
|
|
- waitForHeartBeat(totalUsedSpace, totalCapacity);
|
|
|
+ waitForHeartBeat(totalUsedSpace, totalCapacity, client, cluster);
|
|
|
LOG.info("Rebalancing with default ctor.");
|
|
|
- waitForBalancer(totalUsedSpace, totalCapacity);
|
|
|
+ waitForBalancer(totalUsedSpace, totalCapacity, client, cluster);
|
|
|
}
|
|
|
|
|
|
/** one-node cluster test*/
|
|
@@ -403,7 +406,8 @@ public class TestBalancer extends TestCase {
|
|
|
|
|
|
// fill up the cluster to be 30% full
|
|
|
long totalUsedSpace = totalCapacity * 3 / 10;
|
|
|
- createFile(totalUsedSpace / numOfDatanodes, (short) numOfDatanodes);
|
|
|
+ createFile(cluster, filePath, totalUsedSpace / numOfDatanodes,
|
|
|
+ (short) numOfDatanodes, 0);
|
|
|
// start up an empty node with the same capacity and on the same rack
|
|
|
cluster.startDataNodes(conf, 1, true, null, new String[] { newRack },
|
|
|
new long[] { newCapacity });
|