From 125e0038e7312f7a717f7cb8f2f0ede0d8ca9b06 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 28 Sep 2023 01:14:53 +0800 Subject: [PATCH 1/5] Rename StorageNodeIdentifier --- ...aSourceGeneratedDatabaseConfiguration.java | 9 +- .../database/resource/ResourceMetaData.java | 6 +- .../database/resource/StorageResource.java | 8 +- ...geNode.java => StorageNodeIdentifier.java} | 6 +- .../resource/node/StorageNodeUtils.java | 6 +- .../database/resource/unit/StorageUnit.java | 26 +++--- .../resource/unit/StorageUnitMetaData.java | 4 +- .../resource/unit/StorageUnitNodeMapper.java | 8 +- .../unit/StorageUnitNodeMapperUtils.java | 23 ++--- ...rceGeneratedDatabaseConfigurationTest.java | 4 +- ...urceProvidedDatabaseConfigurationTest.java | 4 +- .../ShardingSphereDataSourceTest.java | 6 +- .../persist/MetaDataPersistService.java | 6 +- .../persist/NewMetaDataPersistService.java | 6 +- .../context/ConfigurationContextManager.java | 12 +-- .../switcher/NewResourceSwitchManager.java | 32 +++---- .../switcher/ResourceSwitchManager.java | 83 ++++++++++--------- .../mode/manager/ContextManagerTest.java | 6 +- .../switcher/ResourceSwitchManagerTest.java | 8 +- .../switcher/SwitchingResourceTest.java | 6 +- .../YamlProxyConfigurationSwapperTest.java | 4 +- 21 files changed, 140 insertions(+), 133 deletions(-) rename infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/{StorageNode.java => StorageNodeIdentifier.java} (87%) 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..aab3722a1bac9 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.StorageNodeIdentifier; 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().getStorageNodeIdentifier(), + 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..a9c5a3e8b89e7 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.StorageNodeIdentifier; 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..2de2ca343e58e 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 @@ -19,7 +19,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.StorageNodeIdentifier; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; import javax.sql.DataSource; @@ -33,13 +33,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,7 +48,7 @@ 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()); + DataSource dataSource = dataSourceMap.get(entry.getValue().getStorageNodeIdentifier()); if (null != dataSource) { result.put(entry.getKey(), new CatalogSwitchableDataSource(dataSource, entry.getValue().getCatalog(), entry.getValue().getUrl())); } 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/StorageNodeIdentifier.java similarity index 87% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeIdentifier.java index f967ddc99e026..2d7a547bbeac9 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/StorageNodeIdentifier.java @@ -22,17 +22,17 @@ import lombok.RequiredArgsConstructor; /** - * Storage node. + * Storage node identifier. */ @RequiredArgsConstructor @Getter -public final class StorageNode { +public final class StorageNodeIdentifier { private final String name; @Override public boolean equals(final Object obj) { - return obj instanceof StorageNode && ((StorageNode) obj).name.equalsIgnoreCase(name); + return obj instanceof StorageNodeIdentifier && ((StorageNodeIdentifier) obj).name.equalsIgnoreCase(name); } @Override 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..4c36e6dfccded 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 StorageNodeIdentifier(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..4adb1201c6e9b 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 @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; 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.StorageNodeIdentifier; import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import javax.sql.DataSource; @@ -53,24 +53,24 @@ 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()); + private DataSource getStorageUnitDataSource(final Map storageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper) { + DataSource dataSource = storageNodeDataSources.get(unitNodeMapper.getStorageNodeIdentifier()); return new CatalogSwitchableDataSource(dataSource, unitNodeMapper.getCatalog(), unitNodeMapper.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,19 +78,19 @@ 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.getStorageNodeIdentifier()) + ? Collections.singleton(enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNodeIdentifier())) : Collections.emptyList()); } - private Optional createConnectionProperties(final Map enabledStorageNodeDataSources, + private Optional createConnectionProperties(final Map enabledStorageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper, final DatabaseType storageType) { - if (!enabledStorageNodeDataSources.containsKey(unitNodeMapper.getStorageNode())) { + if (!enabledStorageNodeDataSources.containsKey(unitNodeMapper.getStorageNodeIdentifier())) { return Optional.empty(); } Map standardProps = DataSourcePoolPropertiesCreator.create( - enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNode())).getConnectionPropertySynonyms().getStandardProperties(); + enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNodeIdentifier())).getConnectionPropertySynonyms().getStandardProperties(); ConnectionPropertiesParser parser = DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, storageType); return Optional.of(parser.parse(standardProps.get("url").toString(), standardProps.get("username").toString(), unitNodeMapper.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..0878b2524ab69 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.StorageNodeIdentifier; 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..3b5ef6cf34641 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 @@ -19,7 +19,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeIdentifier; /** * Storage unit and node mapper. @@ -30,13 +30,13 @@ public final class StorageUnitNodeMapper { private final String name; - private final StorageNode storageNode; + private final StorageNodeIdentifier storageNodeIdentifier; private final String url; private final String catalog; - public StorageUnitNodeMapper(final String name, final StorageNode storageNode, final String url) { - this(name, storageNode, url, null); + public StorageUnitNodeMapper(final String name, final StorageNodeIdentifier storageNodeIdentifier, final String url) { + this(name, storageNodeIdentifier, 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..3266911b65d22 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 @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeIdentifier; import javax.sql.DataSource; import java.util.LinkedHashMap; @@ -54,7 +54,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); + StorageNodeIdentifier storageNodeIdentifier = new StorageNodeIdentifier(getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); + return createStorageUnitNodeMapper(storageNodeIdentifier, storageUnitName, url, isInstanceConnectionAvailable); } private static String getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) { @@ -93,10 +93,11 @@ 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 StorageNodeIdentifier storageNodeIdentifier, + 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, storageNodeIdentifier, url, new StandardJdbcUrlParser().parse(url).getDatabase()) + : new StorageUnitNodeMapper(storageUnitName, storageNodeIdentifier, 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()); + StorageNodeIdentifier storageNodeIdentifier = new StorageNodeIdentifier(getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); + result.putIfAbsent(storageNodeIdentifier, 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..ff0a33911d61c 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.StorageNodeIdentifier; 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 StorageNodeIdentifier("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..034ae50e210fe 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.StorageNodeIdentifier; 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 StorageNodeIdentifier("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..711e3a6a75ff8 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.StorageNodeIdentifier; 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 StorageNodeIdentifier("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..32ed997f03629 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.StorageNodeIdentifier; 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..7b93950749717 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.StorageNodeIdentifier; 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..b11c5e2caa226 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.StorageNodeIdentifier; 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..286f6c0b32a7b 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.StorageNodeIdentifier; 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 storageNodeIdentifiers = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + Map newStorageNodes = new LinkedHashMap<>(storageNodeIdentifiers.size(), 1F); + for (StorageNodeIdentifier each : storageNodeIdentifiers) { 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 toBeAlteredStorageNodeIdentifiers = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodeIdentifiers.size(), 1F); + for (StorageNodeIdentifier each : toBeAlteredStorageNodeIdentifiers) { 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 toBeAlteredStorageNodeIdentifiers = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodeIdentifiers.size(), 1F); + for (Entry entry : resourceMetaData.getDataSourceMap().entrySet()) { + if (toBeAlteredStorageNodeIdentifiers.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); - if (reservedStorageUnitNodeMappers.values().stream().noneMatch(each -> each.getStorageNode().equals(storageUnitNodeMapper.getStorageNode()))) { - storageNodes.put(storageUnitNodeMapper.getStorageNode(), resourceMetaData.getDataSourceMap().get(storageUnitNodeMapper.getStorageNode())); + Map storageNodes = new LinkedHashMap<>(1, 1F); + if (reservedStorageUnitNodeMappers.values().stream().noneMatch(each -> each.getStorageNodeIdentifier().equals(storageUnitNodeMapper.getStorageNodeIdentifier()))) { + storageNodes.put(storageUnitNodeMapper.getStorageNodeIdentifier(), resourceMetaData.getDataSourceMap().get(storageUnitNodeMapper.getStorageNodeIdentifier())); } 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..8804d254960f6 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.StorageNodeIdentifier; 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 toBeChangedStorageNodeIdentifiers = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifiers)); 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 toBeChangedStorageNodeIdentifier = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + Map storageNodes = + getNewStorageNodes(resourceMetaData, toBeChangedStorageNodeIdentifier, 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 toBeChangedStorageNodeIdentifier, + final Map propsMap) { + Map result = new LinkedHashMap<>(resourceMetaData.getDataSourceMap()); + result.keySet().removeAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifier).keySet()); + result.putAll(getChangedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifier, propsMap)); + result.putAll(getToBeAddedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifier, 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 toBeChangedStorageNodeIdentifier, + final Map propsMap) { + Collection toBeChangedDataSourceNames = toBeChangedStorageNodeIdentifier.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<>(toBeChangedStorageNodeIdentifier.size(), 1F); + for (StorageNodeIdentifier 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 StorageNodeIdentifier storageNodeIdentifier, final DataSourcePoolProperties propsMap) { + return originalDataSources.containsKey(storageNodeIdentifier) && !propsMap.equals(DataSourcePoolPropertiesCreator.create(originalDataSources.get(storageNodeIdentifier))); } - 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 toBeChangedStorageNodeIdentifier, + final Map propsMap) { + Collection toBeAddedDataSourceNames = toBeChangedStorageNodeIdentifier.stream().filter(each -> !storageNodes.containsKey(each)).collect(Collectors.toList()); + Map result = new LinkedHashMap<>(); + for (StorageNodeIdentifier 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 toBeRemovedStorageNodeIdentifiers = toRemovedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Collection inUsedDataSourceNames = reservedStorageUnitNodeMappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Map staleStorageNodes = resourceMetaData.getDataSourceMap().entrySet().stream() + .filter(entry -> toBeRemovedStorageNodeIdentifiers.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()) { + StorageNodeIdentifier storageNodeIdentifier = entry.getKey(); + if (isModifiedDataSource(storageNodes, storageNodeIdentifier, entry.getValue())) { + result.put(storageNodeIdentifier, storageNodes.get(storageNodeIdentifier)); } } 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..eb2868a3f0238 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.StorageNodeIdentifier; 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 StorageNodeIdentifier("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..face5419b7849 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.StorageNodeIdentifier; 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 StorageNodeIdentifier("not_change"))); + assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeIdentifier("new"))); + assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeIdentifier("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..b2ad9221da3bf 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.StorageNodeIdentifier; 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 StorageNodeIdentifier("new_ds"), new MockedDataSource()), Collections.emptyMap()); + StorageResource staleStorageResource = new StorageResource(Collections.singletonMap(new StorageNodeIdentifier("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..ecf976187987b 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.StorageNodeIdentifier; 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 StorageNodeIdentifier("foo_db")); assertThat(dataSource.getJdbcUrl(), is("jdbc:h2:mem:foo_db;DB_CLOSE_DELAY=-1")); assertThat(dataSource.getUsername(), is("sa")); assertThat(dataSource.getPassword(), is("")); From 5d2ca7856c60ea8d7493faf6f57f592e39aa649a Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 28 Sep 2023 01:39:33 +0800 Subject: [PATCH 2/5] Refactor StorageNode --- ...aSourceGeneratedDatabaseConfiguration.java | 2 +- .../database/resource/StorageResource.java | 6 ++- .../database/resource/node/StorageNode.java | 43 +++++++++++++++++++ .../database/resource/unit/StorageUnit.java | 21 +++++---- .../resource/unit/StorageUnitNodeMapper.java | 12 +----- .../unit/StorageUnitNodeMapperUtils.java | 14 +++--- .../switcher/NewResourceSwitchManager.java | 10 ++--- .../switcher/ResourceSwitchManager.java | 8 ++-- 8 files changed, 78 insertions(+), 38 deletions(-) create mode 100644 infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.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 aab3722a1bac9..fffb14ebb0323 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 @@ -59,7 +59,7 @@ public DataSourceGeneratedDatabaseConfiguration(final Map getStorageNodeDataSourceMap(final Map mappers) { Map result = new LinkedHashMap<>(mappers.size(), 1F); for (Entry entry : mappers.entrySet()) { - result.computeIfAbsent(entry.getValue().getStorageNodeIdentifier(), + 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/StorageResource.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java index 2de2ca343e58e..9a2d17636d27a 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 @@ -19,6 +19,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.StorageNodeIdentifier; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; @@ -48,9 +49,10 @@ public StorageResource(final Map dataSourceMa private Map createWrappedDataSources() { Map result = new LinkedHashMap<>(storageUnitNodeMappers.size(), 1F); for (Entry entry : storageUnitNodeMappers.entrySet()) { - DataSource dataSource = dataSourceMap.get(entry.getValue().getStorageNodeIdentifier()); + 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 new file mode 100644 index 0000000000000..8fda655822d9a --- /dev/null +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java @@ -0,0 +1,43 @@ +/* + * 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 lombok.Getter; + +/** + * Storage node. + */ +@Getter +public final class StorageNode { + + private final StorageNodeIdentifier name; + + private final String url; + + private final String catalog; + + public StorageNode(final String name, final String url) { + this(name, url, null); + } + + public StorageNode(final String name, final String url, final String catalog) { + this.name = new StorageNodeIdentifier(name); + this.url = url; + this.catalog = catalog; + } +} 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 4adb1201c6e9b..e940c22317996 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 @@ -26,6 +26,7 @@ import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; 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.StorageNodeIdentifier; import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; @@ -63,9 +64,10 @@ public StorageUnit(final String databaseName, final Map storageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper) { - DataSource dataSource = storageNodeDataSources.get(unitNodeMapper.getStorageNodeIdentifier()); - 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) { @@ -79,19 +81,20 @@ private Map getEnabledStorageNodeDataSources( } private DatabaseType createStorageType(final Map enabledStorageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper) { - return DatabaseTypeEngine.getStorageType(enabledStorageNodeDataSources.containsKey(unitNodeMapper.getStorageNodeIdentifier()) - ? Collections.singleton(enabledStorageNodeDataSources.get(unitNodeMapper.getStorageNodeIdentifier())) + 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.getStorageNodeIdentifier())) { + 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.getStorageNodeIdentifier())).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/StorageUnitNodeMapper.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapper.java index 3b5ef6cf34641..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 @@ -19,7 +19,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeIdentifier; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; /** * Storage unit and node mapper. @@ -30,13 +30,5 @@ public final class StorageUnitNodeMapper { private final String name; - private final StorageNodeIdentifier storageNodeIdentifier; - - private final String url; - - private final String catalog; - - public StorageUnitNodeMapper(final String name, final StorageNodeIdentifier storageNodeIdentifier, final String url) { - this(name, storageNodeIdentifier, url, null); - } + private final StorageNode storageNode; } 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 3266911b65d22..b3229140f6dc6 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 @@ -26,6 +26,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeIdentifier; import javax.sql.DataSource; @@ -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(); - StorageNodeIdentifier storageNodeIdentifier = new StorageNodeIdentifier(getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); - return createStorageUnitNodeMapper(storageNodeIdentifier, 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,11 +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 StorageNodeIdentifier storageNodeIdentifier, - 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, storageNodeIdentifier, url, new StandardJdbcUrlParser().parse(url).getDatabase()) - : new StorageUnitNodeMapper(storageUnitName, storageNodeIdentifier, url); + ? new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageNodeName, url, new StandardJdbcUrlParser().parse(url).getDatabase())) + : new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageNodeName, url)); } /** 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 286f6c0b32a7b..afd30b2a699dd 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 @@ -57,7 +57,7 @@ public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMeta private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, final Map mappers, final Map storageUnitDataSourcePoolProps) { - Collection storageNodeIdentifiers = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Collection storageNodeIdentifiers = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map newStorageNodes = new LinkedHashMap<>(storageNodeIdentifiers.size(), 1F); for (StorageNodeIdentifier each : storageNodeIdentifiers) { @@ -84,7 +84,7 @@ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaDat } private StorageResource getAlterNewStorageResource(final Map mappers, final Map storageUnitDataSourcePoolProps) { - Collection toBeAlteredStorageNodeIdentifiers = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Collection toBeAlteredStorageNodeIdentifiers = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodeIdentifiers.size(), 1F); for (StorageNodeIdentifier each : toBeAlteredStorageNodeIdentifiers) { @@ -94,7 +94,7 @@ private StorageResource getAlterNewStorageResource(final Map mappers) { - Collection toBeAlteredStorageNodeIdentifiers = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Collection toBeAlteredStorageNodeIdentifiers = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodeIdentifiers.size(), 1F); for (Entry entry : resourceMetaData.getDataSourceMap().entrySet()) { if (toBeAlteredStorageNodeIdentifiers.contains(entry.getKey())) { @@ -123,8 +123,8 @@ private StorageResource getToBeRemovedStaleStorageResource(final ResourceMetaDat StorageUnitNodeMapper storageUnitNodeMapper = resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().remove(storageUnitName); Map reservedStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(); Map storageNodes = new LinkedHashMap<>(1, 1F); - if (reservedStorageUnitNodeMappers.values().stream().noneMatch(each -> each.getStorageNodeIdentifier().equals(storageUnitNodeMapper.getStorageNodeIdentifier()))) { - storageNodes.put(storageUnitNodeMapper.getStorageNodeIdentifier(), resourceMetaData.getDataSourceMap().get(storageUnitNodeMapper.getStorageNodeIdentifier())); + if (reservedStorageUnitNodeMappers.values().stream().noneMatch(each -> each.getStorageNode().equals(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 8804d254960f6..fca32d1365fa5 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 @@ -84,7 +84,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap); Map toBeChangedMappers = StorageUnitNodeMapperUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap); - Collection toBeChangedStorageNodeIdentifiers = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Collection toBeChangedStorageNodeIdentifiers = toBeChangedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifiers)); staleStorageResource.getStorageUnitNodeMappers().putAll( getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedMappers.keySet())); @@ -94,7 +94,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, final Map toBeChangedMappers, final Map storageUnitDataSourcePoolProps) { - Collection toBeChangedStorageNodeIdentifier = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Collection toBeChangedStorageNodeIdentifier = toBeChangedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = getNewStorageNodes(resourceMetaData, toBeChangedStorageNodeIdentifier, storageNodeDataSourcePoolProps); @@ -151,8 +151,8 @@ 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 toBeRemovedStorageNodeIdentifiers = toRemovedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); - Collection inUsedDataSourceNames = reservedStorageUnitNodeMappers.values().stream().map(StorageUnitNodeMapper::getStorageNodeIdentifier).collect(Collectors.toSet()); + Collection toBeRemovedStorageNodeIdentifiers = 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 -> toBeRemovedStorageNodeIdentifiers.contains(entry.getKey()) && !inUsedDataSourceNames.contains(entry.getKey())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); From f07257f4218f9a3fbd87ce597593cdf53315389b Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 28 Sep 2023 01:40:25 +0800 Subject: [PATCH 3/5] Refactor StorageNodeName --- ...aSourceGeneratedDatabaseConfiguration.java | 6 +- .../database/resource/ResourceMetaData.java | 6 +- .../database/resource/StorageResource.java | 6 +- .../database/resource/node/StorageNode.java | 4 +- ...deIdentifier.java => StorageNodeName.java} | 6 +- .../resource/node/StorageNodeUtils.java | 4 +- .../database/resource/unit/StorageUnit.java | 16 ++-- .../resource/unit/StorageUnitMetaData.java | 4 +- .../unit/StorageUnitNodeMapperUtils.java | 10 +-- ...rceGeneratedDatabaseConfigurationTest.java | 4 +- ...urceProvidedDatabaseConfigurationTest.java | 4 +- .../ShardingSphereDataSourceTest.java | 6 +- .../persist/MetaDataPersistService.java | 6 +- .../persist/NewMetaDataPersistService.java | 6 +- .../context/ConfigurationContextManager.java | 12 +-- .../switcher/NewResourceSwitchManager.java | 28 +++--- .../switcher/ResourceSwitchManager.java | 88 +++++++++---------- .../mode/manager/ContextManagerTest.java | 6 +- .../switcher/ResourceSwitchManagerTest.java | 8 +- .../switcher/SwitchingResourceTest.java | 6 +- .../YamlProxyConfigurationSwapperTest.java | 4 +- 21 files changed, 120 insertions(+), 120 deletions(-) rename infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/{StorageNodeIdentifier.java => StorageNodeName.java} (87%) 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 fffb14ebb0323..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.StorageNodeIdentifier; +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,8 +56,8 @@ 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().getName(), key -> DataSourcePoolCreator.create(entry.getKey(), dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values())); 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 a9c5a3e8b89e7..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.StorageNodeIdentifier; +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 9a2d17636d27a..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,7 +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.StorageNodeIdentifier; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; import javax.sql.DataSource; @@ -34,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(); 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 8fda655822d9a..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 @@ -25,7 +25,7 @@ @Getter public final class StorageNode { - private final StorageNodeIdentifier name; + private final StorageNodeName name; private final String url; @@ -36,7 +36,7 @@ public StorageNode(final String name, final String url) { } public StorageNode(final String name, final String url, final String catalog) { - this.name = new StorageNodeIdentifier(name); + 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/StorageNodeIdentifier.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java similarity index 87% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeIdentifier.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java index 2d7a547bbeac9..ec6941710ba83 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeIdentifier.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java @@ -22,17 +22,17 @@ import lombok.RequiredArgsConstructor; /** - * Storage node identifier. + * Storage node name. */ @RequiredArgsConstructor @Getter -public final class StorageNodeIdentifier { +public final class StorageNodeName { private final String name; @Override public boolean equals(final Object obj) { - return obj instanceof StorageNodeIdentifier && ((StorageNodeIdentifier) obj).name.equalsIgnoreCase(name); + return obj instanceof StorageNodeName && ((StorageNodeName) obj).name.equalsIgnoreCase(name); } @Override 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 4c36e6dfccded..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) { + public static Map getStorageNodeDataSources(final Map dataSources) { return dataSources.entrySet().stream().collect( - Collectors.toMap(entry -> new StorageNodeIdentifier(entry.getKey()), Entry::getValue, (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(dataSources.size(), 1F))); + 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 e940c22317996..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,7 +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.StorageNodeIdentifier; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import javax.sql.DataSource; @@ -54,25 +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 mapper) { + 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); @@ -80,13 +80,13 @@ private Map getEnabledStorageNodeDataSources( .filter(entry -> enabledDataSources.containsKey(entry.getKey().getName())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); } - private DatabaseType createStorageType(final Map enabledStorageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper) { + 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, + private Optional createConnectionProperties(final Map enabledStorageNodeDataSources, final StorageUnitNodeMapper mapper, final DatabaseType storageType) { StorageNode storageNode = mapper.getStorageNode(); if (!enabledStorageNodeDataSources.containsKey(storageNode.getName())) { 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 0878b2524ab69..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.StorageNodeIdentifier; +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/StorageUnitNodeMapperUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java index b3229140f6dc6..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,7 +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.StorageNodeIdentifier; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import javax.sql.DataSource; import java.util.LinkedHashMap; @@ -106,14 +106,14 @@ private static StorageUnitNodeMapper createStorageUnitNodeMapper(final String st * @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(); - StorageNodeIdentifier storageNodeIdentifier = new StorageNodeIdentifier(getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); - result.putIfAbsent(storageNodeIdentifier, 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 ff0a33911d61c..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.StorageNodeIdentifier; +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 StorageNodeIdentifier("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 034ae50e210fe..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.StorageNodeIdentifier; +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 StorageNodeIdentifier("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 711e3a6a75ff8..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.StorageNodeIdentifier; +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 StorageNodeIdentifier("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 32ed997f03629..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.StorageNodeIdentifier; +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 7b93950749717..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.StorageNodeIdentifier; +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 b11c5e2caa226..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.StorageNodeIdentifier; +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 afd30b2a699dd..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.StorageNodeIdentifier; +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 storageNodeIdentifiers = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); - Map newStorageNodes = new LinkedHashMap<>(storageNodeIdentifiers.size(), 1F); - for (StorageNodeIdentifier each : storageNodeIdentifiers) { + 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 toBeAlteredStorageNodeIdentifiers = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); - Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodeIdentifiers.size(), 1F); - for (StorageNodeIdentifier each : toBeAlteredStorageNodeIdentifiers) { + 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 toBeAlteredStorageNodeIdentifiers = mappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodeIdentifiers.size(), 1F); - for (Entry entry : resourceMetaData.getDataSourceMap().entrySet()) { - if (toBeAlteredStorageNodeIdentifiers.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,7 +122,7 @@ 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().getName(), resourceMetaData.getDataSourceMap().get(storageUnitNodeMapper.getStorageNode().getName())); } 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 fca32d1365fa5..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.StorageNodeIdentifier; +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 toBeChangedStorageNodeIdentifiers = toBeChangedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifiers)); + 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 toBeChangedStorageNodeIdentifier = toBeChangedMappers.values().stream().map(each -> each.getStorageNode().getName()).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); - Map storageNodes = - getNewStorageNodes(resourceMetaData, toBeChangedStorageNodeIdentifier, 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 toBeChangedStorageNodeIdentifier, - final Map propsMap) { - Map result = new LinkedHashMap<>(resourceMetaData.getDataSourceMap()); - result.keySet().removeAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifier).keySet()); - result.putAll(getChangedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifier, propsMap)); - result.putAll(getToBeAddedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodeIdentifier, 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,29 +119,29 @@ private Map getNewStorageUnitNodeMappers(final Re return result; } - private Map getChangedDataSources(final Map storageNodes, - final Collection toBeChangedStorageNodeIdentifier, - final Map propsMap) { - Collection toBeChangedDataSourceNames = toBeChangedStorageNodeIdentifier.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<>(toBeChangedStorageNodeIdentifier.size(), 1F); - for (StorageNodeIdentifier 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 StorageNodeIdentifier storageNodeIdentifier, final DataSourcePoolProperties propsMap) { - return originalDataSources.containsKey(storageNodeIdentifier) && !propsMap.equals(DataSourcePoolPropertiesCreator.create(originalDataSources.get(storageNodeIdentifier))); + 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 toBeChangedStorageNodeIdentifier, - final Map propsMap) { - Collection toBeAddedDataSourceNames = toBeChangedStorageNodeIdentifier.stream().filter(each -> !storageNodes.containsKey(each)).collect(Collectors.toList()); - Map result = new LinkedHashMap<>(); - for (StorageNodeIdentifier 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; @@ -151,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 toBeRemovedStorageNodeIdentifiers = 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 -> toBeRemovedStorageNodeIdentifiers.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())); @@ -163,29 +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()) { - StorageNodeIdentifier storageNodeIdentifier = entry.getKey(); - if (isModifiedDataSource(storageNodes, storageNodeIdentifier, entry.getValue())) { - result.put(storageNodeIdentifier, storageNodes.get(storageNodeIdentifier)); + 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 eb2868a3f0238..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.StorageNodeIdentifier; +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 StorageNodeIdentifier("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 face5419b7849..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.StorageNodeIdentifier; +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 StorageNodeIdentifier("not_change"))); - assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeIdentifier("new"))); - assertTrue(actual.getNewStorageResource().getDataSourceMap().containsKey(new StorageNodeIdentifier("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 b2ad9221da3bf..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.StorageNodeIdentifier; +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 StorageNodeIdentifier("new_ds"), new MockedDataSource()), Collections.emptyMap()); - StorageResource staleStorageResource = new StorageResource(Collections.singletonMap(new StorageNodeIdentifier("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 ecf976187987b..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.StorageNodeIdentifier; +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 StorageNodeIdentifier("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("")); From 3cf91c682ed1765f4a73d2120fdfa9ee538e1543 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 28 Sep 2023 12:02:07 +0800 Subject: [PATCH 4/5] Refactor StorageNodeName --- .../database/resource/node/StorageNode.java | 10 +++------- .../resource/node/StorageNodeName.java | 4 ++++ .../unit/StorageUnitNodeMapperUtils.java | 19 ++++++++----------- 3 files changed, 15 insertions(+), 18 deletions(-) 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 98e0b0337299e..fa836f44b44f9 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 @@ -18,10 +18,12 @@ package org.apache.shardingsphere.infra.metadata.database.resource.node; import lombok.Getter; +import lombok.RequiredArgsConstructor; /** * Storage node. */ +@RequiredArgsConstructor @Getter public final class StorageNode { @@ -31,13 +33,7 @@ public final class StorageNode { private final String catalog; - public StorageNode(final String name, final String url) { + public StorageNode(final StorageNodeName name, final String url) { this(name, url, null); } - - 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 index ec6941710ba83..3ec99ec0e1a63 100644 --- 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 @@ -30,6 +30,10 @@ public final class StorageNodeName { private final String name; + public StorageNodeName(final String hostname, final int port, final String username) { + name = String.format("%s_%s_%s", hostname, port, username); + } + @Override public boolean equals(final Object obj) { return obj instanceof StorageNodeName && ((StorageNodeName) obj).name.equalsIgnoreCase(name); 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 e5c6144e3f92e..2bb78e1275e01 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 @@ -55,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(); - String storageNodeName = getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable); + StorageNodeName 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) { + private static StorageNodeName getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) { try { JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url); - return isInstanceConnectionAvailable ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName; + return isInstanceConnectionAvailable ? new StorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : new StorageNodeName(dataSourceName); } catch (final UnrecognizedDatabaseURLException ex) { - return dataSourceName; + return new StorageNodeName(dataSourceName); } } - private static String generateStorageNodeName(final String hostname, final int port, final String username) { - return String.format("%s_%s_%s", hostname, port, username); - } - - private static StorageUnitNodeMapper createStorageUnitNodeMapper(final String storageNodeName, final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) { + 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)); @@ -112,7 +109,7 @@ public static Map getStorageNodeDataS Map standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties(); String url = standardProps.get("url").toString(); boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); - StorageNodeName storageNodeName = new StorageNodeName(getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); + StorageNodeName storageNodeName = getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable); result.putIfAbsent(storageNodeName, entry.getValue()); } return result; From 35146f057fe76f162628f20cb773fbdfbd570399 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 28 Sep 2023 12:06:14 +0800 Subject: [PATCH 5/5] Refactor StorageNodeName --- .../infra/metadata/database/resource/node/StorageNodeName.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index a74bde4e905e7..3ec99ec0e1a63 100644 --- 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 @@ -33,7 +33,7 @@ public final class StorageNodeName { public StorageNodeName(final String hostname, final int port, final String username) { name = String.format("%s_%s_%s", hostname, port, username); } - + @Override public boolean equals(final Object obj) { return obj instanceof StorageNodeName && ((StorageNodeName) obj).name.equalsIgnoreCase(name);