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 ce5a6ad46c89 CAMEL-26904: camel-kafka - KafkaIdempotentRepository
skips a record without a value or key in its topic (#29895)
ce5a6ad46c89 is described below
commit ce5a6ad46c89f7aa4cfa332ccf1071c8a6d7db3c
Author: allthingssecurity <[email protected]>
AuthorDate: Sat Nov 14 15:11:27 2026 +0530
CAMEL-26904: camel-kafka - KafkaIdempotentRepository skips a record without
a value or key in its topic (#29895)
KafkaIdempotentRepository takes the action of each record of its topic
from the record value. A record without a value (a tombstone, which the
repository never writes) threw a NullPointerException: at start the
repository failed to start, and in the background poll the records after
it in that poll were not applied. An add or remove record without a key
failed the same way.
Such records are now logged at WARN and skipped (a clear has no key, as
before). A tombstone is skipped, not applied as a remove, as
SingleNodeKafkaResumeStrategy does. Tests in KafkaIdempotentRepositoryTest.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../kafka/KafkaIdempotentRepository.java | 15 +++++++--
.../kafka/KafkaIdempotentRepositoryTest.java | 38 ++++++++++++++++++++++
2 files changed, 50 insertions(+), 3 deletions(-)
diff --git
a/components/camel-kafka/src/main/java/org/apache/camel/processor/idempotent/kafka/KafkaIdempotentRepository.java
b/components/camel-kafka/src/main/java/org/apache/camel/processor/idempotent/kafka/KafkaIdempotentRepository.java
index b4163ee69fb8..b35026fe0654 100644
---
a/components/camel-kafka/src/main/java/org/apache/camel/processor/idempotent/kafka/KafkaIdempotentRepository.java
+++
b/components/camel-kafka/src/main/java/org/apache/camel/processor/idempotent/kafka/KafkaIdempotentRepository.java
@@ -325,10 +325,19 @@ public class KafkaIdempotentRepository extends
ServiceSupport implements Idempot
private void addToCache(ConsumerRecord<String, String> consumerRecord) {
cacheCounter.incrementAndGet();
+ String value = consumerRecord.value();
+ String messageId = consumerRecord.key();
+ // a record without a value (a tombstone) or an add or remove without
a key is not written by this repository:
+ // skip it, so the records after it in the same poll are still
applied, and the start does not fail
+ if (value == null || (messageId == null &&
!CacheAction.clear.name().equals(value))) {
+ LOG.warn(
+ "Unexpected record with key:\"{}\" and value:\"{}\"
received on [topic:{}, partition:{}, offset:{}]. Ignoring.",
+ messageId, value, consumerRecord.topic(),
consumerRecord.partition(), consumerRecord.offset());
+ return;
+ }
CacheAction action;
try {
- action = CacheAction.valueOf(consumerRecord.value());
- String messageId = consumerRecord.key();
+ action = CacheAction.valueOf(value);
if (action == CacheAction.add) {
LOG.debug("Adding to cache messageId:{}", messageId);
cache.put(messageId, messageId);
@@ -343,7 +352,7 @@ public class KafkaIdempotentRepository extends
ServiceSupport implements Idempot
} catch (IllegalArgumentException iax) {
LOG.warn(
"Unexpected action value:\"{}\" received on [topic:{},
partition:{}, offset:{}]. Ignoring.",
- consumerRecord.key(), consumerRecord.topic(),
+ value, consumerRecord.topic(),
consumerRecord.partition(), consumerRecord.offset());
}
}
diff --git
a/components/camel-kafka/src/test/java/org/apache/camel/processor/idempotent/kafka/KafkaIdempotentRepositoryTest.java
b/components/camel-kafka/src/test/java/org/apache/camel/processor/idempotent/kafka/KafkaIdempotentRepositoryTest.java
index a275b340e597..ce907eca0062 100644
---
a/components/camel-kafka/src/test/java/org/apache/camel/processor/idempotent/kafka/KafkaIdempotentRepositoryTest.java
+++
b/components/camel-kafka/src/test/java/org/apache/camel/processor/idempotent/kafka/KafkaIdempotentRepositoryTest.java
@@ -181,6 +181,44 @@ public class KafkaIdempotentRepositoryTest {
assertTrue(repository.add("key1"));
}
+ @Test
+ void testTombstoneIsSkippedAndTheNextRecordsAreApplied() throws Exception {
+ cache.put("key1", "key1");
+
+ // a tombstone (an operator or a tool can write one) in the topic:
skipped, not applied as a remove
+ addToCache(new ConsumerRecord<>("test-topic", 0, 0L, "key1", null));
+ addToCache(new ConsumerRecord<>("test-topic", 0, 1L, "key2", "add"));
+
+ assertTrue(cache.containsKey("key1"));
+ assertTrue(cache.containsKey("key2"));
+ }
+
+ @Test
+ void testAddOrRemoveWithoutKeyIsSkipped() throws Exception {
+ cache.put("key1", "key1");
+
+ addToCache(new ConsumerRecord<>("test-topic", 0, 0L, null, "add"));
+ addToCache(new ConsumerRecord<>("test-topic", 0, 1L, null, "remove"));
+ addToCache(new ConsumerRecord<>("test-topic", 0, 2L, "key2", "add"));
+
+ assertEquals(Map.of("key1", "key1", "key2", "key2"), cache);
+ }
+
+ @Test
+ void testClearWithoutKeyIsApplied() throws Exception {
+ cache.put("key1", "key1");
+
+ addToCache(new ConsumerRecord<>("test-topic", 0, 0L, null, "clear"));
+
+ assertTrue(cache.isEmpty());
+ }
+
+ private void addToCache(ConsumerRecord<String, String> consumerRecord)
throws Exception {
+ Method addToCache =
KafkaIdempotentRepository.class.getDeclaredMethod("addToCache",
ConsumerRecord.class);
+ addToCache.setAccessible(true);
+ addToCache.invoke(repository, consumerRecord);
+ }
+
private static KafkaChangelog<String> changelog(MockProducer<String,
String> producer) {
KafkaChangelog<String> changelog = new KafkaChangelog<>(
"KafkaIdempotentRepository", "test-topic", new Properties(),
new Properties(), StringDeserializer.class,