فهرست منبع

HADOOP-7806. Support binding to sub-interfaces. Contributed by Harsh J, Eli Collins

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1@1304175 13f79535-47bb-0310-9956-ffa450edef68
Eli Collins 13 سال پیش
والد
کامیت
a69b2d1238
2فایلهای تغییر یافته به همراه43 افزوده شده و 12 حذف شده
  1. 2 0
      CHANGES.txt
  2. 41 12
      src/core/org/apache/hadoop/net/DNS.java

+ 2 - 0
CHANGES.txt

@@ -7,6 +7,8 @@ Release 1.1.0 - unreleased
     MAPREDUCE-3118. Backport Gridmix and Rumen features to
                     branch-0.20-security (Ravi Gummadi via amarrk)
 
+    HADOOP-7806. Support binding to sub-interfaces. (harsh, eli via eli)
+
   BUG FIXES
 
     HDFS-2305. Running multiple 2NNs can result in corrupt file system. (atm)

+ 41 - 12
src/core/org/apache/hadoop/net/DNS.java

@@ -77,12 +77,36 @@ public class DNS {
     return attribute.get("PTR").get().toString();
   }
 
+  /**
+   * @return NetworkInterface for the given subinterface name (eg eth0:0)
+   *    or null if no interface with the given name can be found
+   */
+  private static NetworkInterface getSubinterface(String strInterface)
+      throws SocketException {
+    Enumeration<NetworkInterface> nifs =
+      NetworkInterface.getNetworkInterfaces();
+
+    while (nifs.hasMoreElements()) {
+      Enumeration<NetworkInterface> subNifs =
+        nifs.nextElement().getSubInterfaces();
+
+      while (subNifs.hasMoreElements()) {
+        NetworkInterface nif = subNifs.nextElement();
+        if (nif.getName().equals(strInterface)) {
+          return nif;
+        }
+      }
+    }
+    return null;
+  }
+
   /**
    * Returns all the IPs associated with the provided interface, if any, in
    * textual form.
    * 
    * @param strInterface
-   *            The name of the network interface to query (e.g. eth0)
+   *            The name of the network interface or subinterface to query
+   *            (eg eth0 or eth0:0) or the string "default"
    * @return A string vector of all the IPs associated with the provided
    *         interface
    * @throws UnknownHostException
@@ -97,16 +121,14 @@ public class DNS {
           InetAddress.getLocalHost().getHostAddress()
       };
     }
+    NetworkInterface netIf;
     try {
-      NetworkInterface netIF = NetworkInterface.getByName(strInterface);
-      if (netIF == null) {
-        throw new UnknownHostException("Unknown interface " + strInterface);
-      } else {
-        Vector<String> ips = new Vector<String>();
-        Enumeration<InetAddress> e = netIF.getInetAddresses();
-        while (e.hasMoreElements())
-          ips.add(e.nextElement().getHostAddress());
-        return ips.toArray(new String[] {});
+      netIf = NetworkInterface.getByName(strInterface);
+      if (netIf == null) {
+        netIf = getSubinterface(strInterface);
+        if (netIf == null) {
+          throw new UnknownHostException("Unknown interface " + strInterface);
+        }
       }
     } catch (SocketException e) {
       LOG.warn("Unable to get IP for interface " + strInterface, e);
@@ -114,6 +136,11 @@ public class DNS {
           InetAddress.getLocalHost().getHostAddress()
       };
     }
+    Vector<String> ips = new Vector<String>();
+    Enumeration<InetAddress> e = netIf.getInetAddresses();
+    while (e.hasMoreElements())
+      ips.add(e.nextElement().getHostAddress());
+    return ips.toArray(new String[] {});
   }
 
   /**
@@ -121,7 +148,8 @@ public class DNS {
    * network interface
    * 
    * @param strInterface
-   *            The name of the network interface to query (e.g. eth0)
+   *            The name of the network interface or subinterface to query
+   *            (e.g. eth0 or eth0:0) or the string "default"
    * @return The IP address in text form
    * @throws UnknownHostException
    *             If one is encountered in querying the default interface
@@ -137,7 +165,8 @@ public class DNS {
    * address bound to the specified network interface
    * 
    * @param strInterface
-   *            The name of the network interface to query (e.g. eth0)
+   *            The name of the network interface or subinterface to query
+   *            (e.g. eth0 or eth0:0) or the string "default"
    * @param nameserver
    *            The DNS host name
    * @return A string vector of all host names associated with the IPs tied to