Skip to content

Commit edc1381

Browse files
surendralilhoreshvachko
authored andcommitted
HDFS-14216. NullPointerException happens in NamenodeWebHdfs. Contributed by lujie.
(cherry picked from commit 92b53c4) (cherry picked from commit 2e93951)
1 parent 7f32a31 commit edc1381

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/web/resources/NamenodeWebHdfsMethods.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,21 @@ static DatanodeInfo chooseDatanode(final NameNode namenode,
265265
for (String host : StringUtils
266266
.getTrimmedStringCollection(excludeDatanodes)) {
267267
int idx = host.indexOf(":");
268-
if (idx != -1) {
269-
excludes.add(bm.getDatanodeManager().getDatanodeByXferAddr(
270-
host.substring(0, idx), Integer.parseInt(host.substring(idx + 1))));
268+
Node excludeNode = null;
269+
if (idx != -1) {
270+
excludeNode = bm.getDatanodeManager().getDatanodeByXferAddr(
271+
host.substring(0, idx), Integer.parseInt(host.substring(idx + 1)));
271272
} else {
272-
excludes.add(bm.getDatanodeManager().getDatanodeByHost(host));
273+
excludeNode = bm.getDatanodeManager().getDatanodeByHost(host);
274+
}
275+
276+
if (excludeNode != null) {
277+
excludes.add(excludeNode);
278+
} else {
279+
if (LOG.isDebugEnabled()) {
280+
LOG.debug("DataNode " + host + " was requested to be excluded, "
281+
+ "but it was not found.");
282+
}
273283
}
274284
}
275285
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/web/resources/TestWebHdfsDataLocality.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,29 @@ public void testExcludeDataNodes() throws Exception {
238238
}
239239
}
240240

241+
@Test
242+
public void testExcludeWrongDataNode() throws Exception {
243+
final Configuration conf = WebHdfsTestUtil.createConf();
244+
final String[] racks = {RACK0};
245+
final String[] hosts = {"DataNode1"};
246+
final int nDataNodes = hosts.length;
247+
248+
final MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf)
249+
.hosts(hosts).numDataNodes(nDataNodes).racks(racks).build();
250+
try {
251+
cluster.waitActive();
252+
final NameNode namenode = cluster.getNameNode();
253+
NamenodeWebHdfsMethods.chooseDatanode(
254+
namenode, "/path", PutOpParam.Op.CREATE, 0,
255+
DFSConfigKeys.DFS_BLOCK_SIZE_DEFAULT,
256+
"DataNode2", LOCALHOST, null);
257+
} catch (Exception e) {
258+
Assert.fail("Failed to exclude DataNode2" + e.getMessage());
259+
} finally {
260+
cluster.shutdown();
261+
}
262+
}
263+
241264
@Test
242265
public void testChooseDatanodeBeforeNamesystemInit() throws Exception {
243266
NameNode nn = mock(NameNode.class);

0 commit comments

Comments
 (0)