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

   # Description
   
   [CAMEL-27316](https://issues.apache.org/jira/browse/CAMEL-27316)
   
   With `batching=true` the consumer keeps the records of several polls in its 
batch until the batch is full
   (`maxPollRecords`) or `pollTimeoutMs` has passed 
(`KafkaRecordBatchingProcessor`). When a rebalance revokes a partition
   while some of its records are held, nothing happens to them: 
`ClassicRebalanceListener.onPartitionsRevoked` only commits
   what is in the `OffsetCache`, and the held records are not processed yet. 
The batch is dispatched later anyway, and its
   `CommitSynchronization` commits the partition (`recordOffset` + 
`commit(partition)`), although the consumer no longer
   owns it:
   
   1. Consumer A polls records 0 to 2 of partition P and holds them for its 
batch.
   2. A rebalance gives P to consumer B. Nothing was committed for P, so B 
reads from 0, processes up to 9 and commits 10.
   3. A's batch is dispatched (it is full, or the poll timeout has passed): A 
processes records 0 to 2 again and commits 3.
      The committed offset of P goes back from 10 to 3, and the records 3 to 9 
are read again by the next owner.
   
   The group coordinator of the classic protocol checks the generation and 
member id of a commit, not which partitions the
   member owns, and the client does not check it either 
(`ConsumerCoordinator.sendOffsetCommitRequest` only takes the
   generation), so this commit is accepted. With an eager rebalance (the 
default assignors revoke every partition and assign
   them again) the same consumer can also get P back: it then reads records 0 
to 2 again from the committed offset and holds
   them twice in the same batch.
   
   [CAMEL-26978](https://issues.apache.org/jira/browse/CAMEL-26978) handled the 
same case for asynchronous manual commits:
   a commit requested after its partition was revoked is not committed. This 
change does it for the records of an
   incomplete batch:
   
   - `KafkaFetchRecords.PausePreservingRebalanceListener` (which wraps the 
classic and the resume rebalance listeners) asks
     the record processor to drop the records of the revoked (or lost) 
partitions before the listener commits.
   - `KafkaRecordProcessorFacade.discardPendingRecords` is a new default method 
(no-op: the streaming processor handles the
     records of a poll before the next poll). The batching facade removes the 
held exchanges of those partitions from the
     batch and releases them. The records of the other partitions stay in the 
batch.
   
   The next owner of a partition reads the dropped records from the committed 
offset, as it does today for every record
   after it, so nothing is lost. The Kafka client never committed them: with 
`batching=true` Camel turns the client's auto
   commit off (`KafkaConfiguration.isAutoCommitEnable()` is false for batching, 
also with the default
   `autoCommitEnable=true`) and commits each batch with the 
`AsyncCommitManager`, so the bug and the fix apply with the
   default options. Rebalance callbacks run on the consumer thread inside 
`poll()`, `unsubscribe()` and
   `close()`, never while a batch is being processed (checked with `javap` in 
kafka-clients 4.3.1 for both
   `ClassicKafkaConsumer` and `AsyncKafkaConsumer`: `commitSync` does not run 
them), so a batch that is dispatched only
   holds records of assigned partitions, and its commit does not need a second 
check.
   
   Found with a TLA+ model. I adapted the model of the batching consumer (one 
partition, consumer A with the
   `AsyncCommitManager`, the new owner B, the batch buffer across polls, async 
commits whose callbacks run in later polls,
   pause/resume, a failed route with `breakOnFirstError`, stop) to the fix. On 
main it finds A committing the partition after
   it was revoked, and B's committed offset going back. With the change neither 
happens (96,344 distinct states with
   pause/resume, 10,054 without; TLC and everystate agree).
   
   Tests: new `KafkaBatchingConsumerRevokeTest` (a `MockConsumer`, which, like 
the client, calls the rebalance listener
   inside `poll`, and accepts a commit for a partition that is not assigned):
   
   - partition 0 is revoked while 3 of its records are held, and the new owner 
commits 10: the offset 10 must be kept, and
     the next batch must only hold the records of partition 1;
   - partition 0 is revoked and assigned again (eager rebalance): the batch 
must hold each record once;
   - a rebalance that revokes nothing for the held records keeps them (control, 
passes on main).
   
   On main the first two fail (the committed offset goes back to 3; the batch 
holds `a0, a1, a2, a0, a1, a2, ...`). With
   the change the three pass, and the camel-kafka unit tests pass (1363 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:
   60 run, 0 failures.
   
   Not changed:
   
   - With a pause and a resume, the resume seeks to the committed offset while 
the batch can still hold later records; the
     model shows that after a failed async commit this can move the committed 
offset back without any rebalance. I will
     look at it separately.
   
   # 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-27316](https://issues.apache.org/jira/browse/CAMEL-27316).
   
   # 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