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

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new 7a09233  FELIX-6400 : Reduce resource consumption during component 
checks
7a09233 is described below

commit 7a092334693fb8d10bfc9f66aab142d61694d2f6
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Sat Aug 14 16:12:55 2021 +0200

    FELIX-6400 : Reduce resource consumption during component checks
---
 healthcheck/generalchecks/pom.xml                  |   2 +-
 .../felix/hc/generalchecks/DsComponentsCheck.java  | 113 +++++++++++----------
 .../felix/hc/generalchecks/ServicesCheck.java      |  32 ++++--
 .../generalchecks/scrutil/DsRootCauseAdapter.java  |   9 +-
 .../generalchecks/scrutil/DsRootCauseAnalyzer.java |  11 +-
 .../felix/systemready/impl/ComponentsCheck.java    |   4 +-
 6 files changed, 100 insertions(+), 71 deletions(-)

diff --git a/healthcheck/generalchecks/pom.xml 
b/healthcheck/generalchecks/pom.xml
index d5c6c62..bf7864d 100644
--- a/healthcheck/generalchecks/pom.xml
+++ b/healthcheck/generalchecks/pom.xml
@@ -158,7 +158,7 @@
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.rootcause</artifactId>
-            <version>0.1.0</version>
+            <version>0.1.1-SNAPSHOT</version>
         </dependency>
 
         <dependency>
diff --git 
a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/DsComponentsCheck.java
 
