allthingssecurity opened a new pull request, #30573: URL: https://github.com/apache/camel/pull/30573
# Description [CAMEL-27331](https://issues.apache.org/jira/browse/CAMEL-27331) The batch resequencer completes an exchange as soon as it has queued a copy of it (`Resequencer.process` calls `callback.done(true)` right after `enqueueExchange`). The copy waits in the queue until the batch is complete (`size`, the completion predicate, or `timeout`), and the sender thread then moves it to the sorted `collection` and sends it. The queue was a field of the `BatchSender` thread, and each start creates a new sender (CAMEL-20435). When the route is stopped and started while exchanges wait in the queue, the old sender is cancelled and never drains its queue again, and the new sender starts with an empty one. Those exchanges are lost, although their callers were already told that they succeeded. With `batch().size(10).timeout(2000)`, two exchanges sent and a `stopRoute` / `startRoute` within the 2 seconds: nothing is ever delivered. The `collection` is a field of the processor, kept over a restart and delivered by the next sender (CAMEL-26634), and the stream resequencer keeps its held messages over a restart (CAMEL-25053). Only the queue was lost. What restarts the processor: `stopRoute` / `startRoute` from the API, JMX or the controlbus, `ClusteredRoutePolicy` (leadership lost and regained), the quartz `ScheduledRoutePolicy`. This change (`Resequencer`): - The queue, its lock and condition, the "exchange enqueued" flag and the completion predicate ids move from `BatchSender` to the processor, next to `collection`. All senders use the same ones. The sender of a new start already waits for the sender of the previous start to end before it touches anything (CAMEL-26634), so only one sender at a time drains the queue. - `enqueueExchange` is a method of the processor, so `process()` no longer reads the `sender` field (written by `doStart` / `doStop` without a lock). An exchange queued by a `process()` that raced with the stop is in the shared queue and delivered after the next start. - `process()` on a stopped processor sets a `RejectedExecutionException` on the exchange. Before, it failed with a `NullPointerException` on the `sender` field (`null` after a stop). The new sender delivers the kept exchanges within one batch timeout after the start, together with the exchanges that arrive after it, in one sorted batch. Model: found with a TLA+ model of the batch resequencer (producers, the sender of each start, stop and start). I extended it with the fix: on main it finds an accepted exchange that is in no live sender's queue and is never delivered (invariant and liveness). With the fix, every accepted exchange is delivered or held (in the collection or the queue), one sender sends at a time, and every accepted exchange is delivered in the end under fairness: 3 exchanges, batch size 2, 2 restarts, 18,934 distinct states (TLC and everystate agree). Tests: new `BatchResequencerRestartKeepsQueueTest`: - two exchanges queued, the route stopped and started before the batch timeout: both are delivered, in order; - two exchanges queued, two restarts, two more exchanges: all four are delivered in one sorted batch. On main both fail (nothing is delivered, or only the two exchanges sent after the restarts). With the change, the processor tests of camel-core (`org/apache/camel/processor/**`) run 3345 tests: 1 failure, `BacklogTracerCalledRouteTimestampTest.theCalledRouteIsEnteredWhenItIsCalled`, which does not use the resequencer and fails 1 of 3 runs with the resequencer of main too (a timestamp check). The new class and `BatchResequencerRestartTest` 20 times in one JVM: 100 run, 0 failures. Not changed: - When the route is stopped and not started again (or the CamelContext stops), the exchanges in the queue and in the collection are not delivered. This was already the case for the collection; the processor completes the exchanges when it queues them, so the graceful shutdown has nothing in flight to wait for. - The stream resequencer is not changed. # 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-27331](https://issues.apache.org/jira/browse/CAMEL-27331). # 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 commit carries 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]
