Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove StorageResource.wrappedDataSources #28647

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,30 +30,23 @@
public interface DatabaseConfiguration {

/**
* Get data sources.
* Get rule configurations.
*
* @return data sources
* @return rule configurations
*/
Map<String, DataSource> getDataSources();
Collection<RuleConfiguration> getRuleConfigurations();

/**
* Get rule configurations.
* Get storage units.
*
* @return rule configurations
* @return storage units
*/
Collection<RuleConfiguration> getRuleConfigurations();
Map<String, StorageUnit> getStorageUnits();

/**
* Get storage resource.
*
* @return storage resource
*/
StorageResource getStorageResource();

/**
* Get data source pool properties map.
*
* @return data source pool properties map
*/
Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,30 +43,34 @@
@Getter
public final class DataSourceGeneratedDatabaseConfiguration implements DatabaseConfiguration {

private final StorageResource storageResource;

private final Collection<RuleConfiguration> ruleConfigurations;

private final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap;
private final Map<String, StorageUnit> storageUnits;

private final StorageResource storageResource;

public DataSourceGeneratedDatabaseConfiguration(final Map<String, DataSourceConfiguration> dataSourceConfigs, final Collection<RuleConfiguration> ruleConfigs) {
ruleConfigurations = ruleConfigs;
dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream()
Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap);
storageResource = new StorageResource(getStorageNodeDataSourceMap(storageUnitNodeMap), storageUnitNodeMap);
Map<StorageNodeName, DataSource> storageNodeDataSources = getStorageNodeDataSourceMap(dataSourcePoolPropertiesMap, storageUnitNodeMap);
storageUnits = 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);
}
storageResource = new StorageResource(storageNodeDataSources, storageUnitNodeMap);
}

private Map<StorageNodeName, DataSource> getStorageNodeDataSourceMap(final Map<String, StorageNode> storageUnitNodeMap) {
private Map<StorageNodeName, DataSource> getStorageNodeDataSourceMap(final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final Map<String, StorageNode> storageUnitNodeMap) {
Map<StorageNodeName, DataSource> result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F);
for (Entry<String, StorageNode> 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<String, DataSource> getDataSources() {
return storageResource.getWrappedDataSources();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,29 +39,47 @@
/**
* Data source provided database configuration.
*/
@RequiredArgsConstructor
@Getter
public final class DataSourceProvidedDatabaseConfiguration implements DatabaseConfiguration {

private final StorageResource storageResource;

private final Collection<RuleConfiguration> ruleConfigurations;

private final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap;
private final Map<String, StorageUnit> storageUnits;

private final StorageResource storageResource;

public DataSourceProvidedDatabaseConfiguration(final Map<String, DataSource> dataSources, final Collection<RuleConfiguration> ruleConfigs) {
this.ruleConfigurations = ruleConfigs;
storageResource = new StorageResource(StorageNodeAggregator.aggregateDataSources(dataSources), StorageUnitNodeMapUtils.fromDataSources(dataSources));
dataSourcePoolPropertiesMap = createDataSourcePoolPropertiesMap(dataSources);
Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(dataSources);
Map<StorageNodeName, DataSource> storageNodeDataSources = StorageNodeAggregator.aggregateDataSources(dataSources);
Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap = createDataSourcePoolPropertiesMap(dataSources);
storageUnits = 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);
}
storageResource = new StorageResource(storageNodeDataSources, storageUnitNodeMap);
}

public DataSourceProvidedDatabaseConfiguration(final StorageResource storageResource,
final Collection<RuleConfiguration> ruleConfigs, final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap) {
this.storageResource = storageResource;
this.ruleConfigurations = ruleConfigs;
Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap);
Map<StorageNodeName, DataSource> storageNodeDataSources = storageResource.getDataSources();
storageUnits = 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);
}
}

private Map<String, DataSourcePoolProperties> createDataSourcePoolPropertiesMap(final Map<String, DataSource> dataSources) {
return dataSources.entrySet().stream().collect(Collectors
.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

@Override
public Map<String, DataSource> getDataSources() {
return storageResource.getWrappedDataSources();
}
}
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 @@ -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;
Expand Down 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 Expand Up @@ -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<ShardingSphereRule> rules, final Map<String, ShardingSphereSchema> 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<String, DataSourcePoolProperties> propsMap) {
return new ResourceMetaData(storageResource.getDataSources(), storageResource.getStorageUnitNodeMap(), propsMap);
private static ResourceMetaData createResourceMetaData(final StorageResource storageResource, final Map<String, StorageUnit> storageUnits) {
return new ResourceMetaData(storageResource.getDataSources(), storageUnits);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,7 @@
/**
* Resource meta data.
*/
@RequiredArgsConstructor
@Getter
public final class ResourceMetaData {

Expand All @@ -55,14 +57,6 @@ public ResourceMetaData(final Map<String, DataSource> dataSources) {
}
}

public ResourceMetaData(final Map<StorageNodeName, DataSource> dataSources, final Map<String, StorageNode> storageNodes, final Map<String, DataSourcePoolProperties> dataSourcePoolPropsMap) {
this.dataSources = dataSources;
storageUnits = new LinkedHashMap<>();
for (Entry<String, StorageNode> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<StorageNodeName, DataSource> dataSources;

private final Map<String, StorageNode> storageUnitNodeMap;

private final Map<String, DataSource> wrappedDataSources;

public StorageResource(final Map<StorageNodeName, DataSource> dataSources, final Map<String, StorageNode> storageUnitNodeMap) {
this.dataSources = dataSources;
this.storageUnitNodeMap = storageUnitNodeMap;
wrappedDataSources = createWrappedDataSources();
}

private Map<String, DataSource> createWrappedDataSources() {
Map<String, DataSource> result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F);
for (Entry<String, StorageNode> 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;
}
}
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
Loading