Skip to content

Commit ad6910f

Browse files
committed
spring-projectsGH-2471: RetryableTopic Default Replication Factor
Resolves spring-projects#2471 Set the default to -1 (use broker default).
1 parent 606146c commit ad6910f

File tree

6 files changed

+24
-7
lines changed

6 files changed

+24
-7
lines changed

spring-kafka-docs/src/main/asciidoc/retrytopic.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ IMPORTANT: When using this configuration approach, the `@EnableKafkaRetryTopic`
216216
Use the simple `@EnableKafka` annotation instead.
217217

218218
When `autoCreateTopics` is true, the main and retry topics will be created with the specified number of partitions and replication factor.
219+
Starting with version 3.0, the default replication factor is `-1`, meaning use the broker default.
220+
If your broker version is earlier than 2.4, you will need to set an explicit value.
219221
To override these values for a particular topic (e.g. the main topic or DLT), simply add a `NewTopic` `@Bean` with the required properties; that will override the auto creation properties.
220222

221223
IMPORTANT: By default, records are published to the retry topic(s) using the original partition of the received record.
@@ -546,6 +548,8 @@ NOTE: The default behavior is to include all topics.
546548

547549
Unless otherwise specified the framework will auto create the required topics using `NewTopic` beans that are consumed by the `KafkaAdmin` bean.
548550
You can specify the number of partitions and the replication factor with which the topics will be created, and you can turn this feature off.
551+
Starting with version 3.0, the default replication factor is `-1`, meaning use the broker default.
552+
If your broker version is earlier than 2.4, you will need to set an explicit value.
549553

550554
IMPORTANT: Note that if you're not using Spring Boot you'll have to provide a KafkaAdmin bean in order to use this feature.
551555

@@ -584,7 +588,8 @@ public RetryTopicConfiguration myOtherRetryTopic(KafkaTemplate<Integer, MyPojo>
584588
----
585589
====
586590

587-
NOTE: By default the topics are autocreated with one partition and a replication factor of one.
591+
NOTE: By default the topics are autocreated with one partition and a replication factor of -1 (meaning use the broker default).
592+
If your broker version is earlier than 2.4, you will need to set an explicit value.
588593

589594
[[retry-headers]]
590595
===== Failure Header Management

spring-kafka-docs/src/main/asciidoc/whats-new.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ You can now set a different `concurrency` for the retry containers; by default,
4242

4343
See <<retry-config>> for more information.
4444

45+
The default replication factor for the retry topics is now `-1` (use broker default).
46+
If your broker is earlier that version 2.4, you will now need to explicitly set the property.
47+
4548
You can now configure multiple `@RetryableTopic` listeners on the same topic in the same application context.
4649
Previously, this was not possible.
4750
See <<multi-retry>> for more information.

spring-kafka/src/main/java/org/springframework/kafka/annotation/RetryableTopic.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@
113113

114114
/**
115115
* The replication factor for the automatically created topics. Expressions must
116-
* resolve to a short or a String that can be parsed as such.
116+
* resolve to a short or a String that can be parsed as such. Default is -1 to use the
117+
* broker default if the broker is earlier than version 2.4, an explicit value is
118+
* required.
119+
*
117120
* @return the replication factor.
118121
*/
119-
String replicationFactor() default "1";
122+
String replicationFactor() default "-1";
120123

121124
/**
122125
* The exception types that should be retried.

spring-kafka/src/main/java/org/springframework/kafka/retrytopic/RetryTopicConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ static class TopicCreation {
109109
TopicCreation() {
110110
this.shouldCreateTopics = true;
111111
this.numPartitions = 1;
112-
this.replicationFactor = 1;
112+
this.replicationFactor = -1;
113113
}
114114

115115
TopicCreation(boolean shouldCreateTopics) {
116116
this.shouldCreateTopics = shouldCreateTopics;
117117
this.numPartitions = 1;
118-
this.replicationFactor = 1;
118+
this.replicationFactor = -1;
119119
}
120120

121121
public int getNumPartitions() {
@@ -130,4 +130,5 @@ public boolean shouldCreateTopics() {
130130
return this.shouldCreateTopics;
131131
}
132132
}
133+
133134
}

spring-kafka/src/main/java/org/springframework/kafka/retrytopic/RetryTopicConfigurationBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ public RetryTopicConfigurationBuilder doNotAutoCreateRetryTopics() {
407407
* Configure the topic creation behavior to auto create topics with the provided
408408
* properties.
409409
* @param numPartitions the number of partitions.
410-
* @param replicationFactor the replication factor.
410+
* @param replicationFactor the replication factor (-1 to use the broker default if the
411+
* broker is version 2.4 or later).
411412
* @return the builder.
412413
*/
413414
public RetryTopicConfigurationBuilder autoCreateTopicsWith(int numPartitions, short replicationFactor) {
@@ -421,7 +422,8 @@ public RetryTopicConfigurationBuilder autoCreateTopicsWith(int numPartitions, sh
421422
* properties.
422423
* @param shouldCreate true to auto create.
423424
* @param numPartitions the number of partitions.
424-
* @param replicationFactor the replication factor.
425+
* @param replicationFactor the replication factor (-1 to use the broker default if the
426+
* broker is version 2.4 or later).
425427
* @return the builder.
426428
*/
427429
public RetryTopicConfigurationBuilder autoCreateTopics(boolean shouldCreate, int numPartitions,

spring-kafka/src/test/java/org/springframework/kafka/retrytopic/RetryTopicConfigurationBuilderTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
3030
import org.springframework.kafka.core.KafkaOperations;
31+
import org.springframework.kafka.test.utils.KafkaTestUtils;
3132
import org.springframework.test.util.ReflectionTestUtils;
3233

3334
/**
@@ -59,6 +60,8 @@ void shouldExcludeTopics() {
5960

6061
// then
6162
assertThat(configuration.hasConfigurationForTopics(topicNames)).isFalse();
63+
assertThat(KafkaTestUtils.getPropertyValue(builder, "topicCreationConfiguration.replicationFactor"))
64+
.isEqualTo((short) -1);
6265
}
6366

6467
@Test

0 commit comments

Comments
 (0)