Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Arora <hishiv@amazon.com>
  • Loading branch information
shiv0408 committed Jun 5, 2024
1 parent 08a10cb commit 2d2127e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

/**
* A transport address used for IP socket address (wraps {@link java.net.InetSocketAddress}).
Expand Down Expand Up @@ -166,15 +168,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
/**
* Converts a string in the format [hostname/ip]:[port] into a transport address.
* @throws UnknownHostException if the hostname or ip address is invalid
* @throws IllegalArgumentException if invalid string format provided
*/
public static TransportAddress fromString(String address) throws UnknownHostException {
String[] addressSplit = address.split(":");
if (addressSplit.length != 2) {
throw new IllegalArgumentException("address must be of the form [hostname/ip]:[port]");
if (addressSplit.length == 2) {
String hostname = addressSplit[0];
int port = Integer.parseInt(addressSplit[1]);
return new TransportAddress(InetAddress.getByName(hostname), port);
} else {
// this should be an IPv6 ip
int port = Integer.parseInt(addressSplit[addressSplit.length - 1]);
String hostname = String.join(":", Arrays.copyOfRange(addressSplit, 0, addressSplit.length - 1));
return new TransportAddress(Inet6Address.getByName(hostname), port);
}
String hostname = addressSplit[0];
int port = Integer.parseInt(addressSplit[1]);
return new TransportAddress(InetAddress.getByName(hostname), port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

package org.opensearch.cluster.node;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.Version;
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.common.UUIDs;
Expand Down Expand Up @@ -85,6 +87,7 @@ public class DiscoveryNode implements Writeable, ToXContentFragment {
static final String KEY_ATTRIBUTES = "attributes";
static final String KEY_VERSION = "version";
static final String KEY_ROLES = "roles";
private static final Logger logger = LogManager.getLogger(DiscoveryNode.class);

public static boolean nodeRequiresLocalStorage(Settings settings) {
boolean localStorageEnable = Node.NODE_LOCAL_STORAGE_SETTING.get(settings);
Expand Down Expand Up @@ -583,6 +586,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

/**
* This method is able to parse either a complete XContentObject with start and end object
* or only a fragment with the key and value.
*/
public static DiscoveryNode fromXContent(XContentParser parser) throws IOException {
if (parser.currentToken() == null) { // fresh parser? move to the first token
parser.nextToken();
Expand Down Expand Up @@ -627,7 +634,7 @@ public static DiscoveryNode fromXContent(XContentParser parser) throws IOExcepti
version = Version.fromString(parser.text());
break;
default:
throw new IllegalArgumentException("Unexpected field [ " + currentFieldName + " ]");
logger.warn("unknown field [{}]", currentFieldName);
}
} else if (token == XContentParser.Token.START_OBJECT) {
assert currentFieldName.equals(KEY_ATTRIBUTES) : "expecting field with name ["
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.core.common;

import org.opensearch.core.common.transport.TransportAddress;
import org.opensearch.test.OpenSearchTestCase;

import java.net.UnknownHostException;

public class TransportAddressTests extends OpenSearchTestCase {
public void testFromString() throws UnknownHostException {
TransportAddress address = TransportAddress.fromString("127.0.0.1:9300");
assertEquals("127.0.0.1", address.getAddress());
assertEquals(9300, address.getPort());

address = TransportAddress.fromString("1080:0:0:0:8:800:200C:417A:9300");
assertEquals("1080::8:800:200c:417a", address.getAddress());
assertEquals(9300, address.getPort());

address = TransportAddress.fromString("FF01:0:0:0:0:0:0:101:9300");
assertEquals("ff01::101", address.getAddress());
assertEquals(9300, address.getPort());

address = TransportAddress.fromString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:9200");
assertEquals("fedc:ba98:7654:3210:fedc:ba98:7654:3210", address.getAddress());
assertEquals(9200, address.getPort());
}
}

0 comments on commit 2d2127e

Please sign in to comment.