diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java index 0dea48a7c9..1e8d1a5a00 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java @@ -273,11 +273,19 @@ static DatanodeInfo chooseDatanode(final NameNode namenode, for (String host : StringUtils .getTrimmedStringCollection(excludeDatanodes)) { int idx = host.indexOf(":"); - if (idx != -1) { - excludes.add(bm.getDatanodeManager().getDatanodeByXferAddr( - host.substring(0, idx), Integer.parseInt(host.substring(idx + 1)))); + Node excludeNode = null; + if (idx != -1) { + excludeNode = bm.getDatanodeManager().getDatanodeByXferAddr( + host.substring(0, idx), Integer.parseInt(host.substring(idx + 1))); } else { - excludes.add(bm.getDatanodeManager().getDatanodeByHost(host)); + excludeNode = bm.getDatanodeManager().getDatanodeByHost(host); + } + + if (excludeNode != null) { + excludes.add(excludeNode); + } else { + LOG.debug("DataNode {} was requested to be excluded, " + + "but it was not found.", host); } } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/web/resources/TestWebHdfsDataLocality.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/web/resources/TestWebHdfsDataLocality.java index 028e18c346..e009bc6032 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/web/resources/TestWebHdfsDataLocality.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/web/resources/TestWebHdfsDataLocality.java @@ -239,6 +239,29 @@ public void testExcludeDataNodes() throws Exception { } } + @Test + public void testExcludeWrongDataNode() throws Exception { + final Configuration conf = WebHdfsTestUtil.createConf(); + final String[] racks = {RACK0}; + final String[] hosts = {"DataNode1"}; + final int nDataNodes = hosts.length; + + final MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf) + .hosts(hosts).numDataNodes(nDataNodes).racks(racks).build(); + try { + cluster.waitActive(); + final NameNode namenode = cluster.getNameNode(); + NamenodeWebHdfsMethods.chooseDatanode( + namenode, "/path", PutOpParam.Op.CREATE, 0, + DFSConfigKeys.DFS_BLOCK_SIZE_DEFAULT, + "DataNode2", LOCALHOST, null); + } catch (Exception e) { + Assert.fail("Failed to exclude DataNode2" + e.getMessage()); + } finally { + cluster.shutdown(); + } + } + @Test public void testChooseDatanodeBeforeNamesystemInit() throws Exception { NameNode nn = mock(NameNode.class);