allthingssecurity opened a new pull request, #30905: URL: https://github.com/apache/camel/pull/30905
# Description [CAMEL-27590](https://issues.apache.org/jira/browse/CAMEL-27590) With `optimistic=true` and `optimisticLocking` on the Aggregate EIP, `RedisAggregationRepository` could not complete a stored group. `add(key, old, new)` and `remove(key, exchange)` compared the value in Redis with the exchange they were given, serialized again. When a group is completed, `AggregateProcessor` sets `CamelAggregatedCorrelationKey` on the group it read and then calls `remove`, and the stored group does not have that property, so the compare never matched: - the message that completes a group failed with "Exhausted optimistic locking retry attempts". With the default `OptimisticLockRetryPolicy` (no retry limit) it was retried forever and the sender never returned; - a group completed by its completion timeout stayed in Redis and was never sent: the failed remove was taken as a timeout handled by another instance (DEBUG log only). An aggregation strategy that changes the old exchange and returns it, as the Aggregate EIP page shows, also failed every add after the first message, as the expected value was serialized from the old exchange after the strategy changed it. And the byte compare could fail for equal exchanges: Java serialization writes a second reference to the same String instance as a back-reference, so a header and a property that share a value serialize differently once read back. The fix is in the repository. `get()` now keeps, in optimistic mode, the bytes it read from Redis for each group it returns, with the key, in a weak map keyed by the exchange it returns. It is a `WeakHashMap`, which compares keys with `equals()`; the keys are the `DefaultExchange` instances that `get()` creates, which do not override it, so an entry is found only for 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 again otherwise (as before). A group that another aggregator changed after the read still fails the compare. Thread-safe mode (`optimistic=false`) is not changed, and neither is the stored format. Without the `codec` option, the value kept is the bytes that were read, so the exchange is not serialized again for the compare, which also fixes the back-reference case. With the `codec` option (new in 4.24, for example `Kryo5Codec` to read the groups stored by 4.23), `get()` reads the bytes too, through a second `RMap` view of the same hash with `CompositeCodec(codec, ByteArrayCodec, ByteArrayCodec)`. The fields still go through the key encoder of the codec, as in the main view, so both views address the same field and the stored format does not change. `get()` keeps those bytes and builds the exchange from the holder that the codec decodes from them. Nothing else references that holder, so a strategy that changes the body in place (adds to a `List` body) does not change what is compared. `add` (`replace`), `remove` and the Lua script are given the bytes kept. Comparing with the decoded holder encoded again does not work, because a codec does not always give back the same bytes: `Kryo5Codec` reads a `HashSet` or `HashMap` back with another capacity, so the order of the elements changes once there are about a dozen of them (on a real Redis a `HashSet` body failed from message 13, a `HashMap` body from message 7), and `SerializationCodec` writes the capacity of a `HashMap`, which the decoded map does not keep (every body failed from message 2). This also saves an encode and a decode of the group per `get()` and an encode per `add` and `remove`. The Lua script of the optimistic remove with recovery now writes the given exchange to the completed hash, as the thread-safe mode does (and as the optimistic mode did before CAMEL-26395), instead of the stored group. The stored group lacks the correlation key (which the recover task reads) and, with a strategy that returns the old exchange, the last message. I did not change `AggregateProcessor` for this. Setting the correlation key after the remove would drop it from the exchange kept for recovery, and in-place strategies change the old exchange by design (only `MemoryAggregationRepository` gets a copy). The JDBC repository solves the same problem with a version read by `get()`; this keeps the Redis format as it is. Not changed, and the same as with `optimistic=false`: 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 old group (without the last message) once more, as redelivered (the aggregator logs a WARN about such strategies with a recoverable repository). In optimistic mode this case is newly reachable: before, such a group never completed at all. Fixing it needs the Aggregate EIP to give the aggregated exchange to the repository (core), so it is a follow-up, and the upgrade guide says so. Also not changed: the recover task of every instance sharing the hashes recovers completed entries that another instance is still processing, and the thread-safe mode shared by several instances can lose an update. I will raise these separately. Upgrade guide (4.24): groups that Camel 4.23 or older left in Redis this way are read only with the `codec` option set to the codec they were stored with (by default `Kryo5Codec`, see the entry above it); they then complete after the upgrade, with the next message of their key or, when the group records its completion timeout, by that timeout once the route starts again. The completed entry is now the exchange the aggregator removes. With a strategy that returns the new exchange and `useRecovery`, the groups now complete and the recover task sends the partial group once more, as with `optimistic=false`; fixing that needs a change in the Aggregate EIP. Model: found with a TLA+ model of the repository and the aggregator (liveness: every send returns, every group settles). In the model, the compare alone is not enough: the completed entry must also be the given exchange, otherwise a recovered group misses its last message. With several instances, sending a completed group at most once also needs each instance to recover only its own completed entries, which is the separate issue above. Tests: `RedisAggregationRepositoryOptimisticTest` runs the repository through the Aggregate EIP with an in-memory stand-in for the Redis hashes and the Lua script, which stores the values encoded by the codec of the hash and compares them by their bytes, as Redis does (no Docker): completion size 2 with a strategy that changes the old exchange (with and without recovery) and one that returns the new exchange; completion size 3 with `Kryo5Codec` and a strategy that adds to a `List` body in place; completion size 20 with `Kryo5Codec` and a `HashSet` body; completion size 3 with `SerializationCodec`; completion timeout; two repositories on the same hashes aggregating 20 keys concurrently; and at repository level the remove after the aggregator set the correlation key, the completed entry, equal values that serialize differently, a stale read that still fails to update or remove, and an exchange that `get()` did not return. On main 12 of the 13 tests fail, with "Exhausted optimistic l ocking retry attempts" or `OptimisticLockingException` (the one that passes, an exchange that `get()` did not return, is a control for the unchanged fallback). A variant of the fix whose script still writes the stored group fails only the completed-entry test, a variant that builds the exchange from the decoded holder itself fails only the `List` body codec test, and a variant that compares with the decoded holder encoded again fails only the `HashSet` test (at message 13) and the `SerializationCodec` test (at message 2), all with exhausted retries. With the fix all 13 pass, 20 times in a row, and the camel-redis unit tests pass. The stand-in follows the value that the real script writes, read from the script text. I also ran the module's Redis ITs (RedisAggregationRepositoryOperationsIT, RedisAggregationRepositoryCodecIT, AggregateRedisIT, AggregateRedisRecoverIT, RedisKeyValueRepositoryIT, 64 tests) against a local redis-server: all pass. With a real redis-server and completion si ze 2: on main the second message fails (bounded retry) or hangs (default retry) and the group stays in Redis, and a completion timeout never sends the group; with the fix every group is sent once and Redis is left empty, also with `Kryo5Codec` and a strategy that adds to a `List` body in place, and on the interval, force-completion, complete-all-on-stop and predicate paths. Completion size 20 into an `ArrayList`, `LinkedHashSet`, `HashSet` and `HashMap` body, without codec, with `Kryo5Codec` and with `SerializationCodec` (12 cases): every message accepted and every group sent once. Two contexts on the same hashes, 8 threads, 300 groups of 3 messages, without codec and with `Kryo5Codec`: each group sent once, none left. The same run shows the unchanged case above: with a strategy that returns the new exchange and recovery, the old group is sent again as redelivered, in both modes. # 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-27590](https://issues.apache.org/jira/browse/CAMEL-27590). # 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.) # 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]
