Skip to content

Commit

Permalink
Move resolvePublishPort from TcpTransport -> Transport.
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Carroll <carrofin@amazon.com>
  • Loading branch information
finnegancarroll committed Dec 30, 2024
1 parent ecd998e commit d62c8eb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 deletions.
41 changes: 2 additions & 39 deletions server/src/main/java/org/opensearch/transport/TcpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<InetSocketAddress> 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<Integer> 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<TransportAddress> boundAddresses, InetAddress publishInetAddress) {
return resolvePublishPort(
return Transport.resolvePublishPort(
publishPort,
boundAddresses.stream().map(TransportAddress::address).collect(Collectors.toList()),
publishInetAddress
Expand Down
41 changes: 41 additions & 0 deletions server/src/main/java/org/opensearch/transport/Transport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<InetSocketAddress> 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<Integer> 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}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -72,43 +71,43 @@ 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")
);
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")
Expand Down

0 comments on commit d62c8eb

Please sign in to comment.