Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate offset topic #20

Merged
merged 7 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/main/java/com/bakdata/kafka/KafkaConnectorSourceResetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
* }</pre>
*
* Kafka Connect stores offsets for source connectors in a dedicated topic. The key of such an offset consists of the
* connector name and a connector specific partition name, e.g., {@code ["connector-name", { some-source-specific
* -data... }] }. This tool finds all partitions belonging to the connector that should be reset and deletes the
* corresponding offsets.
* connector name and a connector specific partition name, e.g.,
* {@code ["connector-name", { some-source-specific -data... }] }. This tool finds all partitions belonging to the
* connector that should be reset and deletes the corresponding offsets.
*/

@Slf4j
Expand Down Expand Up @@ -122,6 +122,18 @@ private static Converter createConverter(final Map<String, Object> kafkaConfig)
return converter;
}

private static <K, V> List<PartitionInfo> partitionsFor(final Consumer<K, V> consumer, final String topic) {
final Map<String, List<PartitionInfo>> topicsWithPartition = consumer.listTopics();
if (!topicsWithPartition.containsKey(topic)) {
final String message = String.format(
"Could not fetch partitions from the offset topic '%s'. Check if the offset topic name is set "
raminqaf marked this conversation as resolved.
Show resolved Hide resolved
+ "correctly.",
topic);
throw new IllegalArgumentException(message);
}
return topicsWithPartition.get(topic);
}

@Override
public void run() {
final String id = this.createId();
Expand Down Expand Up @@ -179,7 +191,7 @@ private Consumer<byte[], byte[]> createConsumer(final Map<String, Object> kafkaC
final Deserializer<byte[]> byteArrayDeserializer = new ByteArrayDeserializer();
final Consumer<byte[], byte[]> consumer =
new KafkaConsumer<>(kafkaConfig, byteArrayDeserializer, byteArrayDeserializer);
final List<PartitionInfo> partitions = consumer.partitionsFor(this.offsetTopic);
final List<PartitionInfo> partitions = partitionsFor(consumer, this.offsetTopic);
raminqaf marked this conversation as resolved.
Show resolved Hide resolved
final List<TopicPartition> topicPartitions = partitions.stream()
.map(KafkaConnectorSourceResetter::toTopicPartition)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,38 @@ void test() throws InterruptedException {
this.softly.assertThat(valuesAfterReset).hasSize(6);
}

@Test
void shouldExitOneWhenOffsetTopicIsSetIncorrectly() throws InterruptedException {
this.createConnectCluster();
delay(10, TimeUnit.SECONDS);
final List<String> values = this.kafkaCluster.readValues(ReadKeyValues.from(TOPIC)
.with(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class)
.with(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class)
.build());
this.softly.assertThat(values)
.hasSize(3);
this.connectCluster.stop();
delay(10, TimeUnit.SECONDS);
final KafkaConnectResetterApplication app = new KafkaConnectResetterApplication();

final CommandLine commandLine = getCLI(app);
final int exitCode = commandLine.execute("source",
CONNECTOR_NAME,
"--brokers", this.kafkaCluster.getBrokerList(),
"--offset-topic", "wrong-offset-topic"
);
this.softly.assertThat(exitCode)
.isEqualTo(1);
this.createConnectCluster();
delay(10, TimeUnit.SECONDS);
final List<String> valuesAfterReset = this.kafkaCluster.readValues(ReadKeyValues.from(TOPIC)
.with(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class)
.with(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class)
.build());
this.softly.assertThat(valuesAfterReset)
.hasSize(3);
}

private void createConnectCluster() {
this.connectCluster = new EmbeddedConnect(EmbeddedConnectConfig.kafkaConnect()
.with(DistributedConfig.OFFSET_STORAGE_TOPIC_CONFIG, OFFSETS)
Expand Down
Loading