This is an automated email from the ASF dual-hosted git repository.
david-streamlio pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-connectors.git
The following commit(s) were added to refs/heads/master by this push:
new 7814717d [fix][io] Fix loading offset issues in Kafka Adaptor (#31)
7814717d is described below
commit 7814717ddadee990d1eae7ddf80a580b3eca3324
Author: Jaroslav Kaspar <[email protected]>
AuthorDate: Thu Jul 9 19:52:20 2026 +0200
[fix][io] Fix loading offset issues in Kafka Adaptor (#31)
* [fix][io] Create new JSON converters for offset storage in Kafka Connect
Adaptor
Pulsar Kafka Connect adaptor previously used the same converters for both
data and offset storage. This coudl cause various issues. For example when data
were using AvroConverter, offsets were serialized using
MockSchemaRegistryClient (in memory only). After a connector restart, the fresh
MockSchemaRegistryClient had no schema records, causing deserialization to fail
with "Subject Not Found; error code: 40401" and the connector losing its offset
position.
Kafka Connect do not reuse the data converters for offset, but creates new
JSON converters configured with `schema.enable` set to `false`. Thus the
adaptor was changed to have the same behavior.
This is a breaking change for connectors that previously stored offsets in
a non-JSON format, those offsets do not have to be readable after upgrade. The
offsets probably weren't readable even before this fix (as we can see for the
Avro converter). But since we cannot besure what converters users used and how
they behaved, this change should be probably included in a major release.
Note: The tests for adaptor currently do not compile. We are waiting for
matching pulsar artifact to be publihsed. The fix was verified E2E by manual
testing.
Fixes #30
* Add unit test for offset converter round-trip across restart
- Extract createOffsetConverter(boolean) as a @VisibleForTesting seam
- Add KafkaConnectOffsetConverterTest proving offsets survive a restart
with the JSON offset converters, and documenting the pre-fix loss with
Avro data converters
- Re-enable compileTestJava/test for the module, excluding only the
broker-dependent test sources that need unpublished pulsar artifacts
* Add TODO for E2E offset converter test
* Update TODO comment to add rather than replace tests
---------
Co-authored-by: David Kjerrumgaard
<[email protected]>
---
kafka-connect-adaptor/build.gradle.kts | 19 ++-
.../kafka/connect/AbstractKafkaConnectSource.java | 28 ++++-
.../connect/KafkaConnectOffsetConverterTest.java | 138 +++++++++++++++++++++
3 files changed, 176 insertions(+), 9 deletions(-)
diff --git a/kafka-connect-adaptor/build.gradle.kts
b/kafka-connect-adaptor/build.gradle.kts
index 1349adb1..ca1b7474 100644
--- a/kafka-connect-adaptor/build.gradle.kts
+++ b/kafka-connect-adaptor/build.gradle.kts
@@ -62,8 +62,17 @@ dependencies {
testImplementation(libs.netty.reactive.streams)
}
-// KCA tests depend on pulsar-broker internals that have changed since the
-// last released version. Tests will compile once matching pulsar artifacts
-// are published. Skip for now to unblock CI.
-tasks.named("compileTestJava") { enabled = false }
-tasks.named("test") { enabled = false }
+// Some KCA tests depend on pulsar-broker internals that have changed since the
+// last released version. Those tests will compile once matching pulsar
artifacts
+// are published; exclude only them for now so that self-contained unit tests
+// still compile and run in CI.
+val brokerDependentTestSources = listOf(
+ "**/KafkaConnectSinkTest.java",
+ "**/KafkaConnectSourceErrRecTest.java",
+ "**/KafkaConnectSourceErrTest.java",
+ "**/KafkaConnectSourceTest.java",
+ "**/PulsarOffsetBackingStoreTest.java",
+)
+tasks.named<JavaCompile>("compileTestJava") {
+ exclude(brokerDependentTestSources)
+}
diff --git
a/kafka-connect-adaptor/src/main/java/org/apache/pulsar/io/kafka/connect/AbstractKafkaConnectSource.java
b/kafka-connect-adaptor/src/main/java/org/apache/pulsar/io/kafka/connect/AbstractKafkaConnectSource.java
index 47f691af..d00fb0a0 100644
---
a/kafka-connect-adaptor/src/main/java/org/apache/pulsar/io/kafka/connect/AbstractKafkaConnectSource.java
+++
b/kafka-connect-adaptor/src/main/java/org/apache/pulsar/io/kafka/connect/AbstractKafkaConnectSource.java
@@ -20,6 +20,7 @@ package org.apache.pulsar.io.kafka.connect;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
+import com.google.common.annotations.VisibleForTesting;
import io.confluent.connect.avro.AvroConverter;
import io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient;
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig;
@@ -38,6 +39,8 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.connect.connector.Task;
import org.apache.kafka.connect.errors.ConnectException;
+import org.apache.kafka.connect.json.JsonConverter;
+import org.apache.kafka.connect.json.JsonConverterConfig;
import org.apache.kafka.connect.runtime.TaskConfig;
import org.apache.kafka.connect.source.SourceConnector;
import org.apache.kafka.connect.source.SourceRecord;
@@ -114,6 +117,10 @@ public abstract class AbstractKafkaConnectSource<T>
implements Source<T> {
keyConverter.configure(config, true);
valueConverter.configure(config, false);
+ // initialize offset converters
+ Converter offsetKeyConverter = createOffsetConverter(true);
+ Converter offsetValueConverter = createOffsetConverter(false);
+
offsetStore = new
PulsarOffsetBackingStore(sourceContext.getPulsarClient());
PulsarKafkaWorkerConfig pulsarKafkaWorkerConfig = new
PulsarKafkaWorkerConfig(stringConfig);
offsetStore.configure(pulsarKafkaWorkerConfig);
@@ -122,14 +129,14 @@ public abstract class AbstractKafkaConnectSource<T>
implements Source<T> {
offsetReader = new OffsetStorageReaderImpl(
offsetStore,
"pulsar-kafka-connect-adaptor",
- keyConverter,
- valueConverter
+ offsetKeyConverter,
+ offsetValueConverter
);
offsetWriter = new OffsetStorageWriter(
offsetStore,
"pulsar-kafka-connect-adaptor",
- keyConverter,
- valueConverter
+ offsetKeyConverter,
+ offsetValueConverter
);
sourceTaskContext = new PulsarIOSourceTaskContext(offsetReader,
pulsarKafkaWorkerConfig);
@@ -165,6 +172,19 @@ public abstract class AbstractKafkaConnectSource<T>
implements Source<T> {
sourceTask.initialize(sourceTaskContext);
sourceTask.start(taskConfig);
}
+
+ /**
+ * Creates a converter for offset (de)serialization. Offsets are always
stored as schema-less
+ * JSON, independently of the converters configured for the data topics —
the same hardcoded
+ * behavior as Kafka Connect's internal converters (see KIP-174).
+ */
+ @VisibleForTesting
+ static Converter createOffsetConverter(boolean isKey) {
+ Converter converter = new JsonConverter();
+ converter.configure(Map.of(JsonConverterConfig.SCHEMAS_ENABLE_CONFIG,
false), isKey);
+ return converter;
+ }
+
private void onOffsetsFlushed(Throwable error, CompletableFuture<Void>
snapshotFlushFuture) {
if (error != null) {
log.error("Failed to flush offsets to storage: ", error);
diff --git
a/kafka-connect-adaptor/src/test/java/org/apache/pulsar/io/kafka/connect/KafkaConnectOffsetConverterTest.java
b/kafka-connect-adaptor/src/test/java/org/apache/pulsar/io/kafka/connect/KafkaConnectOffsetConverterTest.java
new file mode 100644
index 00000000..2e48ef87
--- /dev/null
+++
b/kafka-connect-adaptor/src/test/java/org/apache/pulsar/io/kafka/connect/KafkaConnectOffsetConverterTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.pulsar.io.kafka.connect;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+import io.confluent.connect.avro.AvroConverter;
+import io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient;
+import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import org.apache.kafka.connect.storage.Converter;
+import org.apache.kafka.connect.storage.MemoryOffsetBackingStore;
+import org.apache.kafka.connect.storage.OffsetStorageReaderImpl;
+import org.apache.kafka.connect.storage.OffsetStorageWriter;
+import org.testng.annotations.Test;
+
+/**
+ * Tests that connector offsets written before a restart can be read back
after the restart.
+ *
+ * <p>The adaptor previously serialized offsets with the connector's data
converters. With
+ * converters that keep out-of-band state (e.g. {@link AvroConverter} backed
by an in-memory
+ * {@link MockSchemaRegistryClient}), offsets became unreadable after a
restart and connectors
+ * silently lost their position. Offsets must instead use dedicated
schema-less JSON converters,
+ * matching Kafka Connect's internal converters.
+ *
+ * <p><b>TODO:</b> These tests verify the offset converters work in isolation
but don't test the
+ * actual production code path - that {@link AbstractKafkaConnectSource#open}
uses these JSON
+ * converters for offset storage. Once pulsar-broker test artifacts are
available, add a new
+ * E2E test that: (1) initializes the adaptor with AvroConverter for data
topics,
+ * (2) processes a message and stores an offset, (3) simulates a restart with
fresh converter
+ * instances, and (4) verifies the offset was loaded correctly. This would
prove the production
+ * path uses JSON converters for offsets regardless of data converter
configuration.
+ */
+public class KafkaConnectOffsetConverterTest {
+
+ private static final String NAMESPACE = "pulsar-kafka-connect-adaptor";
+ private static final Map<String, Object> PARTITION = Map.of("topic",
"test-topic", "partition", 0);
+ private static final Map<String, Object> OFFSET = Map.of("position", 42L);
+
+ /** Minimal in-memory store standing in for the offset topic that survives
a connector restart. */
+ private static class InMemoryOffsetBackingStore extends
MemoryOffsetBackingStore {
+ @Override
+ public Set<Map<String, Object>> connectorPartitions(String
connectorName) {
+ return Collections.emptySet();
+ }
+ }
+
+ private void writeOffset(MemoryOffsetBackingStore store, Converter
keyConverter, Converter valueConverter)
+ throws Exception {
+ OffsetStorageWriter writer = new OffsetStorageWriter(store, NAMESPACE,
keyConverter, valueConverter);
+ writer.offset(PARTITION, OFFSET);
+ assertTrue(writer.beginFlush(10, TimeUnit.SECONDS));
+ writer.doFlush((error, ignored) -> { }).get(10, TimeUnit.SECONDS);
+ }
+
+ @Test
+ public void testOffsetsSurviveRestartWithJsonOffsetConverters() throws
Exception {
+ MemoryOffsetBackingStore store = new InMemoryOffsetBackingStore();
+ store.start();
+ try {
+ writeOffset(store,
+ AbstractKafkaConnectSource.createOffsetConverter(true),
+ AbstractKafkaConnectSource.createOffsetConverter(false));
+
+ // simulate a connector restart: fresh converter instances, same
backing store
+ OffsetStorageReaderImpl reader = new
OffsetStorageReaderImpl(store, NAMESPACE,
+ AbstractKafkaConnectSource.createOffsetConverter(true),
+ AbstractKafkaConnectSource.createOffsetConverter(false));
+
+ Map<String, Object> restored = reader.offset(PARTITION);
+ assertNotNull(restored, "offset must be readable after a restart");
+ assertEquals(((Number) restored.get("position")).longValue(), 42L);
+ } finally {
+ store.stop();
+ }
+ }
+
+ /**
+ * Documents the failure mode this fix addresses: offsets written with the
Avro data
+ * converters (as the adaptor did before) are not readable after a
restart, because the
+ * fresh {@link MockSchemaRegistryClient} no longer holds the writer's
schemas.
+ */
+ @Test
+ public void testAvroDataConvertersLoseOffsetsAfterRestart() throws
Exception {
+ Map<String, Object> avroConfig =
+
Map.of(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "mock");
+
+ MemoryOffsetBackingStore store = new InMemoryOffsetBackingStore();
+ store.start();
+ try {
+ Converter oldKeyConverter = new AvroConverter(new
MockSchemaRegistryClient());
+ oldKeyConverter.configure(avroConfig, true);
+ Converter oldValueConverter = new AvroConverter(new
MockSchemaRegistryClient());
+ oldValueConverter.configure(avroConfig, false);
+ writeOffset(store, oldKeyConverter, oldValueConverter);
+
+ // simulate a connector restart: the new mock schema registries
are empty
+ Converter newKeyConverter = new AvroConverter(new
MockSchemaRegistryClient());
+ newKeyConverter.configure(avroConfig, true);
+ Converter newValueConverter = new AvroConverter(new
MockSchemaRegistryClient());
+ newValueConverter.configure(avroConfig, false);
+ OffsetStorageReaderImpl reader = new
OffsetStorageReaderImpl(store, NAMESPACE,
+ newKeyConverter, newValueConverter);
+
+ Map<String, Object> restored;
+ try {
+ restored = reader.offset(PARTITION);
+ } catch (Exception e) {
+ // reading failed outright — the offset is lost, which is the
bug
+ return;
+ }
+ assertNull(restored, "expected the offset written by the data
converters to be lost after restart");
+ } finally {
+ store.stop();
+ }
+ }
+}