From 49b71f5ea64f6657e45449f3b4965ed0c336ba47 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 7 Oct 2023 15:52:12 +0800 Subject: [PATCH 1/7] Use DatabaseConfiguration.getDataSources instead of getStorageResource --- .../infra/config/database/DatabaseConfiguration.java | 11 ++++++----- .../DataSourceGeneratedDatabaseConfiguration.java | 5 ++--- .../impl/DataSourceProvidedDatabaseConfiguration.java | 6 +++--- .../metadata/database/ShardingSphereDatabase.java | 8 ++++---- .../DataSourceGeneratedDatabaseConfigurationTest.java | 2 +- .../DataSourceProvidedDatabaseConfigurationTest.java | 2 +- .../metadata/persist/MetaDataPersistService.java | 2 +- .../mode/metadata/MetaDataContextsFactory.java | 2 +- .../mode/metadata/NewMetaDataContextsFactory.java | 2 +- .../swapper/YamlProxyConfigurationSwapperTest.java | 2 +- 10 files changed, 21 insertions(+), 21 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/DatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/DatabaseConfiguration.java index 9ec063f76c05a..25adaa10f6724 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/DatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/DatabaseConfiguration.java @@ -18,9 +18,10 @@ package org.apache.shardingsphere.infra.config.database; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import javax.sql.DataSource; import java.util.Collection; import java.util.Map; @@ -44,9 +45,9 @@ public interface DatabaseConfiguration { Map getStorageUnits(); /** - * Get storage resource. - * - * @return storage resource + * Get data sources. + * + * @return data sources */ - StorageResource getStorageResource(); + Map getDataSources(); } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java index 8f1dd9fb20970..31c28a144ac9f 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java @@ -24,7 +24,6 @@ import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator; @@ -46,7 +45,7 @@ public final class DataSourceGeneratedDatabaseConfiguration implements DatabaseC private final Map storageUnits; - private final StorageResource storageResource; + private final Map dataSources; public DataSourceGeneratedDatabaseConfiguration(final Map dataSourceConfigs, final Collection ruleConfigs) { ruleConfigurations = ruleConfigs; @@ -62,7 +61,7 @@ public DataSourceGeneratedDatabaseConfiguration(final Map getStorageNodeDataSourceMap(final Map dataSourcePoolPropertiesMap, final Map storageUnitNodeMap) { diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java index 1f1fd121fb5d1..35a4f5993581f 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java @@ -45,7 +45,7 @@ public final class DataSourceProvidedDatabaseConfiguration implements DatabaseCo private final Map storageUnits; - private final StorageResource storageResource; + private final Map dataSources; public DataSourceProvidedDatabaseConfiguration(final Map dataSources, final Collection ruleConfigs) { this.ruleConfigurations = ruleConfigs; @@ -60,12 +60,11 @@ public DataSourceProvidedDatabaseConfiguration(final Map dat StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropertiesMap.get(storageUnitName), storageNodeDataSources.get(storageNode)); storageUnits.put(storageUnitName, storageUnit); } - storageResource = new StorageResource(storageNodeDataSources, storageUnitNodeMap); + this.dataSources = storageNodeDataSources; } public DataSourceProvidedDatabaseConfiguration(final StorageResource storageResource, final Collection ruleConfigs, final Map dataSourcePoolPropertiesMap) { - this.storageResource = storageResource; this.ruleConfigurations = ruleConfigs; Map storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropertiesMap); Map storageNodeDataSources = storageResource.getDataSources(); @@ -76,6 +75,7 @@ public DataSourceProvidedDatabaseConfiguration(final StorageResource storageReso StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropertiesMap.get(storageUnitName), storageNodeDataSources.get(storageNode)); storageUnits.put(storageUnitName, storageUnit); } + dataSources = storageNodeDataSources; } private Map createDataSourcePoolPropertiesMap(final Map dataSources) { diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index ed608937ac19b..88a63a1861a97 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder; @@ -121,13 +121,13 @@ public static ShardingSphereDatabase create(final String name, final DatabaseTyp */ public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig, final Collection rules, final Map schemas) { - ResourceMetaData resourceMetaData = createResourceMetaData(databaseConfig.getStorageResource(), databaseConfig.getStorageUnits()); + ResourceMetaData resourceMetaData = createResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits()); RuleMetaData ruleMetaData = new RuleMetaData(rules); return new ShardingSphereDatabase(name, protocolType, resourceMetaData, ruleMetaData, schemas); } - private static ResourceMetaData createResourceMetaData(final StorageResource storageResource, final Map storageUnits) { - return new ResourceMetaData(storageResource.getDataSources(), storageUnits); + private static ResourceMetaData createResourceMetaData(final Map dataSources, final Map storageUnits) { + return new ResourceMetaData(dataSources, storageUnits); } /** diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java index f2180ec39467c..db7ab6ae02a7b 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java @@ -50,7 +50,7 @@ void assertGetDataSources() { @Test void assertGetStorageNodes() { DataSourceGeneratedDatabaseConfiguration databaseConfig = createDataSourceGeneratedDatabaseConfiguration(); - HikariDataSource hikariDataSource = (HikariDataSource) databaseConfig.getStorageResource().getDataSources().get(new StorageNode("normal_db")); + HikariDataSource hikariDataSource = (HikariDataSource) databaseConfig.getDataSources().get(new StorageNode("normal_db")); assertThat(hikariDataSource.getJdbcUrl(), is("jdbc:mock://127.0.0.1/normal_db")); assertThat(hikariDataSource.getUsername(), is("root")); assertThat(hikariDataSource.getPassword(), is("")); diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java index 9c49325a45f95..e866277eb7626 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java @@ -45,7 +45,7 @@ void assertGetDataSources() { @Test void assertGetStorageNodes() { DataSourceProvidedDatabaseConfiguration databaseConfig = createDataSourceProvidedDatabaseConfiguration(); - MockedDataSource dataSource = (MockedDataSource) databaseConfig.getStorageResource().getDataSources().get(new StorageNode("foo_ds")); + MockedDataSource dataSource = (MockedDataSource) databaseConfig.getDataSources().get(new StorageNode("foo_ds")); assertThat(dataSource.getUrl(), is("jdbc:mock://127.0.0.1/foo_ds")); assertThat(dataSource.getUsername(), is("root")); assertThat(dataSource.getPassword(), is("root")); diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java index c219eb1adc0b0..7ee433e5be1b7 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java @@ -114,7 +114,7 @@ private Collection decorateRuleConfigs(final String databaseN private Map getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) { if (!databaseConfigs.getStorageUnits().isEmpty() && databaseConfigs.getStorageUnits().isEmpty()) { - return getDataSourcePoolPropertiesMap(databaseConfigs.getStorageResource().getDataSources()); + return getDataSourcePoolPropertiesMap(databaseConfigs.getDataSources()); } return databaseConfigs.getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java index 44feb29795e6c..164b709e9cd64 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java @@ -125,7 +125,7 @@ private static DatabaseConfiguration createEffectiveDatabaseConfiguration(final private static void closeGeneratedDataSources(final String databaseName, final Map databaseConfigs) { if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getStorageUnits().isEmpty()) { - databaseConfigs.get(databaseName).getStorageResource().getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy()); + databaseConfigs.get(databaseName).getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy()); } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java index ffb7a8c24791f..915ef28f0180b 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java @@ -127,7 +127,7 @@ private static DatabaseConfiguration createEffectiveDatabaseConfiguration(final private static void closeGeneratedDataSources(final String databaseName, final Map databaseConfigs) { if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getStorageUnits().isEmpty()) { - databaseConfigs.get(databaseName).getStorageResource().getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy()); + databaseConfigs.get(databaseName).getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy()); } } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java index d154b74b52c10..7efcf69056f09 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java @@ -56,7 +56,7 @@ void assertSwap() throws IOException { private void assertDataSources(final ProxyConfiguration proxyConfig) { Map actual = proxyConfig.getDatabaseConfigurations(); assertThat(actual.size(), is(1)); - HikariDataSource dataSource = (HikariDataSource) actual.get("swapper_test").getStorageResource().getDataSources().get(new StorageNode("foo_db")); + HikariDataSource dataSource = (HikariDataSource) actual.get("swapper_test").getDataSources().get(new StorageNode("foo_db")); assertThat(dataSource.getJdbcUrl(), is("jdbc:h2:mem:foo_db;DB_CLOSE_DELAY=-1")); assertThat(dataSource.getUsername(), is("sa")); assertThat(dataSource.getPassword(), is("")); From a6c7d3b7d3e2502423c31203a9525006ef27a3e0 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 7 Oct 2023 16:49:03 +0800 Subject: [PATCH 2/7] Refactor DataSourceProvidedDatabaseConfiguration --- .../impl/DataSourceProvidedDatabaseConfiguration.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java index 35a4f5993581f..72df3db5c4aea 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java @@ -64,15 +64,15 @@ public DataSourceProvidedDatabaseConfiguration(final Map dat } public DataSourceProvidedDatabaseConfiguration(final StorageResource storageResource, - final Collection ruleConfigs, final Map dataSourcePoolPropertiesMap) { + final Collection ruleConfigs, final Map dataSourcePoolPropsMap) { this.ruleConfigurations = ruleConfigs; - Map storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropertiesMap); + Map storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropsMap); Map storageNodeDataSources = storageResource.getDataSources(); - storageUnits = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F); - for (Entry entry : dataSourcePoolPropertiesMap.entrySet()) { + storageUnits = new LinkedHashMap<>(dataSourcePoolPropsMap.size(), 1F); + for (Entry entry : dataSourcePoolPropsMap.entrySet()) { String storageUnitName = entry.getKey(); StorageNode storageNode = storageUnitNodeMap.get(storageUnitName); - StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropertiesMap.get(storageUnitName), storageNodeDataSources.get(storageNode)); + StorageUnit storageUnit = new StorageUnit(storageNode, entry.getValue(), storageNodeDataSources.get(storageNode)); storageUnits.put(storageUnitName, storageUnit); } dataSources = storageNodeDataSources; From f267154b782d76fe7d04a7f4ceff3444d75bb76a Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 7 Oct 2023 16:49:54 +0800 Subject: [PATCH 3/7] Refactor DataSourceProvidedDatabaseConfiguration --- .../impl/DataSourceProvidedDatabaseConfiguration.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java index 72df3db5c4aea..1c4cbb9125e4f 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java @@ -52,12 +52,12 @@ public DataSourceProvidedDatabaseConfiguration(final Map dat Map storageUnitNodeMap = dataSources.keySet().stream() .collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); Map storageNodeDataSources = StorageNodeAggregator.aggregateDataSources(dataSources); - Map dataSourcePoolPropertiesMap = createDataSourcePoolPropertiesMap(dataSources); - storageUnits = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F); - for (Entry entry : dataSourcePoolPropertiesMap.entrySet()) { + Map dataSourcePoolPropsMap = createDataSourcePoolPropertiesMap(dataSources); + storageUnits = new LinkedHashMap<>(dataSourcePoolPropsMap.size(), 1F); + for (Entry entry : dataSourcePoolPropsMap.entrySet()) { String storageUnitName = entry.getKey(); StorageNode storageNode = storageUnitNodeMap.get(storageUnitName); - StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropertiesMap.get(storageUnitName), storageNodeDataSources.get(storageNode)); + StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropsMap.get(storageUnitName), storageNodeDataSources.get(storageNode)); storageUnits.put(storageUnitName, storageUnit); } this.dataSources = storageNodeDataSources; From 7d2d42d0db9ad5d34fcb7758aabec86da8a02b1d Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 7 Oct 2023 16:50:39 +0800 Subject: [PATCH 4/7] Refactor DataSourceProvidedDatabaseConfiguration --- .../database/impl/DataSourceProvidedDatabaseConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java index 1c4cbb9125e4f..fb3ce5271587b 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java @@ -57,7 +57,7 @@ public DataSourceProvidedDatabaseConfiguration(final Map dat for (Entry entry : dataSourcePoolPropsMap.entrySet()) { String storageUnitName = entry.getKey(); StorageNode storageNode = storageUnitNodeMap.get(storageUnitName); - StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropsMap.get(storageUnitName), storageNodeDataSources.get(storageNode)); + StorageUnit storageUnit = new StorageUnit(storageNode, entry.getValue(), storageNodeDataSources.get(storageNode)); storageUnits.put(storageUnitName, storageUnit); } this.dataSources = storageNodeDataSources; From d216cd43445dfdba09135cd0888455b17121b43c Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 7 Oct 2023 16:52:58 +0800 Subject: [PATCH 5/7] Refactor DataSourceProvidedDatabaseConfiguration --- ...taSourceProvidedDatabaseConfiguration.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java index fb3ce5271587b..6ec50a2e8bfd1 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java @@ -53,13 +53,7 @@ public DataSourceProvidedDatabaseConfiguration(final Map dat .collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); Map storageNodeDataSources = StorageNodeAggregator.aggregateDataSources(dataSources); Map dataSourcePoolPropsMap = createDataSourcePoolPropertiesMap(dataSources); - storageUnits = new LinkedHashMap<>(dataSourcePoolPropsMap.size(), 1F); - for (Entry entry : dataSourcePoolPropsMap.entrySet()) { - String storageUnitName = entry.getKey(); - StorageNode storageNode = storageUnitNodeMap.get(storageUnitName); - StorageUnit storageUnit = new StorageUnit(storageNode, entry.getValue(), storageNodeDataSources.get(storageNode)); - storageUnits.put(storageUnitName, storageUnit); - } + storageUnits = getStorageUnits(storageUnitNodeMap, storageNodeDataSources, dataSourcePoolPropsMap); this.dataSources = storageNodeDataSources; } @@ -68,14 +62,20 @@ public DataSourceProvidedDatabaseConfiguration(final StorageResource storageReso this.ruleConfigurations = ruleConfigs; Map storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropsMap); Map storageNodeDataSources = storageResource.getDataSources(); - storageUnits = new LinkedHashMap<>(dataSourcePoolPropsMap.size(), 1F); + storageUnits = getStorageUnits(storageUnitNodeMap, storageNodeDataSources, dataSourcePoolPropsMap); + dataSources = storageNodeDataSources; + } + + private Map getStorageUnits(final Map storageUnitNodeMap, + final Map storageNodeDataSources, final Map dataSourcePoolPropsMap) { + Map result = new LinkedHashMap<>(dataSourcePoolPropsMap.size(), 1F); for (Entry entry : dataSourcePoolPropsMap.entrySet()) { String storageUnitName = entry.getKey(); StorageNode storageNode = storageUnitNodeMap.get(storageUnitName); StorageUnit storageUnit = new StorageUnit(storageNode, entry.getValue(), storageNodeDataSources.get(storageNode)); - storageUnits.put(storageUnitName, storageUnit); + result.put(storageUnitName, storageUnit); } - dataSources = storageNodeDataSources; + return result; } private Map createDataSourcePoolPropertiesMap(final Map dataSources) { From 7c618cbb539adadffd4dace14e199b3b1fc1bee6 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 7 Oct 2023 16:55:49 +0800 Subject: [PATCH 6/7] Refactor DataSourceProvidedDatabaseConfiguration --- .../impl/DataSourceProvidedDatabaseConfiguration.java | 7 ++----- .../mode/manager/context/ConfigurationContextManager.java | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java index 6ec50a2e8bfd1..8110de2d35473 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java @@ -22,7 +22,6 @@ import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeAggregator; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; @@ -52,16 +51,14 @@ public DataSourceProvidedDatabaseConfiguration(final Map dat Map storageUnitNodeMap = dataSources.keySet().stream() .collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); Map storageNodeDataSources = StorageNodeAggregator.aggregateDataSources(dataSources); - Map dataSourcePoolPropsMap = createDataSourcePoolPropertiesMap(dataSources); - storageUnits = getStorageUnits(storageUnitNodeMap, storageNodeDataSources, dataSourcePoolPropsMap); + storageUnits = getStorageUnits(storageUnitNodeMap, storageNodeDataSources, createDataSourcePoolPropertiesMap(dataSources)); this.dataSources = storageNodeDataSources; } - public DataSourceProvidedDatabaseConfiguration(final StorageResource storageResource, + public DataSourceProvidedDatabaseConfiguration(final Map storageNodeDataSources, final Collection ruleConfigs, final Map dataSourcePoolPropsMap) { this.ruleConfigurations = ruleConfigs; Map storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropsMap); - Map storageNodeDataSources = storageResource.getDataSources(); storageUnits = getStorageUnits(storageUnitNodeMap, storageNodeDataSources, dataSourcePoolPropsMap); dataSources = storageNodeDataSources; } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java index 4d30b5940f860..dce82dbe5a6b6 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java @@ -362,7 +362,7 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re ? resourceMetaData.getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)) : switchingResource.getMergedDataSourcePoolPropertiesMap(); - return new DataSourceProvidedDatabaseConfiguration(storageResource, toBeCreatedRuleConfigs, propsMap); + return new DataSourceProvidedDatabaseConfiguration(storageResource.getDataSources(), toBeCreatedRuleConfigs, propsMap); } private StorageResource getMergedStorageResource(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { From da2284049116fda69a5a9d7f5b596f6a918a9c23 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 7 Oct 2023 17:01:48 +0800 Subject: [PATCH 7/7] Refactor ConfigurationContextManager --- .../context/ConfigurationContextManager.java | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java index dce82dbe5a6b6..c33ba7bd7b13d 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java @@ -29,7 +29,6 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; @@ -357,26 +356,19 @@ public synchronized Map createChangedDatabases(f private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData resourceMetaData, final SwitchingResource switchingResource, final Collection toBeCreatedRuleConfigs) { - StorageResource storageResource = getMergedStorageResource(resourceMetaData, switchingResource); Map propsMap = null == switchingResource ? resourceMetaData.getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)) : switchingResource.getMergedDataSourcePoolPropertiesMap(); - return new DataSourceProvidedDatabaseConfiguration(storageResource.getDataSources(), toBeCreatedRuleConfigs, propsMap); + return new DataSourceProvidedDatabaseConfiguration(getMergedStorageNodeDataSources(resourceMetaData, switchingResource), toBeCreatedRuleConfigs, propsMap); } - private StorageResource getMergedStorageResource(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { - Map storageNodeDataSources = currentResourceMetaData.getDataSources(); - Map storageUnitNodeMap = new LinkedHashMap<>( - currentResourceMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); + private Map getMergedStorageNodeDataSources(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { + Map result = currentResourceMetaData.getDataSources(); if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSources().isEmpty()) { - storageNodeDataSources.putAll(switchingResource.getNewStorageResource().getDataSources()); + result.putAll(switchingResource.getNewStorageResource().getDataSources()); } - if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getStorageUnitNodeMap().isEmpty()) { - storageUnitNodeMap.putAll(switchingResource.getNewStorageResource().getStorageUnitNodeMap()); - } - return new StorageResource(storageNodeDataSources, storageUnitNodeMap); + return result; } private ShardingSphereDatabase createChangedDatabase(final String databaseName, final boolean internalLoadMetaData, final MetaDataBasedPersistService persistService,