This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 01c59983c01e CAMEL-27316: camel-kafka - with batching, a revoke drops
the records held for an incomplete batch of the revoked partitions (#30569)
01c59983c01e is described below
commit 01c59983c01e86b79d2ff3a82a4dd169beaeb79a
Author: allthingssecurity <[email protected]>
AuthorDate: Sun Nov 22 19:08:11 2026 +0530
CAMEL-27316: camel-kafka - with batching, a revoke drops the records held
for an incomplete batch of the revoked partitions (#30569)
With batching=true the consumer holds the records of several polls until
its batch is full or the poll timeout has
passed. A rebalance that revoked a partition left its held records in the
batch: the batch was dispatched later, and
its CommitSynchronization committed the revoked partition. The classic
group protocol accepts a commit of a partition
the member does not own, so the offset that the new owner committed went
back, and the new owner's records were read
again. With an eager rebalance that gave the partition back, the records
were also held twice in the same batch.
The rebalance listener wrapper of KafkaFetchRecords now asks the record
processor to drop the held records of the
revoked or lost partitions (new default method
KafkaRecordProcessorFacade.discardPendingRecords). The batching facade
removes and releases those exchanges; the next owner reads them from the
committed offset. Same idea as CAMEL-26978 for
asynchronous manual commits.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/component/kafka/KafkaFetchRecords.java | 11 +
.../support/KafkaRecordProcessorFacade.java | 14 ++
.../batching/KafkaRecordBatchingProcessor.java | 31 +++
.../KafkaRecordBatchingProcessorFacade.java | 6 +
.../kafka/KafkaBatchingConsumerRevokeTest.java | 224 +++++++++++++++++++++
5 files changed, 286 insertions(+)
diff --git
a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaFetchRecords.java
b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaFetchRecords.java
index 17f7e6d02f87..ecce1889b472 100644
---
a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaFetchRecords.java
+++
b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaFetchRecords.java
@@ -698,6 +698,7 @@ public class KafkaFetchRecords implements Runnable {
@Override
public void onPartitionsRevoked(Collection<TopicPartition> partitions)
{
+ discardPendingRecords(partitions);
delegate.onPartitionsRevoked(partitions);
}
@@ -718,7 +719,17 @@ public class KafkaFetchRecords implements Runnable {
@Override
public void onPartitionsLost(Collection<TopicPartition> partitions) {
+ discardPendingRecords(partitions);
delegate.onPartitionsLost(partitions);
}
+
+ private void discardPendingRecords(Collection<TopicPartition>
partitions) {
+ // the records of these partitions that are held for an incomplete
batch must not be processed and committed
+ // by this consumer: the new owner of a partition reads them again
from the committed offset
+ KafkaRecordProcessorFacade facade = recordProcessorFacade;
+ if (facade != null) {
+ facade.discardPendingRecords(partitions);
+ }
+ }
}
}
diff --git
a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/KafkaRecordProcessorFacade.java
b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/KafkaRecordProcessorFacade.java
index 89416f86c497..5d2aba6ac18d 100644
---
a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/KafkaRecordProcessorFacade.java
+++
b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/KafkaRecordProcessorFacade.java
@@ -16,7 +16,10 @@
*/
package org.apache.camel.component.kafka.consumer.support;
+import java.util.Collection;
+
import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.common.TopicPartition;
/**
* A processing facade that allows processing consumer records in different
ways
@@ -30,4 +33,15 @@ public interface KafkaRecordProcessorFacade {
* @return The result of processing this set of records
*/
ProcessingResult processPolledRecords(ConsumerRecords<Object, Object>
allRecords);
+
+ /**
+ * Drops the records of the given partitions that were polled but are not
processed yet, such as the records held
+ * for an incomplete batch. Called when the partitions are revoked or
lost: they must not be processed and committed
+ * by this consumer any more, and their next owner reads them again from
the committed offset.
+ *
+ * @param partitions the partitions that are no longer assigned to the
consumer
+ */
+ default void discardPendingRecords(Collection<TopicPartition> partitions) {
+ // NO-OP: by default the records of a poll are processed before the
next poll
+ }
}
diff --git
a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/batching/KafkaRecordBatchingProcessor.java
b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/batching/KafkaRecordBatchingProcessor.java
index bf48ef19caef..395605af4dd0 100644
---
a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/batching/KafkaRecordBatchingProcessor.java
+++
b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/batching/KafkaRecordBatchingProcessor.java
@@ -16,7 +16,9 @@
*/
package org.apache.camel.component.kafka.consumer.support.batching;
+import java.util.Collection;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
@@ -183,6 +185,35 @@ final class KafkaRecordBatchingProcessor extends
KafkaRecordProcessor {
return ProcessingResult.newUnprocessed();
}
+ /**
+ * Removes the records of the given partitions from the incomplete batch,
and releases their exchanges. Called when
+ * the partitions are revoked or lost (inside the poll, on the consumer
thread): the batch must not process them and
+ * commit their offsets, which could move back the offset that the new
owner of a partition committed. The new owner
+ * reads them again from the committed offset (also this consumer, if the
partition is assigned to it again).
+ */
+ void discardBufferedRecords(KafkaConsumer camelKafkaConsumer,
Collection<TopicPartition> partitions) {
+ if (exchangeList.isEmpty() || partitions.isEmpty()) {
+ return;
+ }
+ int discarded = 0;
+ Iterator<Exchange> it = exchangeList.iterator();
+ while (it.hasNext()) {
+ Exchange exchange = it.next();
+ Message message = exchange.getMessage();
+ String topic = message.getHeader(KafkaConstants.TOPIC,
String.class);
+ Integer partition = message.getHeader(KafkaConstants.PARTITION,
Integer.class);
+ if (topic != null && partition != null && partitions.contains(new
TopicPartition(topic, partition))) {
+ it.remove();
+ camelKafkaConsumer.releaseExchange(exchange, false);
+ discarded++;
+ }
+ }
+ if (discarded > 0) {
+ LOG.debug("Discarded {} records of the incomplete batch from the
revoked partitions {} ({} records left)",
+ discarded, partitions, exchangeList.size());
+ }
+ }
+
private boolean hasExpiredRecords(ConsumerRecords<Object, Object>
consumerRecords) {
// no records in batch
if (exchangeList.isEmpty()) {
diff --git
a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/batching/KafkaRecordBatchingProcessorFacade.java
b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/batching/KafkaRecordBatchingProcessorFacade.java
index fa26eff2466d..5e97d2e5d440 100644
---
a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/batching/KafkaRecordBatchingProcessorFacade.java
+++
b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/consumer/support/batching/KafkaRecordBatchingProcessorFacade.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.kafka.consumer.support.batching;
+import java.util.Collection;
import java.util.Set;
import org.apache.camel.component.kafka.KafkaConsumer;
@@ -62,4 +63,9 @@ public class KafkaRecordBatchingProcessorFacade extends
AbstractKafkaRecordProce
return result;
}
+ @Override
+ public void discardPendingRecords(Collection<TopicPartition> partitions) {
+ kafkaRecordProcessor.discardBufferedRecords(camelKafkaConsumer,
partitions);
+ }
+
}
diff --git
a/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaBatchingConsumerRevokeTest.java
b/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaBatchingConsumerRevokeTest.java
new file mode 100644
index 000000000000..3cfdba4a5a45
--- /dev/null
+++
b/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaBatchingConsumerRevokeTest.java
@@ -0,0 +1,224 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.kafka;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.LockSupport;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.kafka.clients.consumer.Consumer;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.clients.consumer.MockConsumer;
+import org.apache.kafka.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.clients.consumer.OffsetCommitCallback;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.common.TopicPartition;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * A batching consumer holds the records of earlier polls until its batch is
complete. When a partition is revoked while
+ * its records are held, the batch must not process them and commit their
offsets: the new owner of the partition reads
+ * them from the committed offset, and the commit of the old owner would move
back the offset that the new owner
+ * committed. Uses a {@link MockConsumer} instead of a broker: like the Kafka
client, it calls the rebalance listener
+ * inside poll, and it accepts a commit for a partition that is not assigned
(the group coordinator checks the
+ * generation of the member, not which partitions it owns).
+ */
+class KafkaBatchingConsumerRevokeTest extends CamelTestSupport {
+
+ private static final String TOPIC = "batching-revoke";
+ private static final TopicPartition PARTITION_0 = new
TopicPartition(TOPIC, 0);
+ private static final TopicPartition PARTITION_1 = new
TopicPartition(TOPIC, 1);
+
+ private final SlowPollMockConsumer mockConsumer = new
SlowPollMockConsumer();
+ private final List<List<String>> batches = new CopyOnWriteArrayList<>();
+
+ @BindToRegistry("mockFactory")
+ private final KafkaClientFactory factory = new KafkaClientFactory() {
+ @Override
+ public Producer getProducer(Properties kafkaProps) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Consumer getConsumer(Properties kafkaProps) {
+ return mockConsumer;
+ }
+
+ @Override
+ public String getBrokers(KafkaConfiguration configuration) {
+ return "localhost:9092";
+ }
+ };
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ // a batch of 10 records, committed by Camel; the poll timeout
is long enough that an incomplete batch
+ // is never dispatched during the test
+ from("kafka:" + TOPIC +
"?brokers=localhost:9092&kafkaClientFactory=#mockFactory&groupId=test"
+ +
"&batching=true&maxPollRecords=10&pollTimeoutMs=60000&autoCommitEnable=false")
+ .routeId("kafka")
+ .process(exchange -> {
+ List<String> batch = new ArrayList<>();
+ for (Object record :
exchange.getMessage().getBody(List.class)) {
+ batch.add(((Exchange)
record).getMessage().getBody(String.class));
+ }
+ batches.add(batch);
+ });
+ }
+ };
+ }
+
+ @Override
+ protected void setupResources() {
+ mockConsumer.updateBeginningOffsets(Map.of(PARTITION_0, 0L,
PARTITION_1, 0L));
+ mockConsumer.schedulePollTask(() ->
mockConsumer.rebalance(List.of(PARTITION_0, PARTITION_1)));
+ }
+
+ @Test
+ void testRevokedPartitionOfIncompleteBatchIsNotCommitted() {
+ addRecords(PARTITION_0, "a", 0, 3);
+ // the three records are polled and held for the batch of 10
+ await().atMost(20, TimeUnit.SECONDS).until(() ->
mockConsumer.position(PARTITION_0) == 3);
+
+ // a rebalance in the next poll gives partition 0 to another consumer,
which reads it from the start and
+ // commits offset 10
+ AtomicBoolean revoked = new AtomicBoolean();
+ mockConsumer.schedulePollTask(() -> {
+ mockConsumer.rebalance(List.of(PARTITION_1));
+ mockConsumer.commitSync(Map.of(PARTITION_0, new
OffsetAndMetadata(10L)));
+ revoked.set(true);
+ });
+ await().atMost(20, TimeUnit.SECONDS).untilTrue(revoked);
+
+ addRecords(PARTITION_1, "b", 0, 10);
+ await().atMost(20, TimeUnit.SECONDS).until(() ->
committedOffset(PARTITION_1) != null);
+
+ assertEquals(10L, committedOffset(PARTITION_0), "the offset committed
by the new owner must be kept");
+ assertEquals(List.of(List.of("b0", "b1", "b2", "b3", "b4", "b5", "b6",
"b7", "b8", "b9")), batches,
+ "the batch must not contain the records of the revoked
partition");
+ assertEquals(10L, committedOffset(PARTITION_1));
+ }
+
+ @Test
+ void testPartitionRevokedAndAssignedAgainIsNotInTheBatchTwice() {
+ addRecords(PARTITION_0, "a", 0, 3);
+ await().atMost(20, TimeUnit.SECONDS).until(() ->
mockConsumer.position(PARTITION_0) == 3);
+
+ // an eager rebalance revokes every partition and assigns them again:
the consumer reads partition 0 again from
+ // the committed offset (none, so from the beginning)
+ AtomicBoolean reassigned = new AtomicBoolean();
+ mockConsumer.schedulePollTask(() -> {
+ mockConsumer.rebalance(List.of());
+ mockConsumer.rebalance(List.of(PARTITION_0, PARTITION_1));
+ // the mock forgets the records it had on a rebalance: these are
the records the broker still has
+ addAssignedRecords(PARTITION_0, "a", 0, 3);
+ reassigned.set(true);
+ });
+ await().atMost(20, TimeUnit.SECONDS).untilTrue(reassigned);
+
+ addRecords(PARTITION_0, "a", 3, 7);
+ await().atMost(20, TimeUnit.SECONDS).until(() -> !batches.isEmpty());
+
+ assertEquals(List.of("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
"a8", "a9"), batches.get(0),
+ "the batch must hold each record once");
+ assertEquals(10L, committedOffset(PARTITION_0));
+ }
+
+ @Test
+ void testRebalanceThatRevokesNothingKeepsTheIncompleteBatch() {
+ addRecords(PARTITION_0, "a", 0, 3);
+ await().atMost(20, TimeUnit.SECONDS).until(() ->
mockConsumer.position(PARTITION_0) == 3);
+
+ // partition 1 is revoked, partition 0 stays assigned
+ AtomicBoolean rebalanced = new AtomicBoolean();
+ mockConsumer.schedulePollTask(() -> {
+ mockConsumer.rebalance(List.of(PARTITION_0));
+ rebalanced.set(true);
+ });
+ await().atMost(20, TimeUnit.SECONDS).untilTrue(rebalanced);
+
+ addRecords(PARTITION_0, "a", 3, 7);
+ await().atMost(20, TimeUnit.SECONDS).until(() -> !batches.isEmpty());
+
+ assertEquals(List.of("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
"a8", "a9"), batches.get(0));
+ assertEquals(10L, committedOffset(PARTITION_0));
+ }
+
+ private void addRecords(TopicPartition partition, String prefix, long
firstOffset, int count) {
+ await().atMost(20, TimeUnit.SECONDS).until(() ->
mockConsumer.assignment().contains(partition));
+ addAssignedRecords(partition, prefix, firstOffset, count);
+ }
+
+ private void addAssignedRecords(TopicPartition partition, String prefix,
long firstOffset, int count) {
+ for (int i = 0; i < count; i++) {
+ long offset = firstOffset + i;
+ mockConsumer.addRecord(new ConsumerRecord<>(TOPIC,
partition.partition(), offset, null, prefix + offset));
+ }
+ }
+
+ private Long committedOffset(TopicPartition partition) {
+ return mockConsumer.groupOffsets.get(partition);
+ }
+
+ private static final class SlowPollMockConsumer extends
MockConsumer<Object, Object> {
+ // the offsets committed for the group: MockConsumer.committed returns
them only for the assigned partitions
+ final Map<TopicPartition, Long> groupOffsets = new
ConcurrentHashMap<>();
+
+ SlowPollMockConsumer() {
+ super("earliest");
+ }
+
+ @Override
+ public synchronized void commitAsync(Map<TopicPartition,
OffsetAndMetadata> offsets, OffsetCommitCallback callback) {
+ offsets.forEach((partition, offset) -> groupOffsets.put(partition,
offset.offset()));
+ super.commitAsync(offsets, callback);
+ }
+
+ @Override
+ public synchronized ConsumerRecords<Object, Object> poll(Duration
timeout) {
+ ConsumerRecords<Object, Object> records = super.poll(timeout);
+ if (records.isEmpty()) {
+ // a real consumer waits up to the poll timeout when there is
nothing to fetch
+ LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10));
+ }
+ return records;
+ }
+
+ @Override
+ public synchronized void close() {
+ // keep the mock usable: a stopped route closes the consumer
+ }
+ }
+}