Skip to content

Commit 6fc1453

Browse files
Move resolve<Transport>PublishPort() from TcpTransport -> Transport.
Signed-off-by: Finn Carroll <[email protected]>
1 parent ecd998e commit 6fc1453

File tree

6 files changed

+71
-63
lines changed

6 files changed

+71
-63
lines changed

plugins/transport-grpc/src/main/java/org/opensearch/transport/grpc/Netty4GrpcServerTransport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import static org.opensearch.common.settings.Setting.intSetting;
4848
import static org.opensearch.common.settings.Setting.listSetting;
4949
import static org.opensearch.common.util.concurrent.OpenSearchExecutors.daemonThreadFactory;
50-
import static org.opensearch.transport.TcpTransport.resolveTransportPublishPort;
50+
import static org.opensearch.transport.Transport.resolveTransportPublishPort;
5151

5252
/**
5353
* Netty4 gRPC server implemented as a LifecycleComponent.

server/src/main/java/org/opensearch/http/AbstractHttpServerTransport.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.opensearch.telemetry.tracing.channels.TraceableRestChannel;
6363
import org.opensearch.threadpool.ThreadPool;
6464
import org.opensearch.transport.BindTransportException;
65+
import org.opensearch.transport.Transport;
6566

6667
import java.io.IOException;
6768
import java.net.InetAddress;
@@ -83,7 +84,6 @@
8384
import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_PORT;
8485
import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_PUBLISH_HOST;
8586
import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_PUBLISH_PORT;
86-
import static org.opensearch.transport.TcpTransport.resolveTransportPublishPort;
8787

8888
/**
8989
* Base HttpServer class
@@ -192,7 +192,11 @@ protected void bindServer() {
192192
throw new BindTransportException("Failed to resolve publish address", e);
193193
}
194194

195-
final int publishPort = resolveTransportPublishPort(SETTING_HTTP_PUBLISH_PORT.get(settings), boundAddresses, publishInetAddress);
195+
final int publishPort = Transport.resolveTransportPublishPort(
196+
SETTING_HTTP_PUBLISH_PORT.get(settings),
197+
boundAddresses,
198+
publishInetAddress
199+
);
196200
if (publishPort < 0) {
197201
throw new BindHttpException(
198202
"Failed to auto-resolve http publish port, multiple bound addresses "

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

+1-46
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,51 +543,6 @@ 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-
583-
public static int resolveTransportPublishPort(int publishPort, List<TransportAddress> boundAddresses, InetAddress publishInetAddress) {
584-
return resolvePublishPort(
585-
publishPort,
586-
boundAddresses.stream().map(TransportAddress::address).collect(Collectors.toList()),
587-
publishInetAddress
588-
);
589-
}
590-
591546
@Override
592547
public TransportAddress[] addressesFromString(String address) throws UnknownHostException {
593548
return parse(address, defaultPortRange()[0]);

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

+50
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@
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;
61+
import java.util.stream.Collectors;
5762

5863
/**
5964
* OpenSearch Transport Interface
@@ -111,6 +116,51 @@ default boolean isSecure() {
111116

112117
RequestHandlers getRequestHandlers();
113118

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

server/src/test/java/org/opensearch/http/AbstractHttpServerTransportTests.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.opensearch.test.rest.FakeRestRequest;
6060
import org.opensearch.threadpool.TestThreadPool;
6161
import org.opensearch.threadpool.ThreadPool;
62+
import org.opensearch.transport.Transport;
6263
import org.junit.After;
6364
import org.junit.Before;
6465

@@ -70,7 +71,6 @@
7071

7172
import static java.net.InetAddress.getByName;
7273
import static java.util.Arrays.asList;
73-
import static org.opensearch.transport.TcpTransport.resolveTransportPublishPort;
7474
import static org.hamcrest.Matchers.equalTo;
7575

7676
public class AbstractHttpServerTransportTests extends OpenSearchTestCase {
@@ -100,39 +100,39 @@ public void testHttpPublishPort() throws Exception {
100100
int boundPort = randomIntBetween(9000, 9100);
101101
int otherBoundPort = randomIntBetween(9200, 9300);
102102

103-
int publishPort = resolveTransportPublishPort(9080, randomAddresses(), getByName("127.0.0.2"));
103+
int publishPort = Transport.resolveTransportPublishPort(9080, randomAddresses(), getByName("127.0.0.2"));
104104
assertThat("Publish port should be explicitly set to 9080", publishPort, equalTo(9080));
105105

106-
publishPort = resolveTransportPublishPort(
106+
publishPort = Transport.resolveTransportPublishPort(
107107
-1,
108108
asList(address("127.0.0.1", boundPort), address("127.0.0.2", otherBoundPort)),
109109
getByName("127.0.0.1")
110110
);
111111
assertThat("Publish port should be derived from matched address", publishPort, equalTo(boundPort));
112112

113-
publishPort = resolveTransportPublishPort(
113+
publishPort = Transport.resolveTransportPublishPort(
114114
-1,
115115
asList(address("127.0.0.1", boundPort), address("127.0.0.2", boundPort)),
116116
getByName("127.0.0.3")
117117
);
118118
assertThat("Publish port should be derived from unique port of bound addresses", publishPort, equalTo(boundPort));
119119

120-
publishPort = resolveTransportPublishPort(
120+
publishPort = Transport.resolveTransportPublishPort(
121121
-1,
122122
asList(address("127.0.0.1", boundPort), address("127.0.0.2", otherBoundPort)),
123123
getByName("127.0.0.3")
124124
);
125125
assertThat(publishPort, equalTo(-1));
126126

127-
publishPort = resolveTransportPublishPort(
127+
publishPort = Transport.resolveTransportPublishPort(
128128
-1,
129129
asList(address("0.0.0.0", boundPort), address("127.0.0.2", otherBoundPort)),
130130
getByName("127.0.0.1")
131131
);
132132
assertThat("Publish port should be derived from matching wildcard address", publishPort, equalTo(boundPort));
133133

134134
if (NetworkUtils.SUPPORTS_V6) {
135-
publishPort = resolveTransportPublishPort(
135+
publishPort = Transport.resolveTransportPublishPort(
136136
-1,
137137
asList(address("0.0.0.0", boundPort), address("127.0.0.2", otherBoundPort)),
138138
getByName("::1")

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)