This is an automated email from the ASF dual-hosted git repository.

wangchao316 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 3e1feac4d9e [IOTDB-1476]IoTDB support IPv6 (#11457)
3e1feac4d9e is described below

commit 3e1feac4d9ed0c9da0281d3bf91f88ed0bf55dc1
Author: wangchao316 <[email protected]>
AuthorDate: Wed Nov 8 17:19:45 2023 +0800

    [IOTDB-1476]IoTDB support IPv6 (#11457)
    
    [IOTDB-1476]IoTDB support IPv6
---
 .../src/main/java/org/apache/iotdb/jdbc/Utils.java | 27 ++++++++--
 .../test/java/org/apache/iotdb/jdbc/UtilsTest.java | 25 +++++++++
 .../main/java/org/apache/iotdb/rpc/UrlUtils.java   | 52 ++++++++++++++++++
 .../java/org/apache/iotdb/rpc/UrlUtilsTest.java    | 61 ++++++++++++++++++++++
 .../apache/iotdb/session/util/SessionUtils.java    | 18 +------
 .../apache/iotdb/commons/utils/NodeUrlUtils.java   | 23 +++-----
 .../iotdb/commons/utils/NodeUrlUtilsTest.java      | 33 ++++++++++++
 7 files changed, 202 insertions(+), 37 deletions(-)

diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java 
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java
index c40b057bfda..af884d3e781 100644
--- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java
+++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java
@@ -32,7 +32,11 @@ public class Utils {
     "squid:S5843",
     "squid:S5998"
   }) // Regular expressions should not be too complicated
-  static final Pattern URL_PATTERN = 
Pattern.compile("([^:]+):([0-9]{1,5})(/|\\?.*=.*(&.*=.*)*)?");
+  static final Pattern SUFFIX_URL_PATTERN = 
Pattern.compile("([0-9]{1,5})(/|\\\\?.*=.*(&.*=.*)*)?");
+
+  static final String COLON = ":";
+  static final String SLASH = "/";
+  static final String PARAMETER_SEPARATOR = "?";
 
   static final String RPC_COMPRESS = "rpc_compress";
 
@@ -45,11 +49,18 @@ public class Utils {
     if (url.trim().equalsIgnoreCase(Config.IOTDB_URL_PREFIX)) {
       return params;
     }
+
     boolean isUrlLegal = false;
     Matcher matcher = null;
+    String host = null;
+    String suffixURL = null;
     if (url.startsWith(Config.IOTDB_URL_PREFIX)) {
       String subURL = url.substring(Config.IOTDB_URL_PREFIX.length());
-      matcher = URL_PATTERN.matcher(subURL);
+      int i = subURL.lastIndexOf(COLON);
+      host = subURL.substring(0, i);
+      suffixURL = subURL.substring(i + 1);
+
+      matcher = SUFFIX_URL_PATTERN.matcher(suffixURL);
       if (matcher.matches() && parseUrlParam(subURL, info)) {
         isUrlLegal = true;
       }
@@ -59,8 +70,16 @@ public class Utils {
           "Error url format, url should be jdbc:iotdb://anything:port/ or 
jdbc:iotdb://anything:port?property1=value1&property2=value2");
     }
 
-    params.setHost(matcher.group(1));
-    params.setPort(Integer.parseInt(matcher.group(2)));
+    params.setHost(host);
+
+    // parse port
+    String port = suffixURL;
+    if (suffixURL.contains(PARAMETER_SEPARATOR)) {
+      port = suffixURL.split("\\" + PARAMETER_SEPARATOR)[0];
+    } else if (suffixURL.contains(SLASH)) {
+      port = suffixURL.substring(0, suffixURL.length() - 1);
+    }
+    params.setPort(Integer.parseInt(port));
 
     if (info.containsKey(Config.AUTH_USER)) {
       params.setUsername(info.getProperty(Config.AUTH_USER));
diff --git 
a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/UtilsTest.java 
b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/UtilsTest.java
index 37063a8d992..4c401b88017 100644
--- a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/UtilsTest.java
+++ b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/UtilsTest.java
@@ -65,6 +65,31 @@ public class UtilsTest {
     assertEquals(params.getPassword(), userPwd);
   }
 
+  @Test
+  public void testParseIPV6URL() throws IoTDBURLException {
+    String userName = "test";
+    String userPwd = "test";
+    String host1 =
+        
"AD80:E32B:CA25:B3AE:DA4A:DAAF:EEAE:BBBE,AD80:E32B:CA25:B3AE:DAAA:DAAF:CADE:EEAE:BBBE,AD80:E32B:CA25:B3AE:DA4A:DAAF:EEAE:BBBE";
+    int port = 6667;
+    Properties properties = new Properties();
+    properties.setProperty(Config.AUTH_USER, userName);
+    properties.setProperty(Config.AUTH_PASSWORD, userPwd);
+    IoTDBConnectionParams params =
+        Utils.parseUrl(String.format(Config.IOTDB_URL_PREFIX + "%s:%s/", 
host1, port), properties);
+    assertEquals(host1, params.getHost());
+    assertEquals(port, params.getPort());
+    assertEquals(userName, params.getUsername());
+    assertEquals(userPwd, params.getPassword());
+
+    params =
+        Utils.parseUrl(String.format(Config.IOTDB_URL_PREFIX + "%s:%s", host1, 
port), properties);
+    assertEquals(params.getHost(), host1);
+    assertEquals(params.getPort(), port);
+    assertEquals(params.getUsername(), userName);
+    assertEquals(params.getPassword(), userPwd);
+  }
+
   @Test(expected = IoTDBURLException.class)
   public void testParseWrongUrl1() throws IoTDBURLException {
     Properties properties = new Properties();
diff --git 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/UrlUtils.java 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/UrlUtils.java
new file mode 100644
index 00000000000..a7994a8a520
--- /dev/null
+++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/UrlUtils.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.rpc;
+
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+
+/** The UrlUtils */
+public class UrlUtils {
+  private static final String POINT_COLON = ":";
+  private static final String ABB_COLON = "[";
+
+  private UrlUtils() {}
+
+  /**
+   * Parse TEndPoint from a given TEndPointUrl
+   * example:[D80:0000:0000:0000:ABAA:0000:00C2:0002]:22227
+   *
+   * @param endPointUrl ip:port
+   * @return TEndPoint null if parse error
+   */
+  public static TEndPoint parseTEndPointIpv4AndIpv6Url(String endPointUrl) {
+    TEndPoint endPoint = new TEndPoint();
+    if (endPointUrl.contains(POINT_COLON)) {
+      int point_position = endPointUrl.lastIndexOf(POINT_COLON);
+      String port = endPointUrl.substring(endPointUrl.lastIndexOf(POINT_COLON) 
+ 1);
+      String ip = endPointUrl.substring(0, point_position);
+      if (ip.contains(ABB_COLON)) {
+        ip = ip.substring(1, ip.length() - 1);
+      }
+      endPoint.setIp(ip);
+      endPoint.setPort(Integer.parseInt(port));
+    }
+    return endPoint;
+  }
+}
diff --git 
a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/UrlUtilsTest.java 
b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/UrlUtilsTest.java
new file mode 100644
index 00000000000..1d206973d13
--- /dev/null
+++ 
b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/UrlUtilsTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.rpc;
+
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class UrlUtilsTest {
+
+  @Test
+  public void testParseIPV6URL() {
+    String hostAndPoint = "D80:0000:0000:0000:ABAA:00BB:EEAA:BBDD:22227";
+    TEndPoint endPoint = UrlUtils.parseTEndPointIpv4AndIpv6Url(hostAndPoint);
+    assertEquals(endPoint.getIp(), "D80:0000:0000:0000:ABAA:00BB:EEAA:BBDD");
+    assertEquals(endPoint.getPort(), 22227);
+  }
+
+  @Test
+  public void testParseIPV6URL_2() {
+    String hostAndPoint = "[D80:0000:0000:0000:ABAA:00BB:EEAA:BBDD]:22227";
+    TEndPoint endPoint = UrlUtils.parseTEndPointIpv4AndIpv6Url(hostAndPoint);
+    assertEquals(endPoint.getIp(), "D80:0000:0000:0000:ABAA:00BB:EEAA:BBDD");
+    assertEquals(endPoint.getPort(), 22227);
+  }
+
+  @Test
+  public void testParseIPV6AbbURL() {
+    String hostAndPoint = "[D80::ABAA:0]:22227";
+    TEndPoint endPoint = UrlUtils.parseTEndPointIpv4AndIpv6Url(hostAndPoint);
+    assertEquals(endPoint.getIp(), "D80::ABAA:0");
+    assertEquals(endPoint.getPort(), 22227);
+  }
+
+  @Test
+  public void testParseIPV4URL() {
+    String hostAndPoint = "192.0.0.1:22227";
+    TEndPoint endPoint = UrlUtils.parseTEndPointIpv4AndIpv6Url(hostAndPoint);
+    assertEquals(endPoint.getIp(), "192.0.0.1");
+    assertEquals(endPoint.getPort(), 22227);
+  }
+}
diff --git 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
index 52265c606ba..3f7a9b60261 100644
--- 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
+++ 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
@@ -21,6 +21,7 @@ package org.apache.iotdb.session.util;
 
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.UrlUtils;
 import org.apache.iotdb.tsfile.common.conf.TSFileConfig;
 import org.apache.iotdb.tsfile.enums.TSDataType;
 import org.apache.iotdb.tsfile.exception.UnSupportedDataTypeException;
@@ -254,26 +255,11 @@ public class SessionUtils {
     }
     List<TEndPoint> endPointsList = new ArrayList<>();
     for (String nodeUrl : nodeUrls) {
-      TEndPoint endPoint = parseNodeUrl(nodeUrl);
+      TEndPoint endPoint = UrlUtils.parseTEndPointIpv4AndIpv6Url(nodeUrl);
       endPointsList.add(endPoint);
     }
     return endPointsList;
   }
 
-  private static TEndPoint parseNodeUrl(String nodeUrl) {
-    TEndPoint endPoint = new TEndPoint();
-    String[] split = nodeUrl.split(":");
-    if (split.length != 2) {
-      throw new NumberFormatException("NodeUrl Incorrect format");
-    }
-    String ip = split[0];
-    try {
-      int rpcPort = Integer.parseInt(split[1]);
-      return endPoint.setIp(ip).setPort(rpcPort);
-    } catch (Exception e) {
-      throw new NumberFormatException("NodeUrl Incorrect format");
-    }
-  }
-
   private SessionUtils() {}
 }
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/NodeUrlUtils.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/NodeUrlUtils.java
index 8cf64a137a4..bac122873f4 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/NodeUrlUtils.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/NodeUrlUtils.java
@@ -22,6 +22,7 @@ package org.apache.iotdb.commons.utils;
 import org.apache.iotdb.common.rpc.thrift.TConfigNodeLocation;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.commons.exception.BadNodeUrlException;
+import org.apache.iotdb.rpc.UrlUtils;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -77,21 +78,7 @@ public class NodeUrlUtils {
    * @throws BadNodeUrlException Throw when unable to parse
    */
   public static TEndPoint parseTEndPointUrl(String endPointUrl) throws 
BadNodeUrlException {
-    String[] split = endPointUrl.split(":");
-    if (split.length != 2) {
-      logger.warn("Illegal endpoint url format: {}", endPointUrl);
-      throw new BadNodeUrlException(String.format("Bad endpoint url: %s", 
endPointUrl));
-    }
-    String ip = split[0];
-    TEndPoint result;
-    try {
-      int port = Integer.parseInt(split[1]);
-      result = new TEndPoint(ip, port);
-    } catch (NumberFormatException e) {
-      logger.warn("Illegal endpoint url format: {}", endPointUrl);
-      throw new BadNodeUrlException(String.format("Bad node url: %s", 
endPointUrl));
-    }
-    return result;
+    return UrlUtils.parseTEndPointIpv4AndIpv6Url(endPointUrl);
   }
 
   /**
@@ -105,7 +92,7 @@ public class NodeUrlUtils {
       throws BadNodeUrlException {
     List<TEndPoint> result = new ArrayList<>();
     for (String url : endPointUrls) {
-      result.add(parseTEndPointUrl(url));
+      result.add(UrlUtils.parseTEndPointIpv4AndIpv6Url(url));
     }
     return result;
   }
@@ -164,7 +151,9 @@ public class NodeUrlUtils {
       throw new BadNodeUrlException(String.format("Bad node url: %s", 
configNodeUrl));
     }
     return new TConfigNodeLocation(
-        Integer.parseInt(split[0]), parseTEndPointUrl(split[1]), 
parseTEndPointUrl(split[2]));
+        Integer.parseInt(split[0]),
+        UrlUtils.parseTEndPointIpv4AndIpv6Url(split[1]),
+        UrlUtils.parseTEndPointIpv4AndIpv6Url(split[2]));
   }
 
   /**
diff --git 
a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/NodeUrlUtilsTest.java
 
b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/NodeUrlUtilsTest.java
index 977ab570393..6f6e86ac8de 100644
--- 
a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/NodeUrlUtilsTest.java
+++ 
b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/NodeUrlUtilsTest.java
@@ -59,4 +59,37 @@ public class NodeUrlUtilsTest {
     Assert.assertEquals(configNodeUrls, 
NodeUrlUtils.convertTConfigNodeUrls(configNodeLocations));
     Assert.assertEquals(configNodeLocations, 
NodeUrlUtils.parseTConfigNodeUrls(configNodeUrls));
   }
+
+  @Test
+  public void parseAndConvertTEndPointUrlsIPV4AndIPV6Test() throws 
BadNodeUrlException {
+    final List<TEndPoint> endPoints =
+        Arrays.asList(
+            new TEndPoint("AD80:E32B:CA25:B3AE:DC4C:DAAF:CCDE:2345", 6667),
+            new TEndPoint("0:0:0:0:0:FFFF:129.144.52.38", 6668),
+            new TEndPoint("::13.1.68.3", 6669));
+    final String endPointUrls =
+        
"AD80:E32B:CA25:B3AE:DC4C:DAAF:CCDE:2345:6667,[0:0:0:0:0:FFFF:129.144.52.38]:6668,[::13.1.68.3]:6669";
+    Assert.assertEquals(endPoints, 
NodeUrlUtils.parseTEndPointUrls(endPointUrls));
+  }
+
+  @Test
+  public void parseAndConvertTConfigNodeUrlsIPV4AndIPV6Test() throws 
BadNodeUrlException {
+    final List<TConfigNodeLocation> configNodeLocations =
+        Arrays.asList(
+            new TConfigNodeLocation(
+                0,
+                new TEndPoint("AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD", 
22277),
+                new TEndPoint("AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD", 
22278)),
+            new TConfigNodeLocation(
+                1,
+                new TEndPoint("AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD", 
22279),
+                new TEndPoint("AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD", 
22280)),
+            new TConfigNodeLocation(
+                2,
+                new TEndPoint("AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD", 
22281),
+                new TEndPoint("AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD", 
22282)));
+    final String configNodeUrls =
+        
"0,AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD:22277,[AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD]:22278;1,AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD:22279,AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD:22280;2,AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD:22281,AD80:E32B:CA25:B3AE:DC4C:DAAF:CDDE:ABFD:22282";
+    Assert.assertEquals(configNodeLocations, 
NodeUrlUtils.parseTConfigNodeUrls(configNodeUrls));
+  }
 }

Reply via email to