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

   # Description
   
   [CAMEL-27297](https://issues.apache.org/jira/browse/CAMEL-27297)
   
   Follow-up to CAMEL-24941. With `closeCorrelationKeyOnCompletion` and 
`optimisticLocking`, a completion of a group (by size, predicate, timeout, 
interval or force completion) first removes the group from the repository and 
only then puts the key into the closed correlation keys 
(`AggregateProcessor.doOnCompletion`). With optimistic locking the aggregation 
lock is a `NoLock`, so the closed key check that CAMEL-24941 added under the 
lock is not atomic with anything. An exchange that read the repository between 
the remove and the close found no group and an open key, and started a new 
group for the key that was then closed. That group was later sent as a second 
aggregated exchange for the closed key (by the timeout, the interval or a force 
completion), while a later exchange for the key was rejected with 
`ClosedCorrelationKeyException`. This happens both when the exchange checks the 
key in that window, and when it checks the key before the remove but reads the 
repository after it.
   
   This change:
   
   - A completion marks the key as closing before it removes the group, and 
clears the mark once the key is closed, or once the remove failed 
(`onCompletion`, so every path into `doOnCompletion` gets it). The mark is a 
count per key in a map of its own (`closingCorrelationKeys`), not an entry in 
the closed keys. So a remove that fails because the group was changed or 
completed by another thread never reopens a key that another completion closed, 
and a bounded (LRU) closed keys cache cannot evict a mark.
   - With optimistic locking, the key is checked again right after the group 
was read from the repository. If the key is closing, the exchange is tried 
again (`OptimisticLockingException`, as for a failed add), as the completion 
may still fail and leave the key open; the next attempt is rejected once the 
key is closed. If the key is closed, the exchange is rejected. The closing 
marks are read before the closed keys, as a completion closes the key before it 
clears its mark.
   
   The pessimistic path (the default) does not change: there the lock is held 
from the check to the add, and from the remove to the close. Without 
`closeCorrelationKeyOnCompletion` nothing changes either. The cost with both 
options set is two map lookups per exchange, and two updates of a small map per 
completion.
   
   Model: the bug was found with a TLA+ model of the Aggregate EIP with 
optimistic locking. I modelled the fixed design in TLA+ too 
(`AggClosedKey.tla`, adapted from that model: one key, two producers with two 
exchanges each, completion size 2, plus the timeout checker or the interval 
task). TLC on main finds a new group for the closed key (the second producer 
checks the key between the remove and the close, then reads the repository 
after the close). With the change, every property holds: no new group for a 
closed key, at most one aggregate for the key, no exchange lost or aggregated 
twice, an exchange is rejected only when the key ends up closed, and a closed 
key stays closed (29,717 distinct states with size and timeout completion, 
4,788 with the interval task). Variants of the change are caught too: checking 
the mark only before the read, reading the closed keys before the marks, and 
using the closed keys as the mark with an undo on a failed remove (the undo 
reopens a key that an
 other completion closed).
   
   Tests: new `AggregateOptimisticLockingClosedKeyTest`. The repository and 
`doProcess` are hooked with latches, so the interleavings are deterministic and 
there are no sleeps:
   
   - an exchange runs completely between the remove and the close of another 
thread's completion: it must be rejected, the repository must stay empty, and 
only `A+B` is sent;
   - an exchange passes the closed key checks before the remove, and reads the 
repository after it: same expectations;
   - the remove of a completion fails because a force completion completed the 
group first: the key must stay closed and the exchange is rejected on its retry 
(control, passes on main);
   - the remove of a completion fails because another exchange was added to the 
group first: the key must stay open, and the retry completes `A+D+B` (control, 
passes on main).
   
   On main the first two fail (`Expected ClosedCorrelationKeyException but was: 
null`), the two controls pass. With the change all `*Aggregat*` tests pass: 312 
run, 0 failures, 5 skipped. The new class 20 times in one JVM: 80 run, 0 
failures.
   
   Not changed (follow-ups, if wanted):
   
   - A group that completes on its first exchange (completion size 1, or a 
predicate that matches the first exchange) is never stored in the repository, 
so with optimistic locking there is nothing to compare against: two such 
exchanges that arrive at the same time can both be sent, although the key is 
closed after the first.
   - An exchange that read "no group" before another thread even created the 
group, and adds its own group only after that group was completed and the key 
closed, still starts a group for the closed key (the repository add only checks 
that the key has no group). This needs the exchange to stall between its read 
and its add for the whole life of another group. The model shows it, and shows 
that it is the only way left to get a group for a closed key.
   - The closed correlation keys are kept in memory by each Camel instance, so 
with several instances on a shared repository a key closed on one instance is 
still open on the others, as before.
   
   # 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-27297](https://issues.apache.org/jira/browse/CAMEL-27297).
   
   # 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]

Reply via email to