You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2020/09/10 12:36:04 UTC

[GitHub] [incubator-dolphinscheduler] CalvinKirs commented on a change in pull request #3695: [Improvement][COMMON-NetUtils] Get the native IP policy problem (获取本机ip策略问题 )

CalvinKirs commented on a change in pull request #3695:
URL: https://github.com/apache/incubator-dolphinscheduler/pull/3695#discussion_r486300170



##########
File path: dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
##########
@@ -227,4 +230,72 @@ private static boolean isSpecifyNetworkInterface(NetworkInterface networkInterfa
         String preferredNetworkInterface = System.getProperty(DOLPHIN_SCHEDULER_PREFERRED_NETWORK_INTERFACE);
         return Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface);
     }
+
+    private static NetworkInterface findAddress(List<NetworkInterface> validNetworkInterfaces) {
+        if (validNetworkInterfaces.isEmpty()) {
+            return null;
+        }
+        String networkPriority = PropertyUtils.getString(Constants.NETWORK_PRIORITY_STRATEGY, NETWORK_PRIORITY_DEFAULT);
+        if (NETWORK_PRIORITY_DEFAULT.equalsIgnoreCase(networkPriority)) {
+            return findAddressByDefaultPolicy(validNetworkInterfaces);
+        } else if (NETWORK_PRIORITY_INNER.equalsIgnoreCase(networkPriority)) {
+            return findInnerAddress(validNetworkInterfaces);
+        } else if (NETWORK_PRIORITY_OUTER.equalsIgnoreCase(networkPriority)) {
+            return findOuterAddress(validNetworkInterfaces);
+        } else {
+            logger.error("There is no matching network card acquisition policy!");
+            return null;
+        }
+    }
+
+    private static NetworkInterface findAddressByDefaultPolicy(List<NetworkInterface> validNetworkInterfaces) {
+        NetworkInterface networkInterface;
+        networkInterface = findInnerAddress(validNetworkInterfaces);
+        if (networkInterface == null) {
+            networkInterface = findOuterAddress(validNetworkInterfaces);
+            if (networkInterface == null) {
+                networkInterface = validNetworkInterfaces.get(0);
+            }
+        }
+        return networkInterface;
+    }
+
+    /**
+     * Get the Intranet IP
+     *
+     * @return If no {@link NetworkInterface} is available , return <code>null</code>
+     */
+    private static NetworkInterface findInnerAddress(List<NetworkInterface> validNetworkInterfaces) {
+
+        NetworkInterface networkInterface = null;
+        for (NetworkInterface ni : validNetworkInterfaces) {
+            Enumeration<InetAddress> address = ni.getInetAddresses();
+            while (address.hasMoreElements()) {
+                InetAddress ip = address.nextElement();
+                if (ip.isSiteLocalAddress()
+                        && !ip.isLoopbackAddress()
+                        && !ip.getHostAddress().contains(":")) {
+                    networkInterface = ni;
+                }
+            }
+        }
+        return networkInterface;
+    }
+
+    private static NetworkInterface findOuterAddress(List<NetworkInterface> validNetworkInterfaces) {
+        NetworkInterface networkInterface = null;
+        for (NetworkInterface ni : validNetworkInterfaces) {
+            Enumeration<InetAddress> address = ni.getInetAddresses();
+            while (address.hasMoreElements()) {
+                InetAddress ip = address.nextElement();
+                if (!ip.isSiteLocalAddress()
+                        && !ip.isLoopbackAddress()
+                        && !ip.getHostAddress().contains(":")) {
+                    networkInterface = ni;

Review comment:
       Hi, there may be a little problem here, we need to consider the IPV6 situation,




----------------------------------------------------------------
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