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

   # Description
   
   [CAMEL-25156](https://issues.apache.org/jira/browse/CAMEL-25156)
   
   The Idempotent Consumer EIP (eager, the default) calls `repository.add(key)` 
without a lock and processes the message only when it returns true, so `add` 
has to be atomic. `MemoryIdempotentRepository` holds a lock for this, and the 
JCache, Ehcache, Infinispan, Hazelcast and Spring cache repositories use 
`putIfAbsent`. `CaffeineIdempotentRepository.add` checks with `containsKey` and 
then calls `put`. Two exchanges with the same message id that reach the EIP at 
the same time can both see the key missing, and both get true. This happens 
with concurrent consumers (seda, jms, kafka `consumersCount`) or a parallel 
split, and the duplicate is processed.
   
   This change: `add` returns `cache.asMap().putIfAbsent(key, Boolean.TRUE) == 
null`. That is atomic for the Caffeine map view and matches the 
`IdempotentRepository.add` contract.
   
   Tests:
   - New `CaffeineIdempotentRepositoryConcurrentAddTest`. The test holds the 
key's entry in a `compute` on the cache's map. While it does, `containsKey` 
reports the key as missing and any write of the key waits. Two threads call 
`add` for the key, and the `compute` returns only when both threads are blocked 
on the entry (Awaitility on the thread state). So both calls have checked for 
the key before either could insert it. The test expects exactly one `add` to 
return true.
   - Without the change the test fails (`expected: <1> but was: <2>`). With it, 
it passes.
   - With the change, all camel-caffeine tests pass: 81 tests, 0 failures. That 
includes `CaffeineIdempotentRepositoryTest` and 
`CaffeineIdempotentRepositoryWithSplitTest`.
   
   Found with a TLA+ model of consumers calling `add` with the check and the 
insert as separate steps. I then reproduced it with the real class: two threads 
processed the same message id, and a stress run with 8 threads added thousands 
of keys twice.
   
   # 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).
   
   # 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 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