You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/09/29 09:33:12 UTC

[camel] branch master updated: CAMEL-15585 - Camel Core health small refactor. (#4307)

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new e452a6a  CAMEL-15585 - Camel Core health small refactor. (#4307)
e452a6a is described below

commit e452a6acd7916fcf8c6234022f07b43f45621006
Author: Djordje Bajic <dj...@gmail.com>
AuthorDate: Tue Sep 29 11:32:55 2020 +0200

    CAMEL-15585 - Camel Core health small refactor. (#4307)
    
    * CAMEL-15585 - Camel Core health small refactor.
    
    * CAMEL-15585 - Move variable declaring on top of the method.
    
    * CAMEL-15585 - rever some changes. Use .orElse() instead .orElseGet() in stream.
    
    * CAMEL-15585 - revert ContextHealhCheck
---
 .../impl/health/DefaultHealthCheckRegistry.java    | 40 ++++++++++++----------
 .../impl/health/HealthCheckRegistryRepository.java | 14 +++-----
 .../apache/camel/impl/health/RouteHealthCheck.java |  3 +-
 .../health/DefaultHealthCheckRegistryTest.java     |  2 +-
 .../apache/camel/impl/health/HealthCheckTest.java  |  2 +-
 .../impl/health/ReadinessAndLivenessTest.java      |  4 +--
 6 files changed, 32 insertions(+), 33 deletions(-)

diff --git a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
index 0a86129..5e8740a 100644
--- a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
+++ b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java
@@ -166,10 +166,9 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
 
     @Override
     public boolean register(Object obj) {
-        boolean accept = obj instanceof HealthCheck || obj instanceof HealthCheckRepository;
-        if (!accept) {
-            throw new IllegalArgumentException();
-        }
+        boolean result;
+
+        checkIfAccepted(obj);
 
         if (obj instanceof HealthCheck) {
             HealthCheck healthCheck = (HealthCheck) obj;
@@ -177,7 +176,7 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
             if (getCheck(healthCheck.getId()).isPresent()) {
                 return false;
             }
-            boolean result = checks.add(healthCheck);
+            result = checks.add(healthCheck);
             if (result) {
                 if (obj instanceof CamelContextAware) {
                     ((CamelContextAware) obj).setCamelContext(camelContext);
@@ -185,14 +184,13 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
 
                 LOG.debug("HealthCheck with id {} successfully registered", healthCheck.getId());
             }
-            return result;
         } else {
             HealthCheckRepository repository = (HealthCheckRepository) obj;
             // do we have this already
             if (getRepository(repository.getId()).isPresent()) {
                 return false;
             }
-            boolean result = this.repositories.add(repository);
+            result = this.repositories.add(repository);
             if (result) {
                 if (repository instanceof CamelContextAware) {
                     ((CamelContextAware) repository).setCamelContext(camelContext);
@@ -200,32 +198,32 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
 
                 LOG.debug("HealthCheckRepository with id {} successfully registered", repository.getId());
             }
-            return result;
         }
+
+        return result;
     }
 
     @Override
     public boolean unregister(Object obj) {
-        boolean accept = obj instanceof HealthCheck || obj instanceof HealthCheckRepository;
-        if (!accept) {
-            throw new IllegalArgumentException();
-        }
+        boolean result;
+
+        checkIfAccepted(obj);
 
         if (obj instanceof HealthCheck) {
             HealthCheck healthCheck = (HealthCheck) obj;
-            boolean result = checks.remove(healthCheck);
+            result = checks.remove(healthCheck);
             if (result) {
                 LOG.debug("HealthCheck with id {} successfully un-registered", healthCheck.getId());
             }
-            return result;
         } else {
             HealthCheckRepository repository = (HealthCheckRepository) obj;
-            boolean result = this.repositories.remove(repository);
+            result = this.repositories.remove(repository);
             if (result) {
                 LOG.debug("HealthCheckRepository with id {} successfully un-registered", repository.getId());
             }
-            return result;
         }
+
+        return result;
     }
 
     // ************************************
@@ -249,8 +247,14 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
             return Stream.concat(
                     checks.stream(),
                     repositories.stream().flatMap(HealthCheckRepository::stream)).distinct();
-        } else {
-            return Stream.empty();
+        }
+        return Stream.empty();
+    }
+
+    private void checkIfAccepted(Object obj) {
+        boolean accept = obj instanceof HealthCheck || obj instanceof HealthCheckRepository;
+        if (!accept) {
+            throw new IllegalArgumentException();
         }
     }
 }
diff --git a/core/camel-health/src/main/java/org/apache/camel/impl/health/HealthCheckRegistryRepository.java b/core/camel-health/src/main/java/org/apache/camel/impl/health/HealthCheckRegistryRepository.java
index 2463f02..5becc66 100644
--- a/core/camel-health/src/main/java/org/apache/camel/impl/health/HealthCheckRegistryRepository.java
+++ b/core/camel-health/src/main/java/org/apache/camel/impl/health/HealthCheckRegistryRepository.java
@@ -109,14 +109,10 @@ public class HealthCheckRegistryRepository implements CamelContextAware, HealthC
     }
 
     private HealthCheckConfiguration matchConfiguration(String id) {
-        if (configurations != null) {
-            for (String key : configurations.keySet()) {
-                if (PatternHelper.matchPattern(id, key)) {
-                    return configurations.get(key);
-                }
-            }
-        }
-        return fallbackConfiguration;
-    }
 
+        return configurations.values().stream()
+                .filter(s -> PatternHelper.matchPattern(id, s.getParent()))
+                .findAny()
+                .orElse(fallbackConfiguration);
+    }
 }
