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

jfrazee pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new fd50d6f  NIFI-3383 Check if usage operations are supported in 
MonitorMemory
fd50d6f is described below

commit fd50d6f0429ebbd676da745ba2bd7def81ad66e6
Author: Matthew Burgess <[email protected]>
AuthorDate: Fri Jan 29 18:38:53 2021 -0500

    NIFI-3383 Check if usage operations are supported in MonitorMemory
    
    This closes #4790
    
    Signed-off-by: Joey Frazee <[email protected]>
---
 .../java/org/apache/nifi/controller/MonitorMemory.java   | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-reporting-tasks/src/main/java/org/apache/nifi/controller/MonitorMemory.java
 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-reporting-tasks/src/main/java/org/apache/nifi/controller/MonitorMemory.java
index 7f6028d..a723a88 100644
--- 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-reporting-tasks/src/main/java/org/apache/nifi/controller/MonitorMemory.java
+++ 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-reporting-tasks/src/main/java/org/apache/nifi/controller/MonitorMemory.java
@@ -19,6 +19,7 @@ package org.apache.nifi.controller;
 import org.apache.nifi.annotation.documentation.CapabilityDescription;
 import org.apache.nifi.annotation.documentation.Tags;
 import org.apache.nifi.annotation.lifecycle.OnScheduled;
+import org.apache.nifi.annotation.lifecycle.OnStopped;
 import org.apache.nifi.components.AllowableValue;
 import org.apache.nifi.components.PropertyDescriptor;
 import org.apache.nifi.components.ValidationContext;
@@ -39,6 +40,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 /**
  * Reporting task used to monitor usage of memory after Garbage Collection has
@@ -91,7 +93,8 @@ public class MonitorMemory extends AbstractReportingTask {
     private static final AllowableValue[] memPoolAllowableValues;
 
     static {
-        List<MemoryPoolMXBean> memoryPoolBeans = 
ManagementFactory.getMemoryPoolMXBeans();
+        // Only allow memory pool beans that support usage thresholds, 
otherwise we wouldn't report anything anyway
+        List<MemoryPoolMXBean> memoryPoolBeans = 
ManagementFactory.getMemoryPoolMXBeans().stream().filter(MemoryPoolMXBean::isUsageThresholdSupported).collect(Collectors.toList());
         memPoolAllowableValues = new AllowableValue[memoryPoolBeans.size()];
         for (int i = 0; i < memPoolAllowableValues.length; i++) {
             memPoolAllowableValues[i] = new 
AllowableValue(memoryPoolBeans.get(i).getName());
@@ -175,7 +178,9 @@ public class MonitorMemory extends AbstractReportingTask {
                         final double pct = Double.parseDouble(percentage) / 
100D;
                         calculatedThreshold = (long) 
(monitoredBean.getUsage().getMax() * pct);
                     }
-                    monitoredBean.setUsageThreshold(calculatedThreshold);
+                    if (monitoredBean.isUsageThresholdSupported()) {
+                        monitoredBean.setUsageThreshold(calculatedThreshold);
+                    }
                 }
             }
         }
@@ -200,7 +205,7 @@ public class MonitorMemory extends AbstractReportingTask {
         }
 
         final double percentageUsed = (double) usage.getUsed() / (double) 
usage.getMax() * 100D;
-        if (bean.isUsageThresholdExceeded()) {
+        if (bean.isUsageThresholdSupported() && 
bean.isUsageThresholdExceeded()) {
             if (System.currentTimeMillis() < reportingIntervalMillis + 
lastReportTime && lastReportTime > 0L) {
                 return;
             }
@@ -223,6 +228,11 @@ public class MonitorMemory extends AbstractReportingTask {
         }
     }
 
+    @OnStopped
+    public void onStopped() {
+        monitoredBean = null;
+    }
+
     private static class ThresholdValidator implements Validator {
 
         @Override

Reply via email to