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 0db149730374 CAMEL-27411: camel-core - parallel Multicast, Split,
Recipient List: aggregate the last part before completing, and report the index
that timed out (#30755)
0db149730374 is described below
commit 0db149730374b00584954ac0886b80e0ea440df2
Author: allthingssecurity <[email protected]>
AuthorDate: Wed Nov 25 16:45:17 2026 +0530
CAMEL-27411: camel-core - parallel Multicast, Split, Recipient List:
aggregate the last part before completing, and report the index that timed out
(#30755)
aggregateCompleted read nbExchangeSent before allSent. The sender increments
nbExchangeSent and then sets allSent for the last part without the lock, so
a
thread that read the count before the last part was numbered and allSent
after
it was set completed the exchange with that part not aggregated (its result
was
lost). allSent is now read first: once it is true the count is final.
In streaming mode the timeout took the number of aggregated exchanges as the
next index to report to AggregationStrategy.timeout, although the exchanges
are
aggregated in the order they complete. With a timeout the task now keeps the
index of each exchange it sends, and in streaming mode it records the
indexes
aggregated or reported (a BitSet guarded by the lock); the timeout reports
the
ones that are not. The timeout no longer reads the index property of the
exchange, as an EIP inside the branch (a nested split or multicast) can
change
it; in ordered mode that was the only wrong case.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/processor/MulticastProcessor.java | 69 +++++++-
.../MulticastParallelTimeoutIndexTest.java | 173 +++++++++++++++++++++
2 files changed, 234 insertions(+), 8 deletions(-)
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
index b3889de76cbd..68980f1bf6d5 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
@@ -18,6 +18,7 @@ package org.apache.camel.processor;
import java.io.Closeable;
import java.util.ArrayList;
+import java.util.BitSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -470,6 +471,19 @@ public class MulticastProcessor extends
BaseProcessorSupport
final AtomicInteger nbAggregated = new AtomicInteger();
final AtomicBoolean allSent = new AtomicBoolean();
final AtomicBoolean done = new AtomicBoolean();
+ /**
+ * The indexes that are aggregated or reported to {@link
AggregationStrategy#timeout}, in streaming mode with a
+ * timeout: the exchanges are aggregated in the order they complete,
so the timeout cannot take the number of
+ * aggregated exchanges as the next index. In ordered mode they are
aggregated in index order, so the number is
+ * the next index. Guarded by the lock.
+ */
+ final BitSet handledIndexes;
+ /**
+ * The index of each exchange sent and not aggregated yet, when there
is a timeout. The timeout does not read
+ * the index property of the exchange, as an EIP inside the branch can
change it (a nested split or multicast
+ * can leave the index property of its own last part on the exchange).
+ */
+ final Map<Exchange, Integer> sentIndexes;
@Deprecated(since = "4.19.0")
final Map<String, String> mdc;
final ScheduledFuture<?> timeoutTask;
@@ -481,6 +495,8 @@ public class MulticastProcessor extends BaseProcessorSupport
this.pairs = pairs;
this.callback = callback;
this.iterator = pairs.iterator();
+ this.handledIndexes = timeout > 0 && isStreaming() ? new BitSet()
: null;
+ this.sentIndexes = timeout > 0 ? new ConcurrentHashMap<>() : null;
if (timeout > 0) {
timeoutTask = schedule(aggregateExecutorService,
this::timeout, timeout, TimeUnit.MILLISECONDS);
} else {
@@ -548,8 +564,16 @@ public class MulticastProcessor extends
BaseProcessorSupport
try {
Exchange exchange;
while (!done.get() && (exchange = completion.poll()) != null) {
+ Integer index = sentIndexes != null ?
sentIndexes.remove(exchange) : null;
doAggregate(result, exchange, original);
- if (nbAggregated.incrementAndGet() >= nbExchangeSent.get()
&& allSent.get()) {
+ if (handledIndexes != null && index != null) {
+ handledIndexes.set(index);
+ }
+ int aggregated = nbAggregated.incrementAndGet();
+ // read allSent before nbExchangeSent: the sender sets
allSent after its last increment of
+ // nbExchangeSent, so the count read afterwards is final
(read the other way round, the count can
+ // miss the last exchange, which would then never be
aggregated)
+ if (allSent.get() && aggregated >= nbExchangeSent.get()) {
doDone(result.get(), true);
}
}
@@ -597,17 +621,28 @@ public class MulticastProcessor extends
BaseProcessorSupport
}
while (nbAggregated.get() < nbExchangeSent.get()) {
Exchange exchange = completion.pollUnordered();
- int index = exchange != null ? getExchangeIndex(exchange)
: nbExchangeSent.get();
- while (nbAggregated.get() < index) {
- int idx = nbAggregated.getAndIncrement();
- AggregationStrategy strategy =
getAggregationStrategy(null);
- if (strategy != null) {
- strategy.timeout(result.get() != null ?
result.get() : original,
- idx, nbExchangeSent.get(), timeout);
+ int index = exchange != null ? indexOf(exchange) :
nbExchangeSent.get();
+ if (handledIndexes != null) {
+ // streaming: report the indexes before it that are
not aggregated (not the first nbAggregated
+ // indexes, as the exchanges are aggregated in the
order they complete)
+ for (int idx = handledIndexes.nextClearBit(0);
+ idx < index;
+ idx = handledIndexes.nextClearBit(idx + 1)) {
+ handledIndexes.set(idx);
+ nbAggregated.incrementAndGet();
+ reportTimeout(idx);
+ }
+ } else {
+ // ordered: the first nbAggregated indexes are
aggregated
+ while (nbAggregated.get() < index) {
+ reportTimeout(nbAggregated.getAndIncrement());
}
}
if (exchange != null) {
doAggregate(result, exchange, original);
+ if (handledIndexes != null) {
+ handledIndexes.set(index);
+ }
nbAggregated.incrementAndGet();
}
}
@@ -624,6 +659,18 @@ public class MulticastProcessor extends
BaseProcessorSupport
}
}
+ private int indexOf(Exchange exchange) {
+ Integer index = sentIndexes != null ? sentIndexes.remove(exchange)
: null;
+ return index != null ? index : getExchangeIndex(exchange);
+ }
+
+ private void reportTimeout(int index) {
+ AggregationStrategy strategy = getAggregationStrategy(null);
+ if (strategy != null) {
+ strategy.timeout(result.get() != null ? result.get() :
original, index, nbExchangeSent.get(), timeout);
+ }
+ }
+
protected void doTimeoutDone(Exchange exchange, boolean forceExhaust) {
if (markDone()) {
MulticastProcessor.this.doDone(original, exchange, pairs,
callback, false, forceExhaust);
@@ -739,6 +786,9 @@ public class MulticastProcessor extends BaseProcessorSupport
Exchange exchange = pair.getExchange();
int index = nbExchangeSent.getAndIncrement();
updateNewExchange(exchange, index, pairs, hasNext);
+ if (sentIndexes != null) {
+ sentIndexes.put(exchange, index);
+ }
if (!hasNext) {
allSent.set(true);
}
@@ -888,6 +938,9 @@ public class MulticastProcessor extends BaseProcessorSupport
Exchange exchange = pair.getExchange();
int index = nbExchangeSent.getAndIncrement();
updateNewExchange(exchange, index, pairs, hasNext);
+ if (sentIndexes != null) {
+ sentIndexes.put(exchange, index);
+ }
if (!hasNext) {
allSent.set(true);
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelTimeoutIndexTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelTimeoutIndexTest.java
new file mode 100644
index 000000000000..2df0a5382fe2
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/MulticastParallelTimeoutIndexTest.java
@@ -0,0 +1,173 @@
+/*
+ * 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.processor;
+
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.AggregationStrategy;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePropertyKey;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * A parallel multicast with a timeout must report to {@link
AggregationStrategy#timeout} the index of each branch that
+ * did not complete, and never the index of a branch that was aggregated. In
streaming mode the branches are aggregated
+ * in the order they complete, so the aggregated indexes are not always the
first ones. A nested multicast changes the
+ * index property of a branch, so the reported indexes must not depend on it.
+ */
+class MulticastParallelTimeoutIndexTest extends ContextTestSupport {
+
+ // holds the branch that must time out until the test is done
+ private final CountDownLatch release = new CountDownLatch(1);
+ private final IndexStrategy strategy = new IndexStrategy();
+
+ @AfterEach
+ void releaseTimedOutBranch() {
+ release.countDown();
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
context.getInflightRepository().size() == 0);
+ }
+
+ @Test
+ void testStreamingFirstBranchTimesOut() {
+ String body = template.requestBody("direct:streamingFirst", "x",
String.class);
+
+ assertEquals(List.of(0), strategy.timedOut, "The branch that timed out
is index 0");
+ assertEquals(Set.of(1, 2), Set.copyOf(strategy.aggregated));
+ assertEquals(Set.of("B", "C"), Set.copyOf(List.of(body.split(","))));
+ }
+
+ @Test
+ void testStreamingMiddleBranchTimesOut() {
+ String body = template.requestBody("direct:streamingMiddle", "x",
String.class);
+
+ assertEquals(List.of(1), strategy.timedOut, "The branch that timed out
is index 1");
+ assertEquals(Set.of(0, 2), Set.copyOf(strategy.aggregated));
+ assertEquals(Set.of("B", "C"), Set.copyOf(List.of(body.split(","))));
+ }
+
+ @Test
+ void testOrderedFirstBranchTimesOut() {
+ String body = template.requestBody("direct:ordered", "x",
String.class);
+
+ assertEquals(List.of(0), strategy.timedOut, "The branch that timed out
is index 0");
+ assertEquals(List.of(1, 2), strategy.aggregated);
+ assertEquals("B,C", body);
+ }
+
+ @Test
+ void testStreamingFirstBranchTimesOutWithNestedMulticast() {
+ String body = template.requestBody("direct:streamingNested", "x",
String.class);
+
+ // the nested multicasts copy the index property of their last branch
(2) to branches 1 and 2
+ assertEquals(List.of(0), strategy.timedOut, "The branch that timed out
is index 0");
+ assertEquals(Set.of("B", "C"), Set.copyOf(List.of(body.split(","))));
+ }
+
+ @Test
+ void testOrderedLastBranchTimesOutAfterNestedMulticast() {
+ String body = template.requestBody("direct:orderedNested", "x",
String.class);
+
+ // the nested multicasts copy the index property of their last branch
(2) to branches 0 and 1
+ assertEquals(List.of(2), strategy.timedOut, "The branch that timed out
is index 2");
+ assertEquals("B,C", body);
+ }
+
+ @Test
+ void testOrderedMiddleBranchTimesOutBeforeNestedMulticast() {
+ String body = template.requestBody("direct:orderedNestedLast", "x",
String.class);
+
+ // the last branch completes while the middle branch runs, and its
nested multicast leaves index 4 on it
+ assertEquals(List.of(1), strategy.timedOut, "The branch that timed out
is index 1");
+ assertEquals("B,C", body);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:streamingFirst")
+
.multicast(strategy).parallelProcessing().streaming().timeout(2000)
+ .to("direct:a", "direct:b", "direct:c");
+
+ from("direct:streamingMiddle")
+
.multicast(strategy).parallelProcessing().streaming().timeout(2000)
+ .to("direct:b", "direct:a", "direct:c");
+
+ from("direct:ordered")
+ .multicast(strategy).parallelProcessing().timeout(2000)
+ .to("direct:a", "direct:b", "direct:c");
+
+ from("direct:orderedNested")
+ .multicast(strategy).parallelProcessing().timeout(2000)
+ .to("direct:nestedB", "direct:nestedC", "direct:a");
+
+ from("direct:streamingNested")
+
.multicast(strategy).parallelProcessing().streaming().timeout(2000)
+ .to("direct:a", "direct:nestedB", "direct:nestedC");
+
+ from("direct:orderedNestedLast")
+ .multicast(strategy).parallelProcessing().timeout(2000)
+ .to("direct:b", "direct:a", "direct:nestedC5");
+
+ from("direct:nestedB")
+ .multicast(new
UseLatestAggregationStrategy()).to("direct:c", "direct:c", "direct:b").end();
+ from("direct:nestedC")
+ .multicast(new
UseLatestAggregationStrategy()).to("direct:b", "direct:b", "direct:c").end();
+ from("direct:nestedC5")
+ .multicast(new UseLatestAggregationStrategy())
+ .to("direct:b", "direct:b", "direct:b", "direct:b",
"direct:c").end();
+
+ from("direct:a").process(e -> release.await(30,
TimeUnit.SECONDS)).setBody(constant("A"));
+ from("direct:b").setBody(constant("B"));
+ from("direct:c").setBody(constant("C"));
+ }
+ };
+ }
+
+ private static final class IndexStrategy implements AggregationStrategy {
+ final List<Integer> aggregated = new CopyOnWriteArrayList<>();
+ final List<Integer> timedOut = new CopyOnWriteArrayList<>();
+
+ @Override
+ public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+
aggregated.add(newExchange.getProperty(ExchangePropertyKey.MULTICAST_INDEX,
Integer.class));
+ if (oldExchange == null) {
+ return newExchange;
+ }
+
oldExchange.getMessage().setBody(oldExchange.getMessage().getBody(String.class)
+ ","
+ +
newExchange.getMessage().getBody(String.class));
+ return oldExchange;
+ }
+
+ @Override
+ public void timeout(Exchange exchange, int index, int total, long
timeout) {
+ timedOut.add(index);
+ }
+ }
+}