From 576e75b777350eb6d2124cd31944647c0f60d6bf Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 28 Sep 2023 02:05:38 +0800 Subject: [PATCH] Refactor StorageNode and add StorageNodeName (#28612) --- ...aSourceGeneratedDatabaseConfiguration.java | 9 +- .../database/resource/ResourceMetaData.java | 6 +- .../database/resource/StorageResource.java | 10 ++- .../database/resource/node/StorageNode.java | 24 +++--- .../resource/node/StorageNodeName.java | 47 +++++++++++ .../resource/node/StorageNodeUtils.java | 6 +- .../database/resource/unit/StorageUnit.java | 33 ++++---- .../resource/unit/StorageUnitMetaData.java | 4 +- .../resource/unit/StorageUnitNodeMapper.java | 8 -- .../unit/StorageUnitNodeMapperUtils.java | 21 ++--- ...rceGeneratedDatabaseConfigurationTest.java | 4 +- ...urceProvidedDatabaseConfigurationTest.java | 4 +- .../ShardingSphereDataSourceTest.java | 6 +- .../persist/MetaDataPersistService.java | 6 +- .../persist/NewMetaDataPersistService.java | 6 +- .../context/ConfigurationContextManager.java | 12 +-- .../switcher/NewResourceSwitchManager.java | 30 +++---- .../switcher/ResourceSwitchManager.java | 83 ++++++++++--------- .../mode/manager/ContextManagerTest.java | 6 +- .../switcher/ResourceSwitchManagerTest.java | 8 +- .../switcher/SwitchingResourceTest.java | 6 +- .../YamlProxyConfigurationSwapperTest.java | 4 +- 22 files changed, 195 insertions(+), 148 deletions(-) create mode 100644 infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.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 a441a0883ef67..0c450f59942fb 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 @@ -26,7 +26,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.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 javax.sql.DataSource; @@ -56,10 +56,11 @@ public DataSourceGeneratedDatabaseConfiguration(final Map getStorageNodeDataSourceMap(final Map mappers) { - Map result = new LinkedHashMap<>(mappers.size(), 1F); + private Map getStorageNodeDataSourceMap(final Map mappers) { + Map result = new LinkedHashMap<>(mappers.size(), 1F); for (Entry entry : mappers.entrySet()) { - result.computeIfAbsent(entry.getValue().getStorageNode(), key -> DataSourcePoolCreator.create(entry.getKey(), dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values())); + result.computeIfAbsent(entry.getValue().getStorageNode().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/metadata/database/resource/ResourceMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java index e96ce55845dab..7564aa9c3956d 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,7 +22,7 @@ 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; @@ -43,7 +43,7 @@ @Getter public final class ResourceMetaData { - private final Map dataSourceMap; + private final Map dataSourceMap; private final StorageUnitMetaData storageUnitMetaData; @@ -54,7 +54,7 @@ public ResourceMetaData(final Map dataSources) { StorageUnitNodeMapperUtils.fromDataSources(dataSources)); } - public ResourceMetaData(final String databaseName, final Map dataSourceMap, + public ResourceMetaData(final String databaseName, final Map dataSourceMap, final Map storageUnitNodeMappers, final Map propsMap) { this.dataSourceMap = dataSourceMap; storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageUnitNodeMappers); 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 b46f82d9799d2..d5f960f63bfeb 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 @@ -20,6 +20,7 @@ import lombok.Getter; 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; @@ -33,13 +34,13 @@ @Getter public final class StorageResource { - private final Map dataSourceMap; + private final Map dataSourceMap; private final Map storageUnitNodeMappers; private final Map wrappedDataSources; - public StorageResource(final Map dataSourceMap, final Map storageUnitNodeMappers) { + public StorageResource(final Map dataSourceMap, final Map storageUnitNodeMappers) { this.dataSourceMap = dataSourceMap; this.storageUnitNodeMappers = storageUnitNodeMappers; wrappedDataSources = createWrappedDataSources(); @@ -48,9 +49,10 @@ public StorageResource(final Map dataSourceMap, final M private Map createWrappedDataSources() { Map result = new LinkedHashMap<>(storageUnitNodeMappers.size(), 1F); for (Entry entry : storageUnitNodeMappers.entrySet()) { - DataSource dataSource = dataSourceMap.get(entry.getValue().getStorageNode()); + StorageNode storageNode = entry.getValue().getStorageNode(); + DataSource dataSource = dataSourceMap.get(storageNode.getName()); if (null != dataSource) { - result.put(entry.getKey(), new CatalogSwitchableDataSource(dataSource, entry.getValue().getCatalog(), entry.getValue().getUrl())); + result.put(entry.getKey(), new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl())); } } return result; diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java index f967ddc99e026..98e0b0337299e 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java @@ -17,31 +17,27 @@ package org.apache.shardingsphere.infra.metadata.database.resource.node; -import com.google.common.base.Objects; import lombok.Getter; -import lombok.RequiredArgsConstructor; /** * Storage node. */ -@RequiredArgsConstructor @Getter public final class StorageNode { - private final String name; + private final StorageNodeName name; - @Override - public boolean equals(final Object obj) { - return obj instanceof StorageNode && ((StorageNode) obj).name.equalsIgnoreCase(name); - } + private final String url; + + private final String catalog; - @Override - public int hashCode() { - return Objects.hashCode(name.toUpperCase()); + public StorageNode(final String name, final String url) { + this(name, url, null); } - @Override - public String toString() { - return name; + public StorageNode(final String name, final String url, final String catalog) { + this.name = new StorageNodeName(name); + this.url = url; + this.catalog = catalog; } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java new file mode 100644 index 0000000000000..ec6941710ba83 --- /dev/null +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java @@ -0,0 +1,47 @@ +/* + * 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.node; + +import com.google.common.base.Objects; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * Storage node name. + */ +@RequiredArgsConstructor +@Getter +public final class StorageNodeName { + + private final String name; + + @Override + public boolean equals(final Object obj) { + return obj instanceof StorageNodeName && ((StorageNodeName) obj).name.equalsIgnoreCase(name); + } + + @Override + public int hashCode() { + return Objects.hashCode(name.toUpperCase()); + } + + @Override + public String toString() { + return name; + } +} diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeUtils.java index 3ed88fdbdfe71..3b6fca81e8dcf 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeUtils.java @@ -38,8 +38,8 @@ public final class StorageNodeUtils { * @param dataSources data sources * @return storage node data sources */ - public static Map getStorageNodeDataSources(final Map dataSources) { - return dataSources.entrySet().stream() - .collect(Collectors.toMap(entry -> new StorageNode(entry.getKey()), Entry::getValue, (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(dataSources.size(), 1F))); + public static Map getStorageNodeDataSources(final Map dataSources) { + return dataSources.entrySet().stream().collect( + Collectors.toMap(entry -> new StorageNodeName(entry.getKey()), Entry::getValue, (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(dataSources.size(), 1F))); } } 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 4d5496e3ac332..75864e6b474c1 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 @@ -27,6 +27,7 @@ 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.state.datasource.DataSourceStateManager; import javax.sql.DataSource; @@ -53,24 +54,25 @@ public final class StorageUnit { private final ConnectionProperties connectionProperties; - public StorageUnit(final String databaseName, final Map storageNodeDataSources, + public StorageUnit(final String databaseName, final Map storageNodeDataSources, final DataSourcePoolProperties props, final StorageUnitNodeMapper unitNodeMapper) { this.dataSourcePoolProperties = props; this.unitNodeMapper = unitNodeMapper; dataSource = getStorageUnitDataSource(storageNodeDataSources, unitNodeMapper); - Map enabledStorageNodeDataSources = getEnabledStorageNodeDataSources(databaseName, storageNodeDataSources); + Map enabledStorageNodeDataSources = getEnabledStorageNodeDataSources(databaseName, storageNodeDataSources); storageType = createStorageType(enabledStorageNodeDataSources, unitNodeMapper); connectionProperties = createConnectionProperties(enabledStorageNodeDataSources, unitNodeMapper, storageType).orElse(null); } - private DataSource getStorageUnitDataSource(final Map storageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper) { - DataSource dataSource = storageNodeDataSources.get(unitNodeMapper.getStorageNode()); - return new CatalogSwitchableDataSource(dataSource, unitNodeMapper.getCatalog(), unitNodeMapper.getUrl()); + private DataSource getStorageUnitDataSource(final Map storageNodeDataSources, final StorageUnitNodeMapper mapper) { + StorageNode storageNode = mapper.getStorageNode(); + DataSource dataSource = storageNodeDataSources.get(storageNode.getName()); + return new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl()); } - private Map getEnabledStorageNodeDataSources(final String databaseName, final Map storageNodeDataSources) { + private Map getEnabledStorageNodeDataSources(final String databaseName, final Map storageNodeDataSources) { Map toBeCheckedDataSources = new LinkedHashMap<>(storageNodeDataSources.size(), 1F); - for (Entry entry : storageNodeDataSources.entrySet()) { + for (Entry entry : storageNodeDataSources.entrySet()) { toBeCheckedDataSources.put(entry.getKey().getName(), entry.getValue()); } Map enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, toBeCheckedDataSources); @@ -78,20 +80,21 @@ private Map getEnabledStorageNodeDataSources(final Stri .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()) - ? Collections.singleton(enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNode())) + private DatabaseType createStorageType(final Map enabledStorageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper) { + return DatabaseTypeEngine.getStorageType(enabledStorageNodeDataSources.containsKey(unitNodeMapper.getStorageNode().getName()) + ? Collections.singleton(enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNode().getName())) : Collections.emptyList()); } - private Optional createConnectionProperties(final Map enabledStorageNodeDataSources, - final StorageUnitNodeMapper unitNodeMapper, final DatabaseType storageType) { - if (!enabledStorageNodeDataSources.containsKey(unitNodeMapper.getStorageNode())) { + private Optional createConnectionProperties(final Map enabledStorageNodeDataSources, + final StorageUnitNodeMapper mapper, final DatabaseType storageType) { + StorageNode storageNode = mapper.getStorageNode(); + if (!enabledStorageNodeDataSources.containsKey(storageNode.getName())) { return Optional.empty(); } Map standardProps = DataSourcePoolPropertiesCreator.create( - enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNode())).getConnectionPropertySynonyms().getStandardProperties(); + enabledStorageNodeDataSources.get(storageNode.getName())).getConnectionPropertySynonyms().getStandardProperties(); ConnectionPropertiesParser parser = DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, storageType); - return Optional.of(parser.parse(standardProps.get("url").toString(), standardProps.get("username").toString(), unitNodeMapper.getCatalog())); + return Optional.of(parser.parse(standardProps.get("url").toString(), standardProps.get("username").toString(), storageNode.getCatalog())); } } 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 d22678057bbac..81ef617ad7ad0 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,7 +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; import java.util.LinkedHashMap; @@ -41,7 +41,7 @@ public final class StorageUnitMetaData { // TODO zhangliang: should refactor private final Map dataSources; - public StorageUnitMetaData(final String databaseName, final Map storageNodeDataSources, + public StorageUnitMetaData(final String databaseName, final Map storageNodeDataSources, final Map dataSourcePoolPropertiesMap, final Map unitNodeMappers) { this.unitNodeMappers = unitNodeMappers; storageUnits = new LinkedHashMap<>(unitNodeMappers.size(), 1F); 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 index 97f6b4d6d3be9..9ac4e1891c481 100644 --- 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 @@ -31,12 +31,4 @@ public final class StorageUnitNodeMapper { private final String name; private final StorageNode storageNode; - - private final String url; - - private final String catalog; - - public StorageUnitNodeMapper(final String name, final StorageNode storageNode, final String url) { - this(name, storageNode, url, null); - } } 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 14a7790a8885b..e5c6144e3f92e 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 @@ -27,6 +27,7 @@ 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 javax.sql.DataSource; import java.util.LinkedHashMap; @@ -54,7 +55,7 @@ public static Map fromDataSources(final Map standardProps = props.getConnectionPropertySynonyms().getStandardProperties(); String url = standardProps.get("url").toString(); boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); - StorageNode storageNode = new StorageNode(getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); - return createStorageUnitNodeMapper(storageNode, storageUnitName, url, isInstanceConnectionAvailable); + String storageNodeName = getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable); + return createStorageUnitNodeMapper(storageNodeName, storageUnitName, url, isInstanceConnectionAvailable); } private static String getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) { @@ -93,10 +94,10 @@ private static String generateStorageNodeName(final String hostname, final int p return String.format("%s_%s_%s", hostname, port, username); } - private static StorageUnitNodeMapper createStorageUnitNodeMapper(final StorageNode storageNode, final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) { + private static StorageUnitNodeMapper createStorageUnitNodeMapper(final String storageNodeName, final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) { return isInstanceConnectionAvailable - ? new StorageUnitNodeMapper(storageUnitName, storageNode, url, new StandardJdbcUrlParser().parse(url).getDatabase()) - : new StorageUnitNodeMapper(storageUnitName, storageNode, url); + ? new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageNodeName, url, new StandardJdbcUrlParser().parse(url).getDatabase())) + : new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageNodeName, url)); } /** @@ -105,14 +106,14 @@ private static StorageUnitNodeMapper createStorageUnitNodeMapper(final StorageNo * @param storageUnitDataSourcePoolProps storage unit grouped data source pool properties map * @return storage node grouped data source pool properties map */ - public static Map getStorageNodeDataSourcePoolProperties(final Map storageUnitDataSourcePoolProps) { - Map result = new LinkedHashMap<>(); + public static Map getStorageNodeDataSourcePoolProperties(final Map storageUnitDataSourcePoolProps) { + Map result = new LinkedHashMap<>(); for (Entry entry : storageUnitDataSourcePoolProps.entrySet()) { Map standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties(); String url = standardProps.get("url").toString(); boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); - StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); - result.putIfAbsent(storageNode, entry.getValue()); + StorageNodeName storageNodeName = new StorageNodeName(getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); + result.putIfAbsent(storageNodeName, entry.getValue()); } return result; } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java index 9d53fc6119a10..2ae87a4f66d89 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfigurationTest.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration; import org.apache.shardingsphere.infra.datasource.pool.config.PoolConfiguration; 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.fixture.FixtureRuleConfiguration; import org.junit.jupiter.api.Test; @@ -50,7 +50,7 @@ void assertGetDataSources() { @Test void assertGetStorageNodes() { DataSourceGeneratedDatabaseConfiguration databaseConfig = createDataSourceGeneratedDatabaseConfiguration(); - HikariDataSource hikariDataSource = (HikariDataSource) databaseConfig.getStorageResource().getDataSourceMap().get(new StorageNode("normal_db")); + HikariDataSource hikariDataSource = (HikariDataSource) databaseConfig.getStorageResource().getDataSourceMap().get(new StorageNodeName("normal_db")); assertThat(hikariDataSource.getJdbcUrl(), is("jdbc:mock://127.0.0.1/normal_db")); assertThat(hikariDataSource.getUsername(), is("root")); assertThat(hikariDataSource.getPassword(), is("")); diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java index 91f909cc285cc..d0ca3628adc38 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfigurationTest.java @@ -19,7 +19,7 @@ import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; 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.fixture.FixtureRuleConfiguration; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.junit.jupiter.api.Test; @@ -45,7 +45,7 @@ void assertGetDataSources() { @Test void assertGetStorageNodes() { DataSourceProvidedDatabaseConfiguration databaseConfig = createDataSourceProvidedDatabaseConfiguration(); - MockedDataSource dataSource = (MockedDataSource) databaseConfig.getStorageResource().getDataSourceMap().get(new StorageNode("foo_ds")); + MockedDataSource dataSource = (MockedDataSource) databaseConfig.getStorageResource().getDataSourceMap().get(new StorageNodeName("foo_ds")); assertThat(dataSource.getUrl(), is("jdbc:mock://127.0.0.1/foo_ds")); assertThat(dataSource.getUsername(), is("root")); assertThat(dataSource.getPassword(), is("root")); diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java index cab631991881f..7f46cd4ffb601 100644 --- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java +++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; -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.executor.sql.execute.engine.ConnectionMode; import org.apache.shardingsphere.infra.state.cluster.ClusterState; import org.apache.shardingsphere.infra.state.instance.InstanceState; @@ -132,9 +132,9 @@ void assertClose() throws Exception { try (HikariDataSource dataSource = createHikariDataSource()) { ShardingSphereDataSource actual = createShardingSphereDataSource(dataSource); actual.close(); - Map dataSourceMap = getContextManager(actual).getMetaDataContexts().getMetaData() + Map dataSourceMap = getContextManager(actual).getMetaDataContexts().getMetaData() .getDatabase(DefaultDatabase.LOGIC_NAME).getResourceMetaData().getDataSourceMap(); - assertTrue(((HikariDataSource) dataSourceMap.get(new StorageNode("ds"))).isClosed()); + assertTrue(((HikariDataSource) dataSourceMap.get(new StorageNodeName("ds"))).isClosed()); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java index 298ab84767d78..e17c37587673a 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java @@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration; 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.node.StorageNodeName; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; @@ -120,9 +120,9 @@ private Map getDataSourcePoolPropertiesMap(fin return databaseConfigs.getDataSourcePoolPropertiesMap(); } - private Map getDataSourcePoolPropertiesMap(final Map storageNodeDataSources) { + private Map getDataSourcePoolPropertiesMap(final Map storageNodeDataSources) { Map result = new LinkedHashMap<>(storageNodeDataSources.size(), 1F); - for (Entry entry : storageNodeDataSources.entrySet()) { + for (Entry entry : storageNodeDataSources.entrySet()) { result.put(entry.getKey().getName(), DataSourcePoolPropertiesCreator.create(entry.getValue())); } return result; diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java index 9850afa876cbf..c557a740b0322 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/NewMetaDataPersistService.java @@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer; 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.node.StorageNodeName; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; @@ -126,9 +126,9 @@ private Map getDataSourcePoolPropertiesMap(fin return databaseConfigs.getDataSourcePoolPropertiesMap(); } - private Map getDataSourcePoolPropertiesMap(final Map storageNodeDataSources) { + private Map getDataSourcePoolPropertiesMap(final Map storageNodeDataSources) { Map result = new LinkedHashMap<>(storageNodeDataSources.size(), 1F); - for (Entry entry : storageNodeDataSources.entrySet()) { + for (Entry entry : storageNodeDataSources.entrySet()) { result.put(entry.getKey().getName(), DataSourcePoolPropertiesCreator.create(entry.getValue())); } return result; 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 02e22e03c09a3..f79132f3c51f9 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,7 +25,7 @@ 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.StorageNode; +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; @@ -274,7 +274,7 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD * @return ShardingSphere databases */ public Map renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) { - Map newStorageNodes = getNewStorageNodes(database.getResourceMetaData().getDataSourceMap(), resource); + Map newStorageNodes = getNewStorageNodes(database.getResourceMetaData().getDataSourceMap(), resource); Map newStorageUnitNodeMappers = getNewStorageUnitNodeMappers(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)); @@ -282,9 +282,9 @@ public Map renewDatabase(final ShardingSphereDat new ResourceMetaData(database.getName(), newStorageNodes, newStorageUnitNodeMappers, propsMap), database.getRuleMetaData(), database.getSchemas())); } - private Map getNewStorageNodes(final Map currentStorageNodes, final SwitchingResource resource) { - Map result = new LinkedHashMap<>(); - for (Entry entry : currentStorageNodes.entrySet()) { + private Map getNewStorageNodes(final Map currentStorageNodes, final SwitchingResource resource) { + Map result = new LinkedHashMap<>(); + for (Entry entry : currentStorageNodes.entrySet()) { if (!resource.getStaleStorageResource().getDataSourceMap().containsKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } @@ -364,7 +364,7 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re } private StorageResource getMergedStorageResource(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { - Map storageNodeDataSources = currentResourceMetaData.getDataSourceMap(); + Map storageNodeDataSources = currentResourceMetaData.getDataSourceMap(); Map storageUnitNodeMappers = currentResourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(); if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSourceMap().isEmpty()) { storageNodeDataSources.putAll(switchingResource.getNewStorageResource().getDataSourceMap()); 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 e5a43bdbe3b69..5448050a69ae6 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 @@ -22,7 +22,7 @@ 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 javax.sql.DataSource; @@ -57,10 +57,10 @@ public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMeta private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, final Map mappers, final Map storageUnitDataSourcePoolProps) { - Collection storageNodes = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); - Map newStorageNodes = new LinkedHashMap<>(storageNodes.size(), 1F); - for (StorageNode each : storageNodes) { + Collection storageNodeNames = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.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))); } @@ -84,20 +84,20 @@ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaDat } private StorageResource getAlterNewStorageResource(final Map mappers, final Map storageUnitDataSourcePoolProps) { - Collection toBeAlteredStorageNodes = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); - Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodes.size(), 1F); - for (StorageNode each : toBeAlteredStorageNodes) { + Collection toBeAlteredStorageNodeNames = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.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); } private StorageResource getStaleStorageResource(final ResourceMetaData resourceMetaData, final Map mappers) { - Collection toBeAlteredStorageNodes = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodes.size(), 1F); - for (Entry entry : resourceMetaData.getDataSourceMap().entrySet()) { - if (toBeAlteredStorageNodes.contains(entry.getKey())) { + Collection toBeAlteredStorageNodeNames = mappers.values().stream().map(each -> each.getStorageNode().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()); } } @@ -122,9 +122,9 @@ 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(); - Map storageNodes = new LinkedHashMap<>(1, 1F); + Map storageNodes = new LinkedHashMap<>(1, 1F); if (reservedStorageUnitNodeMappers.values().stream().noneMatch(each -> each.getStorageNode().equals(storageUnitNodeMapper.getStorageNode()))) { - storageNodes.put(storageUnitNodeMapper.getStorageNode(), resourceMetaData.getDataSourceMap().get(storageUnitNodeMapper.getStorageNode())); + storageNodes.put(storageUnitNodeMapper.getStorageNode().getName(), resourceMetaData.getDataSourceMap().get(storageUnitNodeMapper.getStorageNode().getName())); } return new StorageResource(storageNodes, Collections.singletonMap(storageUnitName, storageUnitNodeMapper)); } 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 48033de1e4925..e4d5429209c9a 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 @@ -23,7 +23,7 @@ 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; @@ -84,8 +84,8 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet mergedDataSourcePoolPropertiesMap.putAll(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)); + Collection toBeChangedStorageNodeNames = toBeChangedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); + staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeNames)); staleStorageResource.getStorageUnitNodeMappers().putAll( getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedMappers.keySet())); return new SwitchingResource(resourceMetaData, @@ -94,20 +94,20 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, final Map toBeChangedMappers, final Map storageUnitDataSourcePoolProps) { - Collection toBeChangedStorageNode = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); - Map storageNodes = - getNewStorageNodes(resourceMetaData, toBeChangedStorageNode, storageNodeDataSourcePoolProps); + Collection toBeChangedStorageNodeName = toBeChangedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + Map storageNodes = + getNewStorageNodes(resourceMetaData, toBeChangedStorageNodeName, storageNodeDataSourcePoolProps); Map storageUnitNodeMappers = getNewStorageUnitNodeMappers(resourceMetaData, toBeChangedMappers); return new StorageResource(storageNodes, storageUnitNodeMappers); } - private Map getNewStorageNodes(final ResourceMetaData resourceMetaData, final Collection toBeChangedStorageNode, - final Map propsMap) { - Map result = new LinkedHashMap<>(resourceMetaData.getDataSourceMap()); - result.keySet().removeAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNode).keySet()); - result.putAll(getChangedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNode, propsMap)); - result.putAll(getToBeAddedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNode, propsMap)); + private Map getNewStorageNodes(final ResourceMetaData resourceMetaData, final Collection toBeChangedStorageNodeName, + final Map propsMap) { + Map result = new LinkedHashMap<>(resourceMetaData.getDataSourceMap()); + result.keySet().removeAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeName).keySet()); + result.putAll(getChangedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeName, propsMap)); + result.putAll(getToBeAddedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeName, propsMap)); return result; } @@ -119,26 +119,29 @@ private Map getNewStorageUnitNodeMappers(final Re return result; } - private Map getChangedDataSources(final Map storageNodes, - final Collection toBeChangedStorageNode, final Map propsMap) { - Collection toBeChangedDataSourceNames = toBeChangedStorageNode.stream() + private Map getChangedDataSources(final Map storageNodes, + final Collection toBeChangedStorageNodeName, + final Map propsMap) { + Collection toBeChangedDataSourceNames = toBeChangedStorageNodeName.stream() .filter(each -> isModifiedDataSource(storageNodes, each, propsMap.get(each))).collect(Collectors.toList()); - Map result = new LinkedHashMap<>(toBeChangedStorageNode.size(), 1F); - for (StorageNode each : toBeChangedDataSourceNames) { + Map result = new LinkedHashMap<>(toBeChangedStorageNodeName.size(), 1F); + for (StorageNodeName each : toBeChangedDataSourceNames) { result.put(each, DataSourcePoolCreator.create(propsMap.get(each))); } return result; } - private boolean isModifiedDataSource(final Map originalDataSources, final StorageNode storageNode, final DataSourcePoolProperties propsMap) { - return originalDataSources.containsKey(storageNode) && !propsMap.equals(DataSourcePoolPropertiesCreator.create(originalDataSources.get(storageNode))); + private boolean isModifiedDataSource(final Map originalDataSources, + final StorageNodeName storageNodeName, final DataSourcePoolProperties propsMap) { + return originalDataSources.containsKey(storageNodeName) && !propsMap.equals(DataSourcePoolPropertiesCreator.create(originalDataSources.get(storageNodeName))); } - private Map getToBeAddedDataSources(final Map storageNodes, final Collection toBeChangedStorageNode, - final Map propsMap) { - Collection toBeAddedDataSourceNames = toBeChangedStorageNode.stream().filter(each -> !storageNodes.containsKey(each)).collect(Collectors.toList()); - Map result = new LinkedHashMap<>(); - for (StorageNode each : toBeAddedDataSourceNames) { + private Map getToBeAddedDataSources(final Map storageNodes, + final Collection toBeChangedStorageNodeName, + final Map propsMap) { + Collection toBeAddedDataSourceNames = toBeChangedStorageNodeName.stream().filter(each -> !storageNodes.containsKey(each)).collect(Collectors.toList()); + Map result = new LinkedHashMap<>(); + for (StorageNodeName each : toBeAddedDataSourceNames) { result.put(each, DataSourcePoolCreator.create(propsMap.get(each))); } return result; @@ -148,10 +151,10 @@ private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData re Map reservedStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() .filter(entry -> !toRemovedMappers.containsKey(entry.getKey())) .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getUnitNodeMapper())); - Collection toBeRemovedStorageNodes = toRemovedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Collection inUsedDataSourceNames = reservedStorageUnitNodeMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Map staleStorageNodes = resourceMetaData.getDataSourceMap().entrySet().stream() - .filter(entry -> toBeRemovedStorageNodes.contains(entry.getKey()) && !inUsedDataSourceNames.contains(entry.getKey())) + 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()); + 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())); @@ -160,27 +163,29 @@ private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData re private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final Map toBeChangedMappers, final Map storageUnitDataSourcePoolProps) { - Map storageNodes = new LinkedHashMap<>(resourceMetaData.getDataSourceMap().size(), 1F); + 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)); return new StorageResource(storageNodes, storageUnitNodeMappers); } - private Map getToBeChangedDataSources(final Map storageNodes, final Map propsMap) { - Map result = new LinkedHashMap<>(storageNodes.size(), 1F); - for (Entry entry : propsMap.entrySet()) { - StorageNode storageNode = entry.getKey(); - if (isModifiedDataSource(storageNodes, storageNode, entry.getValue())) { - result.put(storageNode, storageNodes.get(storageNode)); + private Map getToBeChangedDataSources(final Map storageNodes, + final Map propsMap) { + Map result = new LinkedHashMap<>(storageNodes.size(), 1F); + for (Entry entry : propsMap.entrySet()) { + StorageNodeName storageNodeName = entry.getKey(); + if (isModifiedDataSource(storageNodes, storageNodeName, entry.getValue())) { + result.put(storageNodeName, storageNodes.get(storageNodeName)); } } return result; } - private Map getToBeDeletedDataSources(final Map storageNodes, final Collection toBeChangedDataSourceNames) { - Map result = new LinkedHashMap<>(storageNodes.size(), 1F); - for (Entry entry : storageNodes.entrySet()) { + private Map getToBeDeletedDataSources(final Map storageNodes, + final Collection toBeChangedDataSourceNames) { + Map result = new LinkedHashMap<>(storageNodes.size(), 1F); + for (Entry entry : storageNodes.entrySet()) { if (!toBeChangedDataSourceNames.contains(entry.getKey())) { result.put(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 0247ffeeea7a7..3c63782839066 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 @@ -29,7 +29,7 @@ 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.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; @@ -245,7 +245,7 @@ void assertAlterDataSourceConfiguration() { Collections.singletonMap("foo_ds", new DataSourcePoolProperties(MockedDataSource.class.getName(), createProperties("test", "test")))); assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getStorageUnits().size(), is(3)); assertAlteredDataSource((MockedDataSource) contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db") - .getResourceMetaData().getDataSourceMap().get(new StorageNode("foo_ds"))); + .getResourceMetaData().getDataSourceMap().get(new StorageNodeName("foo_ds"))); } private ResourceMetaData createOriginalResource() { @@ -254,7 +254,7 @@ private ResourceMetaData createOriginalResource() { originalDataSources.put("ds_1", new MockedDataSource()); originalDataSources.put("ds_2", new MockedDataSource()); when(result.getStorageUnitMetaData().getDataSources()).thenReturn(originalDataSources); - Map storageNodeDataSourceMap = StorageNodeUtils.getStorageNodeDataSources(originalDataSources); + Map storageNodeDataSourceMap = StorageNodeUtils.getStorageNodeDataSources(originalDataSources); Map storageUnits = new LinkedHashMap<>(2, 1F); Map storageUnitNodeMappers = StorageUnitNodeMapperUtils.fromDataSources(originalDataSources); for (Entry entry : storageUnitNodeMappers.entrySet()) { diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java index 5175a66badd0f..2dbceba9c2f61 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.mode.manager.switcher; 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.ResourceMetaData; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.awaitility.Awaitility; @@ -86,9 +86,9 @@ private Map getDataSourcePoolProperties(final int initialCapacit private void assertNewDataSources(final SwitchingResource actual) { assertThat(actual.getNewStorageResource().getDataSourceMap().size(), is(3)); - assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNode("not_change"))); - assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNode("new"))); - assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNode("replace"))); + assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeName("not_change"))); + assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeName("new"))); + assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeName("replace"))); } private void assertStaleDataSources(final Map originalDataSourceMap) { diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java index 59453e4936992..8cebd574e4dca 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java @@ -19,7 +19,7 @@ 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.test.fixture.jdbc.MockedDataSource; import org.awaitility.Awaitility; import org.junit.jupiter.api.Test; @@ -36,8 +36,8 @@ class SwitchingResourceTest { void assertCloseStaleDataSources() { MockedDataSource staleDataSource = new MockedDataSource(); ResourceMetaData resourceMetaData = mock(ResourceMetaData.class); - StorageResource newStorageResource = new StorageResource(Collections.singletonMap(new StorageNode("new_ds"), new MockedDataSource()), Collections.emptyMap()); - StorageResource staleStorageResource = new StorageResource(Collections.singletonMap(new StorageNode("stale_ds"), staleDataSource), Collections.emptyMap()); + StorageResource newStorageResource = new StorageResource(Collections.singletonMap(new StorageNodeName("new_ds"), new MockedDataSource()), Collections.emptyMap()); + StorageResource staleStorageResource = new StorageResource(Collections.singletonMap(new StorageNodeName("stale_ds"), staleDataSource), Collections.emptyMap()); new SwitchingResource(resourceMetaData, newStorageResource, staleStorageResource, Collections.emptyMap()).closeStaleDataSources(); Awaitility.await().pollDelay(10L, TimeUnit.MILLISECONDS).until(staleDataSource::isClosed); assertTrue(staleDataSource.isClosed()); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java index 5e9cab83f44e3..d6d690ac1cd6a 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import org.apache.shardingsphere.proxy.backend.config.ProxyConfiguration; import org.apache.shardingsphere.proxy.backend.config.ProxyConfigurationLoader; import org.apache.shardingsphere.proxy.backend.config.YamlProxyConfiguration; @@ -56,7 +56,7 @@ void assertSwap() throws IOException { private void assertDataSources(final ProxyConfiguration proxyConfig) { Map actual = proxyConfig.getDatabaseConfigurations(); assertThat(actual.size(), is(1)); - HikariDataSource dataSource = (HikariDataSource) actual.get("swapper_test").getStorageResource().getDataSourceMap().get(new StorageNode("foo_db")); + HikariDataSource dataSource = (HikariDataSource) actual.get("swapper_test").getStorageResource().getDataSourceMap().get(new StorageNodeName("foo_db")); assertThat(dataSource.getJdbcUrl(), is("jdbc:h2:mem:foo_db;DB_CLOSE_DELAY=-1")); assertThat(dataSource.getUsername(), is("sa")); assertThat(dataSource.getPassword(), is(""));