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 6 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
18 changes: 14 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 @@ -135,6 +135,7 @@ public void run() {
log.info("Finished resetting {}", this.sharedOptions.getConnectorName());
}


private void resetPartitions(final Iterable<byte[]> partitions, final Map<String, Object> kafkaConfig) {
try (final Producer<byte[], byte[]> producer = createProducer(kafkaConfig)) {
producer.initTransactions();
Expand Down Expand Up @@ -179,7 +180,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 = this.partitionsForOffsetTopic(consumer);
final List<TopicPartition> topicPartitions = partitions.stream()
.map(KafkaConnectorSourceResetter::toTopicPartition)
.collect(Collectors.toList());
Expand All @@ -188,4 +189,13 @@ private Consumer<byte[], byte[]> createConsumer(final Map<String, Object> kafkaC
return consumer;
}

private <K, V> List<PartitionInfo> partitionsForOffsetTopic(final Consumer<K, V> consumer) {
final Map<String, List<PartitionInfo>> topicsWithPartition = consumer.listTopics();
if (!topicsWithPartition.containsKey(this.offsetTopic)) {
final String message = String.format("Topic %s does not exist.", this.offsetTopic);
raminqaf marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException(message);
}
return topicsWithPartition.get(this.offsetTopic);
}

}
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