allthingssecurity opened a new pull request, #30631:
URL: https://github.com/apache/camel/pull/30631

   # Description
   
   [CAMEL-27349](https://issues.apache.org/jira/browse/CAMEL-27349)
   
   With the defaults (`eager=true`, `removeOnFailure=true`, 
`completionEager=false`), the Idempotent Consumer adds the message id when the 
exchange enters it and registers an `IdempotentOnCompletion` that removes the 
id when the exchange is done and failed. If the exchange fails inside the 
idempotent consumer and the error handler of a calling route redelivers it, the 
same exchange enters the idempotent consumer again while its own id is still in 
the repository. The error handler page describes two ways this happens: a sub 
route called with `to("direct:sub")` that uses `noErrorHandler`, which is how 
the page tells users to retry an entire sub route, and an exception that 
escapes a `doTry` block of the called route (the called route keeps its own 
error handler, but the calling route deals with that exception and calls the 
whole route again). `add` returns false, and the exchange is taken for a 
duplicate of itself:
   
   - the block is skipped, so the message is never processed;
   - the redelivery counts as successful: the exchange completes without an 
exception and never reaches the dead letter channel, also when every attempt 
would fail;
   - the on completion then sees a successful exchange and keeps the id, so 
later copies of the message are dropped as duplicates too.
   
   With `skipDuplicate(false)` the redelivered exchange is routed on with 
`CamelDuplicateMessage=true`, into the duplicate branch. A `doCatch` of the 
calling route that sends the failed exchange to the sub route again hits the 
same duplicate. `completionEager(true)` is not affected (the id is removed as 
soon as the block fails), nor is `eager(false)` (the id is only added on 
completion).
   
   `completionEager(true)` is not a workaround with the same meaning: it also 
stops a failure after the block from removing the id. Removing the id as soon 
as the block fails (only on failure) would not do either: while the exchange 
waits for its redelivery another exchange could add the id, and the first 
exchange's on completion would then remove an id that the other exchange owns 
(the kind of problem fixed in CAMEL-24953).
   
   Change, in `IdempotentConsumer.process` only:
   
   - For a new key in this mode, the callback given to the block 
(`KeyOwnerCallback`) marks the exchange as the owner of its id when the block 
ends with the exchange failed (an exchange property per idempotent consumer, 
holding the exchange, the message id and its `IdempotentOnCompletion`), and 
removes the mark when the block succeeds.
   - When `add` returns false, the exchange is processed again (no 
`CamelDuplicateMessage`, no duplicate count) only if it carries the mark of 
this idempotent consumer for the same message id, the mark names this exchange 
object, and its on completion is still pending on the exchange 
(`containsOnCompletion`). It runs under the on completion it registered the 
first time; nothing new is registered.
   - Everything else is a duplicate as before: another exchange with the same 
id (also while the first one waits for its redelivery), a copy of the exchange 
that carries the property (a split or wire tap copy is another exchange 
object), an exchange whose block succeeded and that failed after the idempotent 
consumer (the redelivery skips the block, as on main, so the message is not 
processed twice), `removeOnFailure=false` (the id of a failed attempt is kept 
on purpose, so the redelivery stays a duplicate), and the same exchange sent 
again after it was done (its on completion is gone).
   
   The property name starts with `Camel`, so `DefaultExchangeHolder` logs its 
exclusion at DEBUG like other internal properties. It is only left on an 
exchange whose block failed. It is per idempotent consumer so that nested 
idempotent consumers on the same id each recognise their own redelivery. The 
mark holds the exchange itself because `Exchange.copy()` shares the unit of 
work, so `containsOnCompletion` alone would also accept a copy; a copy made 
from a failed exchange therefore keeps a reference to the original while the 
copy lives.
   
   Docs: the Idempotent Consumer EIP page (and its catalog copy) says what a 
redelivery does. Upgrade guide: a 4.24 entry, as a redelivered exchange that 
was skipped before is now processed.
   
   Model: found with a TLA+ model of the Idempotent Consumer, its on completion 
and an in-memory repository (two or three exchanges with the same id, 
redelivery by a calling route, a step after the block, `completionEager`, 
non-eager, `removeOnFailure`, LRU eviction, `add` and `remove` throwing). On 
main, with a redelivery, "when all exchanges are done, the repository holds the 
id only if some exchange processed it" is violated; without a redelivery it 
holds. With the change it holds, together with "eager: at most one exchange per 
id in the block" and "a processed message is not processed again while its id 
is kept", in all the combinations of the options (everystate and TLC agree, up 
to 80,191 distinct states with three exchanges and two redeliveries). A variant 
of the change that processes the redelivery also after a successful block 
violates the last property. The model also shows what this PR does not change: 
a `remove` that throws keeps the id of a failed exchange, and an evicte
 d id lets two exchanges run at the same time (the memory repository is 
bounded); in non-eager mode a failing exchange still removes an id another 
exchange added, which the `IdempotentRepository` javadoc documents.
   
   Tests: new `IdempotentConsumerRedeliveryTest` 
(`KeyValueIdempotentRepository`, a dead letter channel with 2 redeliveries on 
the calling route, a sub route with `noErrorHandler`, no sleeps):
   
   - the first attempt fails: the redelivery is processed, the exchange 
succeeds, the id is kept;
   - every attempt fails: each redelivery runs the block, the message reaches 
the dead letter channel, the id is removed;
   - a step after the block fails: the redelivery does not run the block again 
(control, passes on main);
   - `skipDuplicate(false)`: the redelivery is not routed as a duplicate;
   - another exchange with the same id, sent from `onRedelivery` while the 
first waits for its redelivery, is still a duplicate;
   - `removeOnFailure(false)`: the redelivery is a duplicate, as before 
(control, passes on main);
   - `completionEager(true)`: the redelivery is processed (control, passes on 
main);
   - the called route keeps its error handler and the exception escapes its 
`doTry` block: the redelivery of the calling route is processed;
   - a `doCatch` of the calling route sends the failed exchange to the sub 
route again: it is processed.
   
   On main 6 of the 9 fail (`mock://result Received message count. Expected: 
<1> but was: <0>`, `mock://dead ... Expected: <1> but was: <0>`, 
`mock://duplicate ... Expected: <0> but was: <1>`). With the change: the 
camel-core tests under `org.apache.camel.processor` 3364 run, 1 failure, 17 
skipped; the failure is `BacklogTracerCalledRouteTimestampTest` (a timestamp 7 
ms off in a parallel run, unrelated, 5 of 5 pass alone). All the `*Idempotent*` 
tests among them pass. `IdempotentConsumerRedeliveryTest` 20 times in one JVM: 
180 run, 0 failures. The camel-core tests matching `*Idempotent*`, 
`*Redelivery*`, `*DeadLetter*`, `*ErrorHandler*`, `*OnCompletion*`, `*DoCatch*` 
and `*Direct*`: 855 run, 0 failures. camel-management (whole module, including 
the managed idempotent consumer tests): 491 run, 0 failures. The camel-sql 
`*Idempotent*` tests: 39 run, 0 failures. Nested idempotent consumers, a split 
whose parts are redelivered by the calling route and a block with `threads()` 
were also t
 ried outside the PR and behave the same way.
   
   Not changed:
   
   - An exchange whose block failed but that is handled in the calling route (a 
`doCatch`) and then completes successfully still confirms the id, as on main.
   - With an LRU eviction of the id while the first exchange is in flight, 
another exchange can already run with the same id on main; the redelivered 
first exchange then runs as well.
   
   # 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-27349](https://issues.apache.org/jira/browse/CAMEL-27349).
   
   # 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]

Reply via email to