You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zeppelin.apache.org by GitBox <gi...@apache.org> on 2019/05/15 15:52:28 UTC

[GitHub] [zeppelin] felixcheung commented on a change in pull request #3364: [ZEPPELIN-4155] Unable get available HostAddress in multi-NIC environment

felixcheung commented on a change in pull request #3364: [ZEPPELIN-4155] Unable get available HostAddress in multi-NIC environment
URL: https://github.com/apache/zeppelin/pull/3364#discussion_r284328179
 
 

 ##########
 File path: zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/remote/RemoteInterpreterUtils.java
 ##########
 @@ -93,22 +97,53 @@ public static TServerSocket createTServerSocket(String portRange)
     throw new IOException("No available port in the portRange: " + portRange);
   }
 
+  // 1) Multiple NetworkCard will be configured on some servers
+  // 2) In the docker container environment, A container will also generate multiple virtual NetworkCard
   public static String findAvailableHostAddress() throws UnknownHostException, SocketException {
-    InetAddress address = InetAddress.getLocalHost();
-    if (address.isLoopbackAddress()) {
-      for (NetworkInterface networkInterface : Collections
-          .list(NetworkInterface.getNetworkInterfaces())) {
-        if (!networkInterface.isLoopback()) {
-          for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
-            InetAddress a = interfaceAddress.getAddress();
-            if (a instanceof Inet4Address) {
-              return a.getHostAddress();
-            }
+    List<NetworkInterface> netlist = new ArrayList<NetworkInterface>();
+
+    // Get all the network cards in the current environment
+    Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces();
+    while (netInterfaces.hasMoreElements()) {
+      NetworkInterface networkInterface = (NetworkInterface)netInterfaces.nextElement();
+      LOGGER.info("networkInterface = " + networkInterface.toString());
+      if (networkInterface.isLoopback()) {
+        // Filter lo network card
+        continue;
+      }
+      // When all the network cards are obtained by the above method,
+      // The order obtained is the reverse of the order of the NICs
+      // seen in the server with the ifconfig command.
+      // Therefore, when you want to traverse from the first NIC,
+      // Need to reverse the elements in Enumeration<?>
+      netlist.add(0, networkInterface);
+    }
+
+    for (NetworkInterface list:netlist) {
+      Enumeration<?> enumInetAddress = list.getInetAddresses();
+
+      while (enumInetAddress.hasMoreElements()) {
+        InetAddress ip = (InetAddress) enumInetAddress.nextElement();
+        LOGGER.info("ip = " + ip.toString());
+        if (!ip.isLoopbackAddress()) {
+          if (ip.getHostAddress().equalsIgnoreCase("127.0.0.1")){
 
 Review comment:
   isn't `isLoopbackAddress` enough?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services