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 a89ea2969013 CAMEL-27085: camel-core - the inflight count of a route 
no longer goes negative after it was removed with an exchange in flight and 
added again (#30309)
a89ea2969013 is described below

commit a89ea2969013248ca1781cd10d42c6d77046890e
Author: allthingssecurity <[email protected]>
AuthorDate: Wed Nov 18 15:04:22 2026 +0530

    CAMEL-27085: camel-core - the inflight count of a route no longer goes 
negative after it was removed with an exchange in flight and added again 
(#30309)
    
    DefaultInflightRepository keeps a counter per route id, which RouteService 
adds
    when the route is warmed up and removes when the route is removed, and the
    route advice increments and decrements the counter it finds for the route 
id.
    
    A route stopped with a timeout that ran out (a forced stop) while an 
exchange
    was still in flight, and then removed, lost its counter; a route added again
    with the same id (reload, dev mode, removeRoute and addRoutes) got a new 
one at
    0, and the end of the old exchange took it to -1. From then on the count of
    the route was one too low: its next graceful stop or reload did not wait for
    one exchange in flight, which then failed with a RouteStoppingException, and
    ThrottlingInflightRoutePolicy with the route scope let one more exchange in.
    
    removeRoute now keeps the counter of a route that still has exchanges in
    flight, marked as removed, until they are done; a route added again with the
    same id uses it, so those exchanges stay counted until they end and are not
    taken off the new route.
    
    The comment in addRoute now says that an exchange of the removed route that 
never ends keeps the count of the
    re-added route at 1 or more on purpose (later graceful stops wait for their 
timeout, route-scoped throttling and the
    JMX count include it), which is preferred over the -1 it gave before. 
removeRoute checks again after it marks the
    count, so the last exchange that was done between the check and the mark 
does not leave the entry in the map.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../impl/engine/DefaultInflightRepository.java     |  52 +++++++--
 ...ghtRepositoryRouteRemovedWhileInflightTest.java | 120 +++++++++++++++++++++
 2 files changed, 163 insertions(+), 9 deletions(-)

diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultInflightRepository.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultInflightRepository.java
index 3e40b7f910ed..7f94f40ab123 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultInflightRepository.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultInflightRepository.java
@@ -44,7 +44,7 @@ public class DefaultInflightRepository extends ServiceSupport 
implements Infligh
 
     private final LongAdder size = new LongAdder();
     private final ConcurrentMap<String, Exchange> inflight = new 
ConcurrentHashMap<>();
-    private final ConcurrentMap<String, LongAdder> routeCount = new 
ConcurrentHashMap<>();
+    private final ConcurrentMap<String, RouteCount> routeCount = new 
ConcurrentHashMap<>();
     private boolean inflightExchangeEnabled;
 
     @Override
@@ -67,17 +67,21 @@ public class DefaultInflightRepository extends 
ServiceSupport implements Infligh
 
     @Override
     public void add(Exchange exchange, String routeId) {
-        LongAdder existing = routeCount.get(routeId);
+        RouteCount existing = routeCount.get(routeId);
         if (existing != null) {
-            existing.increment();
+            existing.count.increment();
         }
     }
 
     @Override
     public void remove(Exchange exchange, String routeId) {
-        LongAdder existing = routeCount.get(routeId);
+        RouteCount existing = routeCount.get(routeId);
         if (existing != null) {
-            existing.decrement();
+            existing.count.decrement();
+            if (existing.removed) {
+                // the last exchange of a removed route is done (unless the 
route was added again meanwhile)
+                routeCount.computeIfPresent(routeId, (k, c) -> c.removed && 
c.count.sum() <= 0 ? null : c);
+            }
         }
     }
 
@@ -88,18 +92,41 @@ public class DefaultInflightRepository extends 
ServiceSupport implements Infligh
 
     @Override
     public void addRoute(String routeId) {
-        routeCount.putIfAbsent(routeId, new LongAdder());
+        routeCount.compute(routeId, (k, c) -> {
+            if (c == null) {
+                return new RouteCount();
+            }
+            // a route with the same id was removed while some of its 
exchanges were still in flight (a forced stop):
+            // they are counted until they are done, so that their decrement 
is not taken from the new route.
+            // This is on purpose: an exchange of the removed route that never 
ends (such as a blocking call without a
+            // timeout, often the reason for the forced stop) keeps 
size(routeId) of the new route at 1 or more, so a
+            // later graceful stop or reload of the route waits for its 
timeout, and route-scoped throttling and the
+            // JMX inflight count include it. That exchange is still running, 
so this errs on the side of waiting for
+            // it, where dropping its count would take the new route to -1 
when it is done
+            c.removed = false;
+            return c;
+        });
     }
 
     @Override
     public void removeRoute(String routeId) {
-        routeCount.remove(routeId);
+        // the count of a route with exchanges still in flight (a stop that 
timed out) is kept until they are done
+        routeCount.computeIfPresent(routeId, (k, c) -> {
+            if (c.count.sum() <= 0) {
+                return null;
+            }
+            c.removed = true;
+            return c;
+        });
+        // the last exchange may be done between the check above and the mark, 
and then not see the mark: check again,
+        // so that either this method or that exchange takes the entry out of 
the map
+        routeCount.computeIfPresent(routeId, (k, c) -> c.removed && 
c.count.sum() <= 0 ? null : c);
     }
 
     @Override
     public int size(String routeId) {
-        LongAdder existing = routeCount.get(routeId);
-        return existing != null ? existing.intValue() : 0;
+        RouteCount existing = routeCount.get(routeId);
+        return existing != null ? existing.count.intValue() : 0;
     }
 
     @Override
@@ -285,4 +312,11 @@ public class DefaultInflightRepository extends 
ServiceSupport implements Infligh
         }
     }
 
