Browse Source

Add log for NetUtils (#14578)

3.2.1-prepare
Wenjun Ruan 12 months ago committed by GitHub
parent
commit
399d0d0ce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/config/AlertConfig.java
  2. 101
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
  3. 6
      dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterConfig.java
  4. 6
      dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/config/WorkerConfig.java

7
dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/config/AlertConfig.java

@ -19,6 +19,8 @@ package org.apache.dolphinscheduler.alert.config;
import org.apache.dolphinscheduler.common.utils.NetUtils; import org.apache.dolphinscheduler.common.utils.NetUtils;
import org.apache.commons.lang3.StringUtils;
import java.time.Duration; import java.time.Duration;
import lombok.Data; import lombok.Data;
@ -56,7 +58,10 @@ public final class AlertConfig implements Validator {
errors.rejectValue("heartbeat-interval", null, "should be a valid duration"); errors.rejectValue("heartbeat-interval", null, "should be a valid duration");
} }
alertConfig.setAlertServerAddress(NetUtils.getAddr(port)); if (StringUtils.isEmpty(alertServerAddress)) {
alertConfig.setAlertServerAddress(NetUtils.getAddr(alertConfig.getPort()));
}
printConfig(); printConfig();
} }

101
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java

@ -121,9 +121,9 @@ public class NetUtils {
InetAddress localAddress = null; InetAddress localAddress = null;
try { try {
NetworkInterface networkInterface = findNetworkInterface(); Optional<NetworkInterface> networkInterface = findNetworkInterface();
if (networkInterface != null) { if (networkInterface.isPresent()) {
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); Enumeration<InetAddress> addresses = networkInterface.get().getInetAddresses();
while (addresses.hasMoreElements()) { while (addresses.hasMoreElements()) {
Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement()); Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement());
if (addressOp.isPresent()) { if (addressOp.isPresent()) {
@ -137,8 +137,8 @@ public class NetUtils {
} }
} }
} }
}
}
localAddress = InetAddress.getLocalHost(); localAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
log.warn("InetAddress get LocalHost exception", e); log.warn("InetAddress get LocalHost exception", e);
@ -154,12 +154,15 @@ public class NetUtils {
if (address instanceof Inet6Address) { if (address instanceof Inet6Address) {
Inet6Address v6Address = (Inet6Address) address; Inet6Address v6Address = (Inet6Address) address;
if (isPreferIPV6Address()) { if (isPreferIPV6Address()) {
return Optional.ofNullable(normalizeV6Address(v6Address)); InetAddress inetAddress = normalizeV6Address(v6Address);
log.debug("The host prefer ipv6 address, will use ipv6 address: {} directly", inetAddress);
return Optional.ofNullable(inetAddress);
} }
} }
if (isValidV4Address(address)) { if (isValidV4Address(address)) {
return Optional.of(address); return Optional.of(address);
} }
log.warn("The address of the host is invalid, address: {}", address);
return Optional.empty(); return Optional.empty();
} }
@ -202,7 +205,7 @@ public class NetUtils {
* *
* @return If no {@link NetworkInterface} is available , return <code>null</code> * @return If no {@link NetworkInterface} is available , return <code>null</code>
*/ */
private static NetworkInterface findNetworkInterface() { private static Optional<NetworkInterface> findNetworkInterface() {
List<NetworkInterface> validNetworkInterfaces = emptyList(); List<NetworkInterface> validNetworkInterfaces = emptyList();
@ -211,19 +214,19 @@ public class NetUtils {
} catch (SocketException e) { } catch (SocketException e) {
log.warn("ValidNetworkInterfaces exception", e); log.warn("ValidNetworkInterfaces exception", e);
} }
if (CollectionUtils.isEmpty(validNetworkInterfaces)) {
log.warn("ValidNetworkInterfaces is empty");
return Optional.empty();
}
NetworkInterface result = null;
// Try to specify config NetWork Interface // Try to specify config NetWork Interface
for (NetworkInterface networkInterface : validNetworkInterfaces) { Optional<NetworkInterface> specifyNetworkInterface =
if (isSpecifyNetworkInterface(networkInterface)) { validNetworkInterfaces.stream().filter(NetUtils::isSpecifyNetworkInterface).findFirst();
result = networkInterface; if (specifyNetworkInterface.isPresent()) {
break; log.info("Use specified NetworkInterface: {}", specifyNetworkInterface.get());
} return specifyNetworkInterface;
} }
if (null != result) {
return result;
}
return findAddress(validNetworkInterfaces); return findAddress(validNetworkInterfaces);
} }
@ -239,8 +242,10 @@ public class NetUtils {
NetworkInterface networkInterface = interfaces.nextElement(); NetworkInterface networkInterface = interfaces.nextElement();
// ignore // ignore
if (ignoreNetworkInterface(networkInterface)) { if (ignoreNetworkInterface(networkInterface)) {
log.debug("Info NetworkInterface: {}", networkInterface);
continue; continue;
} }
log.info("Found valid NetworkInterface: {}", networkInterface);
validNetworkInterfaces.add(networkInterface); validNetworkInterfaces.add(networkInterface);
} }
return validNetworkInterfaces; return validNetworkInterfaces;
@ -265,34 +270,40 @@ public class NetUtils {
return Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface); return Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface);
} }
private static NetworkInterface findAddress(List<NetworkInterface> validNetworkInterfaces) { private static Optional<NetworkInterface> findAddress(List<NetworkInterface> validNetworkInterfaces) {
if (CollectionUtils.isEmpty(validNetworkInterfaces)) { if (CollectionUtils.isEmpty(validNetworkInterfaces)) {
return null; return Optional.empty();
} }
String networkPriority = PropertyUtils.getString(Constants.DOLPHIN_SCHEDULER_NETWORK_PRIORITY_STRATEGY, String networkPriority = PropertyUtils.getString(Constants.DOLPHIN_SCHEDULER_NETWORK_PRIORITY_STRATEGY,
NETWORK_PRIORITY_DEFAULT); NETWORK_PRIORITY_DEFAULT);
if (NETWORK_PRIORITY_DEFAULT.equalsIgnoreCase(networkPriority)) { switch (networkPriority) {
return findAddressByDefaultPolicy(validNetworkInterfaces); case NETWORK_PRIORITY_DEFAULT:
} else if (NETWORK_PRIORITY_INNER.equalsIgnoreCase(networkPriority)) { log.debug("Use default NetworkInterface acquisition policy");
return findInnerAddress(validNetworkInterfaces); return findAddressByDefaultPolicy(validNetworkInterfaces);
} else if (NETWORK_PRIORITY_OUTER.equalsIgnoreCase(networkPriority)) { case NETWORK_PRIORITY_INNER:
return findOuterAddress(validNetworkInterfaces); log.debug("Use inner NetworkInterface acquisition policy");
} else { return findInnerAddress(validNetworkInterfaces);
log.error("There is no matching network card acquisition policy!"); case NETWORK_PRIORITY_OUTER:
return null; log.debug("Use outer NetworkInterface acquisition policy");
return findOuterAddress(validNetworkInterfaces);
default:
log.error("There is no matching network card acquisition policy!");
return Optional.empty();
} }
} }
private static NetworkInterface findAddressByDefaultPolicy(List<NetworkInterface> validNetworkInterfaces) { private static Optional<NetworkInterface> findAddressByDefaultPolicy(List<NetworkInterface> validNetworkInterfaces) {
NetworkInterface networkInterface; Optional<NetworkInterface> innerAddress = findInnerAddress(validNetworkInterfaces);
networkInterface = findInnerAddress(validNetworkInterfaces); if (innerAddress.isPresent()) {
if (networkInterface == null) { log.debug("Found inner NetworkInterface: {}", innerAddress.get());
networkInterface = findOuterAddress(validNetworkInterfaces); return innerAddress;
if (networkInterface == null) { }
networkInterface = validNetworkInterfaces.get(0); Optional<NetworkInterface> outerAddress = findOuterAddress(validNetworkInterfaces);
} if (outerAddress.isPresent()) {
log.debug("Found outer NetworkInterface: {}", outerAddress.get());
return outerAddress;
} }
return networkInterface; return Optional.empty();
} }
/** /**
@ -300,35 +311,39 @@ public class NetUtils {
* *
* @return If no {@link NetworkInterface} is available , return <code>null</code> * @return If no {@link NetworkInterface} is available , return <code>null</code>
*/ */
private static NetworkInterface findInnerAddress(List<NetworkInterface> validNetworkInterfaces) { private static Optional<NetworkInterface> findInnerAddress(List<NetworkInterface> validNetworkInterfaces) {
if (CollectionUtils.isEmpty(validNetworkInterfaces)) {
return Optional.empty();
}
NetworkInterface networkInterface = null;
for (NetworkInterface ni : validNetworkInterfaces) { for (NetworkInterface ni : validNetworkInterfaces) {
Enumeration<InetAddress> address = ni.getInetAddresses(); Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) { while (address.hasMoreElements()) {
InetAddress ip = address.nextElement(); InetAddress ip = address.nextElement();
if (ip.isSiteLocalAddress() if (ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()) { && !ip.isLoopbackAddress()) {
networkInterface = ni; return Optional.of(ni);
} }
} }
} }
return networkInterface; return Optional.empty();
} }
private static NetworkInterface findOuterAddress(List<NetworkInterface> validNetworkInterfaces) { private static Optional<NetworkInterface> findOuterAddress(List<NetworkInterface> validNetworkInterfaces) {
NetworkInterface networkInterface = null; if (CollectionUtils.isEmpty(validNetworkInterfaces)) {
return Optional.empty();
}
for (NetworkInterface ni : validNetworkInterfaces) { for (NetworkInterface ni : validNetworkInterfaces) {
Enumeration<InetAddress> address = ni.getInetAddresses(); Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) { while (address.hasMoreElements()) {
InetAddress ip = address.nextElement(); InetAddress ip = address.nextElement();
if (!ip.isSiteLocalAddress() if (!ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()) { && !ip.isLoopbackAddress()) {
networkInterface = ni; return Optional.of(ni);
} }
} }
} }
return networkInterface; return Optional.empty();
} }
} }

