From a220b6901035fc42d1b3651642b15d1ad1ae6572 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 9 Aug 2023 22:11:20 +0800 Subject: [PATCH] Rename StorageUnit to StorageUnitNodeMapper (#28000) --- ...taSourceProvidedDatabaseConfiguration.java | 2 +- .../database/resource/ResourceMetaData.java | 4 +- .../resource/StorageUnitMetaData.java | 32 +++++----- .../pool/creator/DataSourcePoolCreator.java | 50 +++++++-------- .../datasource/storage/StorageResource.java | 14 ++--- .../StorageResourceWithProperties.java | 5 +- ...geUnit.java => StorageUnitNodeMapper.java} | 20 +++--- .../datasource/storage/StorageUtils.java | 10 +-- .../context/ConfigurationContextManager.java | 20 +++--- .../switcher/NewResourceSwitchManager.java | 18 +++--- .../switcher/ResourceSwitchManager.java | 61 ++++++++++--------- .../mode/manager/ContextManagerTest.java | 4 +- .../ConfigurationChangedSubscriberTest.java | 2 +- 13 files changed, 122 insertions(+), 120 deletions(-) rename infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/{StorageUnit.java => StorageUnitNodeMapper.java} (68%) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java index 63a44b1552ed9..7bb684ea4e88b 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceProvidedDatabaseConfiguration.java @@ -46,7 +46,7 @@ public final class DataSourceProvidedDatabaseConfiguration implements DatabaseCo public DataSourceProvidedDatabaseConfiguration(final Map dataSources, final Collection ruleConfigurations) { this.ruleConfigurations = ruleConfigurations; - this.storageResource = new StorageResource(dataSources, StorageUtils.getStorageUnits(dataSources)); + this.storageResource = new StorageResource(dataSources, StorageUtils.getStorageUnitNodeMappers(dataSources)); dataSourcePropsMap = createDataSourcePropertiesMap(dataSources); } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java index cc058de50c6e6..c41d5a78e4f59 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 @@ -54,7 +54,7 @@ public ResourceMetaData(final String databaseName, final Map Map enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, dataSources); Map storageTypes = createStorageTypes(dataSources, enabledDataSources); storageNodeMetaData = new StorageNodeMetaData(dataSources); - storageUnitMetaData = new StorageUnitMetaData(dataSources, storageTypes, StorageUtils.getStorageUnits(dataSources), enabledDataSources); + storageUnitMetaData = new StorageUnitMetaData(dataSources, storageTypes, StorageUtils.getStorageUnitNodeMappers(dataSources), enabledDataSources); } @@ -62,7 +62,7 @@ public ResourceMetaData(final String databaseName, final StorageResource storage Map enabledDataSources = DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, storageResource.getStorageNodes()); Map storageTypes = createStorageTypes(storageResource.getStorageNodes(), enabledDataSources); storageNodeMetaData = new StorageNodeMetaData(storageResource.getStorageNodes()); - storageUnitMetaData = new StorageUnitMetaData(storageResource.getStorageNodes(), dataSourcePropsMap, storageTypes, storageResource.getStorageUnits(), enabledDataSources); + storageUnitMetaData = new StorageUnitMetaData(storageResource.getStorageNodes(), dataSourcePropsMap, storageTypes, storageResource.getStorageUnitNodeMappers(), enabledDataSources); } private Map createStorageTypes(final Map dataSources, final Map enabledDataSources) { diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageUnitMetaData.java index 99775b4221e61..498043c5a7aed 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageUnitMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageUnitMetaData.java @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.datasource.ShardingSphereStorageDataSourceWrapper; import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties; import org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator; -import org.apache.shardingsphere.infra.datasource.storage.StorageUnit; +import org.apache.shardingsphere.infra.datasource.storage.StorageUnitNodeMapper; import javax.sql.DataSource; import java.util.Collections; @@ -46,31 +46,27 @@ public final class StorageUnitMetaData { private final Map storageTypes; - private final Map storageUnits; + private final Map unitNodeMappers; private final Map connectionPropsMap; - public StorageUnitMetaData(final Map dataSources, final Map storageTypes, final Map storageUnits, + public StorageUnitMetaData(final Map dataSources, final Map storageTypes, final Map unitNodeMappers, final Map enabledDataSources) { - this.storageUnits = storageUnits; - this.dataSources = getStorageUnitDataSources(dataSources, storageUnits); - dataSourcePropsMap = DataSourcePropertiesCreator.create(dataSources); - this.storageTypes = getStorageUnitTypes(storageTypes); - connectionPropsMap = createConnectionPropertiesMap(enabledDataSources, storageTypes, storageUnits); + this(dataSources, DataSourcePropertiesCreator.create(dataSources), storageTypes, unitNodeMappers, enabledDataSources); } public StorageUnitMetaData(final Map dataSources, final Map dataSourcePropsMap, - final Map storageTypes, final Map storageUnits, final Map enabledDataSources) { - this.storageUnits = storageUnits; - this.dataSources = getStorageUnitDataSources(dataSources, storageUnits); + final Map storageTypes, final Map unitNodeMappers, final Map enabledDataSources) { + this.unitNodeMappers = unitNodeMappers; + this.dataSources = getStorageUnitDataSources(dataSources, unitNodeMappers); this.dataSourcePropsMap = dataSourcePropsMap; this.storageTypes = getStorageUnitTypes(storageTypes); - connectionPropsMap = createConnectionPropertiesMap(enabledDataSources, storageTypes, storageUnits); + connectionPropsMap = createConnectionPropertiesMap(enabledDataSources, storageTypes, unitNodeMappers); } - private Map getStorageUnitDataSources(final Map storageNodes, final Map storageUnits) { + private Map getStorageUnitDataSources(final Map storageNodes, final Map storageUnits) { Map result = new LinkedHashMap<>(storageUnits.size(), 1F); - for (Entry entry : storageUnits.entrySet()) { + for (Entry entry : storageUnits.entrySet()) { DataSource dataSource = storageNodes.get(entry.getValue().getNodeName()); result.put(entry.getKey(), new ShardingSphereStorageDataSourceWrapper(dataSource, entry.getValue().getCatalog(), entry.getValue().getUrl())); } @@ -78,8 +74,8 @@ private Map getStorageUnitDataSources(final Map getStorageUnitTypes(final Map storageTypes) { - Map result = new LinkedHashMap<>(storageUnits.size(), 1F); - for (Entry entry : storageUnits.entrySet()) { + Map result = new LinkedHashMap<>(unitNodeMappers.size(), 1F); + for (Entry entry : unitNodeMappers.entrySet()) { DatabaseType storageType = storageTypes.containsKey(entry.getValue().getNodeName()) ? storageTypes.get(entry.getValue().getNodeName()) : DatabaseTypeEngine.getStorageType(Collections.emptyList()); @@ -89,9 +85,9 @@ private Map getStorageUnitTypes(final Map createConnectionPropertiesMap(final Map enabledDataSources, - final Map storageTypes, final Map storageUnits) { + final Map storageTypes, final Map storageUnits) { Map result = new LinkedHashMap<>(storageUnits.size(), 1F); - for (Entry entry : storageUnits.entrySet()) { + for (Entry entry : storageUnits.entrySet()) { String nodeName = entry.getValue().getNodeName(); if (enabledDataSources.containsKey(nodeName)) { Map standardProps = DataSourcePropertiesCreator.create(enabledDataSources.get(nodeName)).getConnectionPropertySynonyms().getStandardProperties(); diff --git a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java index e1c864eb8cbff..e7bb432ae9a60 100644 --- a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java +++ b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java @@ -36,7 +36,7 @@ import org.apache.shardingsphere.infra.datasource.storage.StorageNodeProperties; import org.apache.shardingsphere.infra.datasource.storage.StorageResource; import org.apache.shardingsphere.infra.datasource.storage.StorageResourceWithProperties; -import org.apache.shardingsphere.infra.datasource.storage.StorageUnit; +import org.apache.shardingsphere.infra.datasource.storage.StorageUnitNodeMapper; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import javax.sql.DataSource; @@ -70,11 +70,11 @@ public static StorageResource createStorageResource(final Map dataSourcePropsMap, final boolean cacheEnabled) { Map storageNodes = new LinkedHashMap<>(); - Map storageUnits = new LinkedHashMap<>(); + Map storageUnitNodeMappers = new LinkedHashMap<>(); for (Entry entry : dataSourcePropsMap.entrySet()) { - StorageNodeProperties storageNodeProperties = getStorageNodeProperties(entry.getKey(), entry.getValue()); - if (storageNodes.containsKey(storageNodeProperties.getName())) { - appendStorageUnit(storageUnits, storageNodeProperties, entry.getKey(), entry.getValue()); + StorageNodeProperties storageNodeProps = getStorageNodeProperties(entry.getKey(), entry.getValue()); + if (storageNodes.containsKey(storageNodeProps.getName())) { + appendStorageUnitNodeMapper(storageUnitNodeMappers, storageNodeProps, entry.getKey(), entry.getValue()); continue; } DataSource dataSource; @@ -88,10 +88,10 @@ public static StorageResource createStorageResource(final Map dataSourcePropsMap) { Map storageNodes = new LinkedHashMap<>(); - Map storageUnits = new LinkedHashMap<>(); + Map storageUnitNodeMappers = new LinkedHashMap<>(); Map dataSourcePropertiesMap = new LinkedHashMap<>(); for (Entry entry : dataSourcePropsMap.entrySet()) { StorageNodeProperties storageNodeProperties = getStorageNodeProperties(entry.getKey(), entry.getValue()); if (storageNodes.containsKey(storageNodeProperties.getName())) { - appendStorageUnit(storageUnits, storageNodeProperties, entry.getKey(), entry.getValue()); + appendStorageUnitNodeMapper(storageUnitNodeMappers, storageNodeProperties, entry.getKey(), entry.getValue()); continue; } storageNodes.put(storageNodeProperties.getName(), null); - appendStorageUnit(storageUnits, storageNodeProperties, entry.getKey(), entry.getValue()); + appendStorageUnitNodeMapper(storageUnitNodeMappers, storageNodeProperties, entry.getKey(), entry.getValue()); dataSourcePropertiesMap.put(storageNodeProperties.getName(), entry.getValue()); } - return new StorageResourceWithProperties(storageNodes, storageUnits, dataSourcePropertiesMap); + return new StorageResourceWithProperties(storageNodes, storageUnitNodeMappers, dataSourcePropertiesMap); } - private static void appendStorageUnit(final Map storageUnits, final StorageNodeProperties storageNodeProperties, - final String unitName, final DataSourceProperties dataSourceProps) { + private static void appendStorageUnitNodeMapper(final Map storageUnitNodeMappers, final StorageNodeProperties storageNodeProps, + final String unitName, final DataSourceProperties dataSourceProps) { String url = dataSourceProps.getConnectionPropertySynonyms().getStandardProperties().get("url").toString(); - storageUnits.put(unitName, getStorageUnit(storageNodeProperties, unitName, url)); + storageUnitNodeMappers.put(unitName, getStorageUnitNodeMapper(storageNodeProps, unitName, url)); } - private static StorageUnit getStorageUnit(final StorageNodeProperties storageNodeProperties, final String unitName, final String url) { - DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(storageNodeProperties.getDatabaseType()).getDialectDatabaseMetaData(); + private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNodeProperties storageNodeProps, final String unitName, final String url) { + DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(storageNodeProps.getDatabaseType()).getDialectDatabaseMetaData(); return dialectDatabaseMetaData.isInstanceConnectionAvailable() - ? new StorageUnit(unitName, storageNodeProperties.getName(), storageNodeProperties.getDatabase(), url) - : new StorageUnit(unitName, storageNodeProperties.getName(), url); + ? new StorageUnitNodeMapper(unitName, storageNodeProps.getName(), storageNodeProps.getDatabase(), url) + : new StorageUnitNodeMapper(unitName, storageNodeProps.getName(), url); } - private static StorageNodeProperties getStorageNodeProperties(final String dataSourceName, final DataSourceProperties dataSourceProperties) { - Map standardProperties = dataSourceProperties.getConnectionPropertySynonyms().getStandardProperties(); + private static StorageNodeProperties getStorageNodeProperties(final String dataSourceName, final DataSourceProperties storageNodeProps) { + Map standardProperties = storageNodeProps.getConnectionPropertySynonyms().getStandardProperties(); String url = standardProperties.get("url").toString(); String username = standardProperties.get("username").toString(); DatabaseType databaseType = DatabaseTypeFactory.get(url); - return getStorageNodeProperties(dataSourceName, dataSourceProperties, url, username, databaseType); + return getStorageNodeProperties(dataSourceName, storageNodeProps, url, username, databaseType); } - private static StorageNodeProperties getStorageNodeProperties(final String dataSourceName, final DataSourceProperties dataSourceProperties, + private static StorageNodeProperties getStorageNodeProperties(final String dataSourceName, final DataSourceProperties dataSourceProps, final String url, final String username, final DatabaseType databaseType) { try { JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url); DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData(); String nodeName = dialectDatabaseMetaData.isInstanceConnectionAvailable() ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName; - return new StorageNodeProperties(nodeName, databaseType, dataSourceProperties, jdbcUrl.getDatabase()); + return new StorageNodeProperties(nodeName, databaseType, dataSourceProps, jdbcUrl.getDatabase()); } catch (final UnrecognizedDatabaseURLException ex) { - return new StorageNodeProperties(dataSourceName, databaseType, dataSourceProperties, null); + return new StorageNodeProperties(dataSourceName, databaseType, dataSourceProps, null); } } diff --git a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageResource.java b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageResource.java index ad8ca882becb4..d50d4328aedab 100644 --- a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageResource.java +++ b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageResource.java @@ -33,19 +33,19 @@ public class StorageResource { private final Map storageNodes; - private final Map storageUnits; + private final Map storageUnitNodeMappers; private final Map wrappedDataSources; - public StorageResource(final Map storageNodes, final Map storageUnits) { + public StorageResource(final Map storageNodes, final Map storageUnitNodeMappers) { this.storageNodes = storageNodes; - this.storageUnits = storageUnits; - wrappedDataSources = getWrappedDataSources(storageUnits); + this.storageUnitNodeMappers = storageUnitNodeMappers; + wrappedDataSources = createWrappedDataSources(); } - private Map getWrappedDataSources(final Map storageUnits) { - Map result = new LinkedHashMap<>(storageUnits.size(), 1F); - for (Entry entry : storageUnits.entrySet()) { + private Map createWrappedDataSources() { + Map result = new LinkedHashMap<>(storageUnitNodeMappers.size(), 1F); + for (Entry entry : storageUnitNodeMappers.entrySet()) { DataSource dataSource = storageNodes.get(entry.getValue().getNodeName()); if (null != dataSource) { result.put(entry.getKey(), new ShardingSphereStorageDataSourceWrapper(dataSource, entry.getValue().getCatalog(), entry.getValue().getUrl())); diff --git a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageResourceWithProperties.java b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageResourceWithProperties.java index ee64e98b0e9a9..fdcdbdfe16e55 100644 --- a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageResourceWithProperties.java +++ b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageResourceWithProperties.java @@ -31,8 +31,9 @@ public final class StorageResourceWithProperties extends StorageResource { private final Map dataSourcePropertiesMap; - public StorageResourceWithProperties(final Map storageNodes, final Map storageUnits, final Map dataSourcePropertiesMap) { - super(storageNodes, storageUnits); + public StorageResourceWithProperties(final Map storageNodes, + final Map storageUnitNodeMappers, final Map dataSourcePropertiesMap) { + super(storageNodes, storageUnitNodeMappers); this.dataSourcePropertiesMap = dataSourcePropertiesMap; } } diff --git a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUnit.java b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUnitNodeMapper.java similarity index 68% rename from infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUnit.java rename to infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUnitNodeMapper.java index f0d9aca9148eb..76f45297b70c3 100644 --- a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUnit.java +++ b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUnitNodeMapper.java @@ -22,11 +22,11 @@ import lombok.RequiredArgsConstructor; /** - * Storage unit. + * Storage unit and node mapper. */ @RequiredArgsConstructor @Getter -public final class StorageUnit { +public final class StorageUnitNodeMapper { private final String name; @@ -36,23 +36,23 @@ public final class StorageUnit { private final String url; - public StorageUnit(final String name, final String nodeName, final String url) { + public StorageUnitNodeMapper(final String name, final String nodeName, final String url) { this(name, nodeName, null, url); } - private boolean isSameCatalog(final StorageUnit storageUnit) { - return null == catalog ? null == storageUnit : catalog.equalsIgnoreCase(storageUnit.getCatalog()); - } - @Override public boolean equals(final Object obj) { - if (obj instanceof StorageUnit) { - StorageUnit storageUnit = (StorageUnit) obj; - return storageUnit.name.equalsIgnoreCase(name) && storageUnit.nodeName.equalsIgnoreCase(nodeName) && isSameCatalog(storageUnit); + if (obj instanceof StorageUnitNodeMapper) { + StorageUnitNodeMapper storageUnitNodeMapper = (StorageUnitNodeMapper) obj; + return storageUnitNodeMapper.name.equalsIgnoreCase(name) && storageUnitNodeMapper.nodeName.equalsIgnoreCase(nodeName) && isSameCatalog(storageUnitNodeMapper); } return false; } + private boolean isSameCatalog(final StorageUnitNodeMapper storageUnitNodeMapper) { + return null == catalog ? null == storageUnitNodeMapper : catalog.equalsIgnoreCase(storageUnitNodeMapper.getCatalog()); + } + @Override public int hashCode() { return Objects.hashCode(name.toUpperCase(), nodeName.toUpperCase(), null == catalog ? null : catalog.toUpperCase()); diff --git a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUtils.java b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUtils.java index bef99c65b3935..4993155739bb5 100644 --- a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUtils.java +++ b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/storage/StorageUtils.java @@ -34,17 +34,17 @@ public final class StorageUtils { /** - * Get storage units from provided data sources. + * Get storage unit node mappers from provided data sources. * * @param dataSources data sources - * @return storage units + * @return storage unit node mappers */ - public static Map getStorageUnits(final Map dataSources) { - Map result = new LinkedHashMap<>(dataSources.size(), 1F); + public static Map getStorageUnitNodeMappers(final Map dataSources) { + Map result = new LinkedHashMap<>(dataSources.size(), 1F); for (Entry entry : dataSources.entrySet()) { DataSourceProperties dataSourceProperties = DataSourcePropertiesCreator.create(entry.getValue()); String url = dataSourceProperties.getConnectionPropertySynonyms().getStandardProperties().get("url").toString(); - result.put(entry.getKey(), new StorageUnit(entry.getKey(), entry.getKey(), url)); + result.put(entry.getKey(), new StorageUnitNodeMapper(entry.getKey(), entry.getKey(), url)); } 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 b518f8da0a35d..d98ed84c9cdff 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 @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties; import org.apache.shardingsphere.infra.datasource.storage.StorageResource; -import org.apache.shardingsphere.infra.datasource.storage.StorageUnit; +import org.apache.shardingsphere.infra.datasource.storage.StorageUnitNodeMapper; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -270,8 +270,8 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD */ public Map renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) { Map newStorageNodes = getNewStorageNodes(database.getResourceMetaData().getStorageNodeMetaData().getDataSources(), resource); - Map newStorageUnits = getNewStorageUnits(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(), resource); - StorageResource newStorageResource = new StorageResource(newStorageNodes, newStorageUnits); + Map newStorageUnitNodeMappers = getNewStorageUnitNodeMappers(database.getResourceMetaData().getStorageUnitMetaData().getUnitNodeMappers(), resource); + StorageResource newStorageResource = new StorageResource(newStorageNodes, newStorageUnitNodeMappers); return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(database.getName(), database.getProtocolType(), new ResourceMetaData(database.getName(), newStorageResource, database.getResourceMetaData().getStorageUnitMetaData().getDataSourcePropsMap()), @@ -288,10 +288,10 @@ private Map getNewStorageNodes(final Map return result; } - private Map getNewStorageUnits(final Map currentStorageUnits, final SwitchingResource resource) { - Map result = new LinkedHashMap<>(); - for (Entry entry : currentStorageUnits.entrySet()) { - if (!resource.getStaleStorageResource().getStorageUnits().containsKey(entry.getKey())) { + private Map getNewStorageUnitNodeMappers(final Map currentStorageUnitNodeMappers, final SwitchingResource resource) { + Map result = new LinkedHashMap<>(currentStorageUnitNodeMappers.size(), 1F); + for (Entry entry : currentStorageUnitNodeMappers.entrySet()) { + if (!resource.getStaleStorageResource().getStorageUnitNodeMappers().containsKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } @@ -341,13 +341,13 @@ public synchronized Map createChangedDatabases(f if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getStorageNodes().isEmpty()) { resourceMetaData.getStorageNodeMetaData().getDataSources().putAll(switchingResource.getNewStorageResource().getStorageNodes()); } - if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getStorageUnits().isEmpty()) { - resourceMetaData.getStorageUnitMetaData().getStorageUnits().putAll(switchingResource.getNewStorageResource().getStorageUnits()); + if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getStorageUnitNodeMappers().isEmpty()) { + resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().putAll(switchingResource.getNewStorageResource().getStorageUnitNodeMappers()); } Collection toBeCreatedRuleConfigs = null == ruleConfigs ? metaDataContexts.get().getMetaData().getDatabase(databaseName).getRuleMetaData().getConfigurations() : ruleConfigs; - StorageResource storageResource = new StorageResource(resourceMetaData.getStorageNodeMetaData().getDataSources(), resourceMetaData.getStorageUnitMetaData().getStorageUnits()); + StorageResource storageResource = new StorageResource(resourceMetaData.getStorageNodeMetaData().getDataSources(), resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers()); DatabaseConfiguration toBeCreatedDatabaseConfig = new DataSourceProvidedDatabaseConfiguration( storageResource, toBeCreatedRuleConfigs, resourceMetaData.getStorageUnitMetaData().getDataSourcePropsMap()); ShardingSphereDatabase changedDatabase = createChangedDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName).getName(), internalLoadMetaData, 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 82475113ad5ee..72fded8079d95 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java @@ -21,7 +21,7 @@ import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties; import org.apache.shardingsphere.infra.datasource.storage.StorageResource; import org.apache.shardingsphere.infra.datasource.storage.StorageResourceWithProperties; -import org.apache.shardingsphere.infra.datasource.storage.StorageUnit; +import org.apache.shardingsphere.infra.datasource.storage.StorageUnitNodeMapper; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import javax.sql.DataSource; @@ -57,7 +57,7 @@ private StorageResource getRegisterNewStorageResource(final ResourceMetaData res storageNodes.put(each, DataSourcePoolCreator.create(toBeCreatedStorageResource.getDataSourcePropertiesMap().get(each))); } } - return new StorageResource(storageNodes, toBeCreatedStorageResource.getStorageUnits()); + return new StorageResource(storageNodes, toBeCreatedStorageResource.getStorageUnitNodeMappers()); } /** @@ -79,7 +79,7 @@ private StorageResource getAlterNewStorageResource(final StorageResourceWithProp for (String each : toBeAlteredStorageResource.getStorageNodes().keySet()) { storageNodes.put(each, DataSourcePoolCreator.create(toBeAlteredStorageResource.getDataSourcePropertiesMap().get(each))); } - return new StorageResource(storageNodes, toBeAlteredStorageResource.getStorageUnits()); + return new StorageResource(storageNodes, toBeAlteredStorageResource.getStorageUnitNodeMappers()); } private StorageResource getStaleStorageResource(final ResourceMetaData resourceMetaData, final StorageResourceWithProperties toBeAlteredStorageResource) { @@ -89,7 +89,7 @@ private StorageResource getStaleStorageResource(final ResourceMetaData resourceM storageNodes.put(entry.getKey(), entry.getValue()); } } - return new StorageResource(storageNodes, toBeAlteredStorageResource.getStorageUnits()); + return new StorageResource(storageNodes, toBeAlteredStorageResource.getStorageUnitNodeMappers()); } /** @@ -106,12 +106,12 @@ public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMe } private StorageResource getToBeRemovedStaleStorageResource(final ResourceMetaData resourceMetaData, final String storageUnitName) { - StorageUnit storageUnit = resourceMetaData.getStorageUnitMetaData().getStorageUnits().remove(storageUnitName); - Map reservedStorageUnits = resourceMetaData.getStorageUnitMetaData().getStorageUnits(); + StorageUnitNodeMapper storageUnitNodeMapper = resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().remove(storageUnitName); + Map reservedStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(); Map storageNodes = new LinkedHashMap<>(1, 1F); - if (reservedStorageUnits.values().stream().noneMatch(each -> each.getNodeName().equals(storageUnit.getNodeName()))) { - storageNodes.put(storageUnit.getNodeName(), resourceMetaData.getStorageNodeMetaData().getDataSources().get(storageUnit.getNodeName())); + if (reservedStorageUnitNodeMappers.values().stream().noneMatch(each -> each.getNodeName().equals(storageUnitNodeMapper.getNodeName()))) { + storageNodes.put(storageUnitNodeMapper.getNodeName(), resourceMetaData.getStorageNodeMetaData().getDataSources().get(storageUnitNodeMapper.getNodeName())); } - return new StorageResource(storageNodes, Collections.singletonMap(storageUnitName, storageUnit)); + 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 459d7ecdda474..9c4851689ac66 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator; import org.apache.shardingsphere.infra.datasource.storage.StorageResource; import org.apache.shardingsphere.infra.datasource.storage.StorageResourceWithProperties; -import org.apache.shardingsphere.infra.datasource.storage.StorageUnit; +import org.apache.shardingsphere.infra.datasource.storage.StorageUnitNodeMapper; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import javax.sql.DataSource; @@ -84,14 +84,15 @@ public SwitchingResource createByAlterDataSourceProps(final ResourceMetaData res StorageResourceWithProperties toBeChangedStorageResource = DataSourcePoolCreator.createStorageResourceWithoutDataSource(toBeChangedDataSourceProps); StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageResource); staleStorageResource.getStorageNodes().putAll(getToBeDeletedDataSources(resourceMetaData.getStorageNodeMetaData().getDataSources(), toBeChangedStorageResource.getStorageNodes().keySet())); - staleStorageResource.getStorageUnits().putAll(getToBeDeletedStorageUnits(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageResource.getStorageUnits().keySet())); + staleStorageResource.getStorageUnitNodeMappers().putAll( + getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(), toBeChangedStorageResource.getStorageUnitNodeMappers().keySet())); return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageResource), staleStorageResource, toBeChangedDataSourceProps); } private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, final StorageResourceWithProperties toBeChangedStorageResource) { Map storageNodes = getNewStorageNodes(resourceMetaData, toBeChangedStorageResource.getStorageNodes(), toBeChangedStorageResource.getDataSourcePropertiesMap()); - Map storageUnits = getNewStorageUnits(resourceMetaData, toBeChangedStorageResource.getStorageUnits()); - return new StorageResource(storageNodes, storageUnits); + Map storageUnitNodeMappers = getNewStorageUnitNodeMappers(resourceMetaData, toBeChangedStorageResource.getStorageUnitNodeMappers()); + return new StorageResource(storageNodes, storageUnitNodeMappers); } private Map getNewStorageNodes(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageNodes, @@ -103,11 +104,11 @@ private Map getNewStorageNodes(final ResourceMetaData resour return result; } - private Map getNewStorageUnits(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnits) { - Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageUnits()); - result.keySet().removeAll(getToBeDeletedStorageUnits(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnits.keySet()).keySet()); - result.putAll(getChangedStorageUnits(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnits)); - result.putAll(getToBeAddedStorageUnits(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnits)); + private Map getNewStorageUnitNodeMappers(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMappers) { + Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers()); + result.keySet().removeAll(getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(), toBeChangedStorageUnitNodeMappers.keySet()).keySet()); + result.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(), toBeChangedStorageUnitNodeMappers)); + result.putAll(getToBeAddedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(), toBeChangedStorageUnitNodeMappers)); return result; } @@ -137,23 +138,23 @@ private Map getToBeAddedDataSources(final Map reservedStorageUnits = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() - .filter(entry -> !toBeRemovedStorageResource.getStorageUnits().containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - Collection inUsedDataSourceNames = reservedStorageUnits.values().stream().map(StorageUnit::getNodeName).collect(Collectors.toSet()); + Map reservedStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().entrySet().stream() + .filter(entry -> !toBeRemovedStorageResource.getStorageUnitNodeMappers().containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + Collection inUsedDataSourceNames = reservedStorageUnitNodeMappers.values().stream().map(StorageUnitNodeMapper::getNodeName).collect(Collectors.toSet()); Map staleStorageNodes = resourceMetaData.getStorageNodeMetaData().getDataSources().entrySet().stream() .filter(entry -> toBeRemovedStorageResource.getStorageNodes().containsKey(entry.getKey()) && !inUsedDataSourceNames.contains(entry.getKey())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - Map staleStorageUnits = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() - .filter(entry -> !reservedStorageUnits.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - return new StorageResource(staleStorageNodes, staleStorageUnits); + Map staleStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().entrySet().stream() + .filter(entry -> !reservedStorageUnitNodeMappers.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + return new StorageResource(staleStorageNodes, staleStorageUnitNodeMappers); } private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final StorageResourceWithProperties toBeChangedStorageResource) { Map storageNodes = new LinkedHashMap<>(resourceMetaData.getStorageNodeMetaData().getDataSources().size(), 1F); - Map storageUnits = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageUnits().size(), 1F); + Map storageUnitNodeMappers = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().size(), 1F); storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getStorageNodeMetaData().getDataSources(), toBeChangedStorageResource.getDataSourcePropertiesMap())); - storageUnits.putAll(getChangedStorageUnits(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageResource.getStorageUnits())); - return new StorageResource(storageNodes, storageUnits); + storageUnitNodeMappers.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers(), toBeChangedStorageResource.getStorageUnitNodeMappers())); + return new StorageResource(storageNodes, storageUnitNodeMappers); } private Map getToBeChangedDataSources(final Map storageNodes, final Map dataSourcePropertiesMap) { @@ -176,9 +177,10 @@ private Map getToBeDeletedDataSources(final Map getToBeDeletedStorageUnits(final Map storageUnits, final Collection toBeChangedStorageUnitNames) { - Map result = new LinkedHashMap<>(storageUnits.size(), 1F); - for (Entry entry : storageUnits.entrySet()) { + private Map getToBeDeletedStorageUnitNodeMappers(final Map storageUnitNodeMappers, + final Collection toBeChangedStorageUnitNames) { + Map result = new LinkedHashMap<>(storageUnitNodeMappers.size(), 1F); + for (Entry entry : storageUnitNodeMappers.entrySet()) { if (!toBeChangedStorageUnitNames.contains(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } @@ -186,17 +188,20 @@ private Map getToBeDeletedStorageUnits(final Map getChangedStorageUnits(final Map storageUnits, final Map toBeChangedStorageUnits) { - return toBeChangedStorageUnits.entrySet().stream().filter(entry -> isModifiedStorageUnit(storageUnits, entry.getKey(), entry.getValue())) + private Map getChangedStorageUnitNodeMappers(final Map storageUnitNodeMappers, + final Map toBeChangedStorageUnitNodeMappers) { + return toBeChangedStorageUnitNodeMappers.entrySet().stream().filter(entry -> isModifiedStorageUnitNodeMapper(storageUnitNodeMappers, entry.getKey(), entry.getValue())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); } - private boolean isModifiedStorageUnit(final Map originalStorageUnits, final String dataSourceName, final StorageUnit storageUnit) { - return originalStorageUnits.containsKey(dataSourceName) && !storageUnit.equals(originalStorageUnits.get(dataSourceName)); + private boolean isModifiedStorageUnitNodeMapper(final Map originalStorageUnitNodeMappers, + final String dataSourceName, final StorageUnitNodeMapper storageUnitNodeMapper) { + return originalStorageUnitNodeMappers.containsKey(dataSourceName) && !storageUnitNodeMapper.equals(originalStorageUnitNodeMappers.get(dataSourceName)); } - private Map getToBeAddedStorageUnits(final Map storageUnits, final Map toBeChangedStorageUnits) { - return toBeChangedStorageUnits.entrySet().stream() - .filter(entry -> !storageUnits.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + private Map getToBeAddedStorageUnitNodeMappers(final Map storageUnitNodeMappers, + final Map toBeChangedStorageUnitNodeMappers) { + return toBeChangedStorageUnitNodeMappers.entrySet().stream() + .filter(entry -> !storageUnitNodeMappers.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); } } 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 c94bcdb919b72..c4256a64cefcb 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 @@ -215,7 +215,7 @@ void assertAlterRuleConfiguration() { ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); Map dataSources = Collections.singletonMap("ds_0", new MockedDataSource()); when(resourceMetaData.getStorageNodeMetaData().getDataSources()).thenReturn(dataSources); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(StorageUtils.getStorageUnits(dataSources)); + when(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers()).thenReturn(StorageUtils.getStorageUnitNodeMappers(dataSources)); ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), resourceMetaData, mock(RuleMetaData.class), Collections.emptyMap()); when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(database); @@ -245,7 +245,7 @@ private ResourceMetaData createOriginalResource() { originalDataSources.put("ds_2", new MockedDataSource()); when(result.getDataSources()).thenReturn(originalDataSources); when(result.getStorageNodeMetaData().getDataSources()).thenReturn(originalDataSources); - when(result.getStorageUnitMetaData().getStorageUnits()).thenReturn(StorageUtils.getStorageUnits(originalDataSources)); + when(result.getStorageUnitMetaData().getUnitNodeMappers()).thenReturn(StorageUtils.getStorageUnitNodeMappers(originalDataSources)); return result; } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java index d922b52c01063..21dd18e92f7d2 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java @@ -115,7 +115,7 @@ private Map createDatabases() { ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(resourceMetaData.getStorageNodeMetaData().getDataSources()).thenReturn(Collections.emptyMap()); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); + when(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers()).thenReturn(Collections.emptyMap()); when(database.getSchemas()).thenReturn(Collections.singletonMap("foo_schema", new ShardingSphereSchema())); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); when(database.getSchema("foo_schema")).thenReturn(mock(ShardingSphereSchema.class));