+    /**
+     * The number of inflight exchanges of a route, and whether the route was 
removed while some were still in flight.
+     */
+    private static final class RouteCount {
+        private final LongAdder count = new LongAdder();
+        private volatile boolean removed;
+    }
 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryRouteRemovedWhileInflightTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryRouteRemovedWhileInflightTest.java
new file mode 100644
index 000000000000..7392926db855
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryRouteRemovedWhileInflightTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.RouteStoppingException;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A route is stopped with a timeout that forces it down while an exchange is 
still in flight, and removed: the done
+ * exchange must not be taken off the inflight count of a route that is added 
again with the same id.
+ */
+public class InflightRepositoryRouteRemovedWhileInflightTest extends 
ContextTestSupport {
+
+    private final CountDownLatch entered = new CountDownLatch(1);
+    private final CountDownLatch gate = new CountDownLatch(1);
+
+    @Test
+    public void testRouteAddedAgainWithTheSameId() throws Exception {
+        Future<Object> reply = forceRemoveWithExchangeInFlight();
+
+        context.addRoutes(slowRoute());
+
+        gate.countDown();
+        assertOldExchangeFailed(reply);
+        assertEquals(0, context.getInflightRepository().size("slow"),
+                "the exchange of the removed route must not be taken off the 
count of the new route");
+
+        // the count works as before for the exchanges of the new route
+        template.sendBody("direct:slow", "B");
+        assertEquals(0, context.getInflightRepository().size("slow"));
+        assertEquals(0, context.getInflightRepository().size());
+    }
+
+    @Test
+    public void testExchangeOfRemovedRouteCountedUntilDone() throws Exception {
+        Future<Object> reply = forceRemoveWithExchangeInFlight();
+
+        context.addRoutes(slowRoute());
+        // it still runs (and a graceful stop of the new route waits for it)
+        assertEquals(1, context.getInflightRepository().size("slow"),
+                "the exchange of the removed route is still in flight");
+
+        gate.countDown();
+        assertOldExchangeFailed(reply);
+        assertEquals(0, context.getInflightRepository().size("slow"));
+    }
+
+    @Test
+    public void testRouteNotAddedAgain() throws Exception {
+        Future<Object> reply = forceRemoveWithExchangeInFlight();
+
+        gate.countDown();
+        assertOldExchangeFailed(reply);
+        assertEquals(0, context.getInflightRepository().size("slow"));
+        assertEquals(0, context.getInflightRepository().size());
+    }
+
+    private Future<Object> forceRemoveWithExchangeInFlight() throws Exception {
+        Future<Object> reply = template.asyncRequestBody("direct:slow", "A");
+        assertTrue(entered.await(10, TimeUnit.SECONDS));
+        assertEquals(1, context.getInflightRepository().size("slow"));
+
+        // the graceful stop times out and forces the route down, with the 
exchange still in flight
+        context.getRouteController().stopRoute("slow", 100, 
TimeUnit.MILLISECONDS);
+        assertTrue(context.removeRoute("slow"));
+        return reply;
+    }
+
+    private static void assertOldExchangeFailed(Future<Object> reply) {
+        // its route was stopped while it was in flight
+        ExecutionException e = assertThrows(ExecutionException.class, () -> 
reply.get(10, TimeUnit.SECONDS));
+        assertInstanceOf(RouteStoppingException.class, 
e.getCause().getCause());
+    }
+
+    private RouteBuilder slowRoute() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:slow").routeId("slow")
+                        .process(e -> {
+                            if ("A".equals(e.getMessage().getBody())) {
+                                entered.countDown();
+                                assertTrue(gate.await(20, TimeUnit.SECONDS));
+                            }
+                        });
+            }
+        };
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return slowRoute();
+    }
+}

Reply via email to