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

   # Description
   
   [CAMEL-27282](https://issues.apache.org/jira/browse/CAMEL-27282)
   
   With `offsetRepository` (and no manual commits) each fetcher of a Kafka 
consumer uses a `CommitToOffsetManager`. Its `commit(partition)` saves the last 
offset the fetcher processed in that partition, but keeps it in its 
`OffsetCache`; the Sync and Async commit managers remove the entry once it is 
committed. `ClassicRebalanceListener.onPartitionsRevoked` (and 
`ResumeRebalanceListener`) commits every revoked partition. The repository is 
shared by the consumers of the endpoint (`consumersCount`), and often by 
several applications (a JDBC, Infinispan or file state repository).
   
   So a consumer that gets a partition back writes an old offset:
   
   1. Consumer A processes partition P up to offset 100 and saves it; a 
rebalance moves P to consumer B.
   2. B resumes at 101, processes up to 500 and saves 500.
   3. P comes back to A (B leaves, reconnects or the group shrinks; several 
rebalances in a row are common during a rolling restart), and 
`OffsetPartitionAssignmentAdapter` seeks to 501.
   4. Another rebalance revokes P before A polled a record of it: the revoke 
saves A's cached 100. The repository goes back from 500 to 100, and the next 
owner processes 101 to 500 again.
   
   Fix: `commit(partition)` removes the offset from the cache once it is saved, 
as the other commit managers do. The streaming facade commits each partition 
after the records of the poll, so a revoke saves an offset only for records 
processed since.
   
   Model: a small TLA+ model (`KafkaOffsetRepo.tla`: two fetchers, one 
partition, process / commit after the poll / revoke / assign with seek to 
repository + 1) checks that the repository never moves back and that it holds 
everything processed whenever the partition has no owner. On main TLC finds the 
sequence above (9 states); with the change both hold (95 distinct states).
   
   Tests: `CommitToOffsetManagerRevokeTest`, with a kafka-clients 
`MockConsumer`, a `MemoryStateRepository`, a `CommitToOffsetManager` and the 
`ClassicRebalanceListener`: save 100, revoke, the repository is set to 500 (the 
other consumer), assign (position 501), revoke: the repository must still hold 
500. Two more tests check that records processed after the partition came back, 
and a forced commit, are saved. On main the first fails (the repository holds 
100); the other two are controls. With the change the three pass, and the 
camel-kafka unit tests pass (1363 run, 0 failures, 1 skipped; run offline 
without the schema registry vendor serdes, which are not in my local 
repository, so `KafkaSchemaRegistryVendor*Test` were left out; CI runs them). 
The new class 20 times in one JVM: 60 run, 0 failures.
   
   # 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-27282](https://issues.apache.org/jira/browse/CAMEL-27282).
   
   # 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 the touched modules with 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