diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java index 1caf0699e071d..c65ea02dbc2c6 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java @@ -19,37 +19,21 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; -import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; -import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; -import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder; -import org.apache.shardingsphere.metadata.factory.ExternalMetaDataFactory; -import org.apache.shardingsphere.metadata.factory.InternalMetaDataFactory; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.mode.spi.PersistRepository; -import javax.sql.DataSource; import java.sql.SQLException; -import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; /** * Storage unit manager. @@ -123,85 +107,13 @@ public synchronized void unregisterStorageUnit(final String databaseName, final } private void buildNewMetaDataContext(final String databaseName, final SwitchingResource switchingResource) throws SQLException { - MetaDataContexts reloadMetaDataContexts = createMetaDataContexts(databaseName, true, switchingResource, null); + MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createBySwitchResource(databaseName, true, + switchingResource, metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext); metaDataContexts.set(reloadMetaDataContexts); metaDataContexts.get().getMetaData().getDatabases().putAll(buildShardingSphereDatabase(reloadMetaDataContexts.getMetaData().getDatabase(databaseName))); switchingResource.closeStaleDataSources(); } - private MetaDataContexts createMetaDataContexts(final String databaseName, final boolean internalLoadMetaData, - final SwitchingResource switchingResource, final Collection ruleConfigs) throws SQLException { - Map changedDatabases = createChangedDatabases(databaseName, internalLoadMetaData, switchingResource, ruleConfigs); - ConfigurationProperties props = metaDataContexts.get().getMetaData().getProps(); - RuleMetaData changedGlobalMetaData = new RuleMetaData( - GlobalRulesBuilder.buildRules(metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases, props)); - return newMetaDataContexts(new ShardingSphereMetaData(changedDatabases, metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); - } - - private synchronized Map createChangedDatabases(final String databaseName, final boolean internalLoadMetaData, - final SwitchingResource switchingResource, final Collection ruleConfigs) throws SQLException { - ResourceMetaData effectiveResourceMetaData = getEffectiveResourceMetaData(metaDataContexts.get().getMetaData().getDatabase(databaseName), switchingResource); - Collection toBeCreatedRuleConfigs = null == ruleConfigs - ? metaDataContexts.get().getMetaData().getDatabase(databaseName).getRuleMetaData().getConfigurations() - : ruleConfigs; - DatabaseConfiguration toBeCreatedDatabaseConfig = getDatabaseConfiguration(effectiveResourceMetaData, switchingResource, toBeCreatedRuleConfigs); - ShardingSphereDatabase changedDatabase = createChangedDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName).getName(), internalLoadMetaData, - metaDataPersistService, toBeCreatedDatabaseConfig, metaDataContexts.get().getMetaData().getProps(), computeNodeInstanceContext); - Map result = new LinkedHashMap<>(metaDataContexts.get().getMetaData().getDatabases()); - result.put(databaseName.toLowerCase(), changedDatabase); - return result; - } - - private ResourceMetaData getEffectiveResourceMetaData(final ShardingSphereDatabase database, final SwitchingResource resource) { - Map storageNodes = getStorageNodes(database.getResourceMetaData().getDataSources(), resource); - Map storageUnits = getStorageUnits(database.getResourceMetaData().getStorageUnits(), resource); - return new ResourceMetaData(storageNodes, storageUnits); - } - - private Map getStorageNodes(final Map currentStorageNodes, final SwitchingResource resource) { - Map result = new LinkedHashMap<>(currentStorageNodes.size(), 1F); - for (Entry entry : currentStorageNodes.entrySet()) { - if (null == resource || !resource.getStaleDataSources().containsKey(entry.getKey())) { - result.put(entry.getKey(), entry.getValue()); - } - } - return result; - } - - private Map getStorageUnits(final Map currentStorageUnits, final SwitchingResource resource) { - Map result = new LinkedHashMap<>(currentStorageUnits.size(), 1F); - for (Entry entry : currentStorageUnits.entrySet()) { - if (null == resource || !resource.getStaleStorageUnitNames().contains(entry.getKey())) { - result.put(entry.getKey(), entry.getValue()); - } - } - return result; - } - - private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData resourceMetaData, final SwitchingResource switchingResource, - final Collection toBeCreatedRuleConfigs) { - Map propsMap = null == switchingResource ? resourceMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)) - : switchingResource.getMergedDataSourcePoolPropertiesMap(); - return new DataSourceProvidedDatabaseConfiguration(getMergedStorageNodeDataSources(resourceMetaData, switchingResource), toBeCreatedRuleConfigs, propsMap); - } - - private Map getMergedStorageNodeDataSources(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { - Map result = currentResourceMetaData.getDataSources(); - if (null != switchingResource && !switchingResource.getNewDataSources().isEmpty()) { - result.putAll(switchingResource.getNewDataSources()); - } - return result; - } - - private ShardingSphereDatabase createChangedDatabase(final String databaseName, final boolean internalLoadMetaData, final MetaDataPersistService persistService, - final DatabaseConfiguration databaseConfig, final ConfigurationProperties props, - final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException { - return internalLoadMetaData - ? InternalMetaDataFactory.create(databaseName, persistService, databaseConfig, props, computeNodeInstanceContext) - : ExternalMetaDataFactory.create(databaseName, databaseConfig, props, computeNodeInstanceContext); - } - private Map buildShardingSphereDatabase(final ShardingSphereDatabase originalDatabase) { return Collections.singletonMap(originalDatabase.getName().toLowerCase(), new ShardingSphereDatabase(originalDatabase.getName(), originalDatabase.getProtocolType(), originalDatabase.getResourceMetaData(), originalDatabase.getRuleMetaData(), buildSchemas(originalDatabase))); @@ -223,8 +135,4 @@ private void closeStaleRules(final String databaseName) { } } } - - private MetaDataContexts newMetaDataContexts(final ShardingSphereMetaData metaData) { - return MetaDataContextsFactory.create(metaDataPersistService, metaData); - } }