You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by da...@apache.org on 2022/03/03 13:11:38 UTC

[hudi] branch master updated: [HUDI-3552] Strength the NetworkUtils#getHostname by checking network interfaces first (#4942)

This is an automated email from the ASF dual-hosted git repository.

danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git


The following commit(s) were added to refs/heads/master by this push:
     new a4ba0ff  [HUDI-3552] Strength the NetworkUtils#getHostname by checking network interfaces first (#4942)
a4ba0ff is described below

commit a4ba0fff07861bdff338074fb5d84726f430883f
Author: Danny Chan <yu...@gmail.com>
AuthorDate: Thu Mar 3 21:11:08 2022 +0800

    [HUDI-3552] Strength the NetworkUtils#getHostname by checking network interfaces first (#4942)
    
    * In some complex network environment, the current code returns wildcard address 0.0.0.0 which is not desired.
---
 .../org/apache/hudi/common/util/NetworkUtils.java     | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/hudi-common/src/main/java/org/apache/hudi/common/util/NetworkUtils.java b/hudi-common/src/main/java/org/apache/hudi/common/util/NetworkUtils.java
index 1e320a5..29c42e3 100644
--- a/hudi-common/src/main/java/org/apache/hudi/common/util/NetworkUtils.java
+++ b/hudi-common/src/main/java/org/apache/hudi/common/util/NetworkUtils.java
@@ -23,6 +23,10 @@ import org.apache.hudi.exception.HoodieException;
 import java.io.IOException;
 import java.net.DatagramSocket;
 import java.net.InetAddress;
+import java.net.InterfaceAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Enumeration;
 
 /**
  * A utility class for network.
@@ -30,6 +34,21 @@ import java.net.InetAddress;
 public class NetworkUtils {
 
   public static synchronized String getHostname() {
+    try {
+      Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
+      while (networkInterfaceEnumeration.hasMoreElements()) {
+        for (InterfaceAddress interfaceAddress : networkInterfaceEnumeration.nextElement().getInterfaceAddresses()) {
+          InetAddress address = interfaceAddress.getAddress();
+          if (!address.isLinkLocalAddress() && !address.isLoopbackAddress() && !address.isAnyLocalAddress()) {
+            return address.getHostAddress();
+          }
+        }
+      }
+    } catch (SocketException e) {
+      throw new HoodieException("Unable to find server port", e);
+    }
+
+    // fallback
     try (DatagramSocket s = new DatagramSocket()) {
       // see https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
       // for details.