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

   # Description
   
   [CAMEL-27419](https://issues.apache.org/jira/browse/CAMEL-27419)
   
   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 the route is suspended and
   resumed, `KafkaFetchRecords.updateTaskState` seeks every 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, and each of them
   can move the committed offset back without any rebalance:
   
   1. The records of the partition that the batch still holds. They are read 
again after the seek, so the batch holds them
      twice. Its commit covers the held records, and then the records read 
again commit a lower offset.
   2. The offset recorded in the `OffsetCache`. With `batching=true` Camel 
commits each batch with the
      `AsyncCommitManager`. When those async 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 that stale offset. The batch of the 
records read again then commits a lower one.
   
   Example with `maxPollRecords=2`, committed offset 2: the batches [2, 3] and 
[4, 5] are processed but their commits fail,
   so the cache keeps 5 while record 6 is held for the next batch. The route is 
suspended and resumed: the consumer seeks
   to 2. On main the next batch is [6, 2] and commits 7, then [3, 4] commits 5: 
the committed offsets are 2, 7, 5, 7, and
   record 6 is processed twice (case 1). If only the held records are dropped, 
case 2 remains: the first poll returns only
   record 2, the facade commits the cached offset (6), and then the batch [2, 
3] commits 4, so the committed offsets are
   2, 6, 4, 6, 8.
   
   [CAMEL-27316](https://issues.apache.org/jira/browse/CAMEL-27316) added
   `KafkaRecordProcessorFacade.discardPendingRecords` for revoked partitions, 
and
   [CAMEL-27317](https://issues.apache.org/jira/browse/CAMEL-27317) added 
`CommitManager.discardRecordedOffset` for revoked
   and lost partitions. A partition that the resume seeks back is in the same 
position: everything after its committed
   offset is read again. So `updateTaskState` now does both for each partition 
it seeks:
   
   - `commitManager.discardRecordedOffset(tp)` right after `consumer.seek(tp, 
committed)`;
   - `recordProcessorFacade.discardPendingRecords(seeked)` once the partitions 
are seeked.
   
   Both run on the consumer thread, between two polls, like the rest of 
`updateTaskState`. Partitions without a committed
   offset are not seeked, so their held records and recorded offset stay as 
they are. Nothing is lost: the records after
   the committed offset are read, processed and committed again, which the 
resume already did for every other record.
   This relies on the committed offset being one that Camel committed after 
processing, which is the case with batching
   (Camel turns off Kafka's auto-commit for it). If `enable.auto.commit=true` 
is forced back through
   `additionalProperties`, Kafka can commit past the held records, and the 
resume then seeks past them: they are dropped
   and not read again (before this change they were processed, but any 
rebalance or restart skipped them already).
   Both halves are needed: with only one of them, a test below still fails.
   
   With `batching=false` the streaming processor records the offset of each 
record it processes before the facade
   commits, so on the usual path a record read again overwrites the stale cache 
entry first. There the new
   `discardRecordedOffset` call only drops an offset that the seek has already 
made stale. With `allowManualCommit` and
   the async commit manager, a manual commit that was requested before the 
resume and not sent yet is dropped as well
   (`AsyncCommitManager.discardRecordedOffset` also forgets the requested 
offset, as for a lost partition; a revoke commits
   it first): its records are after the committed offset, so they are processed 
again and the route commits them again.
   With a route that commits on the consumer thread, that is a commit requested 
for a batch processed in the same poll
   loop iteration in which the resume is handled; a commit requested earlier is 
sent at the start of the next iteration,
   before the resume reads the committed offsets.
   
   Besides `KafkaFetchRecords`, only the javadoc of 
`CommitManager.discardRecordedOffset`,
   `KafkaRecordProcessorFacade.discardPendingRecords` and 
`KafkaRecordBatchingProcessor.discardBufferedRecords` changes;
   no option or public API changes.
   
   Found with a TLA+ model. I extended the model of the batching consumer that 
I used for CAMEL-27316 (one partition,
   the `AsyncCommitManager`, the batch buffer across polls, async commits whose 
callbacks run in later polls, pause/resume,
   a failed route with `breakOnFirstError`, a rebalance, stop) to current main, 
with CAMEL-27317 in it. On main the
   committed offset can go back after a resume. With only the buffer half or 
only the cache half it still can (each one
   gives one of the two cases above). With both, the committed offset never 
moves back and no record is lost (63,246
   distinct states with 3 offsets, 268,969 with 4; TLC and everystate agree on 
the 3-offset run).
   
   Tests: new `KafkaBatchingConsumerResumeTest` (a `MockConsumer` whose 
`commitAsync` can fail and reports the failure to
   the callback, as the client does in a later poll):
   
   - record 2 is held for the next batch when the route is suspended and 
resumed: each record must be processed once, and
     the committed offset ends at 4;
   - the commits of two batches fail, record 6 is held, and the route is 
suspended and resumed; then record 2 is read again
     alone (fewer than a batch, so it is only held), and then records 3 to 7: 
the committed offsets must be 2, 4, 6, 8,
     and the records read again must be processed in order, once.
   
   On main both fail. In the first test the batch after the resume is `[a2, 
a2]`. In the second the batches after the
   resume are `[a6, a2], [a3, a4], [a5, a6]` (record 6 twice) with the 
committed offsets `2, 7, 5, 7`, so the wait for
   offset 8 times out. With only `discardPendingRecords` the first test passes 
and the second fails with the committed
   offsets `[2, 6, 4, 6, 8]` (the commit after the poll that returns record 2 
alone commits the stale offset); with only
   `discardRecordedOffset` both still fail as on main. With the change both 
pass, and the camel-kafka unit tests pass
   (1376 run, 0 failures, 1 skipped, with kafka-clients 4.4.0; run offline 
without the schema registry vendor serdes, so
   `KafkaSchemaRegistryVendor*Test` were left out locally; CI runs them), also 
with kafka-clients 4.3.1. The new class
   20 times in one JVM: 40 run, 0 failures, with each of the two client 
versions.
   
   This builds on CAMEL-27316 and CAMEL-27317, which are only on main, so a 
backport needs those two first.
   
   # 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-27419](https://issues.apache.org/jira/browse/CAMEL-27419).
   
   # 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