shashank created CAMEL-27590:
--------------------------------
Summary: camel-redis - RedisAggregationRepository with
optimistic=true never completes a stored group, and cannot add to it with a
strategy that changes the old exchange
Key: CAMEL-27590
URL: https://issues.apache.org/jira/browse/CAMEL-27590
Project: Camel
Issue Type: Bug
Components: camel-spring-redis
Reporter: shashank
Assignee: shashank
Fix For: 4.24.0
h3. What happens
With {{optimistic=true}} and {{optimisticLocking}} on the Aggregate EIP,
{{add(key, old, new)}} and {{remove(key, exchange)}} compare the value stored
in Redis with the exchange they are given, serialized again. It never matches
when a group is completed: {{AggregateProcessor.doOnCompletion}} sets
{{CamelAggregatedCorrelationKey}} on the group it read before it calls
{{remove}}, and the stored group does not have that property. So no stored
group is ever completed, with or without {{useRecovery}}, and with any
aggregation strategy:
* The message that completes a group fails with "Exhausted optimistic locking
retry attempts". With the default {{OptimisticLockRetryPolicy}} (no retry
limit) it is retried forever, so the sender never returns.
* A group completed by its completion timeout stays in Redis and is never sent:
the failed remove is taken as a timeout that another instance handled, and is
logged at DEBUG only.
An aggregation strategy that changes the old exchange and returns it (the style
of the Aggregate EIP page) also fails every add after the first message: the
expected value is serialized from the old exchange after the strategy changed
it.
The byte compare also fails for equal exchanges: Java serialization writes a
second reference to the same String instance as a back-reference, so a group
whose header and property share a value serializes differently from an equal
exchange read back from Redis.
h3. How to reproduce
A route with {{aggregate(header("id"),
strategy).aggregationRepository(repo).optimisticLocking().completionSize(2)}}
on a {{RedisAggregationRepository}} with {{optimistic=true}}, and two messages
with the same id: the second one fails (bounded retry policy) or hangs
(default). With {{completionTimeout}} and one message, nothing is sent and the
key stays in the Redis hash. Confirmed with a real redis-server and with an
in-memory stand-in for the hashes.
Found with a TLA+ model of the repository and the aggregator (liveness: every
send returns, every group settles).
h3. Fix
{{get()}} keeps, for each group it returns in optimistic mode, the bytes it
read from Redis with the key (a {{WeakHashMap}} keyed by the exchange it
returns, which does not override {{equals()}}, so in effect that instance).
{{add}} and {{remove}} compare the stored value with those bytes when the
exchange is one that {{get()}} returned for the same key, and with the exchange
serialized as before otherwise. Nothing is serialized again for the compare.
With the {{codec}} option (for example {{Kryo5Codec}}) {{get()}} reads the
bytes through a second view of the same hash ({{CompositeCodec(codec,
ByteArrayCodec, ByteArrayCodec)}}, so the fields are encoded as in the main
view) and builds the exchange from the holder the codec decodes from them,
which nothing else references, so a strategy may change the body in place (a
{{List}} body). Comparing with the decoded holder encoded again does not work:
{{Kryo5Codec}} reads a {{HashSet}} or {{HashMap}} back with another capacity
(element order changes from about a dozen elements), and {{SerializationCodec}}
writes the capacity of a {{HashMap}}. The Lua script of the optimistic remove
with recovery now writes the given exchange (with the correlation key, and the
last message for a strategy that returns the old exchange) to the completed
hash, as the thread-safe mode does, instead of the stored group before the last
message. The stored format does not change.
Not covered (follow-up): with a strategy that returns the new exchange and
{{useRecovery}}, the completed entry is stored under the id of the old group
while the aggregator confirms the id of the new exchange, so the recover task
sends the partial group (without the last message) once more, as with
{{optimistic=false}} today (the aggregator warns about such strategies with a
recoverable repository). In optimistic mode this is newly reachable, as such a
group never completed before. It needs the Aggregate EIP to give the aggregated
exchange to the repository (core change), to be filed separately. The upgrade
guide says so, and that groups left by 4.23 or older are read only with the
{{codec}} option set.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)