GH-3557: Adjust the replication factor for transactions topic on @EmbeddedKafka
Fixes: #3557 https://github.com/spring-projects/spring-kafka/issues/3557 * Adjust the replication factor for the transaction state topic on `EmbeddedKafka` based on the broker count in `EmbeddedKafka`. * Keep the default replication factor of 3. * Adding tests to verify
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2023 the original author or authors.
|
||||
* Copyright 2017-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -47,6 +47,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Oleg Artyomov
|
||||
* @author Sergio Lourenco
|
||||
* @author Pawel Lozinski
|
||||
* @author Seonghwan Lee
|
||||
*
|
||||
* @since 1.3
|
||||
*/
|
||||
@@ -54,6 +55,8 @@ class EmbeddedKafkaContextCustomizer implements ContextCustomizer {
|
||||
|
||||
private final EmbeddedKafka embeddedKafka;
|
||||
|
||||
private final String TRANSACTION_STATE_LOG_REPLICATION_FACTOR = "transaction.state.log.replication.factor";
|
||||
|
||||
EmbeddedKafkaContextCustomizer(EmbeddedKafka embeddedKafka) {
|
||||
this.embeddedKafka = embeddedKafka;
|
||||
}
|
||||
@@ -121,6 +124,8 @@ class EmbeddedKafkaContextCustomizer implements ContextCustomizer {
|
||||
}
|
||||
}
|
||||
|
||||
properties.putIfAbsent(TRANSACTION_STATE_LOG_REPLICATION_FACTOR, String.valueOf(Math.min(3, embeddedKafka.count())));
|
||||
|
||||
embeddedKafkaBroker.brokerProperties((Map<String, String>) (Map<?, ?>) properties);
|
||||
if (StringUtils.hasText(this.embeddedKafka.bootstrapServersProperty())) {
|
||||
embeddedKafkaBroker.brokerListProperty(this.embeddedKafka.bootstrapServersProperty());
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.kafka.test.context;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -25,6 +27,7 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.kafka.test.EmbeddedKafkaBroker;
|
||||
import org.springframework.kafka.test.utils.KafkaTestUtils;
|
||||
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -32,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Sergio Lourenco
|
||||
* @author Artem Bilan
|
||||
* @author Gary Russell
|
||||
* @author Seonghwan Lee
|
||||
*
|
||||
* @since 1.3
|
||||
*/
|
||||
@@ -91,6 +95,21 @@ public class EmbeddedKafkaContextCustomizerTests {
|
||||
.matches("127.0.0.1:[0-9]+,127.0.0.1:[0-9]+");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransactionReplicationFactor() {
|
||||
EmbeddedKafka annotationWithPorts =
|
||||
AnnotationUtils.findAnnotation(TestWithEmbeddedKafkaTransactionFactor.class, EmbeddedKafka.class);
|
||||
EmbeddedKafkaContextCustomizer customizer = new EmbeddedKafkaContextCustomizer(annotationWithPorts);
|
||||
ConfigurableApplicationContext context = new GenericApplicationContext();
|
||||
customizer.customizeContext(context, null);
|
||||
context.refresh();
|
||||
|
||||
EmbeddedKafkaBroker embeddedKafkaBroker = context.getBean(EmbeddedKafkaBroker.class);
|
||||
Map<String, Object> properties = (Map<String, Object>) KafkaTestUtils.getPropertyValue(embeddedKafkaBroker, "brokerProperties");
|
||||
|
||||
assertThat(properties.get("transaction.state.log.replication.factor")).isEqualTo("2");
|
||||
}
|
||||
|
||||
@EmbeddedKafka(kraft = false)
|
||||
private static final class TestWithEmbeddedKafka {
|
||||
|
||||
@@ -111,4 +130,9 @@ public class EmbeddedKafkaContextCustomizerTests {
|
||||
|
||||
}
|
||||
|
||||
@EmbeddedKafka(kraft = false, count = 2)
|
||||
private static final class TestWithEmbeddedKafkaTransactionFactor {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user