diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockPlacementPolicyWithNodeGroup.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockPlacementPolicyWithNodeGroup.java index 194f6ba9c8..39f1519153 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockPlacementPolicyWithNodeGroup.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockPlacementPolicyWithNodeGroup.java @@ -46,6 +46,11 @@ protected BlockPlacementPolicyWithNodeGroup() { public void initialize(Configuration conf, FSClusterStats stats, NetworkTopology clusterMap, Host2NodesMap host2datanodeMap) { + if (!(clusterMap instanceof NetworkTopologyWithNodeGroup)) { + throw new IllegalArgumentException( + "Configured cluster topology should be " + + NetworkTopologyWithNodeGroup.class.getName()); + } super.initialize(conf, stats, clusterMap, host2datanodeMap); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancerWithNodeGroup.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancerWithNodeGroup.java index caac02c1bf..6088722ef2 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancerWithNodeGroup.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/balancer/TestBalancerWithNodeGroup.java @@ -47,6 +47,7 @@ import org.apache.hadoop.hdfs.server.blockmanagement.BlockPlacementPolicyWithNodeGroup; import org.apache.hadoop.net.NetworkTopology; import org.apache.hadoop.net.NetworkTopologyWithNodeGroup; +import org.apache.hadoop.test.LambdaTestUtils; import org.junit.Assert; import org.junit.Test; @@ -361,4 +362,30 @@ public void testBalancerEndInNoMoveProgress() throws Exception { cluster.shutdown(); } } + + /** + * verify BlockPlacementPolicyNodeGroup uses NetworkTopologyWithNodeGroup. + */ + + @Test + public void testBPPNodeGroup() throws Exception { + Configuration conf = createConf(); + conf.setBoolean(DFSConfigKeys.DFS_USE_DFS_NETWORK_TOPOLOGY_KEY, true); + long[] capacities = new long[] {CAPACITY, CAPACITY, CAPACITY, CAPACITY}; + String[] racks = new String[] {RACK0, RACK0, RACK1, RACK1}; + String[] nodeGroups = + new String[] {NODEGROUP0, NODEGROUP0, NODEGROUP1, NODEGROUP2}; + + int numOfDatanodes = capacities.length; + assertEquals(numOfDatanodes, racks.length); + assertEquals(numOfDatanodes, nodeGroups.length); + MiniDFSCluster.Builder builder = + new MiniDFSCluster.Builder(conf).numDataNodes(capacities.length) + .racks(racks).simulatedCapacities(capacities); + MiniDFSClusterWithNodeGroup.setNodeGroups(nodeGroups); + LambdaTestUtils.intercept(IllegalArgumentException.class, + "Configured cluster topology should be " + + "org.apache.hadoop.net.NetworkTopologyWithNodeGroup", + () -> new MiniDFSClusterWithNodeGroup(builder)); + } }