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

Refactor StorageUnitNodeMapperUtils #28603

Merged
merged 2 commits into from
Sep 27, 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 @@ -52,7 +52,7 @@ 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.getStorageUnitNodeMappers(dataSourcePoolPropertiesMap);
Map<String, StorageUnitNodeMapper> mappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap);
storageResource = new StorageResource(getStorageNodeDataSourceMap(mappers), mappers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
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.domain.DataSourcePoolProperties;
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.StorageResourceUtils;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapperUtils;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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 javax.sql.DataSource;
import java.util.Collection;
Expand Down Expand Up @@ -53,7 +54,7 @@ public ResourceMetaData(final String databaseName, final Map<String, DataSource>
dataSourceMap = StorageResourceUtils.getStorageNodeDataSources(dataSources);
storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, dataSources.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
StorageResourceUtils.getStorageUnitNodeMappers(dataSources));
StorageUnitNodeMapperUtils.fromDataSources(dataSources));
}

public ResourceMetaData(final String databaseName, final Map<StorageNode, DataSource> dataSourceMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
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.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper;

import javax.sql.DataSource;
import java.util.LinkedHashMap;
Expand All @@ -46,28 +43,4 @@ public static Map<StorageNode, DataSource> getStorageNodeDataSources(final Map<S
return dataSources.entrySet().stream()
.collect(Collectors.toMap(entry -> new StorageNode(entry.getKey()), Entry::getValue, (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(dataSources.size(), 1F)));
}

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

/**
* Get storage unit node mapper from provided data source.
*
* @param dataSourceName data source name
* @param dataSource data source
* @return storage unit node mapper
*/
public static StorageUnitNodeMapper getStorageUnitNodeMapper(final String dataSourceName, final DataSource dataSource) {
DataSourcePoolProperties props = DataSourcePoolPropertiesCreator.create(dataSource);
String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString();
return new StorageUnitNodeMapper(dataSourceName, new StorageNode(dataSourceName), url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
import org.apache.shardingsphere.infra.database.core.connector.url.UnrecognizedDatabaseURLException;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
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 javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
* Storage unit node mapper utility class.
Expand All @@ -38,21 +41,38 @@
public final class StorageUnitNodeMapperUtils {

/**
* Get storage unit node mappers.
* Get storage unit node mappers from data sources.
*
* @param dataSources data sources
* @return storage unit node mappers
*/
public static Map<String, StorageUnitNodeMapper> 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) {
DataSourcePoolProperties props = DataSourcePoolPropertiesCreator.create(dataSource);
String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString();
return new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageUnitName), url);
}

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

private static StorageUnitNodeMapper getStorageUnitNodeMapper(final String storageUnitName, final DataSourcePoolProperties props) {
private static StorageUnitNodeMapper 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class NewResourceSwitchManager {
public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map<String, DataSourcePoolProperties> storageUnitDataSourcePoolProps) {
Map<String, DataSourcePoolProperties> mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap());
mergedPropsMap.putAll(storageUnitDataSourcePoolProps);
Map<String, StorageUnitNodeMapper> toBeCreatedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(storageUnitDataSourcePoolProps);
Map<String, StorageUnitNodeMapper> toBeCreatedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(storageUnitDataSourcePoolProps);
return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedMappers, storageUnitDataSourcePoolProps),
new StorageResource(Collections.emptyMap(), Collections.emptyMap()), mergedPropsMap);
}
Expand Down Expand Up @@ -78,7 +78,7 @@ private StorageResource getRegisterNewStorageResource(final ResourceMetaData res
public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaData, final Map<String, DataSourcePoolProperties> propsMap) {
Map<String, DataSourcePoolProperties> mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap());
mergedDataSourcePoolPropertiesMap.putAll(propsMap);
Map<String, StorageUnitNodeMapper> toBeAlteredMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(mergedDataSourcePoolPropertiesMap);
Map<String, StorageUnitNodeMapper> toBeAlteredMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(mergedDataSourcePoolPropertiesMap);
return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredMappers, mergedDataSourcePoolPropertiesMap),
getStaleStorageResource(resourceMetaData, toBeAlteredMappers), mergedDataSourcePoolPropertiesMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class ResourceSwitchManager {
public SwitchingResource create(final ResourceMetaData resourceMetaData, final Map<String, DataSourcePoolProperties> toBeChangedPropsMap) {
Map<String, DataSourcePoolProperties> mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap());
mergedPropsMap.putAll(toBeChangedPropsMap);
Map<String, StorageUnitNodeMapper> toBeChangedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeChangedPropsMap);
Map<String, StorageUnitNodeMapper> toBeChangedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(toBeChangedPropsMap);
return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap),
getStaleDataSources(resourceMetaData, toBeChangedMappers, mergedPropsMap), mergedPropsMap);
}
Expand All @@ -66,7 +66,7 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M
public SwitchingResource createByDropResource(final ResourceMetaData resourceMetaData, final Map<String, DataSourcePoolProperties> toBeDeletedPropsMap) {
Map<String, DataSourcePoolProperties> mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap());
mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey);
Map<String, StorageUnitNodeMapper> toRemovedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeDeletedPropsMap);
Map<String, StorageUnitNodeMapper> toRemovedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(toBeDeletedPropsMap);
return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyMap()),
getToBeRemovedStaleDataSources(resourceMetaData, toRemovedMappers), mergedDataSourcePoolPropertiesMap);
}
Expand All @@ -82,7 +82,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet
Map<String, DataSourcePoolProperties> mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap());
mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each));
mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap);
Map<String, StorageUnitNodeMapper> toBeChangedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeChangedPropsMap);
Map<String, StorageUnitNodeMapper> toBeChangedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(toBeChangedPropsMap);
StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap);
Collection<StorageNode> toBeChangedStorageNodes = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet());
staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
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.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResourceUtils;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.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.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
Expand Down Expand Up @@ -255,7 +256,7 @@ private ResourceMetaData createOriginalResource() {
when(result.getStorageUnitMetaData().getDataSources()).thenReturn(originalDataSources);
Map<StorageNode, DataSource> storageNodeDataSourceMap = StorageResourceUtils.getStorageNodeDataSources(originalDataSources);
Map<String, StorageUnit> storageUnits = new LinkedHashMap<>(2, 1F);
Map<String, StorageUnitNodeMapper> storageUnitNodeMappers = StorageResourceUtils.getStorageUnitNodeMappers(originalDataSources);
Map<String, StorageUnitNodeMapper> storageUnitNodeMappers = StorageUnitNodeMapperUtils.fromDataSources(originalDataSources);
for (Entry<String, StorageUnitNodeMapper> entry : storageUnitNodeMappers.entrySet()) {
storageUnits.put(entry.getKey(), new StorageUnit("foo_db", storageNodeDataSourceMap, mock(DataSourcePoolProperties.class), entry.getValue()));
}
Expand Down