Skip to content

Commit

Permalink
Move resolvePublishPort to NetworkService for plugin re-use
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Carroll <[email protected]>
  • Loading branch information
finnegancarroll committed Dec 11, 2024
1 parent c82cd2e commit 687221f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Setting.Property;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.transport.TransportAddress;
import org.opensearch.core.common.unit.ByteSizeValue;

import java.io.IOException;
Expand All @@ -45,6 +46,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

Expand Down Expand Up @@ -237,6 +239,43 @@ public InetAddress resolvePublishHostAddresses(String publishHosts[]) throws IOE
return addresses[0];
}

/**
* 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<TransportAddress> boundAddresses, InetAddress publishInetAddress) {
if (publishPort < 0) {
for (TransportAddress boundAddress : boundAddresses) {
InetAddress boundInetAddress = boundAddress.address().getAddress();
if (boundInetAddress.isAnyLocalAddress() || boundInetAddress.equals(publishInetAddress)) {
publishPort = boundAddress.getPort();
break;
}
}
}

if (publishPort < 0) {
final Set<Integer> ports = new HashSet<>();
for (TransportAddress boundAddress : boundAddresses) {
ports.add(boundAddress.getPort());
}
if (ports.size() == 1) {
publishPort = ports.iterator().next();
}
}

return publishPort;
}

/** resolves (and deduplicates) host specification */
private InetAddress[] resolveInetAddresses(String hosts[]) throws IOException {
if (hosts.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import static org.opensearch.common.network.NetworkService.resolvePublishPort;
import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_BIND_HOST;
import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_MAX_CONTENT_LENGTH;
import static org.opensearch.http.HttpTransportSettings.SETTING_HTTP_PORT;
Expand Down Expand Up @@ -192,7 +192,21 @@ protected void bindServer() {
throw new BindTransportException("Failed to resolve publish address", e);
}

final int publishPort = resolvePublishPort(settings, boundAddresses, publishInetAddress);
final int publishPort = resolvePublishPort(SETTING_HTTP_PUBLISH_PORT.get(settings), boundAddresses, publishInetAddress);
if (publishPort < 0) {
throw new BindHttpException(
"Failed to auto-resolve http publish port, multiple bound addresses "
+ boundAddresses
+ " with distinct ports and none of them matched the publish address ("
+ publishInetAddress
+ "). "
+ "Please specify a unique port by setting "
+ SETTING_HTTP_PORT.getKey()
+ " or "
+ SETTING_HTTP_PUBLISH_PORT.getKey()
);
}

TransportAddress publishAddress = new TransportAddress(new InetSocketAddress(publishInetAddress, publishPort));
this.boundAddress = new BoundTransportAddress(boundAddresses.toArray(new TransportAddress[0]), publishAddress);
logger.info("{}", boundAddress);
Expand Down Expand Up @@ -258,47 +272,6 @@ protected void doClose() {}
*/
protected abstract void stopInternal();

// package private for tests
static int resolvePublishPort(Settings settings, List<TransportAddress> boundAddresses, InetAddress publishInetAddress) {
int publishPort = SETTING_HTTP_PUBLISH_PORT.get(settings);

if (publishPort < 0) {
for (TransportAddress boundAddress : boundAddresses) {
InetAddress boundInetAddress = boundAddress.address().getAddress();
if (boundInetAddress.isAnyLocalAddress() || boundInetAddress.equals(publishInetAddress)) {
publishPort = boundAddress.getPort();
break;
}
}
}

// if no matching boundAddress found, check if there is a unique port for all bound addresses
if (publishPort < 0) {
final Set<Integer> ports = new HashSet<>();
for (TransportAddress boundAddress : boundAddresses) {
ports.add(boundAddress.getPort());
}
if (ports.size() == 1) {
publishPort = ports.iterator().next();
}
}

if (publishPort < 0) {
throw new BindHttpException(
"Failed to auto-resolve http publish port, multiple bound addresses "
+ boundAddresses
+ " with distinct ports and none of them matched the publish address ("
+ publishInetAddress
+ "). "
+ "Please specify a unique port by setting "
+ SETTING_HTTP_PORT.getKey()
+ " or "
+ SETTING_HTTP_PUBLISH_PORT.getKey()
);
}
return publishPort;
}

public void onException(HttpChannel channel, Exception e) {
channel.handleException(e);
if (lifecycle.started() == false) {
Expand Down

0 comments on commit 687221f

Please sign in to comment.