6
dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterConfig.java

@ -26,6 +26,8 @@ import org.apache.dolphinscheduler.server.master.dispatch.host.assign.HostSelect
import org.apache.dolphinscheduler.server.master.processor.queue.TaskExecuteRunnable; import org.apache.dolphinscheduler.server.master.processor.queue.TaskExecuteRunnable;
import org.apache.dolphinscheduler.server.master.runner.WorkflowExecuteRunnable; import org.apache.dolphinscheduler.server.master.runner.WorkflowExecuteRunnable;
import org.apache.commons.lang3.StringUtils;
import java.time.Duration; import java.time.Duration;
import lombok.Data; import lombok.Data;
@ -157,8 +159,10 @@ public class MasterConfig implements Validator {
if (masterConfig.getWorkerGroupRefreshInterval().getSeconds() < 10) { if (masterConfig.getWorkerGroupRefreshInterval().getSeconds() < 10) {
errors.rejectValue("worker-group-refresh-interval", null, "should >= 10s"); errors.rejectValue("worker-group-refresh-interval", null, "should >= 10s");
} }
if (StringUtils.isEmpty(masterConfig.getMasterAddress())) {
masterConfig.setMasterAddress(NetUtils.getAddr(masterConfig.getListenPort()));
}
masterConfig.setMasterAddress(NetUtils.getAddr(masterConfig.getListenPort()));
masterConfig.setMasterRegistryPath( masterConfig.setMasterRegistryPath(
RegistryNodeType.MASTER.getRegistryPath() + "/" + masterConfig.getMasterAddress()); RegistryNodeType.MASTER.getRegistryPath() + "/" + masterConfig.getMasterAddress());
printConfig(); printConfig();

6
dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/config/WorkerConfig.java

@ -24,6 +24,8 @@ import org.apache.dolphinscheduler.registry.api.ConnectStrategyProperties;
import org.apache.dolphinscheduler.remote.config.NettyClientConfig; import org.apache.dolphinscheduler.remote.config.NettyClientConfig;
import org.apache.dolphinscheduler.remote.config.NettyServerConfig; import org.apache.dolphinscheduler.remote.config.NettyServerConfig;
import org.apache.commons.lang3.StringUtils;
import java.time.Duration; import java.time.Duration;
import lombok.Data; import lombok.Data;
@ -80,7 +82,9 @@ public class WorkerConfig implements Validator {
if (workerConfig.getMaxCpuLoadAvg() <= 0) { if (workerConfig.getMaxCpuLoadAvg() <= 0) {
workerConfig.setMaxCpuLoadAvg(Runtime.getRuntime().availableProcessors() * 2); workerConfig.setMaxCpuLoadAvg(Runtime.getRuntime().availableProcessors() * 2);
} }
workerConfig.setWorkerAddress(NetUtils.getAddr(workerConfig.getListenPort())); if (StringUtils.isEmpty(workerConfig.getWorkerAddress())) {
workerConfig.setWorkerAddress(NetUtils.getAddr(workerConfig.getListenPort()));
}
workerConfig.setWorkerRegistryPath(REGISTRY_DOLPHINSCHEDULER_WORKERS + "/" + workerConfig.getWorkerAddress()); workerConfig.setWorkerRegistryPath(REGISTRY_DOLPHINSCHEDULER_WORKERS + "/" + workerConfig.getWorkerAddress());
printConfig(); printConfig();

Loading…
Cancel
Save