shashank created CAMEL-27586:
--------------------------------
Summary: camel-kafka - asynchronous manual commit: a commit() of a
record polled before a rebalance is made after the partition is assigned back,
and a late commit callback moves the offset repository back
Key: CAMEL-27586
URL: https://issues.apache.org/jira/browse/CAMEL-27586
Project: Camel
Issue Type: Bug
Components: camel-kafka
Reporter: shashank
Assignee: shashank
Fix For: 4.24.0
h3. What happens
With {{allowManualCommit=true}} and the
{{DefaultKafkaManualAsyncCommitFactory}}, {{KafkaManualCommit.commit()}} only
records the offset, and the consumer thread commits it before its next poll
({{AsyncCommitManager}}). The documentation says: "A commit called after the
partition was revoked from the consumer, for example in a rebalance, is not
made." Two cases break this, and a third one in the commit callback moves the
offset repository back.
*1. A commit from an earlier assignment, after the partition is assigned back.*
The revoke removes the recorded offset once ({{discardRecordedOffset}}). A
{{commit()}} that comes after it puts the offset again, and
{{commitRequestedOffsets}} only checks that the partition is assigned now. So
when the partition comes back to the same consumer (with the eager protocol and
the default Range assignor, every rebalance revokes and assigns back), a route
that still held a record from before (an aggregator, seda, threads) commits it
in the new assignment:
* the partition is revoked while the route holds record 0, another member
commits up to offset 9, the partition comes back, then the route commits record
0: the committed offset goes back from 10 to 1;
* the stale {{commit()}} also replaces a {{commit()}} of the current assignment
made in the same poll interval (a plain {{put}}): the commit of record 6 is
never made, and offset 1 is committed instead.
*2. A late commit callback writes an older offset to the shared offset
repository.* With {{offsetRepository}}, the callback of each successful async
commit saves the offset in the repository, which all {{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 in
between, and that consumer committed and saved a newer offset, the late
callback overwrites it with the older one. The next assignment of the partition
seeks to the repository value + 1 ({{OffsetPartitionAssignmentAdapter}}) and
consumes the records in between again.
In both cases records are consumed again (duplicates); no record is skipped.
h3. How to reproduce
With a kafka-clients {{MockConsumer}} whose async commit callbacks run in the
next poll, an {{AsyncCommitManager}} and the {{ClassicRebalanceListener}}:
* assign, create the manual commit of record 0, revoke, set the group offset to
10, assign again, {{commit()}}: offset 1 is committed;
* two commit managers sharing a {{MemoryStateRepository}}: the first commits
offset 2 (callback pending) and loses the partition, the second commits offset
5 and saves 5, then the first polls: the repository holds 2.
Found with a TLA+ model of the async manual commit (revoke, lost, reassign,
delayed callbacks, two consumers).
h3. Fix
* The commit manager numbers the assignments of each partition: the number
changes when the partition is revoked, lost or read again (the places that
already call {{discardRecordedOffset}}). The {{DefaultKafkaManualAsyncCommit}}
keeps the number of the assignment in which its record was polled, and its
{{commit()}} is only recorded if the partition is still in that assignment. So
a commit after a revoke is not made, also when the partition is assigned to the
consumer again, as the documentation says.
* The commit callback saves the offset in the repository as before when the
partition is still in the assignment in which the commit was sent. Otherwise
(the partition was revoked since) it saves it only if it is newer than the
saved offset, under a lock on the repository, so a newer offset saved by the
consumer that owns the partition now is kept.
Not changed: the repository can still lag the broker when the partition is
assigned before the revoke's commit callback ran (the seek uses the older saved
value), and a failed async commit is still only logged. Both need a separate
decision.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)