From 7012bdd0542b6c494ff3209df32b80647edb033c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E9=BE=99=E9=BE=99?= Date: Tue, 26 Nov 2024 17:06:18 +0800 Subject: [PATCH] =?UTF-8?q?[DSIP-84][Feature]=20Update=20the=20connection?= =?UTF-8?q?=20address=20of=20Cach=C3=A9=20datasource(#16833)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/param/CacheDataSourceProcessor.java | 26 ++++++++++++++++--- .../param/CacheDataSourceProcessorTest.java | 17 ++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-cache/src/main/java/org/apache/dolphinscheduler/plugin/datasource/cache/param/CacheDataSourceProcessor.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-cache/src/main/java/org/apache/dolphinscheduler/plugin/datasource/cache/param/CacheDataSourceProcessor.java index 11edb79fd873..1b3b3a29e28b 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-cache/src/main/java/org/apache/dolphinscheduler/plugin/datasource/cache/param/CacheDataSourceProcessor.java +++ b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-cache/src/main/java/org/apache/dolphinscheduler/plugin/datasource/cache/param/CacheDataSourceProcessor.java @@ -59,12 +59,30 @@ public BaseDataSourceParamDTO createDatasourceParamDTO(String connectionJson) { mysqlDatasourceParamDTO.setUserName(connectionParams.getUser()); mysqlDatasourceParamDTO.setDatabase(connectionParams.getDatabase()); String address = connectionParams.getAddress(); + try { + String[] hostPortSplit = getHostPortSplit(address); + mysqlDatasourceParamDTO.setPort(Integer.parseInt(hostPortSplit[1])); + mysqlDatasourceParamDTO.setHost(hostPortSplit[0]); + } catch (NumberFormatException e) { + log.error("Invalid port number", e); + } + return mysqlDatasourceParamDTO; + } + + public static String[] getHostPortSplit(String address) { String[] hostSeperator = address.split(Constants.DOUBLE_SLASH); + if (hostSeperator.length < 2) { + log.error("Invalid address format"); + } String[] hostPortArray = hostSeperator[hostSeperator.length - 1].split(Constants.COMMA); - mysqlDatasourceParamDTO.setPort(Integer.parseInt(hostPortArray[0].split(Constants.COLON)[1])); - mysqlDatasourceParamDTO.setHost(hostPortArray[0].split(Constants.COLON)[0]); - - return mysqlDatasourceParamDTO; + if (hostPortArray.length < 1) { + log.error("Invalid hostPortArray format"); + } + String[] hostPortSplit = hostPortArray[0].split(Constants.COLON); + if (hostPortSplit.length < 2) { + log.error("Invalid hostPortSplit format"); + } + return hostPortSplit; } @Override diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-cache/src/test/java/org/apache/dolphinscheduler/plugin/datasource/cache/param/CacheDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-cache/src/test/java/org/apache/dolphinscheduler/plugin/datasource/cache/param/CacheDataSourceProcessorTest.java index 14308ab1e5a0..0069e553ac2c 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-cache/src/test/java/org/apache/dolphinscheduler/plugin/datasource/cache/param/CacheDataSourceProcessorTest.java +++ b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-cache/src/test/java/org/apache/dolphinscheduler/plugin/datasource/cache/param/CacheDataSourceProcessorTest.java @@ -21,6 +21,7 @@ import org.apache.dolphinscheduler.plugin.datasource.api.utils.PasswordUtils; import org.apache.dolphinscheduler.spi.enums.DbType; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -126,4 +127,20 @@ void testGetDatasourceUniqueId() { } } + @Test + void testGetHostPortSplit() { + String address = "jdbc:Cache://localhost:1972"; + String[] expected = {"localhost", "1972"}; + String[] result = CacheDataSourceProcessor.getHostPortSplit(address); + Assertions.assertArrayEquals(expected, result); + } + + @Test + void testGetHostPortSplit_InvalidFormat() { + String invalidAddress = "invalid-format"; + String[] hostPortSplit = CacheDataSourceProcessor.getHostPortSplit(invalidAddress); + Assertions.assertEquals("[invalid-format]", Arrays.toString(hostPortSplit)); + + } + }