allthingssecurity opened a new pull request, #30755: URL: https://github.com/apache/camel/pull/30755
# Description [CAMEL-27411](https://issues.apache.org/jira/browse/CAMEL-27411) Two fixes in `MulticastProcessor.MulticastTask` (parallel Multicast, Split and Recipient List). **1. The exchange could complete before the last part was aggregated.** `aggregateCompleted` decided that it was done with ```java nbAggregated.incrementAndGet() >= nbExchangeSent.get() && allSent.get() ``` The sender task increments `nbExchangeSent` and then sets `allSent` for the last part, without the lock. When a thread that aggregates a part read `nbExchangeSent` just before the sender numbered the last part, and `allSent` just after the sender set it, it saw every part as aggregated and completed the exchange: ``` part 0 thread: aggregates part 0, nbAggregated = 1, reads nbExchangeSent = 1 sender: nbExchangeSent = 2 (last part), allSent = true, submits part 1 part 0 thread: reads allSent = true -> doDone with part 0 only part 1 thread: part 1 is processed, but never aggregated (the loop stops on done) ``` The route went on with an incomplete aggregate, and nothing was logged. This needs no timeout, failure or `stopOnException`: a plain `multicast().parallelProcessing()` or `split().parallelProcessing()`, ordered or streaming. The window is two adjacent reads, so it is rare: a two-endpoint parallel multicast run about 14 million times gave one result with a part missing. The change reads `allSent` first. The sender sets `allSent` after its last increment of `nbExchangeSent`, so once `allSent` reads true the count is final. The `doDoneNoMorePairs` path (an iterator that ends with a null, CAMEL-24959) sets `allSent` under the lock and is not changed. **2. A streaming parallel timeout reported the wrong index to `AggregationStrategy.timeout`.** `timeout()` took the number of aggregated exchanges as the next index to report. In streaming mode the exchanges are aggregated in the order they complete, so the aggregated indexes are not always the first ones. A multicast to a (slow), b and c: b and c are aggregated, `nbAggregated` is 2, and the timeout called `strategy.timeout(..., 2, ...)` (c, which was aggregated) and never reported index 0 (a, which timed out). With a timeout, the task now keeps the index of each exchange it sends (a map from the sub-exchange to its index, filled by the sender before it submits the exchange, removed when the exchange is aggregated). In streaming mode it also records in a `BitSet` (guarded by the aggregation lock, like the counters) the indexes that were aggregated or reported. The timeout reports the indexes that are not in it, in index order, before it aggregates the exchanges that completed meanwhile (as before). The index is not read from the `CamelMulticastIndex` / `CamelSplitIndex` property of the sub-exchange: an EIP inside the branch can change that property (a nested multicast or split with `UseLatestAggregationStrategy` leaves the index of its own last part on the exchange), and the `BitSet` would then mark the wrong index. Ordered mode still takes the number of aggregated exchanges as the next index (its exchanges are aggregated in index order). Only the index of an exchange that the timeout fi nds completed now comes from the map: on main it came from the property, so a completed later branch whose nested EIP left index 4 on it made the timeout of a 3-branch multicast report indexes 1, 2 and 3 when only branch 1 timed out. Without a timeout nothing is recorded. Found with a TLA+ model of the task (sender, completion threads, the tryLock aggregation, the one-shot timeout, stopOnException failures and an iterator that ends with a null, in ordered and streaming mode). On main it finds a normal completion with a part missing (2 parts, no timeout, no failure) and, in streaming mode, an index both aggregated and reported to `strategy.timeout`. With both changes, every property holds up to 3 parts, 2 failures and the timeout (everystate and TLC: 295,554 distinct states): a normal completion has every part aggregated, an index is never both aggregated and reported, the callback is called exactly once, no hang, and ordered mode keeps the index order. Each change alone leaves the other property violated. The model also shows a separate, known case that this PR does not change: a part the sender numbers while the timeout completes the exchange is neither aggregated nor reported (the documentation already says sub-exchanges can continue after a time out). Tests: - `MulticastParallelTimeoutIndexTest`: streaming, the first branch times out: `strategy.timeout` gets index 0 and the aggregated indexes are 1 and 2; streaming, the middle branch times out: index 1; ordered (control): index 0; streaming, the first branch times out and branches 1 and 2 run a nested multicast that leaves index 2 on both: index 0; ordered (control), branches 0 and 1 run that nested multicast and the last branch times out: index 2; ordered, the middle branch times out and the last branch, completed meanwhile, runs a nested multicast that leaves index 4 on it: index 1. The branch that times out waits on a latch released after the test; no sleeps. - Change 1 has no unit test: the window is between two adjacent atomic reads, and there is no callback, hook or lock between them where a test could hold the thread. I checked it with a debugger outside the test suite: a JDI breakpoint suspends only the aggregating thread between the two reads (no code change) during a parallel streaming split of two parts. On main the split completes with `[p0-done]`; with the change, the same pause (now between the read of `allSent` and the read of `nbExchangeSent`) gives `[p0-done, p1-done]`. The TLA+ model covers it as well. On main 4 of the 6 new tests fail (`strategy.timeout` gets index 2 in the three streaming cases, and 1, 2, 3 in the last ordered case; the two ordered controls pass). With the change the camel-core processor tests (`org.apache.camel.processor.**`) pass: 3363 run, 1 failure, 17 skipped. The failure is `BacklogTracerCalledRouteTimestampTest` (a timestamp comparison), which fails now and then on other branches too and does not touch the multicast (it passed in the run before the last test was added). The new test and 14 existing parallel multicast and split timeout test classes 10 times in one JVM: 0 failures. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it): [CAMEL-27411](https://issues.apache.org/jira/browse/CAMEL-27411). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built and tested the affected modules, including the formatter and import-sort plugins. I did not run the full root build.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. This PR was prepared with Claude Code (Claude Opus 5.5). The commits carry a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
