From a8f3e12d216873806f45830776e5b26418e7ef21 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 26 Sep 2023 00:07:50 +0800 Subject: [PATCH 1/5] Refactor StorageResourceCreator --- .../database/resource/StorageResourceCreator.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java index c76ec08d82cf1..93f7ad5ec0514 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java @@ -104,13 +104,11 @@ public static StorageResource createStorageResourceWithoutDataSource(final Map newPropsMap = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), entry.getValue())); - if (storageNodes.containsKey(storageNode)) { - appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue()); - continue; + if (!storageNodes.containsKey(storageNode)) { + storageNodes.put(storageNode, null); + newPropsMap.put(storageNode.getName(), entry.getValue()); } - storageNodes.put(storageNode, null); appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue()); - newPropsMap.put(storageNode.getName(), entry.getValue()); } return new StorageResource(storageNodes, mappers, newPropsMap); } From 52c5fe14897d8fe81d190664fa7a4c020be2f223 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 26 Sep 2023 00:12:15 +0800 Subject: [PATCH 2/5] Refactor StorageResourceCreator --- .../database/resource/StorageResourceCreator.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java index 93f7ad5ec0514..a8f52f0f6bda6 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java @@ -79,17 +79,17 @@ private static String generateStorageNodeName(final String hostname, final int p return String.format("%s_%s_%s", hostname, port, username); } - private static void appendStorageUnitNodeMapper(final Map storageUnitNodeMappers, final StorageNode storageNode, + private static void appendStorageUnitNodeMapper(final Map mappers, final StorageNode storageNode, final String storageUnitName, final DataSourcePoolProperties props) { String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString(); - storageUnitNodeMappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, DatabaseTypeFactory.get(url), storageUnitName, url)); + mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, DatabaseTypeFactory.get(url), storageUnitName, url)); } - private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode storageNode, final DatabaseType databaseType, final String unitName, final String url) { + private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode storageNode, final DatabaseType databaseType, final String storageUnitName, final String url) { DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData(); return dialectDatabaseMetaData.isInstanceConnectionAvailable() - ? new StorageUnitNodeMapper(unitName, storageNode, new StandardJdbcUrlParser().parse(url).getDatabase(), url) - : new StorageUnitNodeMapper(unitName, storageNode, url); + ? new StorageUnitNodeMapper(storageUnitName, storageNode, new StandardJdbcUrlParser().parse(url).getDatabase(), url) + : new StorageUnitNodeMapper(storageUnitName, storageNode, url); } /** From eca8a61e03cfe231468893251c4e0ac477c565ba Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 26 Sep 2023 00:19:56 +0800 Subject: [PATCH 3/5] Refactor StorageResourceCreator --- .../resource/StorageResourceCreator.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java index a8f52f0f6bda6..e99c5806d1a09 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java @@ -52,20 +52,21 @@ public static StorageResource createStorageResource(final Map storageNodes = new LinkedHashMap<>(); Map mappers = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { - StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), entry.getValue())); + String storageUnitName = entry.getKey(); + Map standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties(); + String url = standardProps.get("url").toString(); + String username = standardProps.get("username").toString(); + DatabaseType databaseType = DatabaseTypeFactory.get(url); + StorageNode storageNode = new StorageNode(getStorageNodeName(storageUnitName, url, username, databaseType)); if (!storageNodes.containsKey(storageNode)) { - storageNodes.put(storageNode, DataSourcePoolCreator.create(entry.getKey(), entry.getValue(), true, storageNodes.values())); + storageNodes.put(storageNode, DataSourcePoolCreator.create(storageUnitName, entry.getValue(), true, storageNodes.values())); } - appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue()); + mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, databaseType, storageUnitName, url)); } return new StorageResource(storageNodes, mappers); } - private static String getStorageNodeName(final String dataSourceName, final DataSourcePoolProperties storageNodeProps) { - Map standardProps = storageNodeProps.getConnectionPropertySynonyms().getStandardProperties(); - String url = standardProps.get("url").toString(); - String username = standardProps.get("username").toString(); - DatabaseType databaseType = DatabaseTypeFactory.get(url); + private static String getStorageNodeName(final String dataSourceName, final String url, final String username, final DatabaseType databaseType) { try { JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url); DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData(); @@ -79,12 +80,6 @@ private static String generateStorageNodeName(final String hostname, final int p return String.format("%s_%s_%s", hostname, port, username); } - private static void appendStorageUnitNodeMapper(final Map mappers, final StorageNode storageNode, - final String storageUnitName, final DataSourcePoolProperties props) { - String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString(); - mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, DatabaseTypeFactory.get(url), storageUnitName, url)); - } - private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode storageNode, final DatabaseType databaseType, final String storageUnitName, final String url) { DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData(); return dialectDatabaseMetaData.isInstanceConnectionAvailable() @@ -103,12 +98,17 @@ public static StorageResource createStorageResourceWithoutDataSource(final Map mappers = new LinkedHashMap<>(); Map newPropsMap = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { - StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), entry.getValue())); + String storageUnitName = entry.getKey(); + Map standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties(); + String url = standardProps.get("url").toString(); + String username = standardProps.get("username").toString(); + DatabaseType databaseType = DatabaseTypeFactory.get(url); + StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), url, username, databaseType)); if (!storageNodes.containsKey(storageNode)) { storageNodes.put(storageNode, null); newPropsMap.put(storageNode.getName(), entry.getValue()); } - appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue()); + mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, databaseType, storageUnitName, url)); } return new StorageResource(storageNodes, mappers, newPropsMap); } From 7c0b07f9e4889cb43189cf8fae591ec11b683ed5 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 26 Sep 2023 00:23:43 +0800 Subject: [PATCH 4/5] Refactor StorageResourceCreator --- .../resource/StorageResourceCreator.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java index e99c5806d1a09..7880cb328c6eb 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java @@ -22,7 +22,6 @@ import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrl; import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser; import org.apache.shardingsphere.infra.database.core.connector.url.UnrecognizedDatabaseURLException; -import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; @@ -57,20 +56,20 @@ public static StorageResource createStorageResource(final Map Date: Tue, 26 Sep 2023 00:26:21 +0800 Subject: [PATCH 5/5] Refactor StorageResourceCreator --- .../database/resource/StorageResourceCreator.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java index 7880cb328c6eb..8f1bf5c93a071 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java @@ -22,7 +22,6 @@ import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrl; import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser; import org.apache.shardingsphere.infra.database.core.connector.url.UnrecognizedDatabaseURLException; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; @@ -54,10 +53,8 @@ public static StorageResource createStorageResource(final Map standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties(); String url = standardProps.get("url").toString(); - String username = standardProps.get("username").toString(); - DatabaseType databaseType = DatabaseTypeFactory.get(url); - boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); - StorageNode storageNode = new StorageNode(getStorageNodeName(storageUnitName, url, username, isInstanceConnectionAvailable)); + boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); + StorageNode storageNode = new StorageNode(getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); if (!storageNodes.containsKey(storageNode)) { storageNodes.put(storageNode, DataSourcePoolCreator.create(storageUnitName, entry.getValue(), true, storageNodes.values())); } @@ -99,10 +96,8 @@ public static StorageResource createStorageResourceWithoutDataSource(final Map standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties(); String url = standardProps.get("url").toString(); - String username = standardProps.get("username").toString(); - DatabaseType databaseType = DatabaseTypeFactory.get(url); - boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); - StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), url, username, isInstanceConnectionAvailable)); + boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); + StorageNode storageNode = new StorageNode(getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); if (!storageNodes.containsKey(storageNode)) { storageNodes.put(storageNode, null); newPropsMap.put(storageNode.getName(), entry.getValue());