-
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 all 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
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
/* | ||
* 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.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; | ||
|
||
/** | ||
* Creates Kafka topics via {@link AdminClient}. | ||
* | ||
* @author Guillermo Calvo | ||
* @since 5.2 | ||
*/ | ||
@Context | ||
@Requires(bean = AdminClient.class) | ||
@Requires(bean = NewTopic.class) | ||
public class KafkaNewTopics { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(KafkaNewTopics.class); | ||
|
||
@NonNull | ||
private final CreateTopicsResult result; | ||
|
||
/** | ||
* @param adminClient The Kafka admin client. | ||
* @param options Optional {@link CreateTopicsOptions}. | ||
* @param topics The list of {@link NewTopic} beans to create. | ||
*/ | ||
public KafkaNewTopics( | ||
@NonNull AdminClient adminClient, | ||
@Nullable CreateTopicsOptions options, | ||
@NonNull List<NewTopic> topics | ||
) { | ||
LOG.info("Creating new topics: {}", topics); | ||
this.result = adminClient.createTopics(topics, options != null ? options : new CreateTopicsOptions()); | ||
} | ||
|
||
/** | ||
* @return The {@link CreateTopicsResult}. | ||
*/ | ||
@Experimental | ||
@NonNull | ||
public CreateTopicsResult getResult() { | ||
return result; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
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: | ||
new PollingConditions(timeout: 10).eventually { | ||
kafkaNewTopics.result.all().done == true | ||
} | ||
kafkaNewTopics.result.numPartitions(TOPIC_1).get() == 1 | ||
kafkaNewTopics.result.numPartitions(TOPIC_2).get() == 2 | ||
kafkaNewTopics.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()) } | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
kafka/src/test/groovy/io/micronaut/configuration/kafka/admin/KafkaNoNewTopicsSpec.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,28 @@ | ||
package io.micronaut.configuration.kafka.admin | ||
|
||
import io.micronaut.configuration.kafka.AbstractKafkaContainerSpec | ||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.core.annotation.Nullable | ||
import jakarta.inject.Inject | ||
import jakarta.inject.Singleton | ||
import org.apache.kafka.clients.admin.NewTopic | ||
|
||
@Property(name = "kafka.health.enabled", value = "false") | ||
class KafkaNoNewTopicsSpec extends AbstractKafkaContainerSpec { | ||
|
||
void "don't create any kafka topics"() { | ||
expect: | ||
context.findBean(NewTopic).isEmpty() | ||
context.findBean(KafkaNewTopics).isEmpty() | ||
context.getBean(MyService).kafkaNewTopics == null | ||
} | ||
|
||
@Requires(property = 'spec.name', value = 'KafkaNoNewTopicsSpec') | ||
@Singleton | ||
static class MyService { | ||
@Inject | ||
@Nullable | ||
KafkaNewTopics kafkaNewTopics | ||
} | ||
} |
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, add a bean of type a link:{kafkaapi}/org/apache/kafka/clients/admin/NewTopic[`NewTopic`] 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 a bean of type link:{kafkaapi}/org/apache/kafka/clients/admin/CreateTopicsOptions[`CreateTopicsOptions`] that will be used when the new topics are created. | ||
|
||
.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[] |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
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) | ||
newTopics.getResult().numPartitions("my-new-topic-1").get() == 1 | ||
newTopics.getResult().numPartitions("my-new-topic-2").get() == 2 | ||
} | ||
} | ||
|
||
// tag::result[] | ||
boolean areNewTopicsDone(KafkaNewTopics newTopics) { | ||
newTopics.result.all().done | ||
} | ||
// 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[] |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
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) } | ||
assertEquals(1, newTopics.result.numPartitions("my-new-topic-1").get()) | ||
assertEquals(2, newTopics.result.numPartitions("my-new-topic-2").get()) | ||
} | ||
} | ||
|
||
// tag::result[] | ||
fun areNewTopicsDone(newTopics: KafkaNewTopics): Boolean { | ||
return newTopics.result.all().isDone | ||
} | ||
// 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[] |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
package io.micronaut.kafka.docs.admin; | ||
|
||
import io.micronaut.configuration.kafka.admin.KafkaNewTopics; | ||
import io.micronaut.context.ApplicationContext; | ||
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)); | ||
assertEquals(1, newTopics.getResult().numPartitions("my-new-topic-1").get()); | ||
assertEquals(2, newTopics.getResult().numPartitions("my-new-topic-2").get()); | ||
} | ||
} | ||
|
||
// tag::result[] | ||
boolean areNewTopicsDone(KafkaNewTopics newTopics) { | ||
return newTopics.getResult().all().isDone(); | ||
} | ||
// 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.