This is an automated email from the ASF dual-hosted git repository.

gabrywu pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new 0e7403e  [Feature-2927][common] Fix NEF when get info from OSUtils 
(#2927) (#2964)
0e7403e is described below

commit 0e7403e6a8ad75416937122740397eb66742bf0f
Author: Yichao Yang <[email protected]>
AuthorDate: Sun Jun 14 11:17:12 2020 +0800

    [Feature-2927][common] Fix NEF when get info from OSUtils (#2927) (#2964)
    
    * [Feature-2927][common] Fix NEF when get info from OSUtils (#2927)
    
    * Update OSUtils.java
    
    Change java doc
    
    Co-authored-by: gabry.wu <[email protected]>
---
 .../org/apache/dolphinscheduler/common/utils/OSUtils.java | 15 ++++++++++++---
 .../apache/dolphinscheduler/common/utils/OSUtilsTest.java |  8 ++++----
 2 files changed, 16 insertions(+), 7 deletions(-)

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 652b981..e3b2cc2 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
@@ -53,6 +53,12 @@ public class OSUtils {
   private static final SystemInfo SI = new SystemInfo();
   public static final String TWO_DECIMAL = "0.00";
 
+  /**
+   * return -1 when the function can not get hardware env info
+   * e.g {@link OSUtils#loadAverage()} {@link OSUtils#cpuUsage()}
+   */
+  public static final double NEGATIVE_ONE = -1;
+
   private static HardwareAbstractionLayer hal = SI.getHardware();
 
   private OSUtils() {}
@@ -118,9 +124,11 @@ public class OSUtils {
    */
   public static double loadAverage() {
     double loadAverage =  hal.getProcessor().getSystemLoadAverage();
+    if (Double.isNaN(loadAverage)) {
+      return NEGATIVE_ONE;
+    }
 
     DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
-
     df.setRoundingMode(RoundingMode.HALF_UP);
     return Double.parseDouble(df.format(loadAverage));
   }
@@ -133,10 +141,12 @@ public class OSUtils {
   public static double cpuUsage() {
     CentralProcessor processor = hal.getProcessor();
     double cpuUsage = processor.getSystemCpuLoad();
+    if (Double.isNaN(cpuUsage)) {
+      return NEGATIVE_ONE;
+    }
 
     DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
     df.setRoundingMode(RoundingMode.HALF_UP);
-
     return Double.parseDouble(df.format(cpuUsage));
   }
 
@@ -393,7 +403,6 @@ public class OSUtils {
     return null;
   }
 
-
   /**
    * whether is macOS
    * @return true if mac
diff --git 
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java
 
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java
index b955787..e1fa0c5 100644
--- 
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java
+++ 
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java
@@ -41,15 +41,15 @@ public class OSUtilsTest {
     public void testOSMetric(){
         if (!OSUtils.isWindows()) {
             double availablePhysicalMemorySize = 
OSUtils.availablePhysicalMemorySize();
-            Assert.assertTrue(availablePhysicalMemorySize > 0.0f);
+            Assert.assertTrue(availablePhysicalMemorySize >= 0.0d);
             double totalMemorySize = OSUtils.totalMemorySize();
-            Assert.assertTrue(totalMemorySize > 0.0f);
+            Assert.assertTrue(totalMemorySize >= 0.0d);
             double loadAverage = OSUtils.loadAverage();
             logger.info("loadAverage {}", loadAverage);
             double memoryUsage = OSUtils.memoryUsage();
-            Assert.assertTrue(memoryUsage > 0.0f);
+            Assert.assertTrue(memoryUsage >= 0.0d);
             double cpuUsage = OSUtils.cpuUsage();
-            Assert.assertTrue(cpuUsage > 0.0f);
+            Assert.assertTrue(cpuUsage >= 0.0d || cpuUsage == -1.0d);
         } else {
             // TODO window ut
         }

Reply via email to