-
Notifications
You must be signed in to change notification settings - Fork 109
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
Auto-create topic with N partitions #879
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
36c2fe7
Introduce `KafkaNewTopics`, which creates Kafka topics via `AdminClient`
guillermocalvo c80de94
Add tests
guillermocalvo a5a5997
Add documentation and code snippets
guillermocalvo 5ce3b17
Update src/main/docs/guide/kafkaApplications/kafkaNewTopics.adoc
guillermocalvo 6c93cde
Update kafka/src/main/java/io/micronaut/configuration/kafka/admin/Kaf…
guillermocalvo 6de05c6
Apply suggestions from code review
guillermocalvo 30cf856
Update kafka/src/main/java/io/micronaut/configuration/kafka/admin/Kaf…
guillermocalvo f48488e
Update kafka/src/test/groovy/io/micronaut/configuration/kafka/admin/K…
guillermocalvo 3c47081
Update KafkaNewTopics
guillermocalvo d023185
Remove unused import
guillermocalvo a380095
Make `KafkaNewTopics` depend directly on `AdminClient` and `NewTopic`
guillermocalvo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
kafka/src/main/java/io/micronaut/configuration/kafka/admin/KafkaNewTopics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2017-2020 original authors | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 io.micronaut.configuration.kafka.admin; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.context.annotation.Context; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.core.annotation.Experimental; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import org.apache.kafka.clients.admin.AdminClient; | ||
import org.apache.kafka.clients.admin.CreateTopicsOptions; | ||
import org.apache.kafka.clients.admin.CreateTopicsResult; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static java.util.function.Predicate.not; | ||
|
||
/** | ||
* Creates Kafka topics via {@link AdminClient}. | ||
* | ||
* @author Guillermo Calvo | ||
* @since 5.2 | ||
*/ | ||
@Context | ||
@Requires(bean = AdminClient.class) | ||
public class KafkaNewTopics { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(KafkaNewTopics.class); | ||
|
||
@Nullable | ||
private final CreateTopicsOptions options; | ||
|
||
@Nullable | ||
private final CreateTopicsResult result; | ||
|
||
/** | ||
* @param context The application context. | ||
* @param options Optional {@link CreateTopicsOptions}. | ||
* @param topics Optional list of {@link NewTopic} beans. | ||
*/ | ||
KafkaNewTopics( | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@NonNull ApplicationContext context, | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Nullable CreateTopicsOptions options, | ||
@Nullable List<NewTopic> topics | ||
) { | ||
this.options = options; | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.result = Optional.ofNullable(topics).filter(not(List::isEmpty)) | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.map(t -> createNewTopics(context, t)) | ||
.orElse(null); | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @return An optional {@link CreateTopicsOptions} that was used if new topics were created. | ||
*/ | ||
@Experimental | ||
public Optional<CreateTopicsOptions> getOptions() { | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Optional.ofNullable(options); | ||
} | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @return An optional {@link CreateTopicsResult} if new topics were created. | ||
*/ | ||
@Experimental | ||
public Optional<CreateTopicsResult> getResult() { | ||
return Optional.ofNullable(result); | ||
} | ||
|
||
private CreateTopicsResult createNewTopics(@NonNull ApplicationContext context, @NonNull List<NewTopic> topics) { | ||
LOG.info("Creating new topics: {}", topics); | ||
final AdminClient adminClient = context.getBean(AdminClient.class); | ||
return adminClient.createTopics(topics, getOptions().orElseGet(CreateTopicsOptions::new)); | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
kafka/src/test/groovy/io/micronaut/configuration/kafka/admin/KafkaNewTopicsSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.micronaut.configuration.kafka.admin | ||
|
||
import io.micronaut.configuration.kafka.AbstractKafkaContainerSpec | ||
import io.micronaut.context.annotation.Bean | ||
import io.micronaut.context.annotation.Factory | ||
import io.micronaut.context.annotation.Requires | ||
import org.apache.kafka.clients.admin.CreateTopicsOptions | ||
import org.apache.kafka.clients.admin.NewTopic | ||
import spock.util.concurrent.PollingConditions | ||
|
||
class KafkaNewTopicsSpec extends AbstractKafkaContainerSpec { | ||
|
||
final static TOPIC_1 = 'new-topic-1' | ||
final static TOPIC_2 = 'new-topic-2' | ||
final static TOPIC_3 = '$illegal/topic:name!' | ||
|
||
void "create kafka topics"() { | ||
given: | ||
final KafkaNewTopics kafkaNewTopics = context.getBean(KafkaNewTopics) | ||
|
||
expect: | ||
kafkaNewTopics.options.isPresent() | ||
kafkaNewTopics.result.isPresent() | ||
|
||
and: | ||
final options = kafkaNewTopics.options.get() | ||
options.timeoutMs == 5000 | ||
options.validateOnly == true | ||
options.retryOnQuotaViolation == false | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
and: | ||
final result = kafkaNewTopics.result.get() | ||
new PollingConditions(timeout: 10).eventually { | ||
result.all().done == true | ||
} | ||
result.numPartitions(TOPIC_1).get() == 1 | ||
result.numPartitions(TOPIC_2).get() == 2 | ||
result.values()[TOPIC_3].completedExceptionally == true | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'KafkaNewTopicsSpec') | ||
@Factory | ||
static class MyTopicFactory { | ||
|
||
@Bean | ||
CreateTopicsOptions options() { new CreateTopicsOptions().timeoutMs(5000).validateOnly(true).retryOnQuotaViolation(false) } | ||
|
||
@Bean | ||
NewTopic topic1() { new NewTopic(TOPIC_1, Optional.of(1), Optional.empty()) } | ||
|
||
@Bean | ||
NewTopic topic2() { new NewTopic(TOPIC_2, Optional.of(2), Optional.empty()) } | ||
|
||
@Bean | ||
NewTopic topic3() { new NewTopic(TOPIC_3, Optional.of(4), Optional.empty()) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
You can automatically add topics to the broker when your application starts. To do so, you can add a link:{kafkaapi}/org/apache/kafka/clients/admin/NewTopic[`NewTopic`] bean for each topic you want to create. `NewTopic` instances let you specify the name, the number of partitions, the replication factor, the replicas assignments and the configuration properties you want to associate with the new topic. Additionally, you can add one link:{kafkaapi}/org/apache/kafka/clients/admin/CreateTopicsOptions[`CreateTopicsOptions`] bean that will be used when the new topics are created. | ||
guillermocalvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.Creating New Kafka Topics with Options | ||
|
||
snippet::io.micronaut.kafka.docs.admin.MyTopicFactory[tags = 'imports,clazz'] | ||
|
||
NOTE: Creating topics is not a transactional operation, so it may succeed for some topics while fail for others. This operation also executes asynchronously, so it may take several seconds until all the brokers become aware that the topics have been created. | ||
|
||
If you ever need to check if the operation has completed, you can `@Inject` or retrieve the api:io.micronaut.configuration.kafka.admin.KafkaNewTopics[] bean from the application context and then retrieve the link:{kafkaapi}/org/apache/kafka/clients/admin/CreateTopicsResult[operation result] that Kafka returned when the topics were created. | ||
|
||
.Checking if Topic Creation is Done | ||
|
||
snippet::io.micronaut.kafka.docs.admin.MyTopicFactoryTest[tags = 'result'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
test-suite-groovy/src/test/groovy/io/micronaut/kafka/docs/admin/MyTopicFactory.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.micronaut.kafka.docs.admin | ||
|
||
// tag::imports[] | ||
import io.micronaut.context.annotation.Bean | ||
import io.micronaut.context.annotation.Factory | ||
import io.micronaut.context.annotation.Requires | ||
import org.apache.kafka.clients.admin.AdminClient | ||
import org.apache.kafka.clients.admin.CreateTopicsOptions | ||
import org.apache.kafka.clients.admin.NewTopic | ||
// end::imports[] | ||
|
||
@Requires(property = "spec.name", value = "MyTopicFactoryTest") | ||
// tag::clazz[] | ||
@Requires(bean = AdminClient) | ||
@Factory | ||
class MyTopicFactory { | ||
|
||
@Bean | ||
CreateTopicsOptions options() { | ||
new CreateTopicsOptions().timeoutMs(5000).validateOnly(true).retryOnQuotaViolation(false) | ||
} | ||
|
||
@Bean | ||
NewTopic topic1() { | ||
new NewTopic("my-new-topic-1", 1, (short) 1) | ||
} | ||
|
||
@Bean | ||
NewTopic topic2() { | ||
new NewTopic("my-new-topic-2", 2, (short) 1) | ||
} | ||
} | ||
// end::clazz[] |
32 changes: 32 additions & 0 deletions
32
test-suite-groovy/src/test/groovy/io/micronaut/kafka/docs/admin/MyTopicFactoryTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.micronaut.kafka.docs.admin | ||
|
||
import io.micronaut.configuration.kafka.admin.KafkaNewTopics | ||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import spock.lang.Specification | ||
import spock.util.concurrent.PollingConditions | ||
|
||
@MicronautTest | ||
@Property(name = "spec.name", value = "MyTopicFactoryTest") | ||
class MyTopicFactoryTest extends Specification { | ||
|
||
@Inject | ||
KafkaNewTopics newTopics | ||
|
||
void "test new topics"() { | ||
expect: | ||
new PollingConditions(timeout: 5).eventually { | ||
areNewTopicsDone(newTopics) | ||
final result = newTopics.getResult().orElseThrow() | ||
result.numPartitions("my-new-topic-1").get() == 1 | ||
result.numPartitions("my-new-topic-2").get() == 2 | ||
} | ||
} | ||
|
||
// tag::result[] | ||
boolean areNewTopicsDone(KafkaNewTopics newTopics) { | ||
newTopics.result.map { it.all().isDone() }.orElse(false) | ||
} | ||
// end::result[] | ||
} |
33 changes: 33 additions & 0 deletions
33
test-suite-kotlin/src/test/kotlin/io/micronaut/kafka/docs/admin/MyTopicFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.micronaut.kafka.docs.admin | ||
|
||
// tag::imports[] | ||
import io.micronaut.context.annotation.Bean | ||
import io.micronaut.context.annotation.Factory | ||
import io.micronaut.context.annotation.Requires | ||
import org.apache.kafka.clients.admin.AdminClient | ||
import org.apache.kafka.clients.admin.CreateTopicsOptions | ||
import org.apache.kafka.clients.admin.NewTopic | ||
// end::imports[] | ||
|
||
@Requires(property = "spec.name", value = "MyTopicFactoryTest") | ||
// tag::clazz[] | ||
@Requires(bean = AdminClient::class) | ||
@Factory | ||
class MyTopicFactory { | ||
|
||
@Bean | ||
fun options(): CreateTopicsOptions { | ||
return CreateTopicsOptions().timeoutMs(5000).validateOnly(true).retryOnQuotaViolation(false) | ||
} | ||
|
||
@Bean | ||
fun topic1(): NewTopic { | ||
return NewTopic("my-new-topic-1", 1, 1) | ||
} | ||
|
||
@Bean | ||
fun topic2(): NewTopic { | ||
return NewTopic("my-new-topic-2", 2, 1) | ||
} | ||
} | ||
// end::clazz[] |
35 changes: 35 additions & 0 deletions
35
test-suite-kotlin/src/test/kotlin/io/micronaut/kafka/docs/admin/MyTopicFactoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.micronaut.kafka.docs.admin | ||
|
||
import io.micronaut.configuration.kafka.admin.KafkaNewTopics | ||
import io.micronaut.context.ApplicationContext | ||
import org.awaitility.Awaitility | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import java.util.concurrent.ExecutionException | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal class MyTopicFactoryTest { | ||
|
||
@Test | ||
@Throws(ExecutionException::class, InterruptedException::class) | ||
fun testNewTopics() { | ||
ApplicationContext.run( | ||
mapOf( | ||
"kafka.enabled" to "true", | ||
"spec.name" to "MyTopicFactoryTest" | ||
) | ||
).use { ctx -> | ||
val newTopics = ctx.getBean(KafkaNewTopics::class.java) | ||
Awaitility.await().atMost(5, TimeUnit.SECONDS).until { areNewTopicsDone(newTopics) } | ||
val result = newTopics.result.orElseThrow() | ||
assertEquals(1, result.numPartitions("my-new-topic-1").get()) | ||
assertEquals(2, result.numPartitions("my-new-topic-2").get()) | ||
} | ||
} | ||
|
||
// tag::result[] | ||
fun areNewTopicsDone(newTopics: KafkaNewTopics): Boolean { | ||
return newTopics.result.map { it.all().isDone }.orElse(false) | ||
} | ||
// end::result[] | ||
} |
33 changes: 33 additions & 0 deletions
33
test-suite/src/test/java/io/micronaut/kafka/docs/admin/MyTopicFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.micronaut.kafka.docs.admin; | ||
|
||
// tag::imports[] | ||
import io.micronaut.context.annotation.Bean; | ||
import io.micronaut.context.annotation.Factory; | ||
import io.micronaut.context.annotation.Requires; | ||
import org.apache.kafka.clients.admin.AdminClient; | ||
import org.apache.kafka.clients.admin.CreateTopicsOptions; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
// end::imports[] | ||
|
||
@Requires(property = "spec.name", value = "MyTopicFactoryTest") | ||
// tag::clazz[] | ||
@Requires(bean = AdminClient.class) | ||
@Factory | ||
public class MyTopicFactory { | ||
|
||
@Bean | ||
CreateTopicsOptions options() { | ||
return new CreateTopicsOptions().timeoutMs(5000).validateOnly(true).retryOnQuotaViolation(false); | ||
} | ||
|
||
@Bean | ||
NewTopic topic1() { | ||
return new NewTopic("my-new-topic-1", 1, (short) 1); | ||
} | ||
|
||
@Bean | ||
NewTopic topic2() { | ||
return new NewTopic("my-new-topic-2", 2, (short) 1); | ||
} | ||
} | ||
// end::clazz[] |
36 changes: 36 additions & 0 deletions
36
test-suite/src/test/java/io/micronaut/kafka/docs/admin/MyTopicFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.micronaut.kafka.docs.admin; | ||
|
||
import io.micronaut.configuration.kafka.admin.KafkaNewTopics; | ||
import io.micronaut.context.ApplicationContext; | ||
import org.apache.kafka.clients.admin.CreateTopicsResult; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class MyTopicFactoryTest { | ||
|
||
@Test | ||
void testNewTopics() throws ExecutionException, InterruptedException { | ||
try (ApplicationContext ctx = ApplicationContext.run(Map.of( | ||
"kafka.enabled", "true", | ||
"spec.name", "MyTopicFactoryTest" | ||
))) { | ||
final KafkaNewTopics newTopics = ctx.getBean(KafkaNewTopics.class); | ||
await().atMost(5, SECONDS).until(() -> areNewTopicsDone(newTopics)); | ||
final CreateTopicsResult result = newTopics.getResult().orElseThrow(); | ||
assertEquals(1, result.numPartitions("my-new-topic-1").get()); | ||
assertEquals(2, result.numPartitions("my-new-topic-2").get()); | ||
} | ||
} | ||
|
||
// tag::result[] | ||
boolean areNewTopicsDone(KafkaNewTopics newTopics) { | ||
return newTopics.getResult().map(result -> result.all().isDone()).orElse(false); | ||
} | ||
// end::result[] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need to be public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it does. Users may need to retrieve
CreateTopicsResult
from it.