From f46ddd94f3bb0f2a49dcac06558ba8bc0db16512 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 28 Sep 2023 18:05:42 +0800 Subject: [PATCH 1/2] Remove StorageUnitNodeMapper --- ...aSourceGeneratedDatabaseConfiguration.java | 17 ++-- ...taSourceProvidedDatabaseConfiguration.java | 4 +- .../database/ShardingSphereDatabase.java | 2 +- .../database/resource/ResourceMetaData.java | 10 +- .../database/resource/StorageResource.java | 13 ++- .../database/resource/unit/StorageUnit.java | 25 +++-- .../resource/unit/StorageUnitMetaData.java | 11 ++- ...tils.java => StorageUnitNodeMapUtils.java} | 29 +++--- .../resource/unit/StorageUnitNodeMapper.java | 34 ------- .../context/ConfigurationContextManager.java | 28 +++--- .../switcher/NewResourceSwitchManager.java | 46 ++++----- .../switcher/ResourceSwitchManager.java | 94 +++++++++---------- .../mode/manager/ContextManagerTest.java | 12 +-- 13 files changed, 141 insertions(+), 184 deletions(-) rename infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/{StorageUnitNodeMapperUtils.java => StorageUnitNodeMapUtils.java} (76%) delete mode 100644 infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapper.java 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 0c450f59942fb..c96d9dce33cbe 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 @@ -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; @@ -52,15 +52,14 @@ public DataSourceGeneratedDatabaseConfiguration(final Map DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - Map mappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap); - storageResource = new StorageResource(getStorageNodeDataSourceMap(mappers), mappers); + Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(dataSourcePoolPropertiesMap); + storageResource = new StorageResource(getStorageNodeDataSourceMap(storageUnitNodeMap), storageUnitNodeMap); } - private Map getStorageNodeDataSourceMap(final Map mappers) { - Map result = new LinkedHashMap<>(mappers.size(), 1F); - for (Entry entry : mappers.entrySet()) { - result.computeIfAbsent(entry.getValue().getStorageNode().getName(), - key -> DataSourcePoolCreator.create(entry.getKey(), dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values())); + private Map getStorageNodeDataSourceMap(final Map storageUnitNodeMap) { + Map result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F); + for (Entry entry : storageUnitNodeMap.entrySet()) { + result.computeIfAbsent(entry.getValue().getName(), key -> DataSourcePoolCreator.create(entry.getKey(), dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values())); } return result; } 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 fa0e04c6b61a0..19773c8075002 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 @@ -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; @@ -49,7 +49,7 @@ public final class DataSourceProvidedDatabaseConfiguration implements DatabaseCo public DataSourceProvidedDatabaseConfiguration(final Map dataSources, final Collection 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); } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index d7571a1280d41..087c5dedb01d1 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -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 propsMap) { - return new ResourceMetaData(databaseName, storageResource.getDataSourceMap(), storageResource.getStorageUnitNodeMappers(), propsMap); + return new ResourceMetaData(databaseName, storageResource.getDataSourceMap(), storageResource.getStorageUnitNodeMap(), propsMap); } /** 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 7564aa9c3956d..51ab60a081c40 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 @@ -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; @@ -51,13 +51,13 @@ public ResourceMetaData(final Map 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 dataSourceMap, - final Map storageUnitNodeMappers, final Map propsMap) { + final Map storageUnitNodeMap, final Map propsMap) { this.dataSourceMap = dataSourceMap; - storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageUnitNodeMappers); + storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageUnitNodeMap); } /** diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java index d5f960f63bfeb..bc995f8d5cef2 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java @@ -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; @@ -36,20 +35,20 @@ public final class StorageResource { private final Map dataSourceMap; - private final Map storageUnitNodeMappers; + private final Map storageUnitNodeMap; private final Map wrappedDataSources; - public StorageResource(final Map dataSourceMap, final Map storageUnitNodeMappers) { + public StorageResource(final Map dataSourceMap, final Map storageUnitNodeMap) { this.dataSourceMap = dataSourceMap; - this.storageUnitNodeMappers = storageUnitNodeMappers; + this.storageUnitNodeMap = storageUnitNodeMap; wrappedDataSources = createWrappedDataSources(); } private Map createWrappedDataSources() { - Map result = new LinkedHashMap<>(storageUnitNodeMappers.size(), 1F); - for (Entry entry : storageUnitNodeMappers.entrySet()) { - StorageNode storageNode = entry.getValue().getStorageNode(); + Map result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F); + for (Entry 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())); diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java index 75864e6b474c1..a1f8dc7f373f3 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java @@ -46,7 +46,7 @@ public final class StorageUnit { private final DataSourcePoolProperties dataSourcePoolProperties; - private final StorageUnitNodeMapper unitNodeMapper; + private final StorageNode storageNode; private final DataSource dataSource; @@ -55,17 +55,16 @@ public final class StorageUnit { private final ConnectionProperties connectionProperties; public StorageUnit(final String databaseName, final Map 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 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 storageNodeDataSources, final StorageUnitNodeMapper mapper) { - StorageNode storageNode = mapper.getStorageNode(); + private DataSource getStorageUnitDataSource(final Map storageNodeDataSources) { DataSource dataSource = storageNodeDataSources.get(storageNode.getName()); return new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl()); } @@ -80,15 +79,13 @@ private Map getEnabledStorageNodeDataSources(final .filter(entry -> enabledDataSources.containsKey(entry.getKey().getName())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); } - private DatabaseType createStorageType(final Map enabledStorageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper) { - return DatabaseTypeEngine.getStorageType(enabledStorageNodeDataSources.containsKey(unitNodeMapper.getStorageNode().getName()) - ? Collections.singleton(enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNode().getName())) + private DatabaseType createStorageType(final Map enabledStorageNodeDataSources) { + return DatabaseTypeEngine.getStorageType(enabledStorageNodeDataSources.containsKey(storageNode.getName()) + ? Collections.singleton(enabledStorageNodeDataSources.get(storageNode.getName())) : Collections.emptyList()); } - private Optional createConnectionProperties(final Map enabledStorageNodeDataSources, - final StorageUnitNodeMapper mapper, final DatabaseType storageType) { - StorageNode storageNode = mapper.getStorageNode(); + private Optional createConnectionProperties(final Map enabledStorageNodeDataSources) { if (!enabledStorageNodeDataSources.containsKey(storageNode.getName())) { return Optional.empty(); } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java index 81ef617ad7ad0..0dfa7ce06c002 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java @@ -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; @@ -34,7 +35,7 @@ public final class StorageUnitMetaData { // TODO zhangliang: should refactor - private final Map unitNodeMappers; + private final Map storageUnitNodeMap; private final Map storageUnits; @@ -42,10 +43,10 @@ public final class StorageUnitMetaData { private final Map dataSources; public StorageUnitMetaData(final String databaseName, final Map storageNodeDataSources, - final Map dataSourcePoolPropertiesMap, final Map unitNodeMappers) { - this.unitNodeMappers = unitNodeMappers; - storageUnits = new LinkedHashMap<>(unitNodeMappers.size(), 1F); - for (Entry entry : unitNodeMappers.entrySet()) { + final Map dataSourcePoolPropertiesMap, final Map storageUnitNodeMap) { + this.storageUnitNodeMap = storageUnitNodeMap; + storageUnits = new LinkedHashMap<>(this.storageUnitNodeMap.size(), 1F); + for (Entry entry : this.storageUnitNodeMap.entrySet()) { storageUnits.put(entry.getKey(), new StorageUnit(databaseName, storageNodeDataSources, dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue())); } dataSources = createDataSources(); 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/StorageUnitNodeMapUtils.java similarity index 76% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapUtils.java index 2bb78e1275e01..cad759e89088c 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/StorageUnitNodeMapUtils.java @@ -36,36 +36,36 @@ 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 fromDataSources(final Map dataSources) { + 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) { + 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 fromDataSourcePoolProperties(final Map propsMap) { - Map result = new LinkedHashMap<>(); + public static Map fromDataSourcePoolProperties(final Map propsMap) { + Map result = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { String storageUnitName = entry.getKey(); result.put(storageUnitName, fromDataSourcePoolProperties(storageUnitName, entry.getValue())); @@ -73,12 +73,12 @@ public static Map fromDataSourcePoolProperties(fi return result; } - private static StorageUnitNodeMapper fromDataSourcePoolProperties(final String storageUnitName, final DataSourcePoolProperties props) { + private static StorageNode 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(); 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) { @@ -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); } /** diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapper.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapper.java deleted file mode 100644 index 9ac4e1891c481..0000000000000 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapper.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.infra.metadata.database.resource.unit; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; - -/** - * Storage unit and node mapper. - */ -@RequiredArgsConstructor -@Getter -public final class StorageUnitNodeMapper { - - private final String name; - - private final StorageNode storageNode; -} diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java index f79132f3c51f9..66e798088bd37 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java @@ -25,14 +25,14 @@ import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; -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.resource.unit.StorageUnitNodeMapper; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; 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.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.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.SchemaManager; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; @@ -275,11 +275,11 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD */ public Map renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) { Map newStorageNodes = getNewStorageNodes(database.getResourceMetaData().getDataSourceMap(), resource); - Map newStorageUnitNodeMappers = getNewStorageUnitNodeMappers(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(), resource); + Map newStorageUnitNodeMap = getNewStorageUnitNodeMap(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(), resource); Map propsMap = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> currentValue, LinkedHashMap::new)); return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(database.getName(), database.getProtocolType(), - new ResourceMetaData(database.getName(), newStorageNodes, newStorageUnitNodeMappers, propsMap), database.getRuleMetaData(), database.getSchemas())); + new ResourceMetaData(database.getName(), newStorageNodes, newStorageUnitNodeMap, propsMap), database.getRuleMetaData(), database.getSchemas())); } private Map getNewStorageNodes(final Map currentStorageNodes, final SwitchingResource resource) { @@ -292,11 +292,11 @@ private Map getNewStorageNodes(final Map getNewStorageUnitNodeMappers(final Map currentStorageUnits, final SwitchingResource resource) { - Map result = new LinkedHashMap<>(currentStorageUnits.size(), 1F); + private Map getNewStorageUnitNodeMap(final Map currentStorageUnits, final SwitchingResource resource) { + Map result = new LinkedHashMap<>(currentStorageUnits.size(), 1F); for (Entry entry : currentStorageUnits.entrySet()) { - if (!resource.getStaleStorageResource().getStorageUnitNodeMappers().containsKey(entry.getKey())) { - result.put(entry.getKey(), entry.getValue().getUnitNodeMapper()); + if (!resource.getStaleStorageResource().getStorageUnitNodeMap().containsKey(entry.getKey())) { + result.put(entry.getKey(), entry.getValue().getStorageNode()); } } return result; @@ -365,14 +365,14 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re private StorageResource getMergedStorageResource(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { Map storageNodeDataSources = currentResourceMetaData.getDataSourceMap(); - Map storageUnitNodeMappers = currentResourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(); + Map storageUnitNodeMap = currentResourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap(); if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSourceMap().isEmpty()) { storageNodeDataSources.putAll(switchingResource.getNewStorageResource().getDataSourceMap()); } - if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getStorageUnitNodeMappers().isEmpty()) { - storageUnitNodeMappers.putAll(switchingResource.getNewStorageResource().getStorageUnitNodeMappers()); + if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getStorageUnitNodeMap().isEmpty()) { + storageUnitNodeMap.putAll(switchingResource.getNewStorageResource().getStorageUnitNodeMap()); } - return new StorageResource(storageNodeDataSources, storageUnitNodeMappers); + return new StorageResource(storageNodeDataSources, storageUnitNodeMap); } private ShardingSphereDatabase createChangedDatabase(final String databaseName, final boolean internalLoadMetaData, final MetaDataBasedPersistService persistService, 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 5448050a69ae6..a4f4368b2d50f 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 @@ -21,9 +21,9 @@ import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; 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.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; @@ -50,22 +50,22 @@ 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.fromDataSourcePoolProperties(storageUnitDataSourcePoolProps); - return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedMappers, storageUnitDataSourcePoolProps), + Map toBeCreatedStorageUintNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(storageUnitDataSourcePoolProps); + return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageUintNodeMap, storageUnitDataSourcePoolProps), new StorageResource(Collections.emptyMap(), Collections.emptyMap()), mergedPropsMap); } private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, - final Map mappers, final Map storageUnitDataSourcePoolProps) { - Collection storageNodeNames = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + final Map storageUintNodeMap, final Map storageUnitDataSourcePoolProps) { + Collection storageNodeNames = storageUintNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map newStorageNodes = new LinkedHashMap<>(storageNodeNames.size(), 1F); for (StorageNodeName each : storageNodeNames) { if (!resourceMetaData.getDataSourceMap().containsKey(each)) { newStorageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each))); } } - return new StorageResource(newStorageNodes, mappers); + return new StorageResource(newStorageNodes, storageUintNodeMap); } /** @@ -78,30 +78,30 @@ 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.fromDataSourcePoolProperties(mergedDataSourcePoolPropertiesMap); - return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredMappers, mergedDataSourcePoolPropertiesMap), - getStaleStorageResource(resourceMetaData, toBeAlteredMappers), mergedDataSourcePoolPropertiesMap); + Map toBeAlteredStorageUintNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(mergedDataSourcePoolPropertiesMap); + return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageUintNodeMap, mergedDataSourcePoolPropertiesMap), + getStaleStorageResource(resourceMetaData, toBeAlteredStorageUintNodeMap), mergedDataSourcePoolPropertiesMap); } - private StorageResource getAlterNewStorageResource(final Map mappers, final Map storageUnitDataSourcePoolProps) { - Collection toBeAlteredStorageNodeNames = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + private StorageResource getAlterNewStorageResource(final Map storageUintNodeMap, final Map storageUnitDataSourcePoolProps) { + Collection toBeAlteredStorageNodeNames = storageUintNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodeNames.size(), 1F); for (StorageNodeName each : toBeAlteredStorageNodeNames) { storageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each))); } - return new StorageResource(storageNodes, mappers); + return new StorageResource(storageNodes, storageUintNodeMap); } - private StorageResource getStaleStorageResource(final ResourceMetaData resourceMetaData, final Map mappers) { - Collection toBeAlteredStorageNodeNames = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); + private StorageResource getStaleStorageResource(final ResourceMetaData resourceMetaData, final Map storageUintNodeMap) { + Collection toBeAlteredStorageNodeNames = storageUintNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodeNames.size(), 1F); for (Entry entry : resourceMetaData.getDataSourceMap().entrySet()) { if (toBeAlteredStorageNodeNames.contains(entry.getKey())) { storageNodes.put(entry.getKey(), entry.getValue()); } } - return new StorageResource(storageNodes, mappers); + return new StorageResource(storageNodes, storageUintNodeMap); } /** @@ -120,12 +120,12 @@ public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMe } private StorageResource getToBeRemovedStaleStorageResource(final ResourceMetaData resourceMetaData, final String storageUnitName) { - StorageUnitNodeMapper storageUnitNodeMapper = resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().remove(storageUnitName); - Map reservedStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(); + StorageNode storageNode = resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap().remove(storageUnitName); + Map reservedStorageUintNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap(); Map storageNodes = new LinkedHashMap<>(1, 1F); - if (reservedStorageUnitNodeMappers.values().stream().noneMatch(each -> each.getStorageNode().equals(storageUnitNodeMapper.getStorageNode()))) { - storageNodes.put(storageUnitNodeMapper.getStorageNode().getName(), resourceMetaData.getDataSourceMap().get(storageUnitNodeMapper.getStorageNode().getName())); + if (reservedStorageUintNodeMap.values().stream().noneMatch(each -> each.equals(storageNode))) { + storageNodes.put(storageNode.getName(), resourceMetaData.getDataSourceMap().get(storageNode.getName())); } - return new StorageResource(storageNodes, Collections.singletonMap(storageUnitName, storageUnitNodeMapper)); + return new StorageResource(storageNodes, Collections.singletonMap(storageUnitName, storageNode)); } } 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 e4d5429209c9a..c457a9a7c00bb 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 @@ -22,10 +22,10 @@ import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; 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.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.StorageUnit; -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; @@ -51,9 +51,9 @@ 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.fromDataSourcePoolProperties(toBeChangedPropsMap); - return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap), - getStaleDataSources(resourceMetaData, toBeChangedMappers, mergedPropsMap), mergedPropsMap); + Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); + return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageUnitNodeMap, toBeChangedPropsMap), + getStaleDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, mergedPropsMap), mergedPropsMap); } /** @@ -66,9 +66,9 @@ 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.fromDataSourcePoolProperties(toBeDeletedPropsMap); + Map toRemovedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeDeletedPropsMap); return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyMap()), - getToBeRemovedStaleDataSources(resourceMetaData, toRemovedMappers), mergedDataSourcePoolPropertiesMap); + getToBeRemovedStaleDataSources(resourceMetaData, toRemovedStorageUnitNodeMap), mergedDataSourcePoolPropertiesMap); } /** @@ -82,24 +82,24 @@ 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.fromDataSourcePoolProperties(toBeChangedPropsMap); - StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap); - Collection toBeChangedStorageNodeNames = toBeChangedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); + Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); + StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, toBeChangedPropsMap); + Collection toBeChangedStorageNodeNames = toBeChangedStorageUnitNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeNames)); - staleStorageResource.getStorageUnitNodeMappers().putAll( - getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedMappers.keySet())); + staleStorageResource.getStorageUnitNodeMap().putAll( + getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet())); return new SwitchingResource(resourceMetaData, - createNewStorageResource(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap), staleStorageResource, mergedDataSourcePoolPropertiesMap); + createNewStorageResource(resourceMetaData, toBeChangedStorageUnitNodeMap, toBeChangedPropsMap), staleStorageResource, mergedDataSourcePoolPropertiesMap); } private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, - final Map toBeChangedMappers, final Map storageUnitDataSourcePoolProps) { - Collection toBeChangedStorageNodeName = toBeChangedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + final Map toBeChangedStorageUnitNodeMap, final Map storageUnitDataSourcePoolProps) { + Collection toBeChangedStorageNodeName = toBeChangedStorageUnitNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = getNewStorageNodes(resourceMetaData, toBeChangedStorageNodeName, storageNodeDataSourcePoolProps); - Map storageUnitNodeMappers = getNewStorageUnitNodeMappers(resourceMetaData, toBeChangedMappers); - return new StorageResource(storageNodes, storageUnitNodeMappers); + Map storageUnitNodeMap = getNewStorageUnitNodeMap(resourceMetaData, toBeChangedStorageUnitNodeMap); + return new StorageResource(storageNodes, storageUnitNodeMap); } private Map getNewStorageNodes(final ResourceMetaData resourceMetaData, final Collection toBeChangedStorageNodeName, @@ -111,11 +111,11 @@ private Map getNewStorageNodes(final ResourceMetaDa return result; } - private Map getNewStorageUnitNodeMappers(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMappers) { - Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers()); - result.keySet().removeAll(getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMappers.keySet()).keySet()); - result.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMappers)); - result.putAll(getToBeAddedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(), toBeChangedStorageUnitNodeMappers)); + private Map getNewStorageUnitNodeMap(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap) { + Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap()); + result.keySet().removeAll(getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet()).keySet()); + result.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap)); + result.putAll(getToBeAddedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap(), toBeChangedStorageUnitNodeMap)); return result; } @@ -147,26 +147,26 @@ private Map getToBeAddedDataSources(final Map toRemovedMappers) { - Map reservedStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() - .filter(entry -> !toRemovedMappers.containsKey(entry.getKey())) - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getUnitNodeMapper())); - Collection toBeRemovedStorageNodeNames = toRemovedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - Collection inUsedDataSourceNames = reservedStorageUnitNodeMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); + private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData resourceMetaData, final Map toRemovedStorageUnitNodeMap) { + Map reservedStorageUnitNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() + .filter(entry -> !toRemovedStorageUnitNodeMap.containsKey(entry.getKey())) + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); + Collection toBeRemovedStorageNodeNames = toRemovedStorageUnitNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); + Collection inUsedDataSourceNames = reservedStorageUnitNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); Map staleStorageNodes = resourceMetaData.getDataSourceMap().entrySet().stream() .filter(entry -> toBeRemovedStorageNodeNames.contains(entry.getKey()) && !inUsedDataSourceNames.contains(entry.getKey())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - Map staleStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() - .filter(entry -> !reservedStorageUnitNodeMappers.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getUnitNodeMapper())); - return new StorageResource(staleStorageNodes, staleStorageUnitNodeMappers); + Map staleStorageUnitNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() + .filter(entry -> !reservedStorageUnitNodeMap.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); + return new StorageResource(staleStorageNodes, staleStorageUnitNodeMap); } - private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final Map toBeChangedMappers, + private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap, final Map storageUnitDataSourcePoolProps) { Map storageNodes = new LinkedHashMap<>(resourceMetaData.getDataSourceMap().size(), 1F); - Map storageUnitNodeMappers = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().size(), 1F); - storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSourceMap(), StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps))); - storageUnitNodeMappers.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedMappers)); + Map storageUnitNodeMappers = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap().size(), 1F); + storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSourceMap(), StorageUnitNodeMapUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps))); + storageUnitNodeMappers.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap)); return new StorageResource(storageNodes, storageUnitNodeMappers); } @@ -193,30 +193,28 @@ private Map getToBeDeletedDataSources(final Map getToBeDeletedStorageUnitNodeMappers(final Map storageUnits, + private Map getToBeDeletedStorageUnitNodeMappers(final Map storageUnits, final Collection toBeChangedStorageUnitNames) { - Map result = new LinkedHashMap<>(storageUnits.size(), 1F); + Map result = new LinkedHashMap<>(storageUnits.size(), 1F); for (Entry entry : storageUnits.entrySet()) { if (!toBeChangedStorageUnitNames.contains(entry.getKey())) { - result.put(entry.getKey(), entry.getValue().getUnitNodeMapper()); + result.put(entry.getKey(), entry.getValue().getStorageNode()); } } return result; } - private Map getChangedStorageUnitNodeMappers(final Map storageUnits, - final Map toBeChangedStorageUnitNodeMappers) { - return toBeChangedStorageUnitNodeMappers.entrySet().stream().filter(entry -> isModifiedStorageUnitNodeMapper(storageUnits, entry.getKey(), entry.getValue())) + private Map getChangedStorageUnitNodeMap(final Map storageUnits, final Map toBeChangedStorageUnitNodeMap) { + return toBeChangedStorageUnitNodeMap.entrySet().stream().filter(entry -> isModifiedStorageUnitNodeMap(storageUnits, entry.getKey(), entry.getValue())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); } - private boolean isModifiedStorageUnitNodeMapper(final Map originalStorageUnits, - final String dataSourceName, final StorageUnitNodeMapper storageUnitNodeMapper) { - return originalStorageUnits.containsKey(dataSourceName) && !storageUnitNodeMapper.equals(originalStorageUnits.get(dataSourceName).getUnitNodeMapper()); + private boolean isModifiedStorageUnitNodeMap(final Map originalStorageUnits, + final String dataSourceName, final StorageNode storageNode) { + return originalStorageUnits.containsKey(dataSourceName) && !storageNode.equals(originalStorageUnits.get(dataSourceName).getStorageNode()); } - private Map getToBeAddedStorageUnitNodeMappers(final Map storageUnitNodeMappers, - final Map toBeChangedStorageUnitNodeMappers) { - return toBeChangedStorageUnitNodeMappers.entrySet().stream().filter(entry -> !storageUnitNodeMappers.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + private Map getToBeAddedStorageUnitNodeMap(final Map storageUnitNodeMap, final Map toBeChangedStorageUnitNodeMap) { + return toBeChangedStorageUnitNodeMap.entrySet().stream().filter(entry -> !storageUnitNodeMap.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); } } 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 3c63782839066..fb2908342acfa 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,12 +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.StorageNodeUtils; +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 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; @@ -256,12 +256,12 @@ private ResourceMetaData createOriginalResource() { when(result.getStorageUnitMetaData().getDataSources()).thenReturn(originalDataSources); Map storageNodeDataSourceMap = StorageNodeUtils.getStorageNodeDataSources(originalDataSources); Map storageUnits = new LinkedHashMap<>(2, 1F); - Map storageUnitNodeMappers = StorageUnitNodeMapperUtils.fromDataSources(originalDataSources); - for (Entry entry : storageUnitNodeMappers.entrySet()) { + Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(originalDataSources); + for (Entry entry : storageUnitNodeMap.entrySet()) { storageUnits.put(entry.getKey(), new StorageUnit("foo_db", storageNodeDataSourceMap, mock(DataSourcePoolProperties.class), entry.getValue())); } when(result.getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); - when(result.getStorageUnitMetaData().getUnitNodeMappers()).thenReturn(storageUnitNodeMappers); + when(result.getStorageUnitMetaData().getStorageUnitNodeMap()).thenReturn(storageUnitNodeMap); when(result.getDataSourceMap()).thenReturn(storageNodeDataSourceMap); return result; } From 9200821f982ba919b6c3492e1e75a1bc2e7c7427 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 28 Sep 2023 18:19:59 +0800 Subject: [PATCH 2/2] Remove StorageUnitNodeMapper --- .../mode/manager/switcher/ResourceSwitchManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c457a9a7c00bb..6da5665c2eabf 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 @@ -194,7 +194,7 @@ private Map getToBeDeletedDataSources(final Map getToBeDeletedStorageUnitNodeMappers(final Map storageUnits, - final Collection toBeChangedStorageUnitNames) { + final Collection toBeChangedStorageUnitNames) { Map result = new LinkedHashMap<>(storageUnits.size(), 1F); for (Entry entry : storageUnits.entrySet()) { if (!toBeChangedStorageUnitNames.contains(entry.getKey())) {