b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/DsComponentsCheck.java
index 69b2999..34df30e 100644
--- 
a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/DsComponentsCheck.java
+++ 
b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/DsComponentsCheck.java
@@ -51,11 +51,13 @@ import org.slf4j.LoggerFactory;
 @Designate(ocd = DsComponentsCheck.Config.class, factory = true)
 public class DsComponentsCheck implements HealthCheck {
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(DsComponentsCheck.class);
+    private final Logger LOG = 
LoggerFactory.getLogger(DsComponentsCheck.class);
 
     public static final String HC_NAME = "DS Components Ready Check";
     public static final String HC_DEFAULT_TAG = "systemalive";
 
+    private static final Result INVALID = new Result(Result.Status.CRITICAL, 
"invalid");
+
     @ObjectClassDefinition(name = "Health Check: "
             + HC_NAME, description = "System ready check that checks a list of 
DS components and provides root cause analysis in case of errors")
     public @interface Config {
@@ -87,8 +89,6 @@ public class DsComponentsCheck implements HealthCheck {
 
     private final AtomicReference<Result> cache = new AtomicReference<>();
 
-    private static final Result INVALID = new Result(Result.Status.CRITICAL, 
"invalid");
-
     @Activate
     public void activate(final BundleContext ctx, final Config config) throws 
InterruptedException {
         componentsList = Arrays.asList(config.components_list());
@@ -108,69 +108,78 @@ public class DsComponentsCheck implements HealthCheck {
             } else if ( result == null ) {
                 FormattingResultLog log = new FormattingResultLog();
 
-                Collection<ComponentDescriptionDTO> componentDescriptionDTOs = 
scr.getComponentDescriptionDTOs();
-                List<ComponentDescriptionDTO> watchedComps = new 
LinkedList<ComponentDescriptionDTO>();
-                List<String> missingComponents = new 
LinkedList<String>(componentsList);
-                for (ComponentDescriptionDTO desc : componentDescriptionDTOs) {
-                    if (componentsList.contains(desc.name)) {
-                        watchedComps.add(desc);
-                        missingComponents.remove(desc.name);
-                    }
+                Collection<ComponentDescriptionDTO> componentDescriptionDTOs = 
null;
+                try {
+                    componentDescriptionDTOs = 
scr.getComponentDescriptionDTOs();
+                } catch ( final Throwable e) {
+                    log.temporarilyUnavailable("Exception while getting ds 
component dtos {}", e.getMessage(), e);
                 }
-                for (String missingComp : missingComponents) {
-                    log.info("No component with name {} is registered in SCR 
runtime", missingComp);
-                }
-
-                int countEnabled = 0;
-                int countDisabled = 0;
-                for (ComponentDescriptionDTO dsComp : watchedComps) {
-
-                    boolean isActive;
-
-                    boolean componentEnabled = scr.isComponentEnabled(dsComp);
-                    if (componentEnabled) {
+                if ( componentDescriptionDTOs != null ) {
+                    final List<ComponentDescriptionDTO> watchedComps = new 
LinkedList<ComponentDescriptionDTO>();
+                    final List<String> missingComponents = new 
LinkedList<String>(componentsList);
+                    for (final ComponentDescriptionDTO desc : 
componentDescriptionDTOs) {
+                        if (componentsList.contains(desc.name)) {
+                            watchedComps.add(desc);
+                            missingComponents.remove(desc.name);
+                        }
+                    }
+                    for (final String missingComp : missingComponents) {
+                        log.info("No component with name {} is registered in 
SCR runtime", missingComp);
+                    }
 
-                        Collection<ComponentConfigurationDTO> 
componentConfigurationDTOs = scr.getComponentConfigurationDTOs(dsComp);
-                        List<String> idStateTuples = new ArrayList<>();
-                        boolean foundActiveOrSatisfiedConfig = false;
-                        for (ComponentConfigurationDTO configDto : 
componentConfigurationDTOs) {
-                            idStateTuples.add("id " + configDto.id + ":" + 
toStateString(configDto.state));
-                            if (configDto.state == 
ComponentConfigurationDTO.ACTIVE || configDto.state == 
ComponentConfigurationDTO.SATISFIED) {
-                                foundActiveOrSatisfiedConfig = true;
+                    int countEnabled = 0;
+                    int countDisabled = 0;
+                    for (final ComponentDescriptionDTO dsComp : watchedComps) {
+
+                        boolean isActive;
+
+                        boolean componentEnabled = 
scr.isComponentEnabled(dsComp);
+                        if (componentEnabled) {
+
+                            try {
+                                Collection<ComponentConfigurationDTO> 
componentConfigurationDTOs = scr.getComponentConfigurationDTOs(dsComp);
+                                List<String> idStateTuples = new ArrayList<>();
+                                boolean foundActiveOrSatisfiedConfig = false;
+                                for (ComponentConfigurationDTO configDto : 
componentConfigurationDTOs) {
+                                    idStateTuples.add("id " + configDto.id + 
":" + toStateString(configDto.state));
+                                    if (configDto.state == 
ComponentConfigurationDTO.ACTIVE || configDto.state == 
ComponentConfigurationDTO.SATISFIED) {
+                                        foundActiveOrSatisfiedConfig = true;
+                                    }
+                                }
+                                log.debug(dsComp.name + " (" + 
String.join(",", idStateTuples) + ")");
+
+                                if (componentConfigurationDTOs.isEmpty() || 
foundActiveOrSatisfiedConfig) {
+                                    countEnabled++;
+                                    isActive = true;
+                                } else {
+                                    countDisabled++;
+                                    isActive = false;
+                                }
+                            } catch ( final Throwable e) {
+                                log.temporarilyUnavailable("Exception while 
getting ds component dtos {}", e.getMessage(), e);
+                                isActive = true; // no info available, doesn't 
make sense to report as inactive
                             }
-                        }
-                        log.debug(dsComp.name + " (" + String.join(",", 
idStateTuples) + ")");
 
-                        if (componentConfigurationDTOs.isEmpty() || 
foundActiveOrSatisfiedConfig) {
-                            countEnabled++;
-                            isActive = true;
                         } else {
                             countDisabled++;
                             isActive = false;
                         }
 
-                    } else {
-                        countDisabled++;
-                        isActive = false;
+                        if (!isActive) {
+                            analyzer.logNotEnabledComponent(log, dsComp, 
componentDescriptionDTOs);
+                        }
                     }
 
-                    if (!isActive) {
-                        analyzer.logNotEnabledComponent(log, dsComp);
+                    if (!missingComponents.isEmpty()) {
+                        log.add(new Entry(statusForMissing, 
missingComponents.size() + " required components are missing in SCR runtime"));
                     }
+                    if (countDisabled > 0) {
+                        log.add(new Entry(statusForMissing, countDisabled + " 
required components are not active"));
+                    }
+                    log.info("{} required components are active", 
countEnabled);
                 }
-
-                if (!missingComponents.isEmpty()) {
-                    log.add(new Entry(statusForMissing, 
missingComponents.size() + " required components are missing in SCR runtime"));
-                }
-                if (countDisabled > 0) {
-                    log.add(new Entry(statusForMissing, countDisabled + " 
required components are not active"));
-                }
-                log.info("{} required components are active", countEnabled);
-
                 result = new Result(log);
-                if ( !this.cache.compareAndSet(null, result) ) {
-                    result = null;
-                }
+                this.cache.compareAndSet(null, result);
             }
         }
         return result;
diff --git 
a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/ServicesCheck.java
 
b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/ServicesCheck.java
index fefe8c7..efb8598 100644
--- 
a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/ServicesCheck.java
+++ 
b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/ServicesCheck.java
@@ -23,6 +23,7 @@ import static java.util.stream.Collectors.toMap;
 
 import java.io.Closeable;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -44,6 +45,7 @@ import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.annotations.ReferencePolicyOption;
 import org.osgi.service.component.runtime.ServiceComponentRuntime;
+import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO;
 import org.osgi.service.metatype.annotations.AttributeDefinition;
 import org.osgi.service.metatype.annotations.Designate;
 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@@ -113,10 +115,10 @@ public class ServicesCheck implements HealthCheck {
         FormattingResultLog log = new FormattingResultLog();
         List<String> missingServiceNames = getMissingServiceNames(log);
 
-
+        final Collection<ComponentDescriptionDTO> dtos = 
getDTOs(missingServiceNames, log);
         for (String missingServiceName : missingServiceNames) {
-            if (!missingServiceName.startsWith("(")) {
-                analyzer.logMissingService(log, missingServiceName);
+            if (!missingServiceName.startsWith("(") && dtos != null) {
+                analyzer.logMissingService(log, missingServiceName, dtos);
             } else {
                 log.info("Service '{}' is missing", missingServiceName);
             }
@@ -127,10 +129,28 @@ public class ServicesCheck implements HealthCheck {
         } else {
             log.add(new Entry(statusForMissing, "Not all required services are 
available ("+missingServiceNames.size()+" are missing)"));
         }
-        
+
         return new Result(log);
     }
 
+    private Collection<ComponentDescriptionDTO> getDTOs(List<String> 
missingServiceNames, FormattingResultLog log) {
+        boolean needsDTOs = false;
+        for (String missingServiceName : missingServiceNames) {
+            if (!missingServiceName.startsWith("(")) {
+                needsDTOs = true;
+                break;
+            }
+        }
+        if ( needsDTOs ) {
+            try {
+                return scr.getComponentDescriptionDTOs();
+            } catch ( final Throwable e) {
+                log.temporarilyUnavailable("Exception while getting ds 
component dtos {}", e.getMessage(), e);
+            }
+        }
+        return null;
+    }
+
     private List<String> getMissingServiceNames(FormattingResultLog log) {
         List<String> missingServicesNames = new LinkedList<>();
 
@@ -162,11 +182,11 @@ public class ServicesCheck implements HealthCheck {
         public boolean present() {
             return getTrackingCount() > 0;
         }
-        
+
         public int getTrackingCount() {
             return this.stracker.getTrackingCount();
         }
-        
+
         @Override
         public void close() {
             stracker.close();
diff --git 
a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/scrutil/DsRootCauseAdapter.java
 
b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/scrutil/DsRootCauseAdapter.java
index c81d373..184a2ac 100644
--- 
a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/scrutil/DsRootCauseAdapter.java
+++ 
b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/scrutil/DsRootCauseAdapter.java
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.hc.generalchecks.scrutil;
 
+import java.util.Collection;
 import java.util.Optional;
 import java.util.function.Consumer;
 
@@ -39,8 +40,8 @@ public class DsRootCauseAdapter {
         this.analyzer = new DSRootCause(scr);
     }
 
-    public void logMissingService(FormattingResultLog log, String 
missingServiceName) {
-        Optional<DSComp> rootCauseOptional = 
analyzer.getRootCause(missingServiceName);
+    public void logMissingService(FormattingResultLog log, String 
missingServiceName, Collection<ComponentDescriptionDTO> 
componentDescriptionDTOs) {
+        Optional<DSComp> rootCauseOptional = 
analyzer.getRootCause(missingServiceName, componentDescriptionDTOs);
         if (rootCauseOptional.isPresent()) {
             logRootCause(log, rootCauseOptional.get());
         } else {
@@ -48,8 +49,8 @@ public class DsRootCauseAdapter {
         }
     }
 
-    public void logNotEnabledComponent(FormattingResultLog log, 
ComponentDescriptionDTO desc) {
-        DSComp component = analyzer.getRootCause(desc);
+    public void logNotEnabledComponent(FormattingResultLog log, 
ComponentDescriptionDTO desc, Collection<ComponentDescriptionDTO> 
componentDescriptionDTOs) {
+        DSComp component = analyzer.getRootCause(desc, 
componentDescriptionDTOs);
         logRootCause(log, component);
     }
 
diff --git 
a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/scrutil/DsRootCauseAnalyzer.java
 
b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/scrutil/DsRootCauseAnalyzer.java
index 51a2839..5de0929 100644
--- 
a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/scrutil/DsRootCauseAnalyzer.java
+++ 
b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/scrutil/DsRootCauseAnalyzer.java
@@ -18,8 +18,9 @@
  */
 package org.apache.felix.hc.generalchecks.scrutil;
 
+import java.util.Collection;
+
 import org.apache.felix.hc.api.FormattingResultLog;
-import org.apache.felix.hc.api.Result.Status;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
@@ -53,17 +54,17 @@ public class DsRootCauseAnalyzer {
         }
     }
 
-    public void logMissingService(FormattingResultLog log, String 
missingServiceName) {
+    public void logMissingService(FormattingResultLog log, String 
missingServiceName, Collection<ComponentDescriptionDTO> 
componentDescriptionDTOs) {
         if (dsRootCauseAdapter != null) {
-            dsRootCauseAdapter.logMissingService(log, missingServiceName);
+            dsRootCauseAdapter.logMissingService(log, missingServiceName, 
componentDescriptionDTOs);
         } else {
             log.info("Service '{}' is missing", missingServiceName);
         }
     }
 
-    public void logNotEnabledComponent(FormattingResultLog log, 
ComponentDescriptionDTO desc) {
+    public void logNotEnabledComponent(FormattingResultLog log, 
ComponentDescriptionDTO desc, Collection<ComponentDescriptionDTO> 
componentDescriptionDTOs) {
         if (dsRootCauseAdapter != null) {
-            dsRootCauseAdapter.logNotEnabledComponent(log, desc);
+            dsRootCauseAdapter.logNotEnabledComponent(log, desc, 
componentDescriptionDTOs);
         } else {
             log.info("Component '{}' is missing", desc.name);
         }
diff --git 
a/systemready/src/main/java/org/apache/felix/systemready/impl/ComponentsCheck.java
 
b/systemready/src/main/java/org/apache/felix/systemready/impl/ComponentsCheck.java
index 3ce23fb..eab264f 100644
--- 
a/systemready/src/main/java/org/apache/felix/systemready/impl/ComponentsCheck.java
+++ 
b/systemready/src/main/java/org/apache/felix/systemready/impl/ComponentsCheck.java
@@ -140,9 +140,7 @@ public class ComponentsCheck implements SystemReadyCheck {
                         result = new CheckStatus(getName(), type, 
CheckStatus.State.RED, "Exception while checking ds component dtos : " + 
e.getMessage());
                     }
                 }
-                if ( !this.cache.compareAndSet(null, result) ) {
-                    result = null;
-                }
+                this.cache.compareAndSet(null, result);
             }
         }
         return result;

Reply via email to