Skip to content

Commit df61740

Browse files
committed
Lower the replication factor for transactions topic
1 parent 4a45905 commit df61740

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 the original author or authors.
2+
* Copyright 2017-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,12 +47,13 @@
4747
* @author Oleg Artyomov
4848
* @author Sergio Lourenco
4949
* @author Pawel Lozinski
50-
*
50+
* @author Dltmd202
5151
* @since 1.3
5252
*/
5353
class EmbeddedKafkaContextCustomizer implements ContextCustomizer {
5454

5555
private final EmbeddedKafka embeddedKafka;
56+
private final String TRANSACTION_STATE_LOG_REPLICATION_FACTOR = "transaction.state.log.replication.factor";
5657

5758
EmbeddedKafkaContextCustomizer(EmbeddedKafka embeddedKafka) {
5859
this.embeddedKafka = embeddedKafka;
@@ -121,6 +122,8 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
121122
}
122123
}
123124

125+
properties.putIfAbsent(TRANSACTION_STATE_LOG_REPLICATION_FACTOR, String.valueOf(Math.min(3, embeddedKafka.value())));
126+
124127
embeddedKafkaBroker.brokerProperties((Map<String, String>) (Map<?, ?>) properties);
125128
if (StringUtils.hasText(this.embeddedKafka.bootstrapServersProperty())) {
126129
embeddedKafkaBroker.brokerListProperty(this.embeddedKafka.bootstrapServersProperty());

spring-kafka-test/src/test/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizerTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
import org.springframework.kafka.test.EmbeddedKafkaBroker;
2626
import org.springframework.kafka.test.utils.KafkaTestUtils;
2727

28+
import java.util.Map;
29+
2830
import static org.assertj.core.api.Assertions.assertThat;
2931

3032
/**
3133
* @author Oleg Artyomov
3234
* @author Sergio Lourenco
3335
* @author Artem Bilan
3436
* @author Gary Russell
35-
*
3637
* @since 1.3
3738
*/
3839
public class EmbeddedKafkaContextCustomizerTests {
@@ -91,6 +92,22 @@ void testMulti() {
9192
.matches("127.0.0.1:[0-9]+,127.0.0.1:[0-9]+");
9293
}
9394

95+
@Test
96+
void testTransactionReplicationFactor() {
97+
EmbeddedKafka annotationWithPorts =
98+
AnnotationUtils.findAnnotation(TestWithEmbeddedKafkaTransactionFactor.class, EmbeddedKafka.class);
99+
EmbeddedKafkaContextCustomizer customizer = new EmbeddedKafkaContextCustomizer(annotationWithPorts);
100+
ConfigurableApplicationContext context = new GenericApplicationContext();
101+
customizer.customizeContext(context, null);
102+
context.refresh();
103+
104+
EmbeddedKafkaBroker embeddedKafkaBroker = context.getBean(EmbeddedKafkaBroker.class);
105+
Map<String, Object> properties = (Map<String, Object>) KafkaTestUtils.getPropertyValue(embeddedKafkaBroker, "brokerProperties");
106+
107+
assertThat(properties.get("transaction.state.log.replication.factor")).isEqualTo("2");
108+
}
109+
110+
94111
@EmbeddedKafka(kraft = false)
95112
private static final class TestWithEmbeddedKafka {
96113

@@ -111,4 +128,9 @@ private static final class TestWithEmbeddedKafkaMulti {
111128

112129
}
113130

131+
@EmbeddedKafka(kraft = false, count = 2)
132+
private static final class TestWithEmbeddedKafkaTransactionFactor {
133+
134+
}
135+
114136
}

0 commit comments

Comments
 (0)