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

   # Description
   
   [CAMEL-27586](https://issues.apache.org/jira/browse/CAMEL-27586)
   
   With `allowManualCommit=true` and the 
`DefaultKafkaManualAsyncCommitFactory`, `KafkaManualCommit.commit()` only 
records the offset, and the consumer thread commits it before its next poll 
(`AsyncCommitManager`, CAMEL-26978). The documentation says that a commit 
called after the partition was revoked from the consumer is not made. Two cases 
break that promise, and a third one moves the offset repository back:
   
   1. A route that still holds a record from before a rebalance (aggregator, 
seda, threads) calls `commit()` after the partition was revoked and assigned 
back to the same consumer. With the eager protocol and the default Range 
assignor every rebalance revokes and assigns back, so this is common. The 
revoke removes the recorded offset once, but the later `put` survives, and 
`commitRequestedOffsets` only checks that the partition is assigned now. If 
another member committed up to 10 in between, the committed offset goes back to 
1.
   2. The same stale `commit()` replaces a `commit()` of the current assignment 
in the same poll interval (plain `put`): the commit of record 6 is never made, 
and offset 1 is committed instead.
   3. With `offsetRepository`, the callback of an async commit saves the offset 
in the repository that the `consumersCount` consumers of the endpoint share. 
The Kafka client runs the callback in a later poll of the committing consumer. 
If the partition moved to another consumer, which committed and saved a newer 
offset, the late callback overwrites it with the older one, and the next 
assignment seeks back to it (`OffsetPartitionAssignmentAdapter`).
   
   All three consume records again (duplicates). No record is skipped.
   
   Fix, in `AsyncCommitManager` and `DefaultKafkaManualAsyncCommit` (no public 
API change):
   
   - The commit manager numbers the assignments of each partition. The number 
changes in `discardRecordedOffset`, which the rebalance listeners already call 
when a partition is revoked or lost, and `KafkaFetchRecords` when a resume 
reads it again. A `DefaultKafkaManualAsyncCommit` keeps the number of the 
assignment in which its record was polled, and its `commit()` is recorded only 
if the partition is still in that assignment. The check runs inside 
`requestedOffsets.compute`, so a `commit()` racing with a revoke is either 
committed by the revoke or not made at all.
   - The commit callback saves the offset as before when the partition is still 
in the assignment in which the commit was sent. Otherwise (the partition was 
revoked since, for example the commit made by the revoke itself) it saves the 
offset only if it is newer than the saved one. The check and the write run 
under a lock on the repository object, which the consumers of the endpoint 
share. The current owner still always writes, so a repository that is ahead of 
the topic (for example after the topic was recreated) is still corrected by the 
next commit.
   
   - The component page says that a commit of a record polled before its 
partition was revoked (also when it is assigned to the consumer again) or 
before the route was suspended and resumed is not made, and that the consumer 
reads that record again. A route that filters the records it reads again (for 
example an idempotent consumer before the `commit()`) leaves the committed 
offset behind until its next commit.
   
   Not changed, and better as separate decisions: the repository can still lag 
the broker when the partition is assigned again before the callback of the 
revoke's commit ran, and a failed async commit is still only logged. The 
synchronous manual commit (`DefaultKafkaManualCommitFactory`) commits from the 
route thread at once and is not touched.
   
   Model: a TLA+ model of the async manual commit (one partition, revoke, lost, 
reassign to the same or another consumer, callbacks in a later poll, two 
consumers sharing the repository, up to 3 rebalances) finds the three sequences 
on main. Its fix variant (a commit of an earlier assignment is dropped, and the 
callback never moves the repository back) keeps "a commit requested in one 
assignment is never sent in another" and "the repository never moves back", 
with one and with two consumers. The code keeps the plain write for the 
consumer that still owns the partition in the same assignment; while the route 
commits in order, that is the same as the model's.
   
   Tests: `AsyncCommitManagerReassignTest`, with a kafka-clients `MockConsumer` 
whose commit callbacks run in the next poll (as with the Kafka client), an 
`AsyncCommitManager` and the `ClassicRebalanceListener`:
   
   - `testCommitOfEarlierAssignmentIsNotMadeAfterReassign` and 
`testCommitOfEarlierAssignmentDoesNotReplaceCurrentCommit` (cases 1 and 2);
   - `testLateCommitCallbackKeepsNewerOffsetInRepository` (case 3, two commit 
managers sharing a `MemoryStateRepository`);
   - `testCommitRacingWithRevokeIsNotMadeAfterReassign`: 500 rounds of a 
`commit()` from another thread while the partition is revoked and assigned back;
   - `testLateCommitCallbackSavesOffsetAfterRevoke`: control, the callback of 
the revoke's commit still saves the offset when nobody saved a newer one.
   
   On main the first four fail (committed offset 1 instead of 10, 1 instead of 
7, the repository holds 2 instead of 5, the race test fails in its first 
round); the control passes. With the change the five pass, and the camel-kafka 
unit tests pass (1412 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 
with `CommitManagerRebalanceTest` and `DefaultKafkaManualAsyncCommitTest` 20 
times in one JVM: 420 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-27586](https://issues.apache.org/jira/browse/CAMEL-27586).
   
   # 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 module 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