shashank created CAMEL-27419:
--------------------------------
Summary: camel-kafka - with batching, a resume keeps the records
held for an incomplete batch and the offset recorded before it, and the
committed offset can move back
Key: CAMEL-27419
URL: https://issues.apache.org/jira/browse/CAMEL-27419
Project: Camel
Issue Type: Bug
Components: camel-kafka
Reporter: shashank
Assignee: shashank
h3. What happens
With {{batching=true}} the consumer keeps the records of several polls in its
batch until it is full ({{maxPollRecords}}) or {{pollTimeoutMs}} has passed.
When the route is suspended and resumed, {{KafkaFetchRecords.updateTaskState}}
seeks each assigned partition that has a committed offset back to that offset,
and the consumer reads the records after it again. Two things from before the
resume are kept:
*1. The records the batch still holds.* They are read again after the seek, so
the batch holds them twice and processes them twice. The batch commits past
them, and then the records read again commit a lower offset.
*2. The offset in the OffsetCache.* With batching Camel commits each batch with
the {{AsyncCommitManager}}. When those commits fail, the cache keeps the last
recorded offset. After the resume, a poll that returns fewer records than a
batch is only buffered, but the facade still calls {{commit(partition)}} for
the polled partition and commits the stale offset. The batch of the records
read again then commits a lower offset.
No rebalance is needed for either.
h3. How to reproduce
{{maxPollRecords=2}}, committed offset 2. The batches [2, 3] and [4, 5] are
processed but their async commits fail, so the cache keeps 5, and record 6 is
held for the next batch. Suspend and resume the route: the consumer seeks to 2.
The next batch is [6, 2] and commits 7, then [3, 4] commits 5: committed
offsets 2, 7, 5, 7, record 6 processed twice (case 1). With only the held
records dropped, case 2 remains: the first poll returns only record 2 and the
facade commits 6, then the batch [2, 3] commits 4: committed offsets 2, 6, 4,
6, 8. Shown with a {{MockConsumer}} whose {{commitAsync}} reports a failure to
the callback.
Found with a TLA+ model of the batching consumer (one partition, async commits
whose callbacks run in later polls, pause/resume, rebalance, a failed route
with {{breakOnFirstError}}, stop).
h3. Fix
A partition the resume seeks back is in the same position as a revoked one:
everything after its committed offset is read again. {{updateTaskState}} now
calls {{commitManager.discardRecordedOffset}} (added by CAMEL-27317) for each
partition it seeks, and {{recordProcessorFacade.discardPendingRecords}} (added
by CAMEL-27316) for those partitions. Both are needed: each one alone fixes
only one of the two cases above. Partitions without a committed offset are not
seeked and keep their state. The records after the committed offset are read
and committed again; this relies on the committed offset being one that Camel
committed after processing, which is the case with batching (Kafka's
auto-commit is turned off for it). Builds on CAMEL-27316 and CAMEL-27317 (main
only).
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)