From 2d2127ec2bf014c3d46d8c3c381cf52e432cef9f Mon Sep 17 00:00:00 2001 From: Shivansh Arora Date: Tue, 4 Jun 2024 20:10:40 +0530 Subject: [PATCH] Address PR comments Signed-off-by: Shivansh Arora --- .../common/transport/TransportAddress.java | 17 ++++++---- .../cluster/node/DiscoveryNode.java | 9 ++++- .../core/common/TransportAddressTests.java | 34 +++++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 server/src/test/java/org/opensearch/core/common/TransportAddressTests.java diff --git a/libs/core/src/main/java/org/opensearch/core/common/transport/TransportAddress.java b/libs/core/src/main/java/org/opensearch/core/common/transport/TransportAddress.java index 42982fefe5952..527b53edd849c 100644 --- a/libs/core/src/main/java/org/opensearch/core/common/transport/TransportAddress.java +++ b/libs/core/src/main/java/org/opensearch/core/common/transport/TransportAddress.java @@ -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}). @@ -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); } } diff --git a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java index 04112b0efc313..77f2b0c8504a0 100644 --- a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java +++ b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java @@ -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; @@ -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); @@ -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(); @@ -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 [" diff --git a/server/src/test/java/org/opensearch/core/common/TransportAddressTests.java b/server/src/test/java/org/opensearch/core/common/TransportAddressTests.java new file mode 100644 index 0000000000000..b234847ca4885 --- /dev/null +++ b/server/src/test/java/org/opensearch/core/common/TransportAddressTests.java @@ -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()); + } +}