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 715ad0bfdc730..8f1dd9fb20970 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 @@ -27,7 +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.unit.StorageUnit; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator; import javax.sql.DataSource; import java.util.Collection; @@ -52,7 +52,7 @@ public DataSourceGeneratedDatabaseConfiguration(final Map dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap); + Map storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropertiesMap); Map storageNodeDataSources = getStorageNodeDataSourceMap(dataSourcePoolPropertiesMap, storageUnitNodeMap); storageUnits = new LinkedHashMap<>(dataSourceConfigs.size(), 1F); for (Entry entry : dataSourceConfigs.entrySet()) { 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 bc450dabfd530..1f1fd121fb5d1 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 @@ -26,7 +26,7 @@ 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.unit.StorageUnit; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator; import javax.sql.DataSource; import java.util.Collection; @@ -49,7 +49,8 @@ public final class DataSourceProvidedDatabaseConfiguration implements DatabaseCo public DataSourceProvidedDatabaseConfiguration(final Map dataSources, final Collection ruleConfigs) { this.ruleConfigurations = ruleConfigs; - Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(dataSources); + Map storageUnitNodeMap = dataSources.keySet().stream() + .collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); Map storageNodeDataSources = StorageNodeAggregator.aggregateDataSources(dataSources); Map dataSourcePoolPropertiesMap = createDataSourcePoolPropertiesMap(dataSources); storageUnits = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F); @@ -66,7 +67,7 @@ public DataSourceProvidedDatabaseConfiguration(final StorageResource storageReso final Collection ruleConfigs, final Map dataSourcePoolPropertiesMap) { this.storageResource = storageResource; this.ruleConfigurations = ruleConfigs; - Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap); + Map storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropertiesMap); Map storageNodeDataSources = storageResource.getDataSources(); storageUnits = new LinkedHashMap<>(dataSourcePoolPropertiesMap.size(), 1F); for (Entry entry : dataSourcePoolPropertiesMap.entrySet()) { 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 ba0432523b23d..97962e66f602b 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 @@ -24,7 +24,6 @@ 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.unit.StorageUnit; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; import javax.sql.DataSource; import java.util.Collection; @@ -47,11 +46,12 @@ public final class ResourceMetaData { public ResourceMetaData(final Map dataSources) { this.dataSources = StorageNodeAggregator.aggregateDataSources(dataSources); - Map storageNodes = StorageUnitNodeMapUtils.fromDataSources(dataSources); + Map storageUnitNodeMap = dataSources.keySet().stream() + .collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); Map dataSourcePoolPropsMap = dataSources.entrySet().stream().collect( Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); storageUnits = new LinkedHashMap<>(); - for (Entry entry : storageNodes.entrySet()) { + for (Entry entry : storageUnitNodeMap.entrySet()) { storageUnits.put(entry.getKey(), new StorageUnit(entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName()))); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapCreator.java similarity index 66% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapUtils.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapCreator.java index 2be8758a11759..246a1e4a8dbbe 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapCreator.java @@ -27,52 +27,39 @@ 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 map utility class. + * Storage unit node map creator. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class StorageUnitNodeMapUtils { +public final class StorageUnitNodeMapCreator { /** - * Get storage unit node map 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 -> new StorageNode(entry.getKey()), (oldValue, currentValue) -> currentValue, LinkedHashMap::new)); - } - - /** - * Get storage unit node map from data source pool properties. + * Create storage unit node map. * * @param propsMap data source pool properties map - * @return storage unit node mappers + * @return storage unit node map */ - public static Map fromDataSourcePoolProperties(final Map propsMap) { + public static Map create(final Map propsMap) { Map result = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { String storageUnitName = entry.getKey(); - result.put(storageUnitName, fromDataSourcePoolProperties(storageUnitName, entry.getValue())); + result.put(storageUnitName, create(storageUnitName, entry.getValue())); } return result; } - private static StorageNode fromDataSourcePoolProperties(final String storageUnitName, final DataSourcePoolProperties props) { + private static StorageNode create(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(); - return getStorageNode(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable); + return create(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable); } - private static StorageNode getStorageNode(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) { + private static StorageNode create(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) { try { JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url); return isInstanceConnectionAvailable ? new StorageNode(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : new StorageNode(dataSourceName); 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 347affcf80f57..7f34a2e3ae410 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 @@ -23,7 +23,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.StorageNodeAggregator; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator; import javax.sql.DataSource; import java.util.Collections; @@ -49,7 +49,7 @@ public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMeta Map mergedPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedPropsMap.putAll(storageUnitDataSourcePoolPropsMap); - Map toBeCreatedStorageUintNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(storageUnitDataSourcePoolPropsMap); + Map toBeCreatedStorageUintNodeMap = StorageUnitNodeMapCreator.create(storageUnitDataSourcePoolPropsMap); Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(storageUnitDataSourcePoolPropsMap); return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageUintNodeMap, dataSourcePoolPropsMap), new StorageResource(Collections.emptyMap(), Collections.emptyMap()), mergedPropsMap); @@ -77,7 +77,7 @@ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaDat Map mergedDataSourcePoolPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropsMap.putAll(propsMap); - Map toBeAlteredStorageUintNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(mergedDataSourcePoolPropsMap); + Map toBeAlteredStorageUintNodeMap = StorageUnitNodeMapCreator.create(mergedDataSourcePoolPropsMap); Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(mergedDataSourcePoolPropsMap); return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageUintNodeMap, dataSourcePoolPropsMap), getStaleStorageResource(resourceMetaData, toBeAlteredStorageUintNodeMap), mergedDataSourcePoolPropsMap); 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 1120384febafa..f979395fa598b 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 @@ -25,7 +25,7 @@ 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.unit.StorageUnit; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator; import javax.sql.DataSource; import java.util.Collection; @@ -51,7 +51,7 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M Map mergedPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedPropsMap.putAll(toBeChangedPropsMap); - Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); + Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeChangedPropsMap); Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(toBeChangedPropsMap); StorageResource newStorageResource = createNewStorageResource(resourceMetaData, toBeChangedStorageUnitNodeMap, dataSourcePoolPropsMap); StorageResource staleDataSources = getStaleDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, mergedPropsMap); @@ -69,7 +69,7 @@ public SwitchingResource createByDropResource(final ResourceMetaData resourceMet Map mergedDataSourcePoolPropertiesMap = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey); - Map toRemovedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeDeletedPropsMap); + Map toRemovedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeDeletedPropsMap); return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyMap()), getToBeRemovedStaleDataSources(resourceMetaData, toRemovedStorageUnitNodeMap), mergedDataSourcePoolPropertiesMap); } @@ -86,7 +86,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each)); mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap); - Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); + Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeChangedPropsMap); StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, toBeChangedPropsMap); staleStorageResource.getDataSources().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageUnitNodeMap.values())); staleStorageResource.getStorageUnitNodeMap().putAll( 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 c8372f0f1fa0c..381362e62fcdd 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 @@ -31,7 +31,6 @@ 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.unit.StorageUnit; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; 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; @@ -60,6 +59,7 @@ import java.util.Map.Entry; import java.util.Optional; import java.util.Properties; +import java.util.stream.Collectors; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -253,7 +253,8 @@ private ResourceMetaData createOriginalResource() { originalDataSources.put("ds_1", new MockedDataSource()); originalDataSources.put("ds_2", new MockedDataSource()); Map storageNodeDataSourceMap = StorageNodeAggregator.aggregateDataSources(originalDataSources); - Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(originalDataSources); + Map storageUnitNodeMap = originalDataSources.keySet().stream() + .collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); Map storageUnits = new LinkedHashMap<>(2, 1F); for (Entry entry : storageUnitNodeMap.entrySet()) { DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java index a704756b65a48..cdc0fa7455716 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java @@ -45,7 +45,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; 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.StorageUnitNodeMapUtils; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; @@ -162,7 +162,7 @@ private void addResources(final String databaseName, final Map storageUnits = ProxyContext.getInstance().getContextManager() .getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnits(); - Map toBeAddedStorageNode = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(propsMap); + Map toBeAddedStorageNode = StorageUnitNodeMapCreator.create(propsMap); for (Entry entry : propsMap.entrySet()) { storageUnits.put(entry.getKey(), new StorageUnit(toBeAddedStorageNode.get(entry.getKey()), entry.getValue(), DataSourcePoolCreator.create(entry.getValue()))); }