From d55f3780fbf9308554ef3362c2be89651db43f46 Mon Sep 17 00:00:00 2001 From: Arun Murthy Date: Sat, 5 Oct 2013 22:20:18 +0000 Subject: [PATCH] YARN-1032. Fixed NPE in RackResolver. Contributed by Lohit Vijayarenu. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1529534 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-yarn-project/CHANGES.txt | 2 ++ .../org/apache/hadoop/yarn/util/RackResolver.java | 12 ++++++++++-- .../apache/hadoop/yarn/util/TestRackResolver.java | 10 ++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/hadoop-yarn-project/CHANGES.txt b/hadoop-yarn-project/CHANGES.txt index 6e8ad1edbd9..0c3a0307fbf 100644 --- a/hadoop-yarn-project/CHANGES.txt +++ b/hadoop-yarn-project/CHANGES.txt @@ -179,6 +179,8 @@ Release 2.1.2 - UNRELEASED YARN-1273. Fixed Distributed-shell to account for containers that failed to start. (Hitesh Shah via vinodkv) + YARN-1032. Fixed NPE in RackResolver. (Lohit Vijayarenu via acmurthy) + Release 2.1.1-beta - 2013-09-23 INCOMPATIBLE CHANGES diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/RackResolver.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/RackResolver.java index 601f8abc08e..cc2a56c3be6 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/RackResolver.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/RackResolver.java @@ -29,6 +29,7 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.CommonConfigurationKeysPublic; import org.apache.hadoop.net.CachedDNSToSwitchMapping; import org.apache.hadoop.net.DNSToSwitchMapping; +import org.apache.hadoop.net.NetworkTopology; import org.apache.hadoop.net.Node; import org.apache.hadoop.net.NodeBase; import org.apache.hadoop.net.ScriptBasedMapping; @@ -98,8 +99,15 @@ public class RackResolver { List tmpList = new ArrayList(1); tmpList.add(hostName); List rNameList = dnsToSwitchMapping.resolve(tmpList); - String rName = rNameList.get(0); - LOG.info("Resolved " + hostName + " to " + rName); + String rName = null; + if (rNameList == null || rNameList.get(0) == null) { + rName = NetworkTopology.DEFAULT_RACK; + LOG.info("Couldn't resolve " + hostName + ". Falling back to " + + NetworkTopology.DEFAULT_RACK); + } else { + rName = rNameList.get(0); + LOG.info("Resolved " + hostName + " to " + rName); + } return new NodeBase(hostName, rName); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestRackResolver.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestRackResolver.java index 42f7b9b5091..70ca23c3a2e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestRackResolver.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestRackResolver.java @@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.CommonConfigurationKeysPublic; import org.apache.hadoop.net.DNSToSwitchMapping; +import org.apache.hadoop.net.NetworkTopology; import org.apache.hadoop.net.Node; import org.junit.Assert; import org.junit.Test; @@ -35,6 +36,8 @@ import org.junit.Test; public class TestRackResolver { private static Log LOG = LogFactory.getLog(TestRackResolver.class); + private static final String invalidHost = "invalidHost"; + public static final class MyResolver implements DNSToSwitchMapping { @@ -50,6 +53,11 @@ public class TestRackResolver { if (hostList.isEmpty()) { return returnList; } + if (hostList.get(0).equals(invalidHost)) { + // Simulate condition where resolving host returns null + return null; + } + LOG.info("Received resolve request for " + hostList.get(0)); if (hostList.get(0).equals("host1") @@ -90,6 +98,8 @@ public class TestRackResolver { Assert.assertEquals("/rack1", node.getNetworkLocation()); node = RackResolver.resolve("host1"); Assert.assertEquals("/rack1", node.getNetworkLocation()); + node = RackResolver.resolve(invalidHost); + Assert.assertEquals(NetworkTopology.DEFAULT_RACK, node.getNetworkLocation()); } }