Skip to content

Commit 2d2127e

Browse files
committed
Address PR comments
Signed-off-by: Shivansh Arora <hishiv@amazon.com>
1 parent 08a10cb commit 2d2127e

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

libs/core/src/main/java/org/opensearch/core/common/transport/TransportAddress.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
import org.opensearch.core.xcontent.XContentBuilder;
4242

4343
import java.io.IOException;
44+
import java.net.Inet6Address;
4445
import java.net.InetAddress;
4546
import java.net.InetSocketAddress;
4647
import java.net.UnknownHostException;
48+
import java.util.Arrays;
4749

4850
/**
4951
* A transport address used for IP socket address (wraps {@link java.net.InetSocketAddress}).
@@ -166,15 +168,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
166168
/**
167169
* Converts a string in the format [hostname/ip]:[port] into a transport address.
168170
* @throws UnknownHostException if the hostname or ip address is invalid
169-
* @throws IllegalArgumentException if invalid string format provided
170171
*/
171172
public static TransportAddress fromString(String address) throws UnknownHostException {
172173
String[] addressSplit = address.split(":");
173-
if (addressSplit.length != 2) {
174-
throw new IllegalArgumentException("address must be of the form [hostname/ip]:[port]");
174+
if (addressSplit.length == 2) {
175+
String hostname = addressSplit[0];
176+
int port = Integer.parseInt(addressSplit[1]);
177+
return new TransportAddress(InetAddress.getByName(hostname), port);
178+
} else {
179+
// this should be an IPv6 ip
180+
int port = Integer.parseInt(addressSplit[addressSplit.length - 1]);
181+
String hostname = String.join(":", Arrays.copyOfRange(addressSplit, 0, addressSplit.length - 1));
182+
return new TransportAddress(Inet6Address.getByName(hostname), port);
175183
}
176-
String hostname = addressSplit[0];
177-
int port = Integer.parseInt(addressSplit[1]);
178-
return new TransportAddress(InetAddress.getByName(hostname), port);
179184
}
180185
}

server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
package org.opensearch.cluster.node;
3434

35+
import org.apache.logging.log4j.LogManager;
36+
import org.apache.logging.log4j.Logger;
3537
import org.opensearch.Version;
3638
import org.opensearch.cluster.metadata.Metadata;
3739
import org.opensearch.common.UUIDs;
@@ -85,6 +87,7 @@ public class DiscoveryNode implements Writeable, ToXContentFragment {
8587
static final String KEY_ATTRIBUTES = "attributes";
8688
static final String KEY_VERSION = "version";
8789
static final String KEY_ROLES = "roles";
90+
private static final Logger logger = LogManager.getLogger(DiscoveryNode.class);
8891

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

589+
/**
590+
* This method is able to parse either a complete XContentObject with start and end object
591+
* or only a fragment with the key and value.
592+
*/
586593
public static DiscoveryNode fromXContent(XContentParser parser) throws IOException {
587594
if (parser.currentToken() == null) { // fresh parser? move to the first token
588595
parser.nextToken();
@@ -627,7 +634,7 @@ public static DiscoveryNode fromXContent(XContentParser parser) throws IOExcepti
627634
version = Version.fromString(parser.text());
628635
break;
629636
default:
630-
throw new IllegalArgumentException("Unexpected field [ " + currentFieldName + " ]");
637+
logger.warn("unknown field [{}]", currentFieldName);
631638
}
632639
} else if (token == XContentParser.Token.START_OBJECT) {
633640
assert currentFieldName.equals(KEY_ATTRIBUTES) : "expecting field with name ["
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.core.common;
10+
11+
import org.opensearch.core.common.transport.TransportAddress;
12+
import org.opensearch.test.OpenSearchTestCase;
13+
14+
import java.net.UnknownHostException;
15+
16+
public class TransportAddressTests extends OpenSearchTestCase {
17+
public void testFromString() throws UnknownHostException {
18+
TransportAddress address = TransportAddress.fromString("127.0.0.1:9300");
19+
assertEquals("127.0.0.1", address.getAddress());
20+
assertEquals(9300, address.getPort());
21+
22+
address = TransportAddress.fromString("1080:0:0:0:8:800:200C:417A:9300");
23+
assertEquals("1080::8:800:200c:417a", address.getAddress());
24+
assertEquals(9300, address.getPort());
25+
26+
address = TransportAddress.fromString("FF01:0:0:0:0:0:0:101:9300");
27+
assertEquals("ff01::101", address.getAddress());
28+
assertEquals(9300, address.getPort());
29+
30+
address = TransportAddress.fromString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:9200");
31+
assertEquals("fedc:ba98:7654:3210:fedc:ba98:7654:3210", address.getAddress());
32+
assertEquals(9200, address.getPort());
33+
}
34+
}

0 commit comments

Comments
 (0)