Skip to content

Commit d62c8eb

Browse files
Move resolvePublishPort from TcpTransport -> Transport.
Signed-off-by: Finn Carroll <[email protected]>
1 parent ecd998e commit d62c8eb

File tree

3 files changed

+49
-46
lines changed

3 files changed

+49
-46
lines changed

server/src/main/java/org/opensearch/transport/TcpTransport.java

+2-39
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ private BoundTransportAddress createBoundTransportAddress(ProfileSettings profil
521521
throw new BindTransportException("Failed to resolve publish address", e);
522522
}
523523

524-
final int publishPort = resolvePublishPort(profileSettings.publishPort, boundAddresses, publishInetAddress);
524+
final int publishPort = Transport.resolvePublishPort(profileSettings.publishPort, boundAddresses, publishInetAddress);
525525
if (publishPort == -1) {
526526
String profileExplanation = profileSettings.isDefaultProfile ? "" : " for profile " + profileSettings.profileName;
527527
throw new BindTransportException(
@@ -543,45 +543,8 @@ private BoundTransportAddress createBoundTransportAddress(ProfileSettings profil
543543
return new BoundTransportAddress(transportBoundAddresses, publishAddress);
544544
}
545545

546-
/**
547-
* Resolve the publishPort for a server provided a list of boundAddresses and a publishInetAddress.
548-
* Resolution strategy is as follows:
549-
* If a configured port exists resolve to that port.
550-
* If a bound address matches the publishInetAddress resolve to that port.
551-
* If a bound address is a wildcard address resolve to that port.
552-
* If all bound addresses share the same port resolve to that port.
553-
*
554-
* @param publishPort -1 if no configured publish port exists
555-
* @param boundAddresses addresses bound by the server
556-
* @param publishInetAddress address published for the server
557-
* @return Resolved port. If publishPort is negative and no port can be resolved return publishPort.
558-
*/
559-
public static int resolvePublishPort(int publishPort, List<InetSocketAddress> boundAddresses, InetAddress publishInetAddress) {
560-
if (publishPort < 0) {
561-
for (InetSocketAddress boundAddress : boundAddresses) {
562-
InetAddress boundInetAddress = boundAddress.getAddress();
563-
if (boundInetAddress.isAnyLocalAddress() || boundInetAddress.equals(publishInetAddress)) {
564-
publishPort = boundAddress.getPort();
565-
break;
566-
}
567-
}
568-
}
569-
570-
if (publishPort < 0) {
571-
final Set<Integer> ports = new HashSet<>();
572-
for (InetSocketAddress boundAddress : boundAddresses) {
573-
ports.add(boundAddress.getPort());
574-
}
575-
if (ports.size() == 1) {
576-
publishPort = ports.iterator().next();
577-
}
578-
}
579-
580-
return publishPort;
581-
}
582-
583546
public static int resolveTransportPublishPort(int publishPort, List<TransportAddress> boundAddresses, InetAddress publishInetAddress) {
584-
return resolvePublishPort(
547+
return Transport.resolvePublishPort(
585548
publishPort,
586549
boundAddresses.stream().map(TransportAddress::address).collect(Collectors.toList()),
587550
publishInetAddress

server/src/main/java/org/opensearch/transport/Transport.java

+41
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@
4747

4848
import java.io.Closeable;
4949
import java.io.IOException;
50+
import java.net.InetAddress;
51+
import java.net.InetSocketAddress;
5052
import java.net.UnknownHostException;
5153
import java.util.ArrayList;
5254
import java.util.Collections;
55+
import java.util.HashSet;
5356
import java.util.List;
5457
import java.util.Map;
58+
import java.util.Set;
5559
import java.util.concurrent.atomic.AtomicLong;
5660
import java.util.function.Predicate;
5761

@@ -111,6 +115,43 @@ default boolean isSecure() {
111115

112116
RequestHandlers getRequestHandlers();
113117

118+
/**
119+
* Resolve the publishPort for a server provided a list of boundAddresses and a publishInetAddress.
120+
* Resolution strategy is as follows:
121+
* If a configured port exists resolve to that port.
122+
* If a bound address matches the publishInetAddress resolve to that port.
123+
* If a bound address is a wildcard address resolve to that port.
124+
* If all bound addresses share the same port resolve to that port.
125+
*
126+
* @param publishPort -1 if no configured publish port exists
127+
* @param boundAddresses addresses bound by the server
128+
* @param publishInetAddress address published for the server
129+
* @return Resolved port. If publishPort is negative and no port can be resolved return publishPort.
130+
*/
131+
static int resolvePublishPort(int publishPort, List<InetSocketAddress> boundAddresses, InetAddress publishInetAddress) {
132+
if (publishPort < 0) {
133+
for (InetSocketAddress boundAddress : boundAddresses) {
134+
InetAddress boundInetAddress = boundAddress.getAddress();
135+
if (boundInetAddress.isAnyLocalAddress() || boundInetAddress.equals(publishInetAddress)) {
136+
publishPort = boundAddress.getPort();
137+
break;
138+
}
139+
}
140+
}
141+
142+
if (publishPort < 0) {
143+
final Set<Integer> ports = new HashSet<>();
144+
for (InetSocketAddress boundAddress : boundAddresses) {
145+
ports.add(boundAddress.getPort());
146+
}
147+
if (ports.size() == 1) {
148+
publishPort = ports.iterator().next();
149+
}
150+
}
151+
152+
return publishPort;
153+
}
154+
114155
/**
115156
* A unidirectional connection to a {@link DiscoveryNode}
116157
*

server/src/test/java/org/opensearch/transport/PublishPortTests.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
import static java.net.InetAddress.getByName;
4545
import static java.util.Arrays.asList;
46-
import static org.opensearch.transport.TcpTransport.resolvePublishPort;
4746
import static org.hamcrest.Matchers.equalTo;
4847

4948
public class PublishPortTests extends OpenSearchTestCase {
@@ -72,43 +71,43 @@ public void testPublishPort() throws Exception {
7271

7372
}
7473

75-
int publishPort = resolvePublishPort(
74+
int publishPort = Transport.resolvePublishPort(
7675
new TcpTransport.ProfileSettings(settings, profile).publishPort,
7776
randomAddresses(),
7877
getByName("127.0.0.2")
7978
);
8079
assertThat("Publish port should be explicitly set", publishPort, equalTo(useProfile ? 9080 : 9081));
8180

82-
publishPort = resolvePublishPort(
81+
publishPort = Transport.resolvePublishPort(
8382
new TcpTransport.ProfileSettings(baseSettings, profile).publishPort,
8483
asList(address("127.0.0.1", boundPort), address("127.0.0.2", otherBoundPort)),
8584
getByName("127.0.0.1")
8685
);
8786
assertThat("Publish port should be derived from matched address", publishPort, equalTo(boundPort));
8887

89-
publishPort = resolvePublishPort(
88+
publishPort = Transport.resolvePublishPort(
9089
new TcpTransport.ProfileSettings(baseSettings, profile).publishPort,
9190
asList(address("127.0.0.1", boundPort), address("127.0.0.2", boundPort)),
9291
getByName("127.0.0.3")
9392
);
9493
assertThat("Publish port should be derived from unique port of bound addresses", publishPort, equalTo(boundPort));
9594

96-
int resPort = resolvePublishPort(
95+
int resPort = Transport.resolvePublishPort(
9796
new TcpTransport.ProfileSettings(baseSettings, profile).publishPort,
9897
asList(address("127.0.0.1", boundPort), address("127.0.0.2", otherBoundPort)),
9998
getByName("127.0.0.3")
10099
);
101100
assertThat("as publish_port not specified and non-unique port of bound addresses", resPort, equalTo(-1));
102101

103-
publishPort = resolvePublishPort(
102+
publishPort = Transport.resolvePublishPort(
104103
new TcpTransport.ProfileSettings(baseSettings, profile).publishPort,
105104
asList(address("0.0.0.0", boundPort), address("127.0.0.2", otherBoundPort)),
106105
getByName("127.0.0.1")
107106
);
108107
assertThat("Publish port should be derived from matching wildcard address", publishPort, equalTo(boundPort));
109108

110109
if (NetworkUtils.SUPPORTS_V6) {
111-
publishPort = resolvePublishPort(
110+
publishPort = Transport.resolvePublishPort(
112111
new TcpTransport.ProfileSettings(baseSettings, profile).publishPort,
113112
asList(address("0.0.0.0", boundPort), address("127.0.0.2", otherBoundPort)),
114113
getByName("::1")

0 commit comments

Comments
 (0)