Skip to content
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

GH-3662: Support Kafka 3.9.0+ in EmbeddedKafkaKraftBroker #3665

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.time.Duration;
import java.util.AbstractMap.SimpleEntry;
Expand Down Expand Up @@ -56,6 +57,8 @@

import org.springframework.core.log.LogAccessor;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

/**
* An embedded Kafka Broker(s) using KRaft.
Expand All @@ -77,6 +80,8 @@
*/
public class EmbeddedKafkaKraftBroker implements EmbeddedKafkaBroker {

private static final String CLASS_EXISTS_ONLY_IN_390 = "org.apache.kafka.server.config.AbstractKafkaConfig";

private static final LogAccessor LOGGER = new LogAccessor(LogFactory.getLog(EmbeddedKafkaKraftBroker.class));

/**
Expand All @@ -87,6 +92,24 @@ public class EmbeddedKafkaKraftBroker implements EmbeddedKafkaBroker {

public static final int DEFAULT_ADMIN_TIMEOUT = 10;

private static final boolean IS_KAFKA_39_OR_LATER;

private static final Method SET_CONFIG_METHOD;

static {
IS_KAFKA_39_OR_LATER = ClassUtils.isPresent(CLASS_EXISTS_ONLY_IN_390, EmbeddedKafkaKraftBroker.class.getClassLoader());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isPresent() doesn't throw an exception: can be used directly in the property declaration line.
Also: I don't see a reason in the CLASS_EXISTS_ONLY_IN_390 constant. You simply can use that class name in this method call.


if (IS_KAFKA_39_OR_LATER) {
SET_CONFIG_METHOD = ReflectionUtils.findMethod(
KafkaClusterTestKit.Builder.class,
"setConfigProp",
String.class, Object.class);
}
else {
SET_CONFIG_METHOD = null;
}
}

private final int count;

private final Set<String> topics;
Expand Down Expand Up @@ -217,7 +240,7 @@ private void start() {
.setNumBrokerNodes(this.count)
.setNumControllerNodes(this.count)
.build());
this.brokerProperties.forEach((k, v) -> clusterBuilder.setConfigProp((String) k, (String) v));
this.brokerProperties.forEach((k, v) -> setConfigProperty(clusterBuilder, k, v));
this.cluster = clusterBuilder.build();
}
catch (Exception ex) {
Expand All @@ -243,6 +266,17 @@ private void start() {
System.setProperty(SPRING_EMBEDDED_KAFKA_BROKERS, getBrokersAsString());
}

private static void setConfigProperty(KafkaClusterTestKit.Builder clusterBuilder, Object key, Object value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick: can we cast key into a String where we call this method?
That way that cast operation would happen only in one place.

if (IS_KAFKA_39_OR_LATER) {
// For Kafka 3.9.0+: use reflection
ReflectionUtils.invokeMethod(SET_CONFIG_METHOD, clusterBuilder, (String) key, value);
}
else {
// For Kafka 3.8.0: direct call
clusterBuilder.setConfigProp((String) key, (String) value);
}
}

@Override
public void destroy() {
AtomicReference<Throwable> shutdownFailure = new AtomicReference<>();
Expand Down
Loading