Skip to content

Commit

Permalink
Remove DatabaseConfiguration.getDataSources()
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Oct 6, 2023
1 parent ebb591f commit fc5a1bb
Show file tree
Hide file tree
Showing 19 changed files with 43 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;

Expand All @@ -44,13 +43,6 @@ public interface DatabaseConfiguration {
*/
Map<String, StorageUnit> getStorageUnits();

/**
* Get data sources.
*
* @return data sources
*/
Map<String, DataSource> getDataSources();

/**
* Get storage resource.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public final class DataSourceGeneratedDatabaseConfiguration implements DatabaseC

private final Map<String, StorageUnit> storageUnits;

private final Map<String, DataSource> dataSources;

private final StorageResource storageResource;

public DataSourceGeneratedDatabaseConfiguration(final Map<String, DataSourceConfiguration> dataSourceConfigs, final Collection<RuleConfiguration> ruleConfigs) {
Expand All @@ -58,14 +56,12 @@ public DataSourceGeneratedDatabaseConfiguration(final Map<String, DataSourceConf
Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap);
Map<StorageNodeName, DataSource> storageNodeDataSources = getStorageNodeDataSourceMap(dataSourcePoolPropertiesMap, storageUnitNodeMap);
storageUnits = new LinkedHashMap<>(dataSourceConfigs.size(), 1F);
dataSources = new LinkedHashMap<>(dataSourceConfigs.size(), 1F);
for (Entry<String, DataSourceConfiguration> 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);
dataSources.put(storageUnitName, storageUnit.getDataSource());
}
storageResource = new StorageResource(storageNodeDataSources, storageUnitNodeMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public final class DataSourceProvidedDatabaseConfiguration implements DatabaseCo

private final Map<String, StorageUnit> storageUnits;

private final Map<String, DataSource> dataSources;

private final StorageResource storageResource;

public DataSourceProvidedDatabaseConfiguration(final Map<String, DataSource> dataSources, final Collection<RuleConfiguration> ruleConfigs) {
Expand All @@ -56,13 +54,11 @@ public DataSourceProvidedDatabaseConfiguration(final Map<String, DataSource> dat
Map<StorageNodeName, DataSource> storageNodeDataSources = StorageNodeAggregator.aggregateDataSources(dataSources);
Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap = createDataSourcePoolPropertiesMap(dataSources);
storageUnits = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F);
this.dataSources = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F);
for (Entry<String, DataSourcePoolProperties> 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);
this.dataSources.put(storageUnitName, storageUnit.getDataSource());
}
storageResource = new StorageResource(storageNodeDataSources, storageUnitNodeMap);
}
Expand All @@ -74,13 +70,11 @@ public DataSourceProvidedDatabaseConfiguration(final StorageResource storageReso
Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap);
Map<StorageNodeName, DataSource> storageNodeDataSources = storageResource.getDataSources();
storageUnits = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F);
this.dataSources = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F);
for (Entry<String, DataSourcePoolProperties> 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);
this.dataSources.put(storageUnitName, storageUnit.getDataSource());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static DatabaseType getProtocolType(final String databaseName, final Data
if (configuredDatabaseType.isPresent()) {
return configuredDatabaseType.get();
}
Collection<DataSource> enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig);
Collection<DataSource> enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig).values();
return enabledDataSources.isEmpty() ? getDefaultStorageType() : getStorageType(enabledDataSources.iterator().next());
}

Expand Down Expand Up @@ -86,7 +86,7 @@ private static Optional<DatabaseType> findConfiguredDatabaseType(final Configura
private static Map<String, DataSource> getEnabledDataSources(final Map<String, ? extends DatabaseConfiguration> databaseConfigs) {
Map<String, DataSource> result = new LinkedHashMap<>();
for (Entry<String, ? extends DatabaseConfiguration> entry : databaseConfigs.entrySet()) {
result.putAll(DataSourceStateManager.getInstance().getEnabledDataSources(entry.getKey(), entry.getValue().getDataSources()));
result.putAll(DataSourceStateManager.getInstance().getEnabledDataSources(entry.getKey(), entry.getValue()));
}
return result;
}
Expand All @@ -99,8 +99,8 @@ private static Map<String, DataSource> getEnabledDataSources(final Map<String, ?
* @return storage types
*/
public static Map<String, DatabaseType> getStorageTypes(final String databaseName, final DatabaseConfiguration databaseConfig) {
Map<String, DatabaseType> result = new LinkedHashMap<>(databaseConfig.getDataSources().size(), 1F);
Map<String, DataSource> enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig.getDataSources());
Map<String, DatabaseType> result = new LinkedHashMap<>(databaseConfig.getStorageUnits().size(), 1F);
Map<String, DataSource> enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, databaseConfig);
for (Entry<String, DataSource> entry : enabledDataSources.entrySet()) {
result.put(entry.getKey(), getStorageType(entry.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShardingSphereRule> databaseRules = DatabaseRulesBuilder.build(name, databaseConfig, instanceContext);
Map<String, ShardingSphereSchema> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public static Collection<ShardingSphereRule> build(final String databaseName, fi
for (Entry<RuleConfiguration, DatabaseRuleBuilder> entry : getRuleBuilderMap(databaseConfig).entrySet()) {
RuleConfigurationChecker configChecker = OrderedSPILoader.getServicesByClass(
RuleConfigurationChecker.class, Collections.singleton(entry.getKey().getClass())).get(entry.getKey().getClass());
Map<String, DataSource> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<String, DataSource> dataSources, final Map<String, DataSourceState> storageDataSourceStates, final boolean forceStart) {
public void initStates(final String databaseName, final Map<String, StorageUnit> storageUnits, final Map<String, DataSourceState> 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()));
}
}

Expand Down Expand Up @@ -98,8 +99,10 @@ private void checkState(final String databaseName, final String actualDataSource
* @param databaseConfig database config
* @return enabled data sources
*/
public Collection<DataSource> getEnabledDataSources(final String databaseName, final DatabaseConfiguration databaseConfig) {
return databaseConfig.getDataSources().isEmpty() ? Collections.emptyList() : getEnabledDataSources(databaseName, databaseConfig.getDataSources()).values();
public Map<String, DataSource> getEnabledDataSources(final String databaseName, final DatabaseConfiguration databaseConfig) {
Map<String, DataSource> dataSources = databaseConfig.getStorageUnits().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
return getEnabledDataSources(databaseName, dataSources);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static Map<String, ShardingSphereDatabase> createGenericDatabases(final
Map<String, ShardingSphereDatabase> result = new HashMap<>(databaseConfigMap.size(), 1F);
for (Entry<String, DatabaseConfiguration> 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<String, DatabaseType> storageTypes = DatabaseTypeEngine.getStorageTypes(entry.getKey(), entry.getValue());
result.put(databaseName.toLowerCase(), ShardingSphereDatabase.create(databaseName, protocolType, storageTypes, entry.getValue(), props, instanceContext));
}
Expand All @@ -92,7 +92,7 @@ private static Map<String, ShardingSphereDatabase> createSystemDatabases(final M
final SystemDatabase systemDatabase, final ConfigurationProperties props) {
Map<String, ShardingSphereDatabase> 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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private static Map<String, ShardingSphereDatabase> createDatabases(final MetaDat
Map<String, ShardingSphereDatabase> result = new ConcurrentHashMap<>(databaseConfigMap.size(), 1F);
for (Entry<String, DatabaseConfiguration> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static Map<String, ShardingSphereDatabase> createDatabases(final NewMeta
Map<String, ShardingSphereDatabase> result = new ConcurrentHashMap<>(databaseConfigMap.size(), 1F);
for (Entry<String, DatabaseConfiguration> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private Collection<RuleConfiguration> decorateRuleConfigs(final String databaseN
}

private Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(final DatabaseConfiguration databaseConfigs) {
if (!databaseConfigs.getDataSources().isEmpty() && databaseConfigs.getStorageUnits().isEmpty()) {
if (!databaseConfigs.getStorageUnits().isEmpty() && databaseConfigs.getStorageUnits().isEmpty()) {
return getDataSourcePoolPropertiesMap(databaseConfigs.getStorageResource().getDataSources());
}
return databaseConfigs.getStorageUnits().entrySet().stream()
Expand All @@ -132,7 +132,7 @@ private Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(fin
@Override
public Map<String, DataSourceConfiguration> getEffectiveDataSources(final String databaseName, final Map<String, ? extends DatabaseConfiguration> databaseConfigs) {
Map<String, DataSourcePoolProperties> 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,
Expand Down
Loading

0 comments on commit fc5a1bb

Please sign in to comment.