diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java index 338b12a6b3491..11ffbaf5b06ca 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java @@ -224,6 +224,29 @@ public static MetaDataContexts createBySwitchResource(final String databaseName, return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, orginalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); } + /** + * Create meta data contexts by alter rule. + * + * @param databaseName database name + * @param internalLoadMetaData internal load meta data + * @param ruleConfigs rule configs + * @param orginalMetaDataContexts original meta data contexts + * @param metaDataPersistService meta data persist service + * @param computeNodeInstanceContext compute node instance context + * @return meta data contexts + * @throws SQLException SQL exception + */ + public static MetaDataContexts createByAlterRule(final String databaseName, final boolean internalLoadMetaData, final Collection ruleConfigs, + final MetaDataContexts orginalMetaDataContexts, final MetaDataPersistService metaDataPersistService, + final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException { + Map changedDatabases = + createChangedDatabases(databaseName, internalLoadMetaData, null, ruleConfigs, orginalMetaDataContexts, metaDataPersistService, computeNodeInstanceContext); + ConfigurationProperties props = orginalMetaDataContexts.getMetaData().getProps(); + RuleMetaData changedGlobalMetaData = new RuleMetaData( + GlobalRulesBuilder.buildRules(orginalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases, props)); + return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, orginalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); + } + /** * Create changed databases by switch resource. * diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java index 224e8cdedf540..665fc12296c52 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java @@ -18,39 +18,26 @@ package org.apache.shardingsphere.mode.metadata.manager; 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.config.rule.scope.DatabaseRuleConfiguration; -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.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.rule.PartialRuleUpdateSupported; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; -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.LinkedHashMap; import java.util.LinkedList; import java.util.Map; -import java.util.Map.Entry; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; /** * Database rule configuration manager. @@ -120,89 +107,15 @@ public synchronized void dropRuleConfiguration(final String databaseName, final private void refreshMetadata(final String databaseName, final ShardingSphereDatabase database, final Collection rules) throws SQLException { database.getRuleMetaData().getRules().clear(); database.getRuleMetaData().getRules().addAll(rules); - MetaDataContexts reloadMetaDataContexts = createMetaDataContextsByAlterRule(databaseName, database.getRuleMetaData().getConfigurations()); + MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createByAlterRule(databaseName, false, + database.getRuleMetaData().getConfigurations(), metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext); metaDataContexts.set(reloadMetaDataContexts); metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchemas().putAll(buildShardingSphereSchemas(metaDataContexts.get().getMetaData().getDatabase(databaseName))); } - private MetaDataContexts createMetaDataContextsByAlterRule(final String databaseName, final Collection ruleConfigs) throws SQLException { - Map changedDatabases = createChangedDatabases(databaseName, false, null, ruleConfigs); - return newMetaDataContexts(new ShardingSphereMetaData(changedDatabases, metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), - metaDataContexts.get().getMetaData().getGlobalRuleMetaData(), metaDataContexts.get().getMetaData().getProps())); - } - - 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 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 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 buildShardingSphereSchemas(final ShardingSphereDatabase database) { Map result = new LinkedHashMap<>(database.getSchemas().size(), 1F); database.getSchemas().forEach((key, value) -> result.put(key, new ShardingSphereSchema(value.getTables(), value.getViews()))); return result; } - - private MetaDataContexts newMetaDataContexts(final ShardingSphereMetaData metaData) { - return MetaDataContextsFactory.create(metaDataPersistService, metaData); - } }