diff --git a/plugins/transport-grpc/src/main/java/org/opensearch/transport/grpc/Netty4GrpcServerTransport.java b/plugins/transport-grpc/src/main/java/org/opensearch/transport/grpc/Netty4GrpcServerTransport.java index 627ea9eb70bdc..61c0722772b92 100644 --- a/plugins/transport-grpc/src/main/java/org/opensearch/transport/grpc/Netty4GrpcServerTransport.java +++ b/plugins/transport-grpc/src/main/java/org/opensearch/transport/grpc/Netty4GrpcServerTransport.java @@ -47,7 +47,7 @@ import static org.opensearch.common.settings.Setting.intSetting; import static org.opensearch.common.settings.Setting.listSetting; import static org.opensearch.common.util.concurrent.OpenSearchExecutors.daemonThreadFactory; -import static org.opensearch.transport.TcpTransport.resolveTransportPublishPort; +import static org.opensearch.transport.Transport.resolveTransportPublishPort; /** * Netty4 gRPC server implemented as a LifecycleComponent. diff --git a/server/src/main/java/org/opensearch/http/AbstractHttpServerTransport.java b/server/src/main/java/org/opensearch/http/AbstractHttpServerTransport.java index 4a68561808641..7f78ae0b9d2ff 100644 --- a/server/src/main/java/org/opensearch/http/AbstractHttpServerTransport.java +++ b/server/src/main/java/org/opensearch/http/AbstractHttpServerTransport.java @@ -62,6 +62,7 @@ import org.opensearch.telemetry.tracing.channels.TraceableRestChannel; import org.opensearch.threadpool.ThreadPool; import org.opensearch.transport.BindTransportException; +import org.opensearch.transport.Transport; import java.io.IOException; import java.net.InetAddress; @@ -83,7 +84,6 @@ import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_PORT; import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_PUBLISH_HOST; import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_PUBLISH_PORT; -import static org.opensearch.transport.TcpTransport.resolveTransportPublishPort; /** * Base HttpServer class @@ -192,7 +192,11 @@ protected void bindServer() { throw new BindTransportException("Failed to resolve publish address", e); } - final int publishPort = resolveTransportPublishPort(SETTING_HTTP_PUBLISH_PORT.get(settings), boundAddresses, publishInetAddress); + final int publishPort = Transport.resolveTransportPublishPort( + SETTING_HTTP_PUBLISH_PORT.get(settings), + boundAddresses, + publishInetAddress + ); if (publishPort < 0) { throw new BindHttpException( "Failed to auto-resolve http publish port, multiple bound addresses " diff --git a/server/src/main/java/org/opensearch/transport/TcpTransport.java b/server/src/main/java/org/opensearch/transport/TcpTransport.java index 59db664abd89d..f80a29872a78d 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,51 +543,6 @@ 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( - publishPort, - boundAddresses.stream().map(TransportAddress::address).collect(Collectors.toList()), - publishInetAddress - ); - } - @Override public TransportAddress[] addressesFromString(String address) throws UnknownHostException { return parse(address, defaultPortRange()[0]); diff --git a/server/src/main/java/org/opensearch/transport/Transport.java b/server/src/main/java/org/opensearch/transport/Transport.java index b89393615c95f..bb0ec22154a81 100644 --- a/server/src/main/java/org/opensearch/transport/Transport.java +++ b/server/src/main/java/org/opensearch/transport/Transport.java @@ -47,13 +47,18 @@ 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; +import java.util.stream.Collectors; /** * OpenSearch Transport Interface @@ -111,6 +116,51 @@ 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; + } + + static int resolveTransportPublishPort(int publishPort, List boundAddresses, InetAddress publishInetAddress) { + return Transport.resolvePublishPort( + publishPort, + boundAddresses.stream().map(TransportAddress::address).collect(Collectors.toList()), + publishInetAddress + ); + } + /** * A unidirectional connection to a {@link DiscoveryNode} * diff --git a/server/src/test/java/org/opensearch/http/AbstractHttpServerTransportTests.java b/server/src/test/java/org/opensearch/http/AbstractHttpServerTransportTests.java index 35138a4dc63bb..a4295289c3109 100644 --- a/server/src/test/java/org/opensearch/http/AbstractHttpServerTransportTests.java +++ b/server/src/test/java/org/opensearch/http/AbstractHttpServerTransportTests.java @@ -59,6 +59,7 @@ import org.opensearch.test.rest.FakeRestRequest; import org.opensearch.threadpool.TestThreadPool; import org.opensearch.threadpool.ThreadPool; +import org.opensearch.transport.Transport; import org.junit.After; import org.junit.Before; @@ -70,7 +71,6 @@ import static java.net.InetAddress.getByName; import static java.util.Arrays.asList; -import static org.opensearch.transport.TcpTransport.resolveTransportPublishPort; import static org.hamcrest.Matchers.equalTo; public class AbstractHttpServerTransportTests extends OpenSearchTestCase { @@ -100,31 +100,31 @@ public void testHttpPublishPort() throws Exception { int boundPort = randomIntBetween(9000, 9100); int otherBoundPort = randomIntBetween(9200, 9300); - int publishPort = resolveTransportPublishPort(9080, randomAddresses(), getByName("127.0.0.2")); + int publishPort = Transport.resolveTransportPublishPort(9080, randomAddresses(), getByName("127.0.0.2")); assertThat("Publish port should be explicitly set to 9080", publishPort, equalTo(9080)); - publishPort = resolveTransportPublishPort( + publishPort = Transport.resolveTransportPublishPort( -1, 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 = resolveTransportPublishPort( + publishPort = Transport.resolveTransportPublishPort( -1, 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)); - publishPort = resolveTransportPublishPort( + publishPort = Transport.resolveTransportPublishPort( -1, asList(address("127.0.0.1", boundPort), address("127.0.0.2", otherBoundPort)), getByName("127.0.0.3") ); assertThat(publishPort, equalTo(-1)); - publishPort = resolveTransportPublishPort( + publishPort = Transport.resolveTransportPublishPort( -1, asList(address("0.0.0.0", boundPort), address("127.0.0.2", otherBoundPort)), getByName("127.0.0.1") @@ -132,7 +132,7 @@ public void testHttpPublishPort() throws Exception { assertThat("Publish port should be derived from matching wildcard address", publishPort, equalTo(boundPort)); if (NetworkUtils.SUPPORTS_V6) { - publishPort = resolveTransportPublishPort( + publishPort = Transport.resolveTransportPublishPort( -1, asList(address("0.0.0.0", boundPort), address("127.0.0.2", otherBoundPort)), getByName("::1") 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")