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

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


The following commit(s) were added to refs/heads/master by this push:
     new c6c8c4db180 Refactor StorageNodeAggregator (#36974)
c6c8c4db180 is described below

commit c6c8c4db180e29f970e2bba562ca8b36ed0c70b0
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Oct 30 22:03:02 2025 +0800

    Refactor StorageNodeAggregator (#36974)
    
    * Refactor StorageNodeAggregator
    
    * Refactor StorageNodeAggregator
---
 .../resource/node/StorageNodeAggregator.java       | 17 ++++---
 .../resource/node/StorageNodeAggregatorTest.java   | 55 ++++++++++++++++++++++
 2 files changed, 65 insertions(+), 7 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeAggregator.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeAggregator.java
index 93bb9a0a255..4cf955c5b6e 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeAggregator.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeAggregator.java
@@ -23,6 +23,7 @@ import 
org.apache.shardingsphere.database.connector.core.exception.UnrecognizedD
 import 
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionProperties;
 import 
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionPropertiesParser;
 import 
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
 import 
org.apache.shardingsphere.database.connector.core.type.DatabaseTypeFactory;
 import 
org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
 import 
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
@@ -57,21 +58,23 @@ public final class StorageNodeAggregator {
      * @return storage node and data source pool properties map
      */
     public static Map<StorageNode, DataSourcePoolProperties> 
aggregateDataSourcePoolProperties(final Map<String, DataSourcePoolProperties> 
storageUnitDataSourcePoolPropsMap) {
-        Map<StorageNode, DataSourcePoolProperties> result = new 
LinkedHashMap<>();
+        Map<StorageNode, DataSourcePoolProperties> result = new 
LinkedHashMap<>(storageUnitDataSourcePoolPropsMap.size(), 1F);
         for (Entry<String, DataSourcePoolProperties> entry : 
storageUnitDataSourcePoolPropsMap.entrySet()) {
             Map<String, Object> standardProps = 
entry.getValue().getConnectionPropertySynonyms().getStandardProperties();
             String url = standardProps.get("url").toString();
-            boolean isInstanceConnectionAvailable = new 
DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().getConnectionOption().isInstanceConnectionAvailable();
-            StorageNode storageNode = getStorageNode(entry.getKey(), url, 
standardProps.get("username").toString(), isInstanceConnectionAvailable);
-            result.putIfAbsent(storageNode, entry.getValue());
+            String username = standardProps.get("username").toString();
+            DatabaseType databaseType = DatabaseTypeFactory.get(url);
+            result.putIfAbsent(getStorageNode(entry.getKey(), url, username, 
databaseType), entry.getValue());
         }
         return result;
     }
     
-    private static StorageNode getStorageNode(final String dataSourceName, 
final String url, final String username, final boolean 
isInstanceConnectionAvailable) {
+    private static StorageNode getStorageNode(final String dataSourceName, 
final String url, final String username, final DatabaseType databaseType) {
         try {
-            ConnectionProperties connectionProps = 
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, 
DatabaseTypeFactory.get(url)).parse(url, username, null);
-            return isInstanceConnectionAvailable ? new 
StorageNode(connectionProps.getHostname(), connectionProps.getPort(), username) 
: new StorageNode(dataSourceName);
+            ConnectionProperties connectionProps = 
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, 
databaseType).parse(url, username, null);
+            return new 
DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().getConnectionOption().isInstanceConnectionAvailable()
+                    ? new StorageNode(connectionProps.getHostname(), 
connectionProps.getPort(), username)
+                    : new StorageNode(dataSourceName);
         } catch (final UnrecognizedDatabaseURLException ex) {
             return new StorageNode(dataSourceName);
         }
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeAggregatorTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeAggregatorTest.java
new file mode 100644
index 00000000000..a0e94a4df68
--- /dev/null
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeAggregatorTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.shardingsphere.infra.metadata.database.resource.node;
+
+import 
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
+import org.junit.jupiter.api.Test;
+import org.mockito.Answers;
+
+import javax.sql.DataSource;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class StorageNodeAggregatorTest {
+    
+    @Test
+    void assertAggregateDataSources() {
+        DataSource dataSource = mock(DataSource.class);
+        Map<StorageNode, DataSource> actual = 
StorageNodeAggregator.aggregateDataSources(Collections.singletonMap("foo_ds", 
dataSource));
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get(new StorageNode("foo_ds")), is(dataSource));
+    }
+    
+    @Test
+    void assertAggregateDataSourcePoolProperties() {
+        Map<String, Object> standardProps = new HashMap<>(2, 1F);
+        standardProps.put("url", "jdbc:mock://127.0.0.1/foo_db");
+        standardProps.put("username", "root");
+        DataSourcePoolProperties dataSourcePoolProps = 
mock(DataSourcePoolProperties.class, Answers.RETURNS_DEEP_STUBS);
+        
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(standardProps);
+        Map<StorageNode, DataSourcePoolProperties> actual = 
StorageNodeAggregator.aggregateDataSourcePoolProperties(Collections.singletonMap("foo_ds",
 dataSourcePoolProps));
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get(actual.keySet().iterator().next()), 
is(dataSourcePoolProps));
+    }
+}

Reply via email to