From d62c8eb5cc13637e3f0ea89ea0c4b1eb8b166f88 Mon Sep 17 00:00:00 2001 From: Finn Carroll Date: Mon, 30 Dec 2024 15:21:55 -0800 Subject: [PATCH] Move resolvePublishPort from TcpTransport -> Transport. Signed-off-by: Finn Carroll --- .../opensearch/transport/TcpTransport.java | 41 +------------------ .../org/opensearch/transport/Transport.java | 41 +++++++++++++++++++ .../transport/PublishPortTests.java | 13 +++--- 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/server/src/main/java/org/opensearch/transport/TcpTransport.java b/server/src/main/java/org/opensearch/transport/TcpTransport.java index 59db664abd89d..abacbfcec70f3 100644 --- a/server/src/main/java/org/opensearch/transport/TcpTransport.java +++ b/server/src/main/java/org/opensearch/transport/TcpTransport.java @@ -521,7 +521,7 @@ private BoundTransportAddress createBoundTransportAddress(ProfileSettings profil throw new BindTransportException("Failed to resolve publish address", e); } - final int publishPort = resolvePublishPort(profileSettings.publishPort, boundAddresses, publishInetAddress); + final int publishPort = Transport.resolvePublishPort(profileSettings.publishPort, boundAddresses, publishInetAddress); if (publishPort == -1) { String profileExplanation = profileSettings.isDefaultProfile ? "" : " for profile " + profileSettings.profileName; throw new BindTransportException( @@ -543,45 +543,8 @@ private BoundTransportAddress createBoundTransportAddress(ProfileSettings profil return new BoundTransportAddress(transportBoundAddresses, publishAddress); } - /** - * Resolve the publishPort for a server provided a list of boundAddresses and a publishInetAddress. - * Resolution strategy is as follows: - * If a configured port exists resolve to that port. - * If a bound address matches the publishInetAddress resolve to that port. - * If a bound address is a wildcard address resolve to that port. - * If all bound addresses share the same port resolve to that port. - * - * @param publishPort -1 if no configured publish port exists - * @param boundAddresses addresses bound by the server - * @param publishInetAddress address published for the server - * @return Resolved port. If publishPort is negative and no port can be resolved return publishPort. - */ - public static int resolvePublishPort(int publishPort, List boundAddresses, InetAddress publishInetAddress) { - if (publishPort < 0) { - for (InetSocketAddress boundAddress : boundAddresses) { - InetAddress boundInetAddress = boundAddress.getAddress(); - if (boundInetAddress.isAnyLocalAddress() || boundInetAddress.equals(publishInetAddress)) { - publishPort = boundAddress.getPort(); - break; - } - } - } - - if (publishPort < 0) { - final Set ports = new HashSet<>(); - for (InetSocketAddress boundAddress : boundAddresses) { - ports.add(boundAddress.getPort()); - } - if (ports.size() == 1) { - publishPort = ports.iterator().next(); - } - } - - return publishPort; - } - public static int resolveTransportPublishPort(int publishPort, List boundAddresses, InetAddress publishInetAddress) { - return resolvePublishPort( + return Transport.resolvePublishPort( publishPort, boundAddresses.stream().map(TransportAddress::address).collect(Collectors.toList()), publishInetAddress diff --git a/server/src/main/java/org/opensearch/transport/Transport.java b/server/src/main/java/org/opensearch/transport/Transport.java index b89393615c95f..08ca7219b4e6e 100644 --- a/server/src/main/java/org/opensearch/transport/Transport.java +++ b/server/src/main/java/org/opensearch/transport/Transport.java @@ -47,11 +47,15 @@ import java.io.Closeable; import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Predicate; @@ -111,6 +115,43 @@ default boolean isSecure() { RequestHandlers getRequestHandlers(); + /** + * Resolve the publishPort for a server provided a list of boundAddresses and a publishInetAddress. + * Resolution strategy is as follows: + * If a configured port exists resolve to that port. + * If a bound address matches the publishInetAddress resolve to that port. + * If a bound address is a wildcard address resolve to that port. + * If all bound addresses share the same port resolve to that port. + * + * @param publishPort -1 if no configured publish port exists + * @param boundAddresses addresses bound by the server + * @param publishInetAddress address published for the server + * @return Resolved port. If publishPort is negative and no port can be resolved return publishPort. + */ + static int resolvePublishPort(int publishPort, List boundAddresses, InetAddress publishInetAddress) { + if (publishPort < 0) { + for (InetSocketAddress boundAddress : boundAddresses) { + InetAddress boundInetAddress = boundAddress.getAddress(); + if (boundInetAddress.isAnyLocalAddress() || boundInetAddress.equals(publishInetAddress)) { + publishPort = boundAddress.getPort(); + break; + } + } + } + + if (publishPort < 0) { + final Set ports = new HashSet<>(); + for (InetSocketAddress boundAddress : boundAddresses) { + ports.add(boundAddress.getPort()); + } + if (ports.size() == 1) { + publishPort = ports.iterator().next(); + } + } + + return publishPort; + } + /** * A unidirectional connection to a {@link DiscoveryNode} * diff --git a/server/src/test/java/org/opensearch/transport/PublishPortTests.java b/server/src/test/java/org/opensearch/transport/PublishPortTests.java index 4621a4f04ebca..2e5a57c4cdd60 100644 --- a/server/src/test/java/org/opensearch/transport/PublishPortTests.java +++ b/server/src/test/java/org/opensearch/transport/PublishPortTests.java @@ -43,7 +43,6 @@ import static java.net.InetAddress.getByName; import static java.util.Arrays.asList; -import static org.opensearch.transport.TcpTransport.resolvePublishPort; import static org.hamcrest.Matchers.equalTo; public class PublishPortTests extends OpenSearchTestCase { @@ -72,35 +71,35 @@ public void testPublishPort() throws Exception { } - int publishPort = resolvePublishPort( + int publishPort = Transport.resolvePublishPort( new TcpTransport.ProfileSettings(settings, profile).publishPort, randomAddresses(), getByName("127.0.0.2") ); assertThat("Publish port should be explicitly set", publishPort, equalTo(useProfile ? 9080 : 9081)); - publishPort = resolvePublishPort( + publishPort = Transport.resolvePublishPort( new TcpTransport.ProfileSettings(baseSettings, profile).publishPort, asList(address("127.0.0.1", boundPort), address("127.0.0.2", otherBoundPort)), getByName("127.0.0.1") ); assertThat("Publish port should be derived from matched address", publishPort, equalTo(boundPort)); - publishPort = resolvePublishPort( + publishPort = Transport.resolvePublishPort( new TcpTransport.ProfileSettings(baseSettings, profile).publishPort, asList(address("127.0.0.1", boundPort), address("127.0.0.2", boundPort)), getByName("127.0.0.3") ); assertThat("Publish port should be derived from unique port of bound addresses", publishPort, equalTo(boundPort)); - int resPort = resolvePublishPort( + int resPort = Transport.resolvePublishPort( new TcpTransport.ProfileSettings(baseSettings, profile).publishPort, asList(address("127.0.0.1", boundPort), address("127.0.0.2", otherBoundPort)), getByName("127.0.0.3") ); assertThat("as publish_port not specified and non-unique port of bound addresses", resPort, equalTo(-1)); - publishPort = resolvePublishPort( + publishPort = Transport.resolvePublishPort( new TcpTransport.ProfileSettings(baseSettings, profile).publishPort, asList(address("0.0.0.0", boundPort), address("127.0.0.2", otherBoundPort)), getByName("127.0.0.1") @@ -108,7 +107,7 @@ public void testPublishPort() throws Exception { assertThat("Publish port should be derived from matching wildcard address", publishPort, equalTo(boundPort)); if (NetworkUtils.SUPPORTS_V6) { - publishPort = resolvePublishPort( + publishPort = Transport.resolvePublishPort( new TcpTransport.ProfileSettings(baseSettings, profile).publishPort, asList(address("0.0.0.0", boundPort), address("127.0.0.2", otherBoundPort)), getByName("::1")