From 2720479988696499e08042bb28d340ad8e6115af Mon Sep 17 00:00:00 2001 From: GuoPhilipse <46367746+GuoPhilipse@users.noreply.github.com> Date: Wed, 8 Dec 2021 19:04:54 +0800 Subject: [PATCH] HDFS-16345. Fix test case fail in TestBlockStoragePolicy (#3696). Contributed by guophilipse. Reviewed-by: Akira Ajisaka Signed-off-by: Ayush Saxena (cherry picked from commit 25849ff7b7430692d1f40f59e9cb24690cbda216) --- .../hadoop/hdfs/TestBlockStoragePolicy.java | 102 ++++++++++-------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestBlockStoragePolicy.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestBlockStoragePolicy.java index 171ebb4a0a..def5afa9ea 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestBlockStoragePolicy.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestBlockStoragePolicy.java @@ -1237,23 +1237,29 @@ public void testChooseTargetWithTopology() throws Exception { DFSTestUtil.formatNameNode(conf); NameNode namenode = new NameNode(conf); - final BlockManager bm = namenode.getNamesystem().getBlockManager(); - BlockPlacementPolicy replicator = bm.getBlockPlacementPolicy(); - NetworkTopology cluster = bm.getDatanodeManager().getNetworkTopology(); - for (DatanodeDescriptor datanode : dataNodes) { - cluster.add(datanode); - } + try { + final BlockManager bm = namenode.getNamesystem().getBlockManager(); + BlockPlacementPolicy replicator = bm.getBlockPlacementPolicy(); + NetworkTopology cluster = bm.getDatanodeManager().getNetworkTopology(); + for (DatanodeDescriptor datanode : dataNodes) { + cluster.add(datanode); + } - DatanodeStorageInfo[] targets = replicator.chooseTarget("/foo", 3, - dataNodes[0], Collections.emptyList(), false, - new HashSet(), 0, policy1, null); - System.out.println(Arrays.asList(targets)); - Assert.assertEquals(3, targets.length); - targets = replicator.chooseTarget("/foo", 3, - dataNodes[0], Collections.emptyList(), false, - new HashSet(), 0, policy2, null); - System.out.println(Arrays.asList(targets)); - Assert.assertEquals(3, targets.length); + DatanodeStorageInfo[] targets = replicator.chooseTarget("/foo", 3, + dataNodes[0], Collections.emptyList(), false, + new HashSet(), 0, policy1, null); + System.out.println(Arrays.asList(targets)); + Assert.assertEquals(3, targets.length); + targets = replicator.chooseTarget("/foo", 3, + dataNodes[0], Collections.emptyList(), false, + new HashSet(), 0, policy2, null); + System.out.println(Arrays.asList(targets)); + Assert.assertEquals(3, targets.length); + } finally { + if (namenode != null) { + namenode.stop(); + } + } } @Test @@ -1284,20 +1290,26 @@ public void testChooseSsdOverDisk() throws Exception { DFSTestUtil.formatNameNode(conf); NameNode namenode = new NameNode(conf); - final BlockManager bm = namenode.getNamesystem().getBlockManager(); - BlockPlacementPolicy replicator = bm.getBlockPlacementPolicy(); - NetworkTopology cluster = bm.getDatanodeManager().getNetworkTopology(); - for (DatanodeDescriptor datanode : dataNodes) { - cluster.add(datanode); - } + try { + final BlockManager bm = namenode.getNamesystem().getBlockManager(); + BlockPlacementPolicy replicator = bm.getBlockPlacementPolicy(); + NetworkTopology cluster = bm.getDatanodeManager().getNetworkTopology(); + for (DatanodeDescriptor datanode : dataNodes) { + cluster.add(datanode); + } - DatanodeStorageInfo[] targets = replicator.chooseTarget("/foo", 3, - dataNodes[0], Collections.emptyList(), false, - new HashSet(), 0, policy, null); - System.out.println(policy.getName() + ": " + Arrays.asList(targets)); - Assert.assertEquals(2, targets.length); - Assert.assertEquals(StorageType.SSD, targets[0].getStorageType()); - Assert.assertEquals(StorageType.DISK, targets[1].getStorageType()); + DatanodeStorageInfo[] targets = replicator.chooseTarget("/foo", 3, + dataNodes[0], Collections.emptyList(), false, + new HashSet(), 0, policy, null); + System.out.println(policy.getName() + ": " + Arrays.asList(targets)); + Assert.assertEquals(2, targets.length); + Assert.assertEquals(StorageType.SSD, targets[0].getStorageType()); + Assert.assertEquals(StorageType.DISK, targets[1].getStorageType()); + } finally { + if (namenode != null) { + namenode.stop(); + } + } } /** @@ -1337,20 +1349,26 @@ public void testAddDatanode2ExistingPipelineInSsd() throws Exception { DFSTestUtil.formatNameNode(conf); NameNode namenode = new NameNode(conf); - final BlockManager bm = namenode.getNamesystem().getBlockManager(); - BlockPlacementPolicy replicator = bm.getBlockPlacementPolicy(); - NetworkTopology cluster = bm.getDatanodeManager().getNetworkTopology(); - for (DatanodeDescriptor datanode : dataNodes) { - cluster.add(datanode); + try { + final BlockManager bm = namenode.getNamesystem().getBlockManager(); + BlockPlacementPolicy replicator = bm.getBlockPlacementPolicy(); + NetworkTopology cluster = bm.getDatanodeManager().getNetworkTopology(); + for (DatanodeDescriptor datanode : dataNodes) { + cluster.add(datanode); + } + // chsenDs are DISK StorageType to simulate not enough SDD Storage + List chsenDs = new ArrayList<>(); + chsenDs.add(diskStorages[0]); + chsenDs.add(diskStorages[1]); + DatanodeStorageInfo[] targets = replicator.chooseTarget("/foo", 1, + null, chsenDs, true, + new HashSet(), 0, policy, null); + Assert.assertEquals(3, targets.length); + } finally { + if (namenode != null) { + namenode.stop(); + } } - // chsenDs are DISK StorageType to simulate not enough SDD Storage - List chsenDs = new ArrayList<>(); - chsenDs.add(diskStorages[0]); - chsenDs.add(diskStorages[1]); - DatanodeStorageInfo[] targets = replicator.chooseTarget("/foo", 1, - null, chsenDs, true, - new HashSet(), 0, policy, null); - Assert.assertEquals(3, targets.length); } @Test