shashank created CAMEL-27533:
--------------------------------
Summary: camel-hazelcast -
ReplicatedHazelcastAggregationRepository with optimistic=true never updates or
completes a stored group, and loses a message when two members start the same
group
Key: CAMEL-27533
URL: https://issues.apache.org/jira/browse/CAMEL-27533
Project: Camel
Issue Type: Bug
Components: camel-hazelcast
Reporter: shashank
Assignee: shashank
h3. What happens
{{ReplicatedHazelcastAggregationRepository}} keeps its groups in a Hazelcast
{{ReplicatedMap}}. With {{optimistic=true}} it uses {{putIfAbsent}},
{{replace(key, old, new)}} and {{remove(key, value)}} on that map for
optimistic locking. {{ReplicatedMap}} extends {{java.util.Map}}, not
{{ConcurrentMap}}, and Hazelcast's {{ReplicatedMapProxy}} (5.4.0) does not
override these three methods, so they are the {{java.util.Map}} default
methods: a read of the local copy, a comparison with {{equals}}, and then a
separate {{put}} or {{remove}}. Two things follow:
* *A stored group can never be updated or completed.* The repository marshals a
new {{DefaultExchangeHolder}} for the expected value, and
{{DefaultExchangeHolder}} has no {{equals}}, so the comparison is always false.
Every message after the first of a group fails with
{{OptimisticLockingException}}, and so does every completion of a stored group.
With the default retry policy of the Aggregate EIP the exchange is retried
forever (the delay grows to 1 second); with {{maximumRetries}} set it fails
with "Exhausted optimistic locking retry attempts". No group of two or more
messages is ever completed, and the group stays in the repository.
* *Two members starting the same group lose a message.* When two members
receive the first message of a key at the same time, 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
only after its {{put}} has replaced the group. The first member's {{add}}
returned normally, so its message is acknowledged and lost.
Adding an {{equals}} to {{DefaultExchangeHolder}} would not be enough: the
check and the write would still be two separate steps on a copy that can be out
of date, so stale updates and the lost first message stay possible.
The default thread-safe mode ({{optimistic=false}}) has a related limit that is
not documented: the cluster lock on the key is held only while the group is
written, not while the Aggregate EIP reads the group and aggregates. When two
members aggregate the same key at the same time, one can overwrite the group
the other just wrote, and messages are lost. This applies to
{{HazelcastAggregationRepository}} too. For the replicated repository a lock
around the read would not help, as the read is from the local copy.
h3. How to reproduce
Two embedded Hazelcast members in one JVM. A route with
{{optimisticLocking()}}, {{completionSize(2)}} and {{new
ReplicatedHazelcastAggregationRepository(name, true, hz)}}: the second message
fails with "Exhausted optimistic locking retry attempts" (with
{{maximumRetries(5)}}), no aggregate is sent, and the group stays in the
repository. Two threads, one per member, call {{add(context, key, null,
exchange)}} for a new key at the same time: in 300 of 300 rounds one {{add}}
returned normally and the stored group was the other member's message.
Found with a TLA+ model of the repository under the Aggregate EIP (local copies
that lag the owner, two members, a crash), and reproduced against real
Hazelcast members.
h3. Fix
A {{ReplicatedMap}} has no atomic compare-and-set, so the replicated repository
cannot do optimistic locking. Backing its optimistic mode with an {{IMap}}
would make it {{HazelcastAggregationRepository}} under another name, and the
mode never completed a stored group.
{{ReplicatedHazelcastAggregationRepository}} now fails to start with
{{optimistic=true}} ({{IllegalArgumentException}}, which also fails the start
of the route). The only route that ran before 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, and runs again with {{optimistic=false}}. Its {{add(key,
old, new)}} throws an {{UnsupportedOperationException}} that says the Aggregate
EIP has {{optimisticLocking}} turned on, which the repository does not support,
for a route with {{optimisticLocking()}} and the repository in thread-safe
mode. The Hazelcast component page gets a section on the aggregation
repositories: the thread-safe mode of both repositories is for one aggregator
per correlation key at a time. Upgrade guide entry for 4.24.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)