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 0700eec748d2 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 (#31014)
0700eec748d2 is described below
commit 0700eec748d28e9ac12053faf768b91d7c6700b6
Author: Aurélien Pupier <[email protected]>
AuthorDate: Fri Nov 27 15:57:37 2026 +0100
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 (#31014)
BacklogTracerMessageHistoryMulticastSedaTest was flaky (about 2% of the
runs):
the history never contained the events of slow-route, so the Awaitility
assertion timed out after 10 seconds.
Cause: in BacklogTracer.traceEvent, the "last" event of a route was merged
into the complete history only when its route matched the route of the head
of the provisional queue ("original"). With a multicast with parallel
processing to seda:fast and seda:slow, fast-route can start before the
starter
route is finished. When the starter route finishes, the history is completed
with the events received so far (including the earlier events of fast-route)
and the provisional queue is cleared. The last event of fast-route then
arrives on an empty provisional queue, so there is no head and "original" is
false: the event stays in the provisional queue and is never merged. The
events of slow-route pile up behind it and are not merged either, as their
route does not match the head. Waiting longer cannot fix this, the events
are never added.
Fix: always merge when the last event of a downstream route is received in
append mode ("event.isLast() && (original || appendMode)").
Tests:
- add BacklogTracerMessageHistoryDownstreamOrderTest, a deterministic test
that feeds the events directly to the BacklogTracer in the order that
triggers the problem (no threads, no SEDA, no timing). It fails without
the fix with "expected: [fast-route, starter, slow-route] but was:
[starter, fast-route]" and passes with it.
- BacklogTracerMessageHistoryMulticastSedaTest, reproduced the failure with
150 repetitions under CPU load (1-2 failures before the fix, none after).
Co-Authored-By: IBM Bob 2.2.1
---
.../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 616293960485..c492306469e8 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
@@ -260,7 +260,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));
+ }
+
+}