From ec02235dd3ae61bcceb77e9f824df206a29ba6bf Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 27 Sep 2023 18:56:39 +0800 Subject: [PATCH] Refactor StorageUnitNodeMapperUtils --- ...aSourceGeneratedDatabaseConfiguration.java | 2 +- ...taSourceProvidedDatabaseConfiguration.java | 5 ++-- .../database/resource/ResourceMetaData.java | 3 +- .../resource/StorageResourceUtils.java | 20 ------------- .../unit/StorageUnitNodeMapperUtils.java | 28 ++++++++++++++++--- .../switcher/NewResourceSwitchManager.java | 4 +-- .../switcher/ResourceSwitchManager.java | 6 ++-- .../mode/manager/ContextManagerTest.java | 5 ++-- 8 files changed, 38 insertions(+), 35 deletions(-) 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 b877a421cb1a2..a441a0883ef67 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 @@ -52,7 +52,7 @@ public DataSourceGeneratedDatabaseConfiguration(final Map DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - Map mappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(dataSourcePoolPropertiesMap); + Map mappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap); storageResource = new StorageResource(getStorageNodeDataSourceMap(mappers), mappers); } 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 2ecb2a7e1d8e2..03c5d7ab69125 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 @@ -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; @@ -48,7 +49,7 @@ public final class DataSourceProvidedDatabaseConfiguration implements DatabaseCo public DataSourceProvidedDatabaseConfiguration(final Map dataSources, final Collection 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); } 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 2d0fd6f44a5f1..9a502a2ce9415 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 @@ -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; @@ -53,7 +54,7 @@ public ResourceMetaData(final String databaseName, final Map 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 dataSourceMap, diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceUtils.java index 845b681263488..0de0005f438db 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceUtils.java @@ -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; @@ -46,21 +43,4 @@ public static Map getStorageNodeDataSources(final Map 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 getStorageUnitNodeMappers(final Map dataSources) { - return dataSources.entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> getStorageUnitNodeMapper(entry.getKey(), entry.getValue()), (oldValue, currentValue) -> currentValue, LinkedHashMap::new)); - } - - private 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); - } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java index 20587b2591481..f016401f34ce0 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java @@ -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. @@ -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 fromDataSources(final Map 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 getStorageUnitNodeMappers(final Map propsMap) { + public static Map fromDataSourcePoolProperties(final Map propsMap) { Map result = new LinkedHashMap<>(); for (Entry 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 standardProps = props.getConnectionPropertySynonyms().getStandardProperties(); String url = standardProps.get("url").toString(); boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java index b13736b6cc1fe..e5a43bdbe3b69 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java @@ -50,7 +50,7 @@ public final class NewResourceSwitchManager { public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolProps) { Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedPropsMap.putAll(storageUnitDataSourcePoolProps); - Map toBeCreatedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(storageUnitDataSourcePoolProps); + Map toBeCreatedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(storageUnitDataSourcePoolProps); return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedMappers, storageUnitDataSourcePoolProps), new StorageResource(Collections.emptyMap(), Collections.emptyMap()), mergedPropsMap); } @@ -78,7 +78,7 @@ private StorageResource getRegisterNewStorageResource(final ResourceMetaData res public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaData, final Map propsMap) { Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedDataSourcePoolPropertiesMap.putAll(propsMap); - Map toBeAlteredMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(mergedDataSourcePoolPropertiesMap); + Map toBeAlteredMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(mergedDataSourcePoolPropertiesMap); return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredMappers, mergedDataSourcePoolPropertiesMap), getStaleStorageResource(resourceMetaData, toBeAlteredMappers), mergedDataSourcePoolPropertiesMap); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java index 864109645184c..48033de1e4925 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java @@ -51,7 +51,7 @@ public final class ResourceSwitchManager { public SwitchingResource create(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedPropsMap.putAll(toBeChangedPropsMap); - Map toBeChangedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeChangedPropsMap); + Map toBeChangedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap), getStaleDataSources(resourceMetaData, toBeChangedMappers, mergedPropsMap), mergedPropsMap); } @@ -66,7 +66,7 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M public SwitchingResource createByDropResource(final ResourceMetaData resourceMetaData, final Map toBeDeletedPropsMap) { Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey); - Map toRemovedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeDeletedPropsMap); + Map toRemovedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(toBeDeletedPropsMap); return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyMap()), getToBeRemovedStaleDataSources(resourceMetaData, toRemovedMappers), mergedDataSourcePoolPropertiesMap); } @@ -82,7 +82,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each)); mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap); - Map toBeChangedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeChangedPropsMap); + Map toBeChangedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap); Collection toBeChangedStorageNodes = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodes)); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index 5399efe36c193..97b29cc7ad680 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -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; @@ -255,7 +256,7 @@ private ResourceMetaData createOriginalResource() { when(result.getStorageUnitMetaData().getDataSources()).thenReturn(originalDataSources); Map storageNodeDataSourceMap = StorageResourceUtils.getStorageNodeDataSources(originalDataSources); Map storageUnits = new LinkedHashMap<>(2, 1F); - Map storageUnitNodeMappers = StorageResourceUtils.getStorageUnitNodeMappers(originalDataSources); + Map storageUnitNodeMappers = StorageUnitNodeMapperUtils.fromDataSources(originalDataSources); for (Entry entry : storageUnitNodeMappers.entrySet()) { storageUnits.put(entry.getKey(), new StorageUnit("foo_db", storageNodeDataSourceMap, mock(DataSourcePoolProperties.class), entry.getValue())); }