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

gnodet pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new 71c00f68b426 [backport camel-4.22.x] CAMEL-27596: camel-base-engine - 
BacklogTracer merges the history of a downstream route also when the head of 
the provisional queue belongs to another route (#31019)
71c00f68b426 is described below

commit 71c00f68b426c1df71d976922c3dadfe6d058d81
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Nov 27 16:22:03 2026 +0100

    [backport camel-4.22.x] CAMEL-27596: camel-base-engine - BacklogTracer 
merges the history of a downstream route also when the head of the provisional 
queue belongs to another route (#31019)
---
 .../apache/camel/impl/debugger/BacklogTracer.java  |  5 +-
 ...logTracerMessageHistoryDownstreamOrderTest.java | 99 ++++++++++++++++++++++
 2 files changed, 103 insertions(+), 1 deletion(-)

diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/BacklogTracer.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/BacklogTracer.java
index 8399000cc572..006cf2c69dd9 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/BacklogTracer.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/debugger/BacklogTracer.java
@@ -266,7 +266,10 @@ public class BacklogTracer extends ServiceSupport 
implements org.apache.camel.sp
                     boolean added = provisionalHistoryQueue.offer(event);
                     boolean original
                             = head != null && event.getRouteId() != null && 
event.getRouteId().equals(head.getRouteId());
-                    if (event.isLast() && original) {
+                    // in append mode the earlier events of a downstream route 
may already have been merged when the
+                    // originating route finished, so its last event can 
arrive on an empty provisional queue (or behind
+                    // the events of another branch): always merge when a 
downstream route is finished
+                    if (event.isLast() && (original || appendMode)) {
                         if (appendMode) {
                             // downstream route finished: merge into existing 
complete history
                             
completeHistoryQueue.addAll(provisionalHistoryQueue);
diff --git 
a/core/camel-management/src/test/java/org/apache/camel/management/BacklogTracerMessageHistoryDownstreamOrderTest.java
 
b/core/camel-management/src/test/java/org/apache/camel/management/BacklogTracerMessageHistoryDownstreamOrderTest.java
new file mode 100644
index 000000000000..f79f70cd6193
--- /dev/null
+++ 
b/core/camel-management/src/test/java/org/apache/camel/management/BacklogTracerMessageHistoryDownstreamOrderTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.management;
+
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.debugger.BacklogTracer;
+import org.apache.camel.impl.debugger.DefaultBacklogTracerEventMessage;
+import org.apache.camel.spi.BacklogTracerEventMessage;
+import org.junit.jupiter.api.AfterEach;
+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.assertTrue;
+
+/**
+ * Deterministic test of the message history of the {@link BacklogTracer} 
where the downstream routes of a multicast
+ * (via SEDA) finish in a different order than they started, as it happens 
when the threads are scheduled differently.
+ * The events are fed directly to the tracer, so there are no threads and no 
timing involved.
+ */
+public class BacklogTracerMessageHistoryDownstreamOrderTest {
+
+    private static final String BREADCRUMB = "bid-1";
+
+    private CamelContext context;
+    private BacklogTracer tracer;
+    private long uid;
+
+    @BeforeEach
+    public void setUp() {
+        context = new DefaultCamelContext();
+        context.setMessageHistory(true);
+        tracer = BacklogTracer.createTracer(context);
+        tracer.setEnabled(true);
+        tracer.setRemoveOnDump(false);
+    }
+
+    @AfterEach
+    public void tearDown() throws Exception {
+        context.close();
+    }
+
+    @Test
+    public void 
testDownstreamRouteStartsAfterOriginatingRouteAndAnotherDownstreamRouteFinished()
 {
+        // the starter route multicasts to the exchanges 1 (fast) and 2 (slow)
+        trace("starter", "exchange-0", null, true, false);
+        trace("starter", "exchange-0", null, false, false);
+        trace("fast-route", "exchange-1", "exchange-0", true, false);
+        trace("fast-route", "exchange-1", "exchange-0", false, false);
+        // the originating route finishes and completes the history
+        trace("starter", "exchange-0", null, false, true);
+        // the fast route finishes after that, which is the only event in the 
provisional queue
+        trace("fast-route", "exchange-1", "exchange-0", false, true);
+        // and the slow route does everything after the fast route finished
+        trace("slow-route", "exchange-2", "exchange-0", true, false);
+        trace("slow-route", "exchange-2", "exchange-0", false, false);
+        trace("slow-route", "exchange-2", "exchange-0", false, true);
+
+        List<BacklogTracerEventMessage> events = 
tracer.dumpLatestMessageHistory();
+
+        Set<String> routeIds = 
events.stream().map(BacklogTracerEventMessage::getRouteId).collect(Collectors.toSet());
+        assertEquals(Set.of("starter", "fast-route", "slow-route"), routeIds);
+        assertEquals(1, count(events, "slow-route", true));
+        assertEquals(1, count(events, "fast-route", true));
+        assertEquals(9, events.size());
+        assertTrue(events.stream().anyMatch(e -> e.isLast() && 
"slow-route".equals(e.getRouteId())),
+                "Should contain the last event of slow-route");
+    }
+
+    private static long count(List<BacklogTracerEventMessage> events, String 
routeId, boolean first) {
+        return events.stream().filter(e -> routeId.equals(e.getRouteId()) && 
e.isFirst() == first).count();
+    }
+
+    private void trace(String routeId, String exchangeId, String 
correlationId, boolean first, boolean last) {
+        tracer.traceEvent(new DefaultBacklogTracerEventMessage(
+                context, first, last, ++uid, System.currentTimeMillis(), null, 
null, routeId, "node" + uid, null, null, null,
+                "to", "to", 1, exchangeId, correlationId, BREADCRUMB, false, 
false, null));
+    }
+
+}

Reply via email to