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 StorageUnitNodeMapper #28614

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -25,9 +25,9 @@
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.unit.StorageUnitNodeMapperUtils;
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.StorageUnitNodeMapper;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils;

import javax.sql.DataSource;
import java.util.Collection;
Expand All @@ -52,15 +52,14 @@ public DataSourceGeneratedDatabaseConfiguration(final Map<String, DataSourceConf
ruleConfigurations = ruleConfigs;
dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Map<String, StorageUnitNodeMapper> mappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap);
storageResource = new StorageResource(getStorageNodeDataSourceMap(mappers), mappers);
Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap);
storageResource = new StorageResource(getStorageNodeDataSourceMap(storageUnitNodeMap), storageUnitNodeMap);
}

private Map<StorageNodeName, DataSource> getStorageNodeDataSourceMap(final Map<String, StorageUnitNodeMapper> mappers) {
Map<StorageNodeName, DataSource> result = new LinkedHashMap<>(mappers.size(), 1F);
for (Entry<String, StorageUnitNodeMapper> entry : mappers.entrySet()) {
result.computeIfAbsent(entry.getValue().getStorageNode().getName(),
key -> DataSourcePoolCreator.create(entry.getKey(), dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values()));
private Map<StorageNodeName, DataSource> getStorageNodeDataSourceMap(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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
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.StorageNodeUtils;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapperUtils;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils;

import javax.sql.DataSource;
import java.util.Collection;
Expand All @@ -49,7 +49,7 @@ public final class DataSourceProvidedDatabaseConfiguration implements DatabaseCo

public DataSourceProvidedDatabaseConfiguration(final Map<String, DataSource> dataSources, final Collection<RuleConfiguration> ruleConfigs) {
this.ruleConfigurations = ruleConfigs;
storageResource = new StorageResource(StorageNodeUtils.getStorageNodeDataSources(dataSources), StorageUnitNodeMapperUtils.fromDataSources(dataSources));
storageResource = new StorageResource(StorageNodeUtils.getStorageNodeDataSources(dataSources), StorageUnitNodeMapUtils.fromDataSources(dataSources));
dataSourcePoolPropertiesMap = createDataSourcePoolPropertiesMap(dataSources);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static ShardingSphereDatabase create(final String name, final DatabaseTyp
}

private static ResourceMetaData createResourceMetaData(final String databaseName, final StorageResource storageResource, final Map<String, DataSourcePoolProperties> propsMap) {
return new ResourceMetaData(databaseName, storageResource.getDataSourceMap(), storageResource.getStorageUnitNodeMappers(), propsMap);
return new ResourceMetaData(databaseName, storageResource.getDataSourceMap(), storageResource.getStorageUnitNodeMap(), propsMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
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;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeUtils;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapperUtils;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils;

import javax.sql.DataSource;
import java.util.Collection;
Expand All @@ -51,13 +51,13 @@ public ResourceMetaData(final Map<String, DataSource> dataSources) {
dataSourceMap = StorageNodeUtils.getStorageNodeDataSources(dataSources);
storageUnitMetaData = new StorageUnitMetaData(null, dataSourceMap, dataSources.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
StorageUnitNodeMapperUtils.fromDataSources(dataSources));
StorageUnitNodeMapUtils.fromDataSources(dataSources));
}

public ResourceMetaData(final String databaseName, final Map<StorageNodeName, DataSource> dataSourceMap,
final Map<String, StorageUnitNodeMapper> storageUnitNodeMappers, final Map<String, DataSourcePoolProperties> propsMap) {
final Map<String, StorageNode> storageUnitNodeMap, final Map<String, DataSourcePoolProperties> propsMap) {
this.dataSourceMap = dataSourceMap;
storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageUnitNodeMappers);
storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageUnitNodeMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource;
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.StorageUnitNodeMapper;

import javax.sql.DataSource;
import java.util.LinkedHashMap;
Expand All @@ -36,20 +35,20 @@ public final class StorageResource {

private final Map<StorageNodeName, DataSource> dataSourceMap;

private final Map<String, StorageUnitNodeMapper> storageUnitNodeMappers;
private final Map<String, StorageNode> storageUnitNodeMap;

private final Map<String, DataSource> wrappedDataSources;

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

private Map<String, DataSource> createWrappedDataSources() {
Map<String, DataSource> result = new LinkedHashMap<>(storageUnitNodeMappers.size(), 1F);
for (Entry<String, StorageUnitNodeMapper> entry : storageUnitNodeMappers.entrySet()) {
StorageNode storageNode = entry.getValue().getStorageNode();
Map<String, DataSource> result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F);
for (Entry<String, StorageNode> entry : storageUnitNodeMap.entrySet()) {
StorageNode storageNode = entry.getValue();
DataSource dataSource = dataSourceMap.get(storageNode.getName());
if (null != dataSource) {
result.put(entry.getKey(), new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class StorageUnit {

private final DataSourcePoolProperties dataSourcePoolProperties;

private final StorageUnitNodeMapper unitNodeMapper;
private final StorageNode storageNode;

private final DataSource dataSource;

Expand All @@ -55,17 +55,16 @@ public final class StorageUnit {
private final ConnectionProperties connectionProperties;

public StorageUnit(final String databaseName, final Map<StorageNodeName, DataSource> storageNodeDataSources,
final DataSourcePoolProperties props, final StorageUnitNodeMapper unitNodeMapper) {
final DataSourcePoolProperties props, final StorageNode storageNode) {
this.dataSourcePoolProperties = props;
this.unitNodeMapper = unitNodeMapper;
dataSource = getStorageUnitDataSource(storageNodeDataSources, unitNodeMapper);
this.storageNode = storageNode;
dataSource = getStorageUnitDataSource(storageNodeDataSources);
Map<StorageNodeName, DataSource> enabledStorageNodeDataSources = getEnabledStorageNodeDataSources(databaseName, storageNodeDataSources);
storageType = createStorageType(enabledStorageNodeDataSources, unitNodeMapper);
connectionProperties = createConnectionProperties(enabledStorageNodeDataSources, unitNodeMapper, storageType).orElse(null);
storageType = createStorageType(enabledStorageNodeDataSources);
connectionProperties = createConnectionProperties(enabledStorageNodeDataSources).orElse(null);
}

private DataSource getStorageUnitDataSource(final Map<StorageNodeName, DataSource> storageNodeDataSources, final StorageUnitNodeMapper mapper) {
StorageNode storageNode = mapper.getStorageNode();
private DataSource getStorageUnitDataSource(final Map<StorageNodeName, DataSource> storageNodeDataSources) {
DataSource dataSource = storageNodeDataSources.get(storageNode.getName());
return new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl());
}
Expand All @@ -80,15 +79,13 @@ private Map<StorageNodeName, DataSource> getEnabledStorageNodeDataSources(final
.filter(entry -> enabledDataSources.containsKey(entry.getKey().getName())).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}

private DatabaseType createStorageType(final Map<StorageNodeName, DataSource> enabledStorageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper) {
return DatabaseTypeEngine.getStorageType(enabledStorageNodeDataSources.containsKey(unitNodeMapper.getStorageNode().getName())
? Collections.singleton(enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNode().getName()))
private DatabaseType createStorageType(final Map<StorageNodeName, DataSource> enabledStorageNodeDataSources) {
return DatabaseTypeEngine.getStorageType(enabledStorageNodeDataSources.containsKey(storageNode.getName())
? Collections.singleton(enabledStorageNodeDataSources.get(storageNode.getName()))
: Collections.emptyList());
}

private Optional<ConnectionProperties> createConnectionProperties(final Map<StorageNodeName, DataSource> enabledStorageNodeDataSources,
final StorageUnitNodeMapper mapper, final DatabaseType storageType) {
StorageNode storageNode = mapper.getStorageNode();
private Optional<ConnectionProperties> createConnectionProperties(final Map<StorageNodeName, DataSource> enabledStorageNodeDataSources) {
if (!enabledStorageNodeDataSources.containsKey(storageNode.getName())) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.Getter;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName;

import javax.sql.DataSource;
Expand All @@ -34,18 +35,18 @@
public final class StorageUnitMetaData {

// TODO zhangliang: should refactor
private final Map<String, StorageUnitNodeMapper> unitNodeMappers;
private final Map<String, StorageNode> storageUnitNodeMap;

private final Map<String, StorageUnit> storageUnits;

// TODO zhangliang: should refactor
private final Map<String, DataSource> dataSources;

public StorageUnitMetaData(final String databaseName, final Map<StorageNodeName, DataSource> storageNodeDataSources,
final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final Map<String, StorageUnitNodeMapper> unitNodeMappers) {
this.unitNodeMappers = unitNodeMappers;
storageUnits = new LinkedHashMap<>(unitNodeMappers.size(), 1F);
for (Entry<String, StorageUnitNodeMapper> entry : unitNodeMappers.entrySet()) {
final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final Map<String, StorageNode> storageUnitNodeMap) {
this.storageUnitNodeMap = storageUnitNodeMap;
storageUnits = new LinkedHashMap<>(this.storageUnitNodeMap.size(), 1F);
for (Entry<String, StorageNode> entry : this.storageUnitNodeMap.entrySet()) {
storageUnits.put(entry.getKey(), new StorageUnit(databaseName, storageNodeDataSources, dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()));
}
dataSources = createDataSources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,49 @@
import java.util.stream.Collectors;

/**
* Storage unit node mapper utility class.
* Storage unit node map utility class.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StorageUnitNodeMapperUtils {
public final class StorageUnitNodeMapUtils {

/**
* Get storage unit node mappers from data sources.
* Get storage unit node map from data sources.
*
* @param dataSources data sources
* @return storage unit node mappers
*/
public static Map<String, StorageUnitNodeMapper> fromDataSources(final Map<String, DataSource> dataSources) {
public static Map<String, StorageNode> fromDataSources(final Map<String, DataSource> dataSources) {
return dataSources.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> fromDataSource(entry.getKey(), entry.getValue()), (oldValue, currentValue) -> currentValue, LinkedHashMap::new));
}

private static StorageUnitNodeMapper fromDataSource(final String storageUnitName, final DataSource dataSource) {
private static StorageNode fromDataSource(final String storageUnitName, final DataSource dataSource) {
DataSourcePoolProperties props = DataSourcePoolPropertiesCreator.create(dataSource);
String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString();
return new StorageUnitNodeMapper(storageUnitName, new StorageNode(new StorageNodeName(storageUnitName), url));
return new StorageNode(new StorageNodeName(storageUnitName), url);
}

/**
* Get storage unit node mappers from data source pool properties.
* Get storage unit node map from data source pool properties.
*
* @param propsMap data source pool properties map
* @return storage unit node mappers
*/
public static Map<String, StorageUnitNodeMapper> fromDataSourcePoolProperties(final Map<String, DataSourcePoolProperties> propsMap) {
Map<String, StorageUnitNodeMapper> result = new LinkedHashMap<>();
public static Map<String, StorageNode> fromDataSourcePoolProperties(final Map<String, DataSourcePoolProperties> propsMap) {
Map<String, StorageNode> result = new LinkedHashMap<>();
for (Entry<String, DataSourcePoolProperties> entry : propsMap.entrySet()) {
String storageUnitName = entry.getKey();
result.put(storageUnitName, fromDataSourcePoolProperties(storageUnitName, entry.getValue()));
}
return result;
}

private static StorageUnitNodeMapper fromDataSourcePoolProperties(final String storageUnitName, final DataSourcePoolProperties props) {
private static StorageNode fromDataSourcePoolProperties(final String storageUnitName, final DataSourcePoolProperties props) {
Map<String, Object> standardProps = props.getConnectionPropertySynonyms().getStandardProperties();
String url = standardProps.get("url").toString();
boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable();
StorageNodeName storageNodeName = getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable);
return createStorageUnitNodeMapper(storageNodeName, storageUnitName, url, isInstanceConnectionAvailable);
return createStorageNode(storageNodeName, url, isInstanceConnectionAvailable);
}

private static StorageNodeName getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) {
Expand All @@ -90,11 +90,8 @@ private static StorageNodeName getStorageNodeName(final String dataSourceName, f
}
}

private static StorageUnitNodeMapper createStorageUnitNodeMapper(final StorageNodeName storageNodeName,
final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) {
return isInstanceConnectionAvailable
? new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageNodeName, url, new StandardJdbcUrlParser().parse(url).getDatabase()))
: new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageNodeName, url));
private static StorageNode createStorageNode(final StorageNodeName storageNodeName, final String url, final boolean isInstanceConnectionAvailable) {
return isInstanceConnectionAvailable ? new StorageNode(storageNodeName, url, new StandardJdbcUrlParser().parse(url).getDatabase()) : new StorageNode(storageNodeName, url);
}

/**
Expand Down
Loading
Loading