diff --git a/core/camel-health/src/main/java/org/apache/camel/impl/health/RouteHealthCheck.java b/core/camel-health/src/main/java/org/apache/camel/impl/health/RouteHealthCheck.java
index 0362b05..44cde6b 100644
--- a/core/camel-health/src/main/java/org/apache/camel/impl/health/RouteHealthCheck.java
+++ b/core/camel-health/src/main/java/org/apache/camel/impl/health/RouteHealthCheck.java
@@ -69,10 +69,9 @@ public class RouteHealthCheck extends AbstractHealthCheck {
 
                     // the supervised route controller would store the last error if the route is regarded
                     // as unhealthy which we use to signal its down, otherwise we are in unknown state
+                    builder.unknown();
                     if (route.getLastError() != null && route.getLastError().isUnhealthy()) {
                         builder.down();
-                    } else {
-                        builder.unknown();
                     }
                 }
             }
diff --git a/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java b/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
index 8963c2d..11c805f 100644
--- a/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
+++ b/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
@@ -200,7 +200,7 @@ public class DefaultHealthCheckRegistryTest {
         }
     }
 
-    private class MyHealthCheck extends AbstractHealthCheck implements CamelContextAware {
+    private static class MyHealthCheck extends AbstractHealthCheck implements CamelContextAware {
 
         private CamelContext context;
 
diff --git a/core/camel-health/src/test/java/org/apache/camel/impl/health/HealthCheckTest.java b/core/camel-health/src/test/java/org/apache/camel/impl/health/HealthCheckTest.java
index 03c90e2..096325d 100644
--- a/core/camel-health/src/test/java/org/apache/camel/impl/health/HealthCheckTest.java
+++ b/core/camel-health/src/test/java/org/apache/camel/impl/health/HealthCheckTest.java
@@ -145,7 +145,7 @@ public class HealthCheckTest {
     //
     // ********************************
 
-    private class MyHealthCheck extends AbstractHealthCheck {
+    private static class MyHealthCheck extends AbstractHealthCheck {
         private HealthCheck.State state;
 
         MyHealthCheck() {
diff --git a/core/camel-health/src/test/java/org/apache/camel/impl/health/ReadinessAndLivenessTest.java b/core/camel-health/src/test/java/org/apache/camel/impl/health/ReadinessAndLivenessTest.java
index 88e5c6a..cade600 100644
--- a/core/camel-health/src/test/java/org/apache/camel/impl/health/ReadinessAndLivenessTest.java
+++ b/core/camel-health/src/test/java/org/apache/camel/impl/health/ReadinessAndLivenessTest.java
@@ -69,7 +69,7 @@ public class ReadinessAndLivenessTest {
         assertTrue(result.getCheck() instanceof MyLiveCheck);
     }
 
-    private class MyReadyCheck extends AbstractHealthCheck implements CamelContextAware {
+    private static class MyReadyCheck extends AbstractHealthCheck implements CamelContextAware {
 
         private CamelContext context;
 
@@ -98,7 +98,7 @@ public class ReadinessAndLivenessTest {
         }
     }
 
-    private class MyLiveCheck extends AbstractHealthCheck implements CamelContextAware {
+    private static class MyLiveCheck extends AbstractHealthCheck implements CamelContextAware {
 
         private CamelContext context;