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

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


The following commit(s) were added to refs/heads/main by this push:
     new 0992ee6  CAMEL-17090: Release view only on shutdown or after no routes 
left in policy (#6290)
0992ee6 is described below

commit 0992ee6bc932391719a40982fa8c06155ebdbe7e
Author: iliya-gr <[email protected]>
AuthorDate: Sun Oct 17 10:10:30 2021 +0300

    CAMEL-17090: Release view only on shutdown or after no routes left in 
policy (#6290)
    
    * CAMEL-17090: Release view only on shutdown and after no routes left in 
policy
    
    * CAMEL-17090: Fix formatting
---
 .../camel/impl/cluster/ClusteredRoutePolicy.java   | 89 ++++++++++-----------
 ...a => ClusteredRoutePolicyLeaderChangeTest.java} | 91 +---------------------
 .../camel/cluster/ClusteredRoutePolicyTest.java    |  7 +-
 3 files changed, 53 insertions(+), 134 deletions(-)

diff --git 
a/core/camel-cluster/src/main/java/org/apache/camel/impl/cluster/ClusteredRoutePolicy.java
 
b/core/camel-cluster/src/main/java/org/apache/camel/impl/cluster/ClusteredRoutePolicy.java
index bcbcf05..349c2ef 100644
--- 
a/core/camel-cluster/src/main/java/org/apache/camel/impl/cluster/ClusteredRoutePolicy.java
+++ 
b/core/camel-cluster/src/main/java/org/apache/camel/impl/cluster/ClusteredRoutePolicy.java
@@ -65,7 +65,6 @@ public final class ClusteredRoutePolicy extends 
RoutePolicySupport implements Ca
     private final CamelClusterService.Selector clusterServiceSelector;
     private CamelClusterService clusterService;
     private CamelClusterView clusterView;
-    private volatile boolean clusterViewAddListenerDone;
     private volatile boolean startManagedRoutesEarly;
 
     private Duration initialDelay;
@@ -97,31 +96,10 @@ public final class ClusteredRoutePolicy extends 
RoutePolicySupport implements Ca
             throw new RuntimeException(e);
         }
 
-        // Cleanup the policy when all the routes it manages have been shut 
down
+        // Cleanup the policy when all the routes it manages have been removed
         // so a single policy instance can be shared among routes.
-        this.refCount = ReferenceCount.onRelease(() -> {
-            if (camelContext != null) {
-                
camelContext.getManagementStrategy().removeEventNotifier(listener);
-                if (executorService != null) {
-                    
camelContext.getExecutorServiceManager().shutdownNow(executorService);
-                }
-            }
-
-            try {
-                // Remove event listener
-                if (clusterView != null) {
-                    clusterView.removeEventListener(leadershipEventListener);
-
-                    // If all the routes have been shut down then the view and 
its
-                    // resources can eventually be released.
-                    clusterView.getClusterService().releaseView(clusterView);
-                }
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            } finally {
-                setLeader(false);
-            }
-        });
+        // Acquire cluster view once a route is added to the policy
+        this.refCount = ReferenceCount.on(this::retainClusterView, 
this::releaseClusterView);
     }
 
     @Override
@@ -178,6 +156,7 @@ public final class ClusteredRoutePolicy extends 
RoutePolicySupport implements Ca
     public void onInit(Route route) {
         super.onInit(route);
 
+        // Increase number of managed routes by this policy, acquire policy 
view on first run
         this.refCount.retain();
 
         if (route.isAutoStartup()) {
@@ -211,28 +190,58 @@ public final class ClusteredRoutePolicy extends 
RoutePolicySupport implements Ca
     }
 
     @Override
-    public void doStart() throws Exception {
-        this.clusterView = clusterService.getView(namespace);
-        if (!clusterViewAddListenerDone) {
-            clusterView.addEventListener(leadershipEventListener);
-            clusterViewAddListenerDone = true;
-        }
-    }
-
-    @Override
     public void onRemove(Route route) {
+        // Decrease number of managed routes, release view once there are no 
route left
+        refCount.release();
         autoStartupRoutes.remove(route);
     }
 
     @Override
-    public void onStop(Route route) {
-        this.refCount.release();
+    protected void doShutdown() throws Exception {
+        releaseClusterView();
+        removeCamelEventListeners();
     }
 
     // ****************************************************
     // Management
     // ****************************************************
 
+    private void removeCamelEventListeners() {
+        if (camelContext != null) {
+            camelContext.getManagementStrategy().removeEventNotifier(listener);
+            if (executorService != null) {
+                
camelContext.getExecutorServiceManager().shutdownNow(executorService);
+            }
+        }
+    }
+
+    private synchronized void retainClusterView() {
+        try {
+            clusterView = clusterService.getView(namespace);
+            clusterView.addEventListener(leadershipEventListener);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private synchronized void releaseClusterView() {
+        try {
+            // Remove event listener
+            if (clusterView != null) {
+                clusterView.removeEventListener(leadershipEventListener);
+
+                // If all the routes have been removed then the view and its
+                // resources can eventually be released.
+                clusterView.getClusterService().releaseView(clusterView);
+                clusterView = null;
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            setLeader(false);
+        }
+    }
+
     @ManagedAttribute(description = "Is this route the master or a slave")
     public boolean isLeader() {
         return leader.get();
@@ -332,14 +341,6 @@ public final class ClusteredRoutePolicy extends 
RoutePolicySupport implements Ca
                     
startedRoutes.stream().map(Route::getId).collect(Collectors.joining(",")));
         }
 
-        if (clusterView != null && !clusterViewAddListenerDone) {
-            clusterView.addEventListener(leadershipEventListener);
-            clusterViewAddListenerDone = true;
-        } else {
-            // cluster view is not initialized yet, so lets add its listener 
in doStart
-            clusterViewAddListenerDone = false;
-        }
-
         if (startManagedRoutesEarly) {
             LOG.debug(
                     "CamelContext is now fully started, can now start managed 
routes eager as we were appointed leader during early startup");
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyLeaderChangeTest.java
similarity index 57%
copy from 
core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyTest.java
copy to 
core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyLeaderChangeTest.java
index 97c92d8..1111e85 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyLeaderChangeTest.java
@@ -24,16 +24,14 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.impl.cluster.ClusteredRoutePolicy;
 import org.apache.camel.support.cluster.AbstractCamelClusterService;
 import org.apache.camel.support.cluster.AbstractCamelClusterView;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.*;
 
-public class ClusteredRoutePolicyTest extends ContextTestSupport {
+public class ClusteredRoutePolicyLeaderChangeTest extends ContextTestSupport {
 
     private ClusteredRoutePolicy policy;
     private TestClusterService cs;
@@ -51,93 +49,14 @@ public class ClusteredRoutePolicyTest extends 
ContextTestSupport {
     }
 
     @Test
-    public void testClusteredRoutePolicy() throws Exception {
-        // route is stopped as we are not leader yet
-        assertEquals(ServiceStatus.Stopped, 
context.getRouteController().getRouteStatus("foo"));
-
-        MockEndpoint mock = getMockEndpoint("mock:foo");
-        mock.expectedBodiesReceived("Hello Foo");
-
+    public void testClusteredRoutePolicyOnLeadershipLost() throws Exception {
         cs.getView().setLeader(true);
 
-        template.sendBody("seda:foo", "Hello Foo");
-
-        assertMockEndpointsSatisfied();
-
         assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("foo"));
-    }
-
-    @Test
-    public void testClusteredRoutePolicyStopAllRoutes() throws Exception {
-        cs.getView().setLeader(true);
-
-        context.getRouteController().stopRoute("foo");
-        context.getRouteController().stopRoute("baz");
-
-        assertFalse(cs.getView().isRunning());
-    }
-
-    @Test
-    public void testClusteredRoutePolicyDontStartAutoStartFalseRoutes() throws 
Exception {
-        cs.getView().setLeader(true);
 
-        assertEquals(ServiceStatus.Stopped, 
context.getRouteController().getRouteStatus("baz"));
-    }
+        cs.getView().setLeader(false);
 
-    @Test
-    public void testClusteredRoutePolicyAddRoute() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("seda:bar").routeId("bar").routePolicy(policy)
-                        .to("mock:bar");
-            }
-        });
-
-        // route is stopped as we are not leader yet
         assertEquals(ServiceStatus.Stopped, 
context.getRouteController().getRouteStatus("foo"));
-        assertEquals(ServiceStatus.Stopped, 
context.getRouteController().getRouteStatus("bar"));
-
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello Foo");
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello Bar");
-
-        cs.getView().setLeader(true);
-
-        template.sendBody("seda:foo", "Hello Foo");
-        template.sendBody("seda:bar", "Hello Bar");
-
-        assertMockEndpointsSatisfied();
-
-        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("foo"));
-        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("bar"));
-    }
-
-    @Test
-    public void testClusteredRoutePolicyAddRouteAlreadyLeader() throws 
Exception {
-        cs.getView().setLeader(true);
-
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("seda:bar").routeId("bar").routePolicy(policy)
-                        .to("mock:bar");
-            }
-        });
-
-        // route is started as we are leader
-        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("foo"));
-        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("bar"));
-
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello Foo");
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello Bar");
-
-        template.sendBody("seda:foo", "Hello Foo");
-        template.sendBody("seda:bar", "Hello Bar");
-
-        assertMockEndpointsSatisfied();
-
-        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("foo"));
-        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("bar"));
     }
 
     @Override
@@ -147,8 +66,6 @@ public class ClusteredRoutePolicyTest extends 
ContextTestSupport {
             public void configure() throws Exception {
                 from("seda:foo").routeId("foo").routePolicy(policy)
                         .to("mock:foo");
-                
from("seda:baz").autoStartup(false).routeId("baz").routePolicy(policy)
-                        .to("mock:baz");
             }
         };
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyTest.java
index 97c92d8..c2cfb81 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/cluster/ClusteredRoutePolicyTest.java
@@ -30,8 +30,7 @@ import 
org.apache.camel.support.cluster.AbstractCamelClusterService;
 import org.apache.camel.support.cluster.AbstractCamelClusterView;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.*;
 
 public class ClusteredRoutePolicyTest extends ContextTestSupport {
 
@@ -68,11 +67,13 @@ public class ClusteredRoutePolicyTest extends 
ContextTestSupport {
     }
 
     @Test
-    public void testClusteredRoutePolicyStopAllRoutes() throws Exception {
+    public void testClusteredRoutePolicyRemoveAllRoutes() throws Exception {
         cs.getView().setLeader(true);
 
         context.getRouteController().stopRoute("foo");
         context.getRouteController().stopRoute("baz");
+        context.removeRoute("foo");
+        context.removeRoute("baz");
 
         assertFalse(cs.getView().isRunning());
     }

Reply via email to