Browse Source

[Improvement][Common] Obtain resource information in the k8s environm… (#14968)

* [Improvement][Common] Obtain resource information in the k8s environment.

* [Improvement][Common] Change the comment

---------

Co-authored-by: 旺阳 <qingwli@cisco.com>
augit-log
winghv 9 months ago committed by GitHub
parent
commit
c5bc535b85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/KubernetesUtils.java
  2. 23
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java

6
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/KubernetesUtils.java

@ -24,9 +24,11 @@ import lombok.experimental.UtilityClass;
@UtilityClass
public class KubernetesUtils {
public static final Boolean KUBERNETES_MODE = !StringUtils.isEmpty(System.getenv("KUBERNETES_SERVICE_HOST"))
&& !StringUtils.isEmpty(System.getenv("KUBERNETES_SERVICE_PORT"));
public boolean isKubernetesMode() {
return !StringUtils.isEmpty(System.getenv("KUBERNETES_SERVICE_HOST"))
&& !StringUtils.isEmpty(System.getenv("KUBERNETES_SERVICE_PORT"));
return KUBERNETES_MODE;
}
}

23
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java

@ -35,6 +35,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.math.RoundingMode;
import java.text.DecimalFormat;
@ -96,16 +97,22 @@ public class OSUtils {
}
/**
* get available physical memory size
* get available physical or pod memory size
* <p>
* Keep 2 decimal
*
* @return available Physical Memory Size, unit: G
* @return Available physical or pod memory size, unit: G
*/
public static double availablePhysicalMemorySize() {
GlobalMemory memory = hal.getMemory();
double availablePhysicalMemorySize = memory.getAvailable() / 1024.0 / 1024 / 1024;
double availablePhysicalMemorySize;
if (KubernetesUtils.isKubernetesMode()) {
long freeMemory = Runtime.getRuntime().freeMemory();
availablePhysicalMemorySize = freeMemory / 1024.0 / 1024 / 1024;
} else {
GlobalMemory memory = hal.getMemory();
availablePhysicalMemorySize = memory.getAvailable() / 1024.0 / 1024 / 1024;
}
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
df.setRoundingMode(RoundingMode.HALF_UP);
return Double.parseDouble(df.format(availablePhysicalMemorySize));
@ -123,7 +130,13 @@ public class OSUtils {
long now = System.currentTimeMillis();
if (now - prevTickTime > 950) {
// Enough time has elapsed.
cpuUsage = processor.getSystemCpuLoadBetweenTicks(prevTicks);
if (KubernetesUtils.isKubernetesMode()) {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
cpuUsage = operatingSystemMXBean.getSystemLoadAverage();
} else {
cpuUsage = processor.getSystemCpuLoadBetweenTicks(prevTicks);
}
prevTickTime = System.currentTimeMillis();
prevTicks = processor.getSystemCpuLoadTicks();
}

Loading…
Cancel
Save