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 dcc53e175939 CAMEL-27094: camel-core - Gather the endpoints in use 
once in removeAllRoutes() (#30276)
dcc53e175939 is described below

commit dcc53e175939f696056b8976b2cf48e8c6430174
Author: Ayush Kulshreshtha <[email protected]>
AuthorDate: Tue Nov 17 20:38:57 2026 +0530

    CAMEL-27094: camel-core - Gather the endpoints in use once in 
removeAllRoutes() (#30276)
    
    removeAllRoutes() removed each route with removeRoute(), and each
    removal of a stopped route gathered the endpoints of all remaining
    routes and counted, for each endpoint of the removed route, how many
    route endpoint sets contain it. Removing n routes was therefore O(n^2).
    
    Gather the endpoints in use once in removeAllRoutes(), as the endpoints
    of each route and a count of the routes using each endpoint, and let
    removeRoute() use and update them, so that a removal only looks at the
    endpoints of the removed route. removeAllRoutes() still calls the
    overridable removeRoute() for each route without holding the lock
    across the loop. Starting a route while it runs discards the gathered
    endpoints, so removeRoute() gathers them again and keeps the endpoints
    of the new route.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../camel/impl/engine/AbstractCamelContext.java    |  99 ++++++++++-----
 .../impl/RemoveAllRoutesGatherEndpointsTest.java   | 141 +++++++++++++++++++++
 2 files changed, 211 insertions(+), 29 deletions(-)

diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index bc45c3fcfd7c..4df82abb5d11 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -255,6 +255,8 @@ public abstract class AbstractCamelContext extends 
BaseService
     private final ContextValue<Boolean> isStartingRoutes = 
ContextValue.newInstance("isStartingRoutes");
     private final ContextValue<Boolean> isLockModel = 
ContextValue.newInstance("isLockModel");
     private final Map<String, RouteService> routeServices = new 
LinkedHashMap<>();
+    // the endpoints in use by the routes while removeAllRoutes() removes 
them, guarded by lock
+    private EndpointsInUse endpointsInUseOfRemovedRoutes;
     private final Map<String, RouteService> suspendedRouteServices = new 
LinkedHashMap<>();
     private final InternalRouteStartupManager internalRouteStartupManager = 
new InternalRouteStartupManager();
     private final List<RouteStartupOrder> routeStartupOrder = new 
ArrayList<>();
@@ -1421,9 +1423,25 @@ public abstract class AbstractCamelContext extends 
BaseService
         // as this is used by route-reload functionality, so lets be brief
         logRouteStopSummary(LoggingLevel.DEBUG);
 
-        // remove all routes
-        for (Route route : getRoutes()) {
-            removeRoute(route.getRouteId(), LoggingLevel.DEBUG);
+        // remove all routes, gathering the endpoints in use once instead of 
for each removed route.
+        // The lock is not held across the loop, as removeRoute may be 
overridden to take other locks first
+        lock.lock();
+        try {
+            endpointsInUseOfRemovedRoutes = EndpointsInUse.of(routeServices);
+        } finally {
+            lock.unlock();
+        }
+        try {
+            for (Route route : getRoutes()) {
+                removeRoute(route.getRouteId(), LoggingLevel.DEBUG);
+            }
+        } finally {
+            lock.lock();
+            try {
+                endpointsInUseOfRemovedRoutes = null;
+            } finally {
+                lock.unlock();
+            }
         }
     }
 
@@ -1651,15 +1669,13 @@ public abstract class AbstractCamelContext extends 
BaseService
             RouteService routeService = routeServices.get(routeId);
             if (routeService != null) {
                 if (getRouteStatus(routeId).isStopped()) {
-                    // gather a map of all the endpoints in use by the routes, 
so we can
-                    // know if a given endpoint is in use by one or more 
routes, when we
-                    // remove the route. This walks every route, so it is only 
done when a
-                    // route is actually removed: adding a route with an 
explicit id calls
-                    // this method too, for an id that is usually not in use.
-                    Map<String, Set<Endpoint>> endpointsInUse = new 
HashMap<>();
-                    for (Map.Entry<String, RouteService> entry : 
routeServices.entrySet()) {
-                        endpointsInUse.put(entry.getKey(), 
entry.getValue().gatherEndpoints());
-                    }
+                    // the endpoints in use by the routes, so we can know if a 
given endpoint
+                    // is in use by one or more routes, when we remove the 
route. Gathering
+                    // them walks every route, so it is only done when a route 
is actually
+                    // removed: adding a route with an explicit id calls this 
method too, for
+                    // an id that is usually not in use. removeAllRoutes() 
gathers them once.
+                    EndpointsInUse endpointsInUse = 
endpointsInUseOfRemovedRoutes != null
+                            ? endpointsInUseOfRemovedRoutes : 
EndpointsInUse.of(routeServices);
                     try {
                         doRemove(routeId, loggingLevel, routeService, 
endpointsInUse);
                     } catch (Exception e) {
@@ -1679,7 +1695,7 @@ public abstract class AbstractCamelContext extends 
BaseService
     }
 
     private void doRemove(
-            String routeId, LoggingLevel loggingLevel, RouteService 
routeService, Map<String, Set<Endpoint>> endpointsInUse) {
+            String routeId, LoggingLevel loggingLevel, RouteService 
routeService, EndpointsInUse endpointsInUse) {
         routeService.setRemovingRoutes(true);
         shutdownRouteService(routeService, loggingLevel);
         routeServices.remove(routeId);
@@ -1696,22 +1712,7 @@ public abstract class AbstractCamelContext extends 
BaseService
         // from the route which we have removed, then remove all its
         // private endpoints
         // (eg the endpoints which are not in use by other routes)
-        Set<Endpoint> toRemove = new LinkedHashSet<>();
-        for (Endpoint endpoint : endpointsInUse.get(routeId)) {
-            // how many times is the endpoint in use
-            int count = 0;
-            for (Set<Endpoint> endpointSet : endpointsInUse.values()) {
-                if (endpointSet.contains(endpoint)) {
-                    count++;
-                }
-            }
-            // notice we will count ourselves so if there is only 1
-            // then its safe to remove
-            if (count <= 1) {
-                toRemove.add(endpoint);
-            }
-        }
-        for (Endpoint endpoint : toRemove) {
+        for (Endpoint endpoint : endpointsInUse.removeRoute(routeId)) {
             LOG.debug("Removing: {} which was only in use by route: {}", 
endpoint, routeId);
             removeEndpoint(endpoint);
         }
@@ -3815,6 +3816,8 @@ public abstract class AbstractCamelContext extends 
BaseService
                 } else {
                     // start the route service
                     routeServices.put(routeService.getId(), routeService);
+                    // the endpoints gathered by removeAllRoutes() do not 
include those of this route
+                    endpointsInUseOfRemovedRoutes = null;
                     // special situation if Camel is stopping and we do 
graceful shutdown, and process remainder
                     // inflight messages, and they trigger a dynamic endpoint 
(toD) that calls a kamelet, then
                     // we need to allow creating the kamelet route to be able 
to process the inflight message
@@ -4987,4 +4990,42 @@ public abstract class AbstractCamelContext extends 
BaseService
     private List<BootstrapCloseable> getBootstraps() {
         return camelContextExtension.getBootstraps();
     }
+
+    /**
+     * The endpoints in use by routes, and by how many of them, to find the 
endpoints that only a removed route uses.
+     */
+    private static final class EndpointsInUse {
+
+        private final Map<String, Set<Endpoint>> endpointsByRoute = new 
HashMap<>();
+        private final Map<Endpoint, Integer> routeCountByEndpoint = new 
HashMap<>();
+
+        static EndpointsInUse of(Map<String, RouteService> routeServices) {
+            EndpointsInUse answer = new EndpointsInUse();
+            for (Map.Entry<String, RouteService> entry : 
routeServices.entrySet()) {
+                Set<Endpoint> endpoints = entry.getValue().gatherEndpoints();
+                answer.endpointsByRoute.put(entry.getKey(), endpoints);
+                for (Endpoint endpoint : endpoints) {
+                    answer.routeCountByEndpoint.merge(endpoint, 1, 
Integer::sum);
+                }
+            }
+            return answer;
+        }
+
+        /**
+         * Removes the route and returns the endpoints that no other route 
uses.
+         */
+        Set<Endpoint> removeRoute(String routeId) {
+            Set<Endpoint> answer = new LinkedHashSet<>();
+            Set<Endpoint> endpoints = endpointsByRoute.remove(routeId);
+            if (endpoints != null) {
+                for (Endpoint endpoint : endpoints) {
+                    if (routeCountByEndpoint.merge(endpoint, -1, Integer::sum) 
<= 0) {
+                        routeCountByEndpoint.remove(endpoint);
+                        answer.add(endpoint);
+                    }
+                }
+            }
+            return answer;
+        }
+    }
 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/RemoveAllRoutesGatherEndpointsTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/RemoveAllRoutesGatherEndpointsTest.java
new file mode 100644
index 000000000000..737848198be5
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/RemoveAllRoutesGatherEndpointsTest.java
@@ -0,0 +1,141 @@
+/*
+ * 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 ASF 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.camel.impl;
+
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.EndpointAware;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Route;
+import org.apache.camel.Service;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.support.LifecycleStrategySupport;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * removeAllRoutes() gathers the endpoints in use by the routes once, instead 
of once for each removed route, and still
+ * keeps the endpoints that a route added in the meantime uses.
+ */
+public class RemoveAllRoutesGatherEndpointsTest extends ContextTestSupport {
+
+    private final CountingEndpointAware countingA = new 
CountingEndpointAware();
+    private final CountingEndpointAware countingB = new 
CountingEndpointAware();
+    private final CountingEndpointAware countingC = new 
CountingEndpointAware();
+
+    @Override
+    @BeforeEach
+    public void setUp() throws Exception {
+        super.setUp();
+        // ignore what starting the context did
+        countingA.calls.set(0);
+        countingB.calls.set(0);
+        countingC.calls.set(0);
+    }
+
+    @Test
+    public void testRemoveAllRoutesGathersTheEndpointsOfEachRouteOnce() throws 
Exception {
+        context.getRouteController().removeAllRoutes();
+
+        assertEquals(0, context.getRoutes().size());
+        assertEquals(1, countingA.calls.get(), "endpoints of route a 
gathered");
+        assertEquals(1, countingB.calls.get(), "endpoints of route b 
gathered");
+        assertEquals(1, countingC.calls.get(), "endpoints of route c 
gathered");
+    }
+
+    @Test
+    public void testRemoveAllRoutesRemovesSharedAndPrivateEndpoints() throws 
Exception {
+        context.getRouteController().removeAllRoutes();
+
+        assertNull(context.hasEndpoint("mock://shared"));
+        assertNull(context.hasEndpoint("mock://c"));
+        assertNull(context.hasEndpoint("direct://a"));
+    }
+
+    @Test
+    public void testRemoveAllRoutesKeepsTheEndpointsOfARouteAddedMeanwhile() 
throws Exception {
+        // while route a is removed, add a route that uses the endpoint shared 
by routes a and b
+        context.addLifecycleStrategy(new LifecycleStrategySupport() {
+            @Override
+            public void onRoutesRemove(Collection<Route> routes) {
+                if (routes.stream().anyMatch(route -> 
"a".equals(route.getRouteId()))) {
+                    try {
+                        context.addRoutes(new RouteBuilder() {
+                            @Override
+                            public void configure() {
+                                
from("direct:late").routeId("late").to("mock:shared");
+                            }
+                        });
+                    } catch (Exception e) {
+                        throw new IllegalStateException(e);
+                    }
+                }
+            }
+        });
+
+        context.getRouteController().removeAllRoutes();
+
+        assertNotNull(context.getRoute("late"));
+        assertNotNull(context.hasEndpoint("mock://shared"), "endpoint of the 
added route removed");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from("direct:a").routeId("a").process(countingA).to("mock:shared");
+                
from("direct:b").routeId("b").process(countingB).to("mock:shared");
+                from("direct:c").routeId("c").process(countingC).to("mock:c");
+            }
+        };
+    }
+
+    private static final class CountingEndpointAware implements Processor, 
EndpointAware, Service {
+
+        private final AtomicInteger calls = new AtomicInteger();
+
+        @Override
+        public Endpoint getEndpoint() {
+            calls.incrementAndGet();
+            return null;
+        }
+
+        @Override
+        public void process(Exchange exchange) {
+            // noop
+        }
+
+        @Override
+        public void start() {
+            // noop
+        }
+
+        @Override
+        public void stop() {
+            // noop
+        }
+    }
+}

Reply via email to