From df7efca786632a4fa30040fd032ff3359ba6f8b8 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 6 Oct 2023 19:12:42 +0800 Subject: [PATCH] Remove StorageResource.wrappedDataSources (#28647) * Remove StorageResource.wrappedDataSources * Remove StorageResource.wrappedDataSources * Use getStorageUnits instead of DatabaseConfiguration.getDataSourcePoolPropertiesMap() * Refactor DatabaseConfiguration * Remove DatabaseConfiguration.getDataSources() --- .../database/DatabaseConfiguration.java | 22 +++------- ...aSourceGeneratedDatabaseConfiguration.java | 27 +++++++----- ...taSourceProvidedDatabaseConfiguration.java | 44 ++++++++++++++----- .../infra/database/DatabaseTypeEngine.java | 8 ++-- .../database/ShardingSphereDatabase.java | 10 ++--- .../database/resource/ResourceMetaData.java | 10 +---- .../database/resource/StorageResource.java | 25 +---------- .../database/DatabaseRulesBuilder.java | 5 ++- .../datasource/DataSourceStateManager.java | 19 ++++---- ...rceGeneratedDatabaseConfigurationTest.java | 6 +-- ...urceProvidedDatabaseConfigurationTest.java | 6 +-- .../factory/ExternalMetaDataFactory.java | 4 +- .../factory/InternalMetaDataFactory.java | 2 +- .../factory/NewInternalMetaDataFactory.java | 2 +- .../persist/MetaDataPersistService.java | 7 +-- .../persist/NewMetaDataPersistService.java | 19 ++------ .../ContextManagerBuilderParameter.java | 2 +- .../context/ConfigurationContextManager.java | 12 +++-- .../metadata/MetaDataContextsFactory.java | 4 +- .../metadata/NewMetaDataContextsFactory.java | 4 +- .../ContextManagerBuilderParameterTest.java | 4 +- .../test/it/rewrite/engine/SQLRewriterIT.java | 19 +++----- 22 files changed, 117 insertions(+), 144 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 9f4b4fa6ca8b0..9ec063f76c05a 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,10 +18,9 @@ package org.apache.shardingsphere.infra.config.database; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -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.unit.StorageUnit; -import javax.sql.DataSource; import java.util.Collection; import java.util.Map; @@ -31,18 +30,18 @@ public interface DatabaseConfiguration { /** - * Get data sources. + * Get rule configurations. * - * @return data sources + * @return rule configurations */ - Map getDataSources(); + Collection getRuleConfigurations(); /** - * Get rule configurations. + * Get storage units. * - * @return rule configurations + * @return storage units */ - Collection getRuleConfigurations(); + Map getStorageUnits(); /** * Get storage resource. @@ -50,11 +49,4 @@ public interface DatabaseConfiguration { * @return storage resource */ StorageResource getStorageResource(); - - /** - * Get data source pool properties map. - * - * @return data source pool properties map - */ - Map getDataSourcePoolPropertiesMap(); } 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 c96d9dce33cbe..83b132e398923 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 @@ -27,6 +27,7 @@ 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.StorageNodeName; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; import javax.sql.DataSource; @@ -42,30 +43,34 @@ @Getter public final class DataSourceGeneratedDatabaseConfiguration implements DatabaseConfiguration { - private final StorageResource storageResource; - private final Collection ruleConfigurations; - private final Map dataSourcePoolPropertiesMap; + private final Map storageUnits; + + private final StorageResource storageResource; public DataSourceGeneratedDatabaseConfiguration(final Map dataSourceConfigs, final Collection ruleConfigs) { ruleConfigurations = ruleConfigs; - dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream() + Map dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap); - storageResource = new StorageResource(getStorageNodeDataSourceMap(storageUnitNodeMap), storageUnitNodeMap); + Map storageNodeDataSources = getStorageNodeDataSourceMap(dataSourcePoolPropertiesMap, storageUnitNodeMap); + storageUnits = new LinkedHashMap<>(dataSourceConfigs.size(), 1F); + for (Entry entry : dataSourceConfigs.entrySet()) { + String storageUnitName = entry.getKey(); + StorageNode storageNode = storageUnitNodeMap.get(storageUnitName); + DataSource dataSource = storageNodeDataSources.get(storageNode.getName()); + StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropertiesMap.get(storageUnitName), dataSource); + storageUnits.put(storageUnitName, storageUnit); + } + storageResource = new StorageResource(storageNodeDataSources, storageUnitNodeMap); } - private Map getStorageNodeDataSourceMap(final Map storageUnitNodeMap) { + private Map getStorageNodeDataSourceMap(final Map dataSourcePoolPropertiesMap, final Map storageUnitNodeMap) { Map result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F); for (Entry entry : storageUnitNodeMap.entrySet()) { result.computeIfAbsent(entry.getValue().getName(), key -> DataSourcePoolCreator.create(entry.getKey(), dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values())); } return result; } - - @Override - public Map getDataSources() { - return storageResource.getWrappedDataSources(); - } } 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 14aaf77972c91..4da86afb39a84 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 @@ -18,13 +18,15 @@ package org.apache.shardingsphere.infra.config.database.impl; import lombok.Getter; -import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; 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.node.StorageNodeName; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; import javax.sql.DataSource; @@ -37,29 +39,47 @@ /** * Data source provided database configuration. */ -@RequiredArgsConstructor @Getter public final class DataSourceProvidedDatabaseConfiguration implements DatabaseConfiguration { - private final StorageResource storageResource; - private final Collection ruleConfigurations; - private final Map dataSourcePoolPropertiesMap; + private final Map storageUnits; + + private final StorageResource storageResource; public DataSourceProvidedDatabaseConfiguration(final Map dataSources, final Collection ruleConfigs) { this.ruleConfigurations = ruleConfigs; - storageResource = new StorageResource(StorageNodeAggregator.aggregateDataSources(dataSources), StorageUnitNodeMapUtils.fromDataSources(dataSources)); - dataSourcePoolPropertiesMap = createDataSourcePoolPropertiesMap(dataSources); + Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(dataSources); + Map storageNodeDataSources = StorageNodeAggregator.aggregateDataSources(dataSources); + Map dataSourcePoolPropertiesMap = createDataSourcePoolPropertiesMap(dataSources); + storageUnits = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F); + for (Entry entry : dataSourcePoolPropertiesMap.entrySet()) { + String storageUnitName = entry.getKey(); + StorageNode storageNode = storageUnitNodeMap.get(storageUnitName); + StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropertiesMap.get(storageUnitName), storageNodeDataSources.get(storageNode.getName())); + storageUnits.put(storageUnitName, storageUnit); + } + storageResource = new StorageResource(storageNodeDataSources, storageUnitNodeMap); + } + + public DataSourceProvidedDatabaseConfiguration(final StorageResource storageResource, + final Collection ruleConfigs, final Map dataSourcePoolPropertiesMap) { + this.storageResource = storageResource; + this.ruleConfigurations = ruleConfigs; + Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap); + Map storageNodeDataSources = storageResource.getDataSources(); + storageUnits = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F); + for (Entry entry : dataSourcePoolPropertiesMap.entrySet()) { + String storageUnitName = entry.getKey(); + StorageNode storageNode = storageUnitNodeMap.get(storageUnitName); + StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropertiesMap.get(storageUnitName), storageNodeDataSources.get(storageNode.getName())); + storageUnits.put(storageUnitName, storageUnit); + } } private Map createDataSourcePoolPropertiesMap(final Map dataSources) { return dataSources.entrySet().stream().collect(Collectors .toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); } - - @Override - public Map getDataSources() { - return storageResource.getWrappedDataSources(); - } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java index 4fcdd47cfe965..1d8610dc8e7bf 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java @@ -58,7 +58,7 @@ public static DatabaseType getProtocolType(final String databaseName, final Data if (configuredDatabaseType.isPresent()) { return configuredDatabaseType.get(); } - Collection enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig); + Collection enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig).values(); return enabledDataSources.isEmpty() ? getDefaultStorageType() : getStorageType(enabledDataSources.iterator().next()); } @@ -86,7 +86,7 @@ private static Optional findConfiguredDatabaseType(final Configura private static Map getEnabledDataSources(final Map databaseConfigs) { Map result = new LinkedHashMap<>(); for (Entry entry : databaseConfigs.entrySet()) { - result.putAll(DataSourceStateManager.getInstance().getEnabledDataSources(entry.getKey(), entry.getValue().getDataSources())); + result.putAll(DataSourceStateManager.getInstance().getEnabledDataSources(entry.getKey(), entry.getValue())); } return result; } @@ -99,8 +99,8 @@ private static Map getEnabledDataSources(final Map getStorageTypes(final String databaseName, final DatabaseConfiguration databaseConfig) { - Map result = new LinkedHashMap<>(databaseConfig.getDataSources().size(), 1F); - Map enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig.getDataSources()); + Map result = new LinkedHashMap<>(databaseConfig.getStorageUnits().size(), 1F); + Map enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig); for (Entry entry : enabledDataSources.entrySet()) { result.put(entry.getKey(), getStorageType(entry.getValue())); } 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 941f270b12ef1..ed608937ac19b 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 @@ -24,10 +24,10 @@ import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; 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.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder; import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial; @@ -90,7 +90,7 @@ public static ShardingSphereDatabase create(final String name, final DatabaseTyp final DatabaseConfiguration databaseConfig, final ConfigurationProperties props, final InstanceContext instanceContext) throws SQLException { Collection databaseRules = DatabaseRulesBuilder.build(name, databaseConfig, instanceContext); Map schemas = new ConcurrentHashMap<>(GenericSchemaBuilder - .build(new GenericSchemaBuilderMaterial(protocolType, storageTypes, DataSourceStateManager.getInstance().getEnabledDataSources(name, databaseConfig.getDataSources()), databaseRules, + .build(new GenericSchemaBuilderMaterial(protocolType, storageTypes, DataSourceStateManager.getInstance().getEnabledDataSources(name, databaseConfig), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name)))); SystemSchemaBuilder.build(name, protocolType, props).forEach(schemas::putIfAbsent); return create(name, protocolType, databaseConfig, databaseRules, schemas); @@ -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.getDataSourcePoolPropertiesMap()); + ResourceMetaData resourceMetaData = createResourceMetaData(databaseConfig.getStorageResource(), databaseConfig.getStorageUnits()); RuleMetaData ruleMetaData = new RuleMetaData(rules); return new ShardingSphereDatabase(name, protocolType, resourceMetaData, ruleMetaData, schemas); } - private static ResourceMetaData createResourceMetaData(final StorageResource storageResource, final Map propsMap) { - return new ResourceMetaData(storageResource.getDataSources(), storageResource.getStorageUnitNodeMap(), propsMap); + private static ResourceMetaData createResourceMetaData(final StorageResource storageResource, final Map storageUnits) { + return new ResourceMetaData(storageResource.getDataSources(), storageUnits); } /** diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java index bd048bd62b70a..0b8871ec912e3 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.metadata.database.resource; import lombok.Getter; +import lombok.RequiredArgsConstructor; 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.node.StorageNode; @@ -37,6 +38,7 @@ /** * Resource meta data. */ +@RequiredArgsConstructor @Getter public final class ResourceMetaData { @@ -55,14 +57,6 @@ public ResourceMetaData(final Map dataSources) { } } - public ResourceMetaData(final Map dataSources, final Map storageNodes, final Map dataSourcePoolPropsMap) { - this.dataSources = dataSources; - storageUnits = new LinkedHashMap<>(); - for (Entry entry : storageNodes.entrySet()) { - storageUnits.put(entry.getKey(), new StorageUnit(entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName()))); - } - } - /** * Get all instance data source names. * diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java index 8c7686ae19141..742bb9c7fbe72 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java @@ -18,42 +18,21 @@ package org.apache.shardingsphere.infra.metadata.database.resource; import lombok.Getter; -import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; +import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import javax.sql.DataSource; -import java.util.LinkedHashMap; import java.util.Map; -import java.util.Map.Entry; /** * Storage resource. */ +@RequiredArgsConstructor @Getter public final class StorageResource { private final Map dataSources; private final Map storageUnitNodeMap; - - private final Map wrappedDataSources; - - public StorageResource(final Map dataSources, final Map storageUnitNodeMap) { - this.dataSources = dataSources; - this.storageUnitNodeMap = storageUnitNodeMap; - wrappedDataSources = createWrappedDataSources(); - } - - private Map createWrappedDataSources() { - Map result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F); - for (Entry entry : storageUnitNodeMap.entrySet()) { - StorageNode storageNode = entry.getValue(); - DataSource dataSource = dataSources.get(storageNode.getName()); - if (null != dataSource) { - result.put(entry.getKey(), new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl())); - } - } - return result; - } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java index f446c5e26b829..18c8ca2dd4fe1 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java @@ -59,10 +59,11 @@ public static Collection build(final String databaseName, fi for (Entry entry : getRuleBuilderMap(databaseConfig).entrySet()) { RuleConfigurationChecker configChecker = OrderedSPILoader.getServicesByClass( RuleConfigurationChecker.class, Collections.singleton(entry.getKey().getClass())).get(entry.getKey().getClass()); + Map dataSources = databaseConfig.getStorageUnits().entrySet().stream().collect(Collectors.toMap(Entry::getKey, storageUnit -> storageUnit.getValue().getDataSource())); if (null != configChecker) { - configChecker.check(databaseName, entry.getKey(), databaseConfig.getDataSources(), result); + configChecker.check(databaseName, entry.getKey(), dataSources, result); } - result.add(entry.getValue().build(entry.getKey(), databaseName, databaseConfig.getDataSources(), result, instanceContext)); + result.add(entry.getValue().build(entry.getKey(), databaseName, dataSources, result, instanceContext)); } return result; } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/DataSourceStateManager.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/DataSourceStateManager.java index 48d7a4c861e28..b497f0de0cc65 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/DataSourceStateManager.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/DataSourceStateManager.java @@ -21,18 +21,19 @@ import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; -import org.apache.shardingsphere.infra.state.datasource.exception.UnavailableDataSourceException; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.state.datasource.exception.UnavailableDataSourceException; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; -import java.util.Collection; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; /** * Data source state manager. @@ -62,14 +63,14 @@ public static DataSourceStateManager getInstance() { * Set data source states when bootstrap. * * @param databaseName database name - * @param dataSources data sources + * @param storageUnits storage units * @param storageDataSourceStates storage node data source state * @param forceStart whether to force start */ - public void initStates(final String databaseName, final Map dataSources, final Map storageDataSourceStates, final boolean forceStart) { + public void initStates(final String databaseName, final Map storageUnits, final Map storageDataSourceStates, final boolean forceStart) { this.forceStart = forceStart; if (initialized.compareAndSet(false, true)) { - dataSources.forEach((key, value) -> initState(databaseName, storageDataSourceStates, key, value)); + storageUnits.forEach((key, value) -> initState(databaseName, storageDataSourceStates, key, value.getDataSource())); } } @@ -98,8 +99,10 @@ private void checkState(final String databaseName, final String actualDataSource * @param databaseConfig database config * @return enabled data sources */ - public Collection getEnabledDataSources(final String databaseName, final DatabaseConfiguration databaseConfig) { - return databaseConfig.getDataSources().isEmpty() ? Collections.emptyList() : getEnabledDataSources(databaseName, databaseConfig.getDataSources()).values(); + public Map getEnabledDataSources(final String databaseName, final DatabaseConfiguration databaseConfig) { + Map dataSources = databaseConfig.getStorageUnits().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + return getEnabledDataSources(databaseName, dataSources); } /** 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 696d8c75aaee6..36fa57f9d5d31 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 @@ -42,7 +42,7 @@ class DataSourceGeneratedDatabaseConfigurationTest { @Test void assertGetDataSources() { DataSourceGeneratedDatabaseConfiguration databaseConfig = createDataSourceGeneratedDatabaseConfiguration(); - DataSource dataSource = databaseConfig.getDataSources().get("normal_db"); + DataSource dataSource = databaseConfig.getStorageUnits().get("normal_db").getDataSource(); assertTrue(dataSource instanceof CatalogSwitchableDataSource); assertTrue(((CatalogSwitchableDataSource) dataSource).getDataSource() instanceof HikariDataSource); } @@ -59,7 +59,7 @@ void assertGetStorageNodes() { @Test void assertGetStorageUnits() { DataSourceGeneratedDatabaseConfiguration databaseConfig = createDataSourceGeneratedDatabaseConfiguration(); - DataSource dataSource = databaseConfig.getDataSources().get("normal_db"); + DataSource dataSource = databaseConfig.getStorageUnits().get("normal_db").getDataSource(); assertTrue(dataSource instanceof CatalogSwitchableDataSource); assertTrue(((CatalogSwitchableDataSource) dataSource).getDataSource() instanceof HikariDataSource); } @@ -74,7 +74,7 @@ void assertGetRuleConfigurations() { @Test void assertGetDataSourcePoolProperties() { DataSourceGeneratedDatabaseConfiguration databaseConfig = createDataSourceGeneratedDatabaseConfiguration(); - DataSourcePoolProperties props = databaseConfig.getDataSourcePoolPropertiesMap().get("normal_db"); + DataSourcePoolProperties props = databaseConfig.getStorageUnits().get("normal_db").getDataSourcePoolProperties(); Map poolStandardProps = props.getPoolPropertySynonyms().getStandardProperties(); assertThat(poolStandardProps.size(), is(6)); assertThat(poolStandardProps.get("connectionTimeoutMilliseconds"), is(2000L)); 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 796b69589758f..241d1c56248fb 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 @@ -37,7 +37,7 @@ class DataSourceProvidedDatabaseConfigurationTest { @Test void assertGetDataSources() { DataSourceProvidedDatabaseConfiguration databaseConfig = createDataSourceProvidedDatabaseConfiguration(); - DataSource dataSource = databaseConfig.getDataSources().get("foo_ds"); + DataSource dataSource = databaseConfig.getStorageUnits().get("foo_ds").getDataSource(); assertTrue(dataSource instanceof CatalogSwitchableDataSource); assertTrue(((CatalogSwitchableDataSource) dataSource).getDataSource() instanceof MockedDataSource); } @@ -54,7 +54,7 @@ void assertGetStorageNodes() { @Test void assertGetStorageUnits() { DataSourceProvidedDatabaseConfiguration databaseConfig = createDataSourceProvidedDatabaseConfiguration(); - DataSource dataSource = databaseConfig.getDataSources().get("foo_ds"); + DataSource dataSource = databaseConfig.getStorageUnits().get("foo_ds").getDataSource(); assertTrue(dataSource instanceof CatalogSwitchableDataSource); assertTrue(((CatalogSwitchableDataSource) dataSource).getDataSource() instanceof MockedDataSource); } @@ -69,7 +69,7 @@ void assertGetRuleConfigurations() { @Test void assertGetDataSourcePoolProperties() { DataSourceProvidedDatabaseConfiguration databaseConfig = createDataSourceProvidedDatabaseConfiguration(); - DataSourcePoolProperties props = databaseConfig.getDataSourcePoolPropertiesMap().get("foo_ds"); + DataSourcePoolProperties props = databaseConfig.getStorageUnits().get("foo_ds").getDataSourcePoolProperties(); Map poolStandardProps = props.getPoolPropertySynonyms().getStandardProperties(); assertThat(poolStandardProps.size(), is(0)); Map connStandardProps = props.getConnectionPropertySynonyms().getStandardProperties(); diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java index af473642c20c9..a336f16bde5c4 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java @@ -80,7 +80,7 @@ private static Map createGenericDatabases(final Map result = new HashMap<>(databaseConfigMap.size(), 1F); for (Entry entry : databaseConfigMap.entrySet()) { String databaseName = entry.getKey(); - if (!entry.getValue().getDataSources().isEmpty() || !systemDatabase.getSystemSchemas().contains(databaseName)) { + if (!entry.getValue().getStorageUnits().isEmpty() || !systemDatabase.getSystemSchemas().contains(databaseName)) { Map storageTypes = DatabaseTypeEngine.getStorageTypes(entry.getKey(), entry.getValue()); result.put(databaseName.toLowerCase(), ShardingSphereDatabase.create(databaseName, protocolType, storageTypes, entry.getValue(), props, instanceContext)); } @@ -92,7 +92,7 @@ private static Map createSystemDatabases(final M final SystemDatabase systemDatabase, final ConfigurationProperties props) { Map result = new HashMap<>(systemDatabase.getSystemDatabaseSchemaMap().size(), 1F); for (String each : systemDatabase.getSystemDatabaseSchemaMap().keySet()) { - if (!databaseConfigMap.containsKey(each) || databaseConfigMap.get(each).getDataSources().isEmpty()) { + if (!databaseConfigMap.containsKey(each) || databaseConfigMap.get(each).getStorageUnits().isEmpty()) { result.put(each.toLowerCase(), ShardingSphereDatabase.create(each, protocolType, props)); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java index 9b5a9a104e0dd..58a8f3e780e2c 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/InternalMetaDataFactory.java @@ -73,7 +73,7 @@ private static Map createDatabases(final MetaDat Map result = new ConcurrentHashMap<>(databaseConfigMap.size(), 1F); for (Entry entry : databaseConfigMap.entrySet()) { String databaseName = entry.getKey(); - if (entry.getValue().getDataSources().isEmpty()) { + if (entry.getValue().getStorageUnits().isEmpty()) { result.put(databaseName.toLowerCase(), ShardingSphereDatabase.create(databaseName, protocolType, props)); } else { result.put(databaseName.toLowerCase(), create(databaseName, persistService, entry.getValue(), props, instanceContext)); diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java index 02e7931f585dd..34c43929adc69 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/NewInternalMetaDataFactory.java @@ -74,7 +74,7 @@ private static Map createDatabases(final NewMeta Map result = new ConcurrentHashMap<>(databaseConfigMap.size(), 1F); for (Entry entry : databaseConfigMap.entrySet()) { String databaseName = entry.getKey(); - if (entry.getValue().getDataSources().isEmpty()) { + if (entry.getValue().getStorageUnits().isEmpty()) { result.put(databaseName.toLowerCase(), ShardingSphereDatabase.create(databaseName, protocolType, props)); } else { result.put(databaseName.toLowerCase(), create(databaseName, persistService, entry.getValue(), props, instanceContext)); 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 5274f108d9f9a..6411201a0e450 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,10 +114,11 @@ private Collection decorateRuleConfigs(final String databaseN } private Map getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) { - if (!databaseConfigs.getDataSources().isEmpty() && databaseConfigs.getDataSourcePoolPropertiesMap().isEmpty()) { + if (!databaseConfigs.getStorageUnits().isEmpty() && databaseConfigs.getStorageUnits().isEmpty()) { return getDataSourcePoolPropertiesMap(databaseConfigs.getStorageResource().getDataSources()); } - return databaseConfigs.getDataSourcePoolPropertiesMap(); + return databaseConfigs.getStorageUnits().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); } private Map getDataSourcePoolPropertiesMap(final Map storageNodeDataSources) { @@ -131,7 +132,7 @@ private Map getDataSourcePoolPropertiesMap(fin @Override public Map getEffectiveDataSources(final String databaseName, final Map databaseConfigs) { Map propsMap = dataSourceUnitService.load(databaseName); - if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getDataSources().isEmpty()) { + if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getStorageUnits().isEmpty()) { databaseConfigs.get(databaseName).getStorageResource().getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy()); } return propsMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java index c94ae23f4c09e..98b50b1b76f59 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java @@ -23,9 +23,8 @@ import org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator; import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration; import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; @@ -120,18 +119,8 @@ private Collection decorateRuleConfigs(final String databaseN } private Map getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) { - if (!databaseConfigs.getDataSources().isEmpty() && databaseConfigs.getDataSourcePoolPropertiesMap().isEmpty()) { - return getDataSourcePoolPropertiesMap(databaseConfigs.getStorageResource().getDataSources()); - } - return databaseConfigs.getDataSourcePoolPropertiesMap(); - } - - private Map getDataSourcePoolPropertiesMap(final Map storageNodeDataSources) { - Map result = new LinkedHashMap<>(storageNodeDataSources.size(), 1F); - for (Entry entry : storageNodeDataSources.entrySet()) { - result.put(entry.getKey().getName(), DataSourcePoolPropertiesCreator.create(entry.getValue())); - } - return result; + return databaseConfigs.getStorageUnits().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); } /** @@ -144,7 +133,7 @@ private Map getDataSourcePoolPropertiesMap(fin @Override public Map getEffectiveDataSources(final String databaseName, final Map databaseConfigs) { Map propsMap = dataSourceUnitService.load(databaseName); - if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getDataSources().isEmpty()) { + if (databaseConfigs.containsKey(databaseName) && !databaseConfigs.get(databaseName).getStorageUnits().isEmpty()) { databaseConfigs.get(databaseName).getStorageResource().getDataSources().values().forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy()); } return propsMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java index c849d10d02e64..d362a7a498f2d 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameter.java @@ -61,7 +61,7 @@ public final class ContextManagerBuilderParameter { */ public boolean isEmpty() { return globalRuleConfigs.isEmpty() && props.isEmpty() - && databaseConfigs.entrySet().stream().allMatch(entry -> entry.getValue().getDataSources().isEmpty() && entry.getValue().getRuleConfigurations().isEmpty()); + && databaseConfigs.entrySet().stream().allMatch(entry -> entry.getValue().getStorageUnits().isEmpty() && entry.getValue().getRuleConfigurations().isEmpty()); } /** 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 2e38aeafa2706..389e43a46d9b4 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 @@ -279,11 +279,9 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD */ public Map renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) { Map newStorageNodes = getNewStorageNodes(database.getResourceMetaData().getDataSources(), resource); - Map newStorageUnitNodeMap = getNewStorageUnitNodes(database.getResourceMetaData().getStorageUnits(), resource); - Map propsMap = database.getResourceMetaData().getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> currentValue, LinkedHashMap::new)); + Map newStorageUnits = getNewStorageUnits(database.getResourceMetaData().getStorageUnits(), resource); return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(database.getName(), database.getProtocolType(), - new ResourceMetaData(newStorageNodes, newStorageUnitNodeMap, propsMap), database.getRuleMetaData(), database.getSchemas())); + new ResourceMetaData(newStorageNodes, newStorageUnits), database.getRuleMetaData(), database.getSchemas())); } private Map getNewStorageNodes(final Map currentStorageNodes, final SwitchingResource resource) { @@ -296,11 +294,11 @@ private Map getNewStorageNodes(final Map getNewStorageUnitNodes(final Map currentStorageUnits, final SwitchingResource resource) { - Map result = new LinkedHashMap<>(currentStorageUnits.size(), 1F); + private Map getNewStorageUnits(final Map currentStorageUnits, final SwitchingResource resource) { + Map result = new LinkedHashMap<>(currentStorageUnits.size(), 1F); for (Entry entry : currentStorageUnits.entrySet()) { if (!resource.getStaleStorageResource().getStorageUnitNodeMap().containsKey(entry.getKey())) { - result.put(entry.getKey(), entry.getValue().getStorageNode()); + result.put(entry.getKey(), entry.getValue()); } } return result; 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 99ebdec90a758..b013a103fd9cc 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 @@ -124,8 +124,8 @@ private static DatabaseConfiguration createEffectiveDatabaseConfiguration(final private static void checkDataSourceStates(final Map databaseConfigs, final Map storageNodes, final boolean force) { Map storageDataSourceStates = getStorageDataSourceStates(storageNodes); databaseConfigs.forEach((key, value) -> { - if (!value.getDataSources().isEmpty()) { - DataSourceStateManager.getInstance().initStates(key, value.getDataSources(), storageDataSourceStates, force); + if (!value.getStorageUnits().isEmpty()) { + DataSourceStateManager.getInstance().initStates(key, value.getStorageUnits(), storageDataSourceStates, force); } }); } 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 f100b404727c9..33c373070721c 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 @@ -126,8 +126,8 @@ private static DatabaseConfiguration createEffectiveDatabaseConfiguration(final private static void checkDataSourceStates(final Map databaseConfigs, final Map storageNodes, final boolean force) { Map storageDataSourceStates = getStorageDataSourceStates(storageNodes); databaseConfigs.forEach((key, value) -> { - if (!value.getDataSources().isEmpty()) { - DataSourceStateManager.getInstance().initStates(key, value.getDataSources(), storageDataSourceStates, force); + if (!value.getStorageUnits().isEmpty()) { + DataSourceStateManager.getInstance().initStates(key, value.getStorageUnits(), storageDataSourceStates, force); } }); } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameterTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameterTest.java index cb61b4262de3e..adf5a5d7d5076 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameterTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerBuilderParameterTest.java @@ -73,9 +73,9 @@ void assertIsEmptyWhenDatabaseRuleIsNotEmpty() { assertFalse(new ContextManagerBuilderParameter(null, mockDatabaseConfigurations(true, false), Collections.emptyMap(), Collections.emptyList(), new Properties(), null, null, false).isEmpty()); } - private Map mockDatabaseConfigurations(final boolean isEmptyDataSources, final boolean isEmptyRuleConfigs) { + private Map mockDatabaseConfigurations(final boolean isEmptyStorageUnits, final boolean isEmptyRuleConfigs) { DatabaseConfiguration databaseConfig = mock(DatabaseConfiguration.class, RETURNS_DEEP_STUBS); - when(databaseConfig.getDataSources().isEmpty()).thenReturn(isEmptyDataSources); + when(databaseConfig.getStorageUnits().isEmpty()).thenReturn(isEmptyStorageUnits); when(databaseConfig.getRuleConfigurations().isEmpty()).thenReturn(isEmptyRuleConfigs); return Collections.singletonMap("foo_ds", databaseConfig); } diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java index bd624913c7b26..aa641eadc8348 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java @@ -33,7 +33,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.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine; @@ -83,6 +82,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; +import java.util.stream.Collectors; import java.util.stream.Stream; import static org.hamcrest.CoreMatchers.is; @@ -118,11 +118,12 @@ private Collection createSQLRewriteUnits(final SQLRewriteEngineT YamlRootConfiguration rootConfig = createRootConfiguration(testParams); DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration( new YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources()), new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rootConfig.getRules())); - mockDataSource(databaseConfig.getDataSources()); + Map dataSources = databaseConfig.getStorageUnits().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + mockDataSource(dataSources); DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, testParams.getDatabaseType()); - Map storageUnits = createStorageUnits(databaseConfig, databaseType); ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - when(resourceMetaData.getStorageUnits()).thenReturn(storageUnits); + when(resourceMetaData.getStorageUnits()).thenReturn(databaseConfig.getStorageUnits()); String schemaName = new DatabaseTypeRegistry(databaseType).getDefaultSchemaName(DefaultDatabase.LOGIC_NAME); SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(TypedSPILoader.getService(DatabaseType.class, testParams.getDatabaseType()), sqlParserRule.getSqlStatementCache(), sqlParserRule.getParseTreeCache(), sqlParserRule.isSqlCommentParseEnabled()); @@ -170,16 +171,6 @@ private Collection createGlobalRules() { return result; } - private Map createStorageUnits(final DatabaseConfiguration databaseConfig, final DatabaseType databaseType) { - Map result = new LinkedHashMap<>(databaseConfig.getDataSources().size(), 1F); - for (Entry entry : databaseConfig.getDataSources().entrySet()) { - StorageUnit storageUnit = mock(StorageUnit.class, RETURNS_DEEP_STUBS); - when(storageUnit.getStorageType()).thenReturn(databaseType); - result.put(entry.getKey(), storageUnit); - } - return result; - } - private CursorStatementContext createCursorDefinition(final String schemaName, final ShardingSphereMetaData metaData, final SQLStatementParserEngine sqlStatementParserEngine) { SQLStatement sqlStatement = sqlStatementParserEngine.parse("CURSOR t_account_cursor FOR SELECT * FROM t_account WHERE account_id = 100", false); return (CursorStatementContext) new SQLBindEngine(metaData, schemaName).bind(sqlStatement, Collections.emptyList());