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

   # Description
   
   [CAMEL-27589](https://issues.apache.org/jira/browse/CAMEL-27589)
   
   Follow-up to CAMEL-27297. With `optimisticLocking` and 
`closeCorrelationKeyOnCompletion`, the aggregator lock is a no-op, so an 
exchange reads the repository and adds its group without a lock. CAMEL-27297 
made a completion mark the key as closing before it removes the group, and 
checks that mark right after the read. That covers an exchange whose read found 
the group being removed. Three other orders were still open. In the first two 
an exchange starts a group for a key that has just been closed:
   
   - Exchange B reads "no group" and passes the checks. Exchange A of the same 
key completes on its own: its group is never stored (the completion predicate 
matches the first exchange, `completionSize(1)`, and so on), so A makes no 
repository call and only closes the key. B's `putIfAbsent` then succeeds.
   - Exchange B reads "no group" and passes the checks. A1 stores a group, A2 
completes it (closing mark, remove, close). B's `putIfAbsent` succeeds because 
the key has no group again.
   
   B's group then lives on for the closed key and is sent later (completion 
timeout, interval, force completion) as a second aggregate of the key, while B 
itself was accepted. In the third, A and B both read "no group", both pass the 
checks and both complete on their own: neither makes a repository call, so both 
are sent. Without optimistic locking none of this can happen, because the lock 
is held from the read to the add or the completion.
   
   The change, in `AggregateProcessor`, only with optimistic locking and 
`closeCorrelationKeyOnCompletion`:
   
   - After an exchange has added a new group (the read found no group), it 
checks the closing marks and the closed keys again. If the key is closing or 
closed, it reads the group again and removes it, but only while it is still the 
group of only this exchange (same exchange id, `CamelAggregatedSize` 1). The 
exchange is then tried again (closing) or rejected with 
`ClosedCorrelationKeyException` (closed). If the group can no longer be 
removed, another thread has changed or completed it with this exchange in it, 
so the add stands and the exchange is not failed. With a recoverable repository 
the removed group goes to the completed store, so it is confirmed right away, 
the same way as a discarded group (marked as being completed until then, so the 
recover task does not send it).
   - A completion of a group that was never stored is now done by one exchange 
of a key at a time (a claim in a set of keys; another exchange that wants to do 
the same is tried again). It marks the key as closing as well, and then reads 
the repository once and checks the closing marks and the closed keys. If the 
key has a group now (another exchange started it after this exchange's read), 
another completion is closing the key, or the key was closed since the read, 
the completion is tried again (optimistic locking retry). The next attempt 
aggregates with that group, or is rejected because the key is closed.
   
   Each side writes first (the closing mark, or the add) and reads the other 
side after, so at least one of them sees the other: either the new group sees 
the key closing or closed, or the completion sees the new group. A completion 
of a stored group never backs off for these checks (its remove is the 
compare-and-set it already had), so the timeout, interval and force completions 
behave as before, and the claim keeps two exchanges that complete on their own 
from backing off from each other forever. Pre-completion is left as it is: an 
exchange that pre-completed the current group and starts the next one closed 
the key itself, and keeps its new group as without optimistic locking.
   
   Nothing changes for routes without optimistic locking, or without 
`closeCorrelationKeyOnCompletion`: every new line is behind `optimisticLocking 
&& closedCorrelationKeys != null`. With both, the only new costs are one 
repository read and a few map operations when the first exchange of a group 
completes it on its own, and two map lookups after a new group is added; the 
second read and the remove only happen in the race.
   
   What is left: in the short window between the add and the check, a 
completion that does not go through the closed check could still complete the 
new group, and then it is sent as before. In the model this is the eviction of 
a timeout entry left over from the earlier group (with optimistic locking a 
completion leaves the entry in the timeout map, which can complete the next 
group of the key early; that is a separate issue for a follow-up); the 
completion interval and force completion scan the repository and could hit the 
same window, which is a few map lookups long.
   
   Found with a TLA+ model of the aggregator with optimistic locking (property: 
at most one aggregate per closed key). I added this fix to the model (each add 
of a group is a new version, as identity and serialized content are for the 
memory, Hazelcast and Redis repositories). On main the model breaks the 
property with 2 or 3 exchanges; with the fix it holds for 2 and 3 exchanges 
with completion by predicate (one or two exchanges completing on their own) and 
by size, also with retries on the scheduler (largest 474,530 distinct states), 
and with the completion timeout once the left-over timeout entry is fixed as 
well (1,889,290 distinct states). With the completion timeout and the left-over 
entry as on main, the only counterexamples are the eviction of the left-over 
entry described above.
   
   Tests, in `AggregateOptimisticLockingClosedKeyTest`, with a 
`MemoryAggregationRepository` whose `get` or `add` (or the aggregation) waits 
on latches, so each order is forced (no sleeps):
   
   - `testNewGroupAfterExchangeCompletedAloneIsRejected`: B completes on its 
own while D is about to add its new group: D is rejected with 
`ClosedCorrelationKeyException`, the repository is empty, only `B` is sent. On 
main D was accepted and its group stayed in the repository.
   - `testNewGroupAfterStoredGroupWasCompletedIsRejected`: `[A, B]` is stored 
and completed while D is about to add: D is rejected, only `A+B` is sent. On 
main D was accepted.
   - `testNewGroupRemovedAgainIsConfirmedWithRecoverableRepository`: the first 
test with a repository that moves a removed exchange to a completed store until 
it is confirmed, as the recoverable repositories do: the group [D] that is 
removed again is confirmed, so the recover task has nothing to send. On main D 
was accepted.
   - `testExchangeThatWouldCompleteAloneAfterKeyClosedIsRejected`: X has passed 
the checks and completes on its own, Y completes on its own and closes the key 
first: X is rejected, only `YB` is sent. On main both were sent.
   - `testExchangeThatWouldCompleteAloneJoinsNewGroup`: B read no group and 
would complete on its own, D adds its group before B closes the key: B sees D's 
group, is tried again and completes `D+B`; D is not failed. On main `B` was 
sent alone and D's group stayed.
   
   On main all 5 new tests fail (the 4 tests of CAMEL-27297 pass); with the fix 
all 9 pass. Each half of the fix is needed: without the checks in the 
completion on its own, 
`testExchangeThatWouldCompleteAloneAfterKeyClosedIsRejected` fails; without the 
confirm, the recoverable test fails. The class 20 times in one JVM: 180 run, 0 
failures; with the other closed key and optimistic locking tests 
(`AggregateClosedCorrelationKeyRaceTest`, `AggregateClosedCorrelationKeyTest`, 
`AggregateOptimisticLockSyncRetryTest`, 
`AggregateOptimisticLockingForceCompletionTest`, 
`AggregateOptimisticLockingTimeoutTest`, 
`DistributedOptimisticLockFailingTest`) 20 times: 500 run, 0 failures. The 
camel-core aggregate tests (`org.apache.camel.processor.aggregator.**`, 
`org.apache.camel.processor.aggregate.**` and the other `*Aggregat*Test`): 348 
run, 0 failures, 5 skipped; camel-core-processor tests: 5 run, 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-27589](https://issues.apache.org/jira/browse/CAMEL-27589).
   
   # 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. No generated files 
change.)
   
   # 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