You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by zh...@apache.org on 2023/10/11 08:06:36 UTC
[dolphinscheduler] 34/40: [Improvement][Common] Obtain resource information in the k8s environm… (#14968)
This is an automated email from the ASF dual-hosted git repository.
zhongjiajie pushed a commit to branch 3.2.0-release
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git
commit f2c68bc97a3e274a3419ef6b85c6583a486f5635
Author: winghv <wi...@gmail.com>
AuthorDate: Thu Sep 28 16:40:17 2023 +0800
[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: 旺阳 <qi...@cisco.com>
(cherry picked from commit c5bc535b85a94211b14863ca1a72dd7043973d48)
---
.../common/utils/KubernetesUtils.java | 6 ++++--
.../dolphinscheduler/common/utils/OSUtils.java | 23 +++++++++++++++++-----
2 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/KubernetesUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/KubernetesUtils.java
index cb5616181f..b0cf7ba0e3 100644
--- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/KubernetesUtils.java
+++ b/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;
}
}
diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java
index 358d77a5f1..a0feb502d8 100644
--- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java
+++ b/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();
}