hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szets...@apache.org
Subject svn commit: r1413969 - in /hadoop/common/branches/branch-1: CHANGES.txt src/core/org/apache/hadoop/net/NetUtils.java src/test/org/apache/hadoop/net/TestNetUtils.java
Date Tue, 27 Nov 2012 06:24:42 GMT
Author: szetszwo
Date: Tue Nov 27 01:44:59 2012
New Revision: 1413969

URL: http://svn.apache.org/viewvc?rev=1413969&view=rev
Log:
HADOOP-9095. Backport HADOOP-8372: NetUtils.normalizeHostName() incorrectly handles hostname
starting with a numeric character.  Contributed by Jing Zhao 

Modified:
    hadoop/common/branches/branch-1/CHANGES.txt
    hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/NetUtils.java
    hadoop/common/branches/branch-1/src/test/org/apache/hadoop/net/TestNetUtils.java

Modified: hadoop/common/branches/branch-1/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1/CHANGES.txt?rev=1413969&r1=1413968&r2=1413969&view=diff
==============================================================================
--- hadoop/common/branches/branch-1/CHANGES.txt (original)
+++ hadoop/common/branches/branch-1/CHANGES.txt Tue Nov 27 01:44:59 2012
@@ -312,6 +312,10 @@ Release 1.2.0 - unreleased
     HDFS-4207. All hadoop fs operations fail if the default fs is down even if 
     a different fs is specified in the command. (Jing Zhao via suresh)
 
+    HADOOP-9095. Backport HADOOP-8372: NetUtils.normalizeHostName() incorrectly
+    handles hostname starting with a numeric character.  (Jing Zhao via
+    szetszwo)
+
 Release 1.1.1 - Unreleased
 
   INCOMPATIBLE CHANGES

Modified: hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/NetUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/NetUtils.java?rev=1413969&r1=1413968&r2=1413969&view=diff
==============================================================================
--- hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/NetUtils.java (original)
+++ hadoop/common/branches/branch-1/src/core/org/apache/hadoop/net/NetUtils.java Tue Nov 27
01:44:59 2012
@@ -536,15 +536,11 @@ public class NetUtils {
    * @return its IP address in the string format
    */
   public static String normalizeHostName(String name) {
-    if (Character.digit(name.charAt(0), 10) != -1) { //FIXME 
+    try {
+      InetAddress ipAddress = InetAddress.getByName(name);
+      return ipAddress.getHostAddress();
+    } catch (UnknownHostException e) {
       return name;
-    } else {
-      try {
-        InetAddress ipAddress = InetAddress.getByName(name);
-        return ipAddress.getHostAddress();
-      } catch (UnknownHostException e) {
-        return name;
-      }
     }
   }
   

Modified: hadoop/common/branches/branch-1/src/test/org/apache/hadoop/net/TestNetUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1/src/test/org/apache/hadoop/net/TestNetUtils.java?rev=1413969&r1=1413968&r2=1413969&view=diff
==============================================================================
--- hadoop/common/branches/branch-1/src/test/org/apache/hadoop/net/TestNetUtils.java (original)
+++ hadoop/common/branches/branch-1/src/test/org/apache/hadoop/net/TestNetUtils.java Tue Nov
27 01:44:59 2012
@@ -18,6 +18,7 @@
 package org.apache.hadoop.net;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -30,6 +31,8 @@ import java.net.Socket;
 import java.net.SocketException;
 import java.net.URI;
 import java.net.UnknownHostException;
+import java.util.Arrays;
+import java.util.List;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.conf.Configuration;
@@ -321,4 +324,26 @@ public class TestNetUtils {
     String gotStr = StringUtils.join(got, ", ");
     assertEquals(expectStr, gotStr);
   }
+  
+  /**
+   * Test for {@link NetUtils#normalizeHostNames
+
+   */
+  @Test
+  public void testNormalizeHostName() {
+    List<String> hosts = Arrays.asList(new String[] { "127.0.0.1", "localhost",
+        "3w.org", "UnknownHost" });
+    List<String> normalizedHosts = NetUtils.normalizeHostNames(hosts);
+    // when ipaddress is normalized, same address is expected in return
+    assertEquals(normalizedHosts.get(0), hosts.get(0));
+    // for normalizing a resolvable hostname, resolved ipaddress is expected in
+    // return
+    assertFalse(normalizedHosts.get(1).equals(hosts.get(1)));
+    assertEquals(normalizedHosts.get(1), hosts.get(0));
+    // when normalizing a valid resolvable hostname start with numeric,
+    // its ipaddress is expected to return
+    assertFalse(normalizedHosts.get(2).equals(hosts.get(2)));
+    // return the same hostname after normalizing a irresolvable hostname.
+    assertEquals(normalizedHosts.get(3), hosts.get(3));
+  }
 }
\ No newline at end of file



Mime
View raw message