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

   # Description
   
   [CAMEL-27533](https://issues.apache.org/jira/browse/CAMEL-27533)
   
   `ReplicatedHazelcastAggregationRepository` keeps its groups in a Hazelcast 
`ReplicatedMap`. With `optimistic=true` it called `putIfAbsent`, `replace(key, 
old, new)` and `remove(key, value)` on that map. `ReplicatedMap` extends 
`java.util.Map`, not `ConcurrentMap`, and `ReplicatedMapProxy` in Hazelcast 
5.4.0 does not override these three methods (checked with `javap`; the only 
`remove` it declares is `remove(Object)`). So they are the `java.util.Map` 
default methods: a `get` on the local copy, an `equals` check, then a separate 
`put` or `remove`. Two things followed:
   
   - **A stored group could never be updated or completed.** The repository 
marshals a new `DefaultExchangeHolder` for the expected value, and 
`DefaultExchangeHolder` has no `equals`, so the check was always false. Every 
message after the first of a group failed with `OptimisticLockingException`, 
and so did every completion of a stored group (`remove(key, value)`). With the 
default retry policy of the Aggregate EIP the exchange is retried forever; with 
`maximumRetries` set it fails with "Exhausted optimistic locking retry 
attempts". The group stays in the repository.
   - **Two members starting the same group lost a message.** Both find no group 
in their local copy, and both `put`. The second `put` returns the first 
member's group, so the second member throws `OptimisticLockingException`, but 
its `put` has already replaced the group. The first member's `add` returned 
normally, so its message was acknowledged and lost. With two embedded members 
and a barrier this happened in 300 of 300 rounds.
   
   ## Why reject the option instead of making it work
   
   I looked at two ways:
   
   - Back the optimistic mode with an `IMap`, as 
`HazelcastAggregationRepository` does. The optimistic mode of the replicated 
class would then be `HazelcastAggregationRepository` under another name: groups 
in a partitioned map, no local copy. That changes what the class is for, and 
users who want an `IMap` already have the other class.
   - Add `equals` to `DefaultExchangeHolder`. The check and the write would 
still be two steps on a copy that can be behind the owner, so the lost first 
message above stays, and an update based on an old read can still win. The TLA+ 
model of the repository shows both with a value `equals`.
   
   The optimistic mode never completed a stored group. The only route that ran 
with `optimistic=true` is one whose groups all complete on their first message 
(for example `completionSize(1)`), where the repository is never written. Such 
a route now fails to start too; the way back is `optimistic=false`, and the 
upgrade guide says so. `OptimisticLockingAggregationRepository` says an 
implementation that cannot do optimistic locking should not implement it; the 
replicated class inherits it from `HazelcastAggregationRepository`, so the 
closest thing is to refuse the option when the repository starts.
   
   ## The change
   
   - `ReplicatedHazelcastAggregationRepository.doStart()` throws an 
`IllegalArgumentException` when `optimistic` is `true`, before it creates its 
own Hazelcast instance (when none was given), so it does not start a member 
only to shut it down again. The Aggregate EIP starts the repository, so the 
route fails to start with that cause.
   - `add(key, oldExchange, newExchange)` always throws an 
`UnsupportedOperationException` whose message says that the Aggregate EIP has 
`optimisticLocking` turned on and that the repository does not support it. It 
is reached when a route turns on `optimisticLocking` with the repository in its 
thread-safe mode; that failed on every message before too, with no message.
   - The optimistic branch of `remove()` is gone (it could not be reached any 
more). The thread-safe branch is unchanged.
   - Javadoc: the class says what it stores where, that optimistic locking is 
not supported, and that thread-safe mode is for one aggregator per correlation 
key at a time. The `optimistic` parameter of the constructors says it must be 
`false`. The class Javadoc of `HazelcastAggregationRepository` gets the same 
note on thread-safe mode.
   - Docs: a new "Aggregation repositories" section on the Hazelcast component 
page (and its catalog copy). In thread-safe mode (the default) the cluster lock 
of the key is held only while a group is written, not while the Aggregate EIP 
reads the group and aggregates. When the aggregators of two members update the 
same key at the same time, one can overwrite the group the other just wrote, 
and messages are lost; with two members sending 300 messages each to one key, 
300 of the 600 were lost with the replicated repository and 214 with 
`HazelcastAggregationRepository` (one run of each, in the hunt). Locking around 
the read would not help the replicated repository, as the read is from the 
local copy. This is documented, not changed.
   - Upgrade guide 4.24 entry.
   
   ## Tests
   
   New `ReplicatedHazelcastAggregationRepositoryOptimisticTest` 
(package-private, JUnit assertions as in the module, two embedded Hazelcast 
members from the module's test support class, no sleeps):
   
   - `optimisticRepositoryFailsToStart`: `start()` throws 
`IllegalArgumentException` with the message.
   - `optimisticRepositoryWithoutInstanceFailsToStart`: the same without a 
Hazelcast instance; no instance of its own is left running.
   - `routeWithOptimisticRepositoryFailsToStart`: a route with 
`optimisticLocking()` and the repository with `optimistic=true` fails to start, 
with the `IllegalArgumentException` as a cause.
   - `optimisticLockingRouteWithThreadSafeRepositoryFailsWithAMessage`: with 
`optimisticLocking()` and `optimistic=false`, the first message fails with the 
`UnsupportedOperationException` and its message, and nothing is sent on.
   - `threadSafeRepositoryWithOneAggregatorLosesNoMessage`: a control for the 
documented use. Four threads send 50 messages each to one key through one 
aggregator in thread-safe mode; exactly one aggregate with the sum 200 is sent, 
and the group is gone from the repository. It passes on main too.
   
   On main, the first four fail and the control passes (3 runs: 12 of 15 fail). 
Without the check in `doStart()` (only the message in `add`), the three start 
tests fail. With the check but `add(key, old, new)` throwing without a message 
as before, only the `optimisticLocking` route test fails.
   
   With the change: the new class 10 times in one JVM, 50/50; the other 
aggregation repository classes of the module 3 times in one JVM, all pass 
except `HazelcastAggregationRepositoryRoutesTest.checkAggregationFromTwoRoutes` 
(see below). camel-hazelcast unit tests: 309 run, 1 failure, 1 error. The error 
is 
`HazelcastUserConfigSerializationFilterWarningTest.warnsForUserClientConfigWithoutFilter`,
 which tries to connect a Hazelcast client to a cluster and fails the same way 
on other branches in my offline environment. The failure is 
`HazelcastAggregationRepositoryRoutesTest.checkAggregationFromTwoRoutes` (the 
`IMap` repository, not changed here), which gets 2 aggregates instead of 1: it 
fails the same way with the code of main (9 of 10 runs in one JVM). Both of its 
test methods share the CamelContext and `mock:gotcha`, so it can receive the 
aggregate of the other method; that is for a separate fix. The formatter and 
import-sort plugins leave the change as it is.
   
   Model: the TLA+ model from the hunt (one key, two or three members with 
local copies that lag the owner, the Aggregate EIP read-aggregate-write, a 
crash) gets the fix as a constant that rules out the optimistic actions, and 
two configurations: one aggregator per key (the documented use) and two 
aggregators on the key (the documented loss). I did not run the model checker 
on the two new configurations for this PR (not enough free memory on the 
machine at the time). The hunt's runs of the code as it is showed the lost 
messages of the optimistic mode, and of thread-safe mode with two aggregators 
on one key.
   
   Not changed:
   
   - Thread-safe mode with several aggregators on the same key still loses 
messages, in both repositories. It is documented here. Holding the `IMap` lock 
of `HazelcastAggregationRepository` across the read and the write would need a 
hook around the read-aggregate-write of the Aggregate EIP, so it is left for a 
separate change. For the replicated repository a lock does not help.
   - `HazelcastAggregationRepository` (`IMap`) with `optimistic=true` does not 
work either, so the docs and the error messages do not point to it for several 
aggregators. On one member, with any aggregation strategy, a group of 2 or more 
messages never completes: the completing message fails with "Exhausted 
optimistic locking retry attempts" (retried forever with the default policy) 
and the group stays in the map. With completion by timeout, the failed remove 
is taken as an eviction stolen by another instance, so the timeout is dropped 
and the group stays in the map, never sent, with no error for the sender. The 
cause: `AggregateProcessor` sets `AGGREGATED_CORRELATION_KEY` on the group 
exchange before it calls `remove(key, original)`, the holder includes that 
property, and the `IMap` compares serialized bytes, so the remove never 
matches. It is older than CAMEL-25211 and CAMEL-26796. It gets its own JIRA; 
this PR does not change it.
   - The recovery task of every member scans the whole completed map, so a 
completed exchange that one member is still sending can be recovered and sent 
again by another member (both repositories). That needs the completing member 
recorded with the entry, and gets its own JIRA and PR.
   - The thread-safe `remove()` of the replicated repository stores the 
completed exchange and then removes the group in two calls (a `ReplicatedMap` 
cannot take part in a transaction), as CAMEL-25211 chose; a crash between the 
two can complete the old messages twice.
   - The constructors that take `optimistic` are kept: `false` is still valid. 
They could be deprecated separately.
   
   Backport: the replicated optimistic mode is broken the same way on 
camel-4.22.x, camel-4.18.x and camel-4.14.x. Because this change makes a start 
fail, I would keep it on main; the docs part can go back.
   
   # 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-27533](https://issues.apache.org/jira/browse/CAMEL-27533).
   
   # 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 module, including the formatter and 
import-sort plugins. I did not run the full root build. The catalog copy of the 
component page is updated by hand, identical to the source.)
   
   # 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