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

cschneider 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 8bb7b2f  FELIX-6465 fix components check, purge the cache in case of 
non-ok results
     new 9f8ddd0  Merge pull request #105 from 
joerghoh/bugfix/FELIX-6465-fix-dscomponentscheck
8bb7b2f is described below

commit 8bb7b2f6d8ab44634d082b28297f2a876087031a
Author: Joerg Hoh <[email protected]>
AuthorDate: Sat Oct 16 21:34:51 2021 +0200

    FELIX-6465 fix components check, purge the cache in case of non-ok results
---
 .../felix/hc/generalchecks/DsComponentsCheck.java  | 134 +++++++++++----------
 .../hc/generalchecks/DsComponentsCheckTest.java    |  51 ++++++++
 2 files changed, 122 insertions(+), 63 deletions(-)

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 098c612..89fb599 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
@@ -102,84 +102,91 @@ public class DsComponentsCheck implements HealthCheck {
     public Result execute() {
         Result result = this.cache.get();
         if ( result == null || this.refreshCache.compareAndSet(true, false) ) {
-            FormattingResultLog log = new FormattingResultLog();
-
-            Collection<ComponentDescriptionDTO> componentDescriptionDTOs = 
null;
-            try {
-                componentDescriptionDTOs = scr.getComponentDescriptionDTOs();
-            } catch ( final Throwable e) {
-                log.temporarilyUnavailable("Exception while getting ds 
component dtos {}", e.getMessage(), e);
+            result = executeInternal();
+            if ( result.isOk() ) {
+                this.cache.set(result);
+            } else {
+               this.cache.set(null);
             }
-            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);
+        }
+        return result;
+    }
+    
+    
+    protected Result executeInternal () {
+       FormattingResultLog log = new FormattingResultLog();
+
+        Collection<ComponentDescriptionDTO> componentDescriptionDTOs = null;
+        try {
+            componentDescriptionDTOs = scr.getComponentDescriptionDTOs();
+        } catch ( final Throwable e) {
+            log.temporarilyUnavailable("Exception while getting ds component 
dtos {}", e.getMessage(), e);
+        }
+        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);
+            }
 
-                int countEnabled = 0;
-                int countDisabled = 0;
-                for (final ComponentDescriptionDTO dsComp : watchedComps) {
+            int countEnabled = 0;
+            int countDisabled = 0;
+            for (final ComponentDescriptionDTO dsComp : watchedComps) {
 
-                    boolean isActive;
+                boolean isActive;
 
-                    boolean componentEnabled = scr.isComponentEnabled(dsComp);
-                    if (componentEnabled) {
+                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;
-                                }
+                    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
                         }
-
-                    } else {
-                        countDisabled++;
-                        isActive = false;
+                        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
                     }
 
-                    if (!isActive) {
-                        analyzer.logNotEnabledComponent(log, dsComp, 
componentDescriptionDTOs);
-                    }
+                } else {
+                    countDisabled++;
+                    isActive = false;
                 }
 
-                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"));
+                if (!isActive) {
+                    analyzer.logNotEnabledComponent(log, dsComp, 
componentDescriptionDTOs);
                 }
-                log.info("{} required components are active", countEnabled);
             }
-            result = new Result(log);
-            if ( result.isOk() ) {
-                this.cache.set(result);
+
+            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);
         }
-        return result;
+        return new Result(log);
     }
 
     static final String toStateString(int state) {
@@ -211,8 +218,9 @@ public class DsComponentsCheck implements HealthCheck {
         this.scr = null;
     }
 
-    private void updatedServiceComponentRuntime(final ServiceComponentRuntime 
c) {
+    protected void updatedServiceComponentRuntime(final 
ServiceComponentRuntime c) {
         // change in DS - mark cache
         this.refreshCache.compareAndSet(false, true);
+        LOG.debug("invalidate DsComponentsCheck cache");
     }
 }
diff --git 
a/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/DsComponentsCheckTest.java
 
b/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/DsComponentsCheckTest.java
new file mode 100644
index 0000000..9d75a8a
--- /dev/null
+++ 
b/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/DsComponentsCheckTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.felix.hc.generalchecks;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.felix.hc.api.Result;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.service.component.runtime.ServiceComponentRuntime;
+
+public class DsComponentsCheckTest {
+
+       @Test
+       public void testCaching() {
+               DsComponentsCheck check = Mockito.spy(new DsComponentsCheck());
+               
+               final Result resultOk = new Result (Result.Status.OK,"ok");
+               final Result resultTemporarilyUnavailable = new Result 
(Result.Status.TEMPORARILY_UNAVAILABLE,
+                               "temporarily unavailable");
+                               
+               Mockito.when(check.executeInternal())
+                       .thenReturn(resultOk)
+                       .thenReturn(resultTemporarilyUnavailable)
+                       .thenReturn(resultTemporarilyUnavailable)
+                       .thenReturn(resultOk);
+               
+               assertEquals(resultOk, check.execute());
+               check.updatedServiceComponentRuntime(null);
+               assertEquals(resultTemporarilyUnavailable, check.execute());
+               assertEquals(resultTemporarilyUnavailable, check.execute());
+               check.updatedServiceComponentRuntime(null);
+               assertEquals(resultOk, check.execute());        
+       }
+       
+}

Reply via email to