-
Notifications
You must be signed in to change notification settings - Fork 14.7k
MINOR: migrate listenerListToEndPoints from CoreUtils to AbstractKafkaConfig #20567
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
base: trunk
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ | |||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||
package org.apache.kafka.server.config; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
import org.apache.kafka.common.Endpoint; | ||||||||||||||||||||||||||||||||||||||||
import org.apache.kafka.common.KafkaException; | ||||||||||||||||||||||||||||||||||||||||
import org.apache.kafka.common.config.AbstractConfig; | ||||||||||||||||||||||||||||||||||||||||
import org.apache.kafka.common.config.ConfigDef; | ||||||||||||||||||||||||||||||||||||||||
|
@@ -39,10 +40,13 @@ | |||||||||||||||||||||||||||||||||||||||
import org.apache.kafka.storage.internals.log.CleanerConfig; | ||||||||||||||||||||||||||||||||||||||||
import org.apache.kafka.storage.internals.log.LogConfig; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
import org.apache.commons.validator.routines.InetAddressValidator; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||||||||||||||||||||||
import java.util.Locale; | ||||||||||||||||||||||||||||||||||||||||
import java.util.Map; | ||||||||||||||||||||||||||||||||||||||||
import java.util.Optional; | ||||||||||||||||||||||||||||||||||||||||
import java.util.Set; | ||||||||||||||||||||||||||||||||||||||||
import java.util.stream.Collectors; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||
|
@@ -52,6 +56,10 @@ | |||||||||||||||||||||||||||||||||||||||
* For more details check KAFKA-15853 | ||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||
public abstract class AbstractKafkaConfig extends AbstractConfig { | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
private static final InetAddressValidator INET_ADDRESS_VALIDATOR = InetAddressValidator.getInstance(); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant empty line. |
||||||||||||||||||||||||||||||||||||||||
public static final ConfigDef CONFIG_DEF = Utils.mergeConfigs(List.of( | ||||||||||||||||||||||||||||||||||||||||
RemoteLogManagerConfig.configDef(), | ||||||||||||||||||||||||||||||||||||||||
ServerConfigs.CONFIG_DEF, | ||||||||||||||||||||||||||||||||||||||||
|
@@ -161,6 +169,80 @@ public static Map<String, String> getMap(String propName, String propValue) { | |||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
public static List<Endpoint> listenerListToEndPoints(List<String> listeners, Map<ListenerName, SecurityProtocol> securityProtocolMap) { | ||||||||||||||||||||||||||||||||||||||||
return listenerListToEndPoints(listeners, securityProtocolMap, true); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
public static List<Endpoint> listenerListToEndPoints(List<String> listeners, Map<ListenerName, SecurityProtocol> securityProtocolMap, boolean requireDistinctPorts) { | ||||||||||||||||||||||||||||||||||||||||
List<Endpoint> endPoints; | ||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||
endPoints = SocketServerConfigs.listenerListToEndPoints(listeners, securityProtocolMap); | ||||||||||||||||||||||||||||||||||||||||
} catch (Exception e) { | ||||||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException(String.format("Error creating broker listeners from '%s': %s", listeners, e.getMessage()), e); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
validate(endPoints, listeners, requireDistinctPorts); | ||||||||||||||||||||||||||||||||||||||||
return endPoints; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+176
to
+185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we do the following?
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
private static void validate(List<Endpoint> endPoints, List<String> listeners, boolean requireDistinctPorts) { | ||||||||||||||||||||||||||||||||||||||||
long distinctListenerNames = endPoints.stream().map(Endpoint::listener).distinct().count(); | ||||||||||||||||||||||||||||||||||||||||
if (distinctListenerNames != endPoints.size()) { | ||||||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException("Each listener must have a different name, listeners: " + listeners); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can return early if private static void validate(List<Endpoint> endPoints, List<String> listeners, boolean requireDistinctPorts) {
long distinct = endPoints.stream().map(Endpoint::listener).distinct().count();
if (distinct != endPoints.size()) {
throw new IllegalArgumentException("Each listener must have a different name, listeners: " + listeners);
}
if (!requireDistinctPorts) return;
... |
||||||||||||||||||||||||||||||||||||||||
Map<Integer, List<Endpoint>> duplicatePorts = endPoints.stream() | ||||||||||||||||||||||||||||||||||||||||
.filter(ep -> ep.port() != 0) // filter port 0 for unit tests | ||||||||||||||||||||||||||||||||||||||||
.collect(Collectors.groupingBy(Endpoint::port)) | ||||||||||||||||||||||||||||||||||||||||
.entrySet().stream() | ||||||||||||||||||||||||||||||||||||||||
.filter(entry -> entry.getValue().size() > 1) | ||||||||||||||||||||||||||||||||||||||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems we can call |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
duplicatePorts.forEach((port, eps) -> { | ||||||||||||||||||||||||||||||||||||||||
Map<Boolean, List<Endpoint>> partitionedByValidIp = eps.stream() | ||||||||||||||||||||||||||||||||||||||||
.collect(Collectors.partitioningBy(ep -> ep.host() != null && INET_ADDRESS_VALIDATOR.isValid(ep.host()))); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
List<Endpoint> duplicatesWithIpHosts = partitionedByValidIp.get(true); | ||||||||||||||||||||||||||||||||||||||||
List<Endpoint> duplicatesWithoutIpHosts = partitionedByValidIp.get(false); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (requireDistinctPorts) { | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
checkDuplicateListenerPorts(duplicatesWithoutIpHosts, listeners); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (duplicatesWithIpHosts.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||
// No-op | ||||||||||||||||||||||||||||||||||||||||
} else if (duplicatesWithIpHosts.size() == 2) { | ||||||||||||||||||||||||||||||||||||||||
if (requireDistinctPorts) { | ||||||||||||||||||||||||||||||||||||||||
String errorMessage = "If you have two listeners on the same port then one needs to be IPv4 and the other IPv6, listeners: " + listeners + ", port: " + port; | ||||||||||||||||||||||||||||||||||||||||
Endpoint ep1 = duplicatesWithIpHosts.get(0); | ||||||||||||||||||||||||||||||||||||||||
Endpoint ep2 = duplicatesWithIpHosts.get(1); | ||||||||||||||||||||||||||||||||||||||||
if (!validateOneIsIpv4AndOtherIpv6(ep1.host(), ep2.host())) { | ||||||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException(errorMessage); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (!duplicatesWithoutIpHosts.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException(errorMessage); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||
if (requireDistinctPorts) { | ||||||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException("Each listener must have a different port unless exactly one listener has an IPv4 address and the other IPv6 address, listeners: " + listeners + ", port: " + port); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
private static boolean validateOneIsIpv4AndOtherIpv6(String first, String second) { | ||||||||||||||||||||||||||||||||||||||||
return (INET_ADDRESS_VALIDATOR.isValidInet4Address(first) && INET_ADDRESS_VALIDATOR.isValidInet6Address(second)) || | ||||||||||||||||||||||||||||||||||||||||
(INET_ADDRESS_VALIDATOR.isValidInet6Address(first) && INET_ADDRESS_VALIDATOR.isValidInet4Address(second)); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
private static void checkDuplicateListenerPorts(List<Endpoint> endpoints, List<String> listeners) { | ||||||||||||||||||||||||||||||||||||||||
Set<String> distinctHosts = endpoints.stream().map(ep -> ep.host() == null ? "" : ep.host()).collect(Collectors.toSet()); | ||||||||||||||||||||||||||||||||||||||||
if (endpoints.size() > distinctHosts.size()) { | ||||||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException("Each listener must have a different port, listeners: " + listeners); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
private static SecurityProtocol securityProtocol(String protocolName, String configName) { | ||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||
return SecurityProtocol.forName(protocolName); | ||||||||||||||||||||||||||||||||||||||||
|
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.
It should be in reverse order.
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.
fixed, thanks!