This is an automated email from the ASF dual-hosted git repository.

zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 1ecfc02  Add alter rule configuration in ContextManager (#13663)
1ecfc02 is described below

commit 1ecfc021f35af304751de2fb47318c7c64e96b67
Author: Haoran Meng <[email protected]>
AuthorDate: Wed Nov 17 18:50:17 2021 +0800

    Add alter rule configuration in ContextManager (#13663)
---
 .../mode/manager/ContextManager.java               | 34 ++++++++++++++++++++++
 .../ClusterContextManagerCoordinator.java          | 20 ++-----------
 2 files changed, 36 insertions(+), 18 deletions(-)

diff --git 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index 6c510fc..e3b236b 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -182,10 +182,34 @@ public final class ContextManager implements 
AutoCloseable {
                     
changedMetaDataContext.getOptimizerContext().getMetaData().getSchemas().get(schemaName));
             metaDataContexts.getMetaDataMap().put(schemaName, 
changedMetaDataContext.getMetaData(schemaName));
         } catch (final SQLException ex) {
+            log.error("Alter schema:{} rule failed", schemaName, ex);
+        }
+    }
+    
+    /**
+     * Alter rule configuration.
+     * 
+     * @param schemaName schema name
+     * @param ruleConfigs collection of rule configurations
+     */
+    public void alterRuleConfiguration(final String schemaName, final 
Collection<RuleConfiguration> ruleConfigs) {
+        try {
+            MetaDataContexts changedMetaDataContexts = 
buildChangedMetaDataContext(metaDataContexts.getMetaDataMap().get(schemaName), 
ruleConfigs);
+            
metaDataContexts.getOptimizerContext().getMetaData().getSchemas().putAll(changedMetaDataContexts.getOptimizerContext().getMetaData().getSchemas());
+            Map<String, ShardingSphereMetaData> metaDataMap = new 
HashMap<>(metaDataContexts.getMetaDataMap());
+            metaDataMap.putAll(changedMetaDataContexts.getMetaDataMap());
+            renewMetaDataContexts(rebuildMetaDataContexts(metaDataMap));
+        } catch (final SQLException ex) {
             log.error("Alter schema:{} rule configuration failed", schemaName, 
ex);
         }
     }
     
+    private MetaDataContexts rebuildMetaDataContexts(final Map<String, 
ShardingSphereMetaData> schemaMetaData) {
+        return new 
MetaDataContexts(metaDataContexts.getMetaDataPersistService().orElse(null),
+                schemaMetaData, metaDataContexts.getGlobalRuleMetaData(), 
metaDataContexts.getExecutorEngine(),
+                metaDataContexts.getProps(), 
metaDataContexts.getOptimizerContext());
+    }
+    
     private void refreshMetaDataContext(final String schemaName, final 
Map<String, DataSourceConfiguration> dataSourceConfigs) throws SQLException {
         MetaDataContexts changedMetaDataContext = 
buildChangedMetaDataContext(metaDataContexts.getMetaDataMap().get(schemaName), 
dataSourceConfigs);
         
metaDataContexts.getMetaDataMap().putAll(changedMetaDataContext.getMetaDataMap());
@@ -207,6 +231,16 @@ public final class ContextManager implements AutoCloseable 
{
                 
.build(metaDataContexts.getMetaDataPersistService().orElse(null));
     }
     
+    private MetaDataContexts buildChangedMetaDataContext(final 
ShardingSphereMetaData originalMetaData, final Collection<RuleConfiguration> 
ruleConfigs) throws SQLException {
+        Map<String, Map<String, DataSource>> dataSourcesMap = 
Collections.singletonMap(originalMetaData.getName(), 
originalMetaData.getResource().getDataSources());
+        Map<String, Collection<RuleConfiguration>> schemaRuleConfigs = 
Collections.singletonMap(originalMetaData.getName(), ruleConfigs);
+        Properties props = metaDataContexts.getProps().getProps();
+        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(dataSourcesMap, schemaRuleConfigs, props);
+        Map<String, ShardingSphereSchema> schemas = new 
SchemaLoader(dataSourcesMap, schemaRuleConfigs, rules, props).load();
+        return new MetaDataContextsBuilder(dataSourcesMap, schemaRuleConfigs, 
metaDataContexts.getGlobalRuleMetaData().getConfigurations(), schemas, rules, 
props)
+                
.build(metaDataContexts.getMetaDataPersistService().orElse(null));
+    }
+    
     private void renewTransactionContext(final String schemaName, final 
ShardingSphereResource resource) {
         closeTransactionEngine(schemaName);
         transactionContexts.getEngines().put(schemaName, 
createNewEngine(resource.getDatabaseType(), resource.getDataSources()));
diff --git 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
index e04f4d3..bafb2ec 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
@@ -160,16 +160,10 @@ public final class ClusterContextManagerCoordinator {
      * Renew rule configurations.
      *
      * @param event rule configurations changed event
-     * @throws SQLException SQL exception
      */
     @Subscribe
-    public synchronized void renew(final RuleConfigurationsChangedEvent event) 
throws SQLException {
-        String schemaName = event.getSchemaName();
-        MetaDataContexts changedMetaDataContexts = 
buildChangedMetaDataContext(contextManager.getMetaDataContexts().getMetaDataMap().get(schemaName),
 event.getRuleConfigurations());
-        
contextManager.getMetaDataContexts().getOptimizerContext().getMetaData().getSchemas().putAll(changedMetaDataContexts.getOptimizerContext().getMetaData().getSchemas());
-        Map<String, ShardingSphereMetaData> metaDataMap = new 
HashMap<>(contextManager.getMetaDataContexts().getMetaDataMap());
-        metaDataMap.putAll(changedMetaDataContexts.getMetaDataMap());
-        
contextManager.renewMetaDataContexts(rebuildMetaDataContexts(metaDataMap));
+    public synchronized void renew(final RuleConfigurationsChangedEvent event) 
{
+        contextManager.alterRuleConfiguration(event.getSchemaName(), 
event.getRuleConfigurations());
     }
     
     /**
@@ -266,16 +260,6 @@ public final class ClusterContextManagerCoordinator {
         }
     }
     
-    private MetaDataContexts buildChangedMetaDataContext(final 
ShardingSphereMetaData originalMetaData, final Collection<RuleConfiguration> 
ruleConfigs) throws SQLException {
-        Map<String, Map<String, DataSource>> dataSourcesMap = 
Collections.singletonMap(originalMetaData.getName(), 
originalMetaData.getResource().getDataSources());
-        Map<String, Collection<RuleConfiguration>> schemaRuleConfigs = 
Collections.singletonMap(originalMetaData.getName(), ruleConfigs);
-        Properties props = 
contextManager.getMetaDataContexts().getProps().getProps();
-        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(dataSourcesMap, schemaRuleConfigs, props);
-        Map<String, ShardingSphereSchema> schemas = new 
SchemaLoader(dataSourcesMap, schemaRuleConfigs, rules, props).load();
-        
metaDataPersistService.getSchemaMetaDataService().persist(originalMetaData.getName(),
 schemas.get(originalMetaData.getName()));
-        return new MetaDataContextsBuilder(dataSourcesMap, schemaRuleConfigs, 
metaDataPersistService.getGlobalRuleService().load(), schemas, rules, 
props).build(metaDataPersistService);
-    }
-    
     private MetaDataContexts buildChangedMetaDataContext(final 
ShardingSphereMetaData originalMetaData, final Map<String, 
DataSourceConfiguration> newDataSourceConfigs) throws SQLException {
         Collection<String> deletedDataSources = 
getDeletedDataSources(originalMetaData, newDataSourceConfigs).keySet();
         Map<String, DataSource> changedDataSources = 
buildChangedDataSources(originalMetaData, newDataSourceConfigs);

Reply via email to