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

   # Description
   
   [CAMEL-27317](https://issues.apache.org/jira/browse/CAMEL-27317)
   
   The Sync, Async and CommitToOffset commit managers keep the last processed 
offset of each partition in an `OffsetCache`
   until it is committed. Three paths left an entry in the cache after the 
consumer no longer owned the partition, or
   dropped an entry too early:
   
   1. **A lost partition was committed.** `ClassicRebalanceListener` (and 
`ResumeRebalanceListener`) do not override
      `onPartitionsLost`, so Kafka's default calls `onPartitionsRevoked`, which 
commits the cached offset. A partition is
      lost when the consumer was dropped from the group (for example it 
exceeded `max.poll.interval.ms`): another member
      owns it already. The commit fails (the consumer is not in the current 
generation), and the entry stays in the
      cache. With an `offsetRepository` the "commit" is a write to the 
repository, which nothing refuses: the repository
      goes back under the new owner.
   2. **A revoke whose commit failed kept the entry.** `SyncCommitManager` 
removes the entry only after `commitSync`
      returned, `AsyncCommitManager` only in a successful callback. A commit 
that times out on a revoke leaves it.
   3. **An earlier commit dropped a newer entry.** The callback of an async 
commit runs in a later `poll()` or
      `commitAsync()`, and `OffsetCache.removeCommittedEntry` removed the entry 
whatever offset it held by then. With
      batching, the commit of a batch runs the callback of the previous one 
first: the entry of the new batch is removed,
      and if its own commit then fails, nothing is left to commit on the revoke 
(the records are read again).
   
   In 1 and 2 the stale entry is committed the next time the partition is 
revoked from this consumer: when the partition
   comes back after another member committed further, and is revoked again (or 
`breakOnFirstError` commits it) before a
   record of it updated the entry, the committed offset goes back to the old 
value and the other member's records are
   read again. The entry can be hours old.
   
   Change:
   
   - `CommitManager.discardRecordedOffset(partition)`, a new default method 
(no-op), forgets the recorded offset without
     committing it. The Sync, Async and CommitToOffset managers remove the 
cache entry (the Async manager also drops a
     manual commit that was requested and not done yet).
   - `ClassicRebalanceListener` and `ResumeRebalanceListener` override 
`onPartitionsLost`: they forget the offsets and do
     not commit. In `onPartitionsRevoked` the commits are unchanged, and the 
offsets of the revoked partitions are
     forgotten afterwards (in a `finally`), whether the commit succeeded or not.
   - `OffsetCache` removes an entry after a commit only if the committed offset 
covers it (cached offset < committed
     offset).
   
   What this does not change: what a commit manager records and what a revoke 
commits for a partition it still owns. With
   manual commits that is the open 
[CAMEL-26993](https://issues.apache.org/jira/browse/CAMEL-26993) (a revoke or a 
stop
   commits the last processed record, not the last one the route committed), 
which is a design decision (breakOnFirstError
   relies on the same cache). This change is about when an entry is removed, 
and works the same with either answer there.
   [CAMEL-27282](https://issues.apache.org/jira/browse/CAMEL-27282) (open PR) 
removes the entry of the CommitToOffset
   manager after a successful save; the forget after a revoke here covers the 
same case on the revoke path, and the two
   changes merge without conflict.
   
   The change is on `main`, independent of the batching fix of the same batch 
(`camel-kafka-batching-revoke-drops-buffered-records`, which changes 
`KafkaFetchRecords` and the batching processor, not these classes); the two 
merge without conflict, also with 
[#30496](https://github.com/apache/camel/pull/30496).
   
   Found with a TLA+ model. I adapted the model of the commit managers (two 
consumers, one partition, streaming with manual
   commits and breakOnFirstError, evictions, rebalances, failed commits, 
deferred async callbacks; sync and async) to the
   fix. On main it finds the committed offset moving back (sync with an 
eviction alone, async with all faults) and a newer
   entry dropped by an older callback. With the change these hold: sync with 
all faults 321,179 distinct states, async with
   all faults and 3 records 5,592,313 (everystate), and 693,271 with 2 records 
(TLC and everystate agree).
   
   Tests: new `CommitManagerRebalanceTest` (a `MockConsumer` that keeps the 
group offsets across rebalances, can refuse a
   commit, and runs async commit callbacks in a later `poll()` or 
`commitAsync()`, as the client does; the real
   `ClassicRebalanceListener`):
   
   - a lost partition (the consumer was evicted, its commits are refused), the 
partition comes back after another member
     committed 10, and is revoked again: the offset 10 must be kept, and 
nothing is committed for the lost partition (sync
     and async);
   - a revoke whose commit fails (timeout / failed async commit), then the 
same: the offset 10 must be kept (sync and
     async);
   - two async commits where the second fails after the callback of the first 
ran: the revoke must commit the second
     offset (async);
   - with an `offsetRepository`, a lost partition is not saved;
   - a revoke commits the recorded offset and nothing again later (control, 
passes on main).
   
   On main 6 of the 7 tests fail (the offset goes back from 10 to 5; the 
repository from 9 to 4; the revoke commits 5
   instead of 10). With the change the 7 pass, and the camel-kafka unit tests 
pass (1367 run, 0 failures, 1 skipped; run offline with kafka-clients 4.3.1, as 
4.4.0 is not in my local repository yet, and without the schema registry vendor 
serdes, so `KafkaSchemaRegistryVendor*Test` were left out; CI runs them). The 
new class 20 times
   in one JVM: 140 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-27317](https://issues.apache.org/jira/browse/CAMEL-27317).
   
   # 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