This is an automated email from the ASF dual-hosted git repository.
kirs pushed a commit to branch 1.3.7-prepare
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git
The following commit(s) were added to refs/heads/1.3.7-prepare by this push:
new 8a26e5b [1.3.7-prepare#5468][Improvement][Common] Fix obtaining IP is
incorrect (#5843)
8a26e5b is described below
commit 8a26e5b5185b9922ef93b6d878c9f888968a58d1
Author: Kirs <[email protected]>
AuthorDate: Tue Jul 27 23:48:33 2021 +0800
[1.3.7-prepare#5468][Improvement][Common] Fix obtaining IP is incorrect
(#5843)
* [1.3.7-prepare#5468][Improvement][Common] Fix obtaining IP is incorrect
pr#5594
issue #5468
* [Improvement][Common] Add unit test for NetUtils
Co-authored-by: chengshiwen <[email protected]>
---
.../build/conf/dolphinscheduler/common.properties.tpl | 6 ++++++
.../org/apache/dolphinscheduler/common/Constants.java | 8 ++++----
.../dolphinscheduler/common/utils/NetUtils.java | 19 ++++++-------------
.../src/main/resources/common.properties | 6 ++++++
.../dolphinscheduler/common/utils/NetUtilsTest.java | 15 ++++++++++++++-
5 files changed, 36 insertions(+), 18 deletions(-)
diff --git a/docker/build/conf/dolphinscheduler/common.properties.tpl
b/docker/build/conf/dolphinscheduler/common.properties.tpl
index 83f7307..84a4496 100644
--- a/docker/build/conf/dolphinscheduler/common.properties.tpl
+++ b/docker/build/conf/dolphinscheduler/common.properties.tpl
@@ -66,5 +66,11 @@
yarn.application.status.address=${YARN_APPLICATION_STATUS_ADDRESS}
# system env path
#dolphinscheduler.env.path=env/dolphinscheduler_env.sh
+# network interface preferred like eth0, default: empty
+#dolphin.scheduler.network.interface.preferred=
+
+# network IP gets priority, default: inner outer
+#dolphin.scheduler.network.priority.strategy=default
+
# development state
development.state=false
diff --git
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java
index d29791b..d25ddd0 100644
---
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java
+++
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java
@@ -998,14 +998,14 @@ public final class Constants {
public static final String START_END_DATE = "startDate,endDate";
/**
- * Network system properties
+ * network interface preferred
*/
- public static final String DOLPHIN_SCHEDULER_PREFERRED_NETWORK_INTERFACE =
"dolphin.scheduler.network.interface.preferred";
+ public static final String DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED =
"dolphin.scheduler.network.interface.preferred";
/**
- * Network IP gets priority, default inner outer
+ * network IP gets priority, default inner outer
*/
- public static final String NETWORK_PRIORITY_STRATEGY =
"dolphin.scheduler.network.priority.strategy";
+ public static final String DOLPHIN_SCHEDULER_NETWORK_PRIORITY_STRATEGY =
"dolphin.scheduler.network.priority.strategy";
/**
diff --git
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
index 4314089..926c3ab 100644
---
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
+++
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
@@ -17,8 +17,6 @@
package org.apache.dolphinscheduler.common.utils;
-import static
org.apache.dolphinscheduler.common.Constants.DOLPHIN_SCHEDULER_PREFERRED_NETWORK_INTERFACE;
-
import static java.util.Collections.emptyList;
import org.apache.dolphinscheduler.common.Constants;
@@ -44,7 +42,6 @@ import org.slf4j.LoggerFactory;
*/
public class NetUtils {
- private static final Pattern STS_PATTERN = Pattern.compile("-\\d+$"); //
StatefulSet pattern
private static final Pattern IP_PATTERN =
Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
private static final String NETWORK_PRIORITY_DEFAULT = "default";
private static final String NETWORK_PRIORITY_INNER = "inner";
@@ -81,13 +78,8 @@ public class NetUtils {
if (inetAddress != null) {
if (Constants.KUBERNETES_MODE) {
String canonicalHost = inetAddress.getCanonicalHostName();
- if (!canonicalHost.contains(".") ||
IP_PATTERN.matcher(canonicalHost).matches()) {
- String host = inetAddress.getHostName();
- if (STS_PATTERN.matcher(host).find()) {
- return String.format("%s.%s", host,
host.replaceFirst("\\d+$", "headless"));
- }
- } else if (canonicalHost.contains(".")) {
- String[] items = canonicalHost.split("\\.");
+ String[] items = canonicalHost.split("\\.");
+ if (items.length == 6 && "svc".equals(items[3])) {
return String.format("%s.%s", items[0], items[1]);
}
return canonicalHost;
@@ -136,7 +128,7 @@ public class NetUtils {
Optional<InetAddress> addressOp =
toValidAddress(addresses.nextElement());
if (addressOp.isPresent()) {
try {
- if (addressOp.get().isReachable(100)) {
+ if (addressOp.get().isReachable(200)) {
LOCAL_ADDRESS = addressOp.get();
return LOCAL_ADDRESS;
}
@@ -266,7 +258,8 @@ public class NetUtils {
}
private static boolean isSpecifyNetworkInterface(NetworkInterface
networkInterface) {
- String preferredNetworkInterface =
System.getProperty(DOLPHIN_SCHEDULER_PREFERRED_NETWORK_INTERFACE);
+ String preferredNetworkInterface =
PropertyUtils.getString(Constants.DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED,
+
System.getProperty(Constants.DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED));
return Objects.equals(networkInterface.getDisplayName(),
preferredNetworkInterface);
}
@@ -274,7 +267,7 @@ public class NetUtils {
if (validNetworkInterfaces.isEmpty()) {
return null;
}
- String networkPriority =
PropertyUtils.getString(Constants.NETWORK_PRIORITY_STRATEGY,
NETWORK_PRIORITY_DEFAULT);
+ String networkPriority =
PropertyUtils.getString(Constants.DOLPHIN_SCHEDULER_NETWORK_PRIORITY_STRATEGY,
NETWORK_PRIORITY_DEFAULT);
if (NETWORK_PRIORITY_DEFAULT.equalsIgnoreCase(networkPriority)) {
return findAddressByDefaultPolicy(validNetworkInterfaces);
} else if (NETWORK_PRIORITY_INNER.equalsIgnoreCase(networkPriority)) {
diff --git a/dolphinscheduler-common/src/main/resources/common.properties
b/dolphinscheduler-common/src/main/resources/common.properties
index b658029..5abb907 100644
--- a/dolphinscheduler-common/src/main/resources/common.properties
+++ b/dolphinscheduler-common/src/main/resources/common.properties
@@ -65,6 +65,12 @@
yarn.application.status.address=http://ds1:%s/ws/v1/cluster/apps/%s
# if custom you resourcemanager port ,you need to replace 8088 else default
value.
resource.manager.httpaddress.port=8088
+# network interface preferred like eth0, default: empty
+#dolphin.scheduler.network.interface.preferred=
+
+# network IP gets priority, default: inner outer
+#dolphin.scheduler.network.priority.strategy=default
+
# system env path
#dolphinscheduler.env.path=env/dolphinscheduler_env.sh
diff --git
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
index 34d4451..bfb78d4 100644
---
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
+++
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
@@ -52,11 +52,24 @@ public class NetUtilsTest {
assertEquals("172.17.0.15", NetUtils.getHost(address));
CommonTest.setFinalStatic(Constants.class.getDeclaredField("KUBERNETES_MODE"),
true);
assertEquals("dolphinscheduler-worker-0.dolphinscheduler-worker-headless",
NetUtils.getHost(address));
+
+ address = mock(InetAddress.class);
+
when(address.getCanonicalHostName()).thenReturn("busybox-1.default-subdomain.my-namespace.svc.cluster-domain.example");
+ when(address.getHostName()).thenReturn("busybox-1");
+
CommonTest.setFinalStatic(Constants.class.getDeclaredField("KUBERNETES_MODE"),
true);
+ assertEquals("busybox-1.default-subdomain", NetUtils.getHost(address));
+
+ address = mock(InetAddress.class);
+
when(address.getCanonicalHostName()).thenReturn("dolphinscheduler.cluster-domain.example");
+ when(address.getHostName()).thenReturn("dolphinscheduler");
+
CommonTest.setFinalStatic(Constants.class.getDeclaredField("KUBERNETES_MODE"),
true);
+ assertEquals("dolphinscheduler.cluster-domain.example",
NetUtils.getHost(address));
+
address = mock(InetAddress.class);
when(address.getCanonicalHostName()).thenReturn("dolphinscheduler-worker-0");
when(address.getHostName()).thenReturn("dolphinscheduler-worker-0");
CommonTest.setFinalStatic(Constants.class.getDeclaredField("KUBERNETES_MODE"),
true);
-
assertEquals("dolphinscheduler-worker-0.dolphinscheduler-worker-headless",
NetUtils.getHost(address));
+ assertEquals("dolphinscheduler-worker-0", NetUtils.getHost(address));
}
@Test