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

jianglongtao 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 37d40a4  Add reload meta data in ContextManager (#13752)
37d40a4 is described below

commit 37d40a4ef48d5846b470631ef830b65eb1b72a1a
Author: Haoran Meng <[email protected]>
AuthorDate: Tue Nov 23 15:52:59 2021 +0800

    Add reload meta data in ContextManager (#13752)
    
    Co-authored-by: shardingsphere <[email protected]>
---
 .../mode/manager/ContextManager.java               | 23 ++++++++++++++++++++++
 .../ClusterContextManagerCoordinatorTest.java      |  4 ++--
 2 files changed, 25 insertions(+), 2 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 0acc65b..b2b0cec 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
@@ -252,6 +252,29 @@ public final class ContextManager implements AutoCloseable 
{
         renewMetaDataContexts(rebuildMetaDataContexts(new 
ConfigurationProperties(props)));
     }
     
+    /**
+     * Reload meta data.
+     *
+     * @param schemaName schema name
+     */
+    public void reloadMetaData(final String schemaName) {
+        try {
+            ShardingSphereSchema schema = loadActualSchema(schemaName);
+            alterSchema(schemaName, schema);
+            metaDataContexts.getMetaDataPersistService().ifPresent(optional -> 
optional.getSchemaMetaDataService().persist(schemaName, schema));
+        } catch (final SQLException ex) {
+            log.error("Reload schema:{} meta data failed", schemaName, ex);
+        }
+    }
+    
+    private ShardingSphereSchema loadActualSchema(final String schemaName) 
throws SQLException {
+        Map<String, Map<String, DataSource>> dataSourcesMap = 
Collections.singletonMap(schemaName, 
metaDataContexts.getMetaData(schemaName).getResource().getDataSources());
+        Map<String, Collection<RuleConfiguration>> schemaRuleConfigs = 
Collections.singletonMap(schemaName, 
metaDataContexts.getMetaData(schemaName).getRuleMetaData().getConfigurations());
+        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(dataSourcesMap, schemaRuleConfigs, 
metaDataContexts.getProps().getProps());
+        Map<String, ShardingSphereSchema> schemas = new 
SchemaLoader(dataSourcesMap, schemaRuleConfigs, rules, 
metaDataContexts.getProps().getProps()).load();
+        return schemas.get(schemaName);
+    }
+    
     private Collection<DataSource> getPendingClosedDataSources(final String 
schemaName, final Map<String, DataSourceConfiguration> 
dataSourceConfigurations) {
         Collection<DataSource> result = new LinkedList<>();
         
result.addAll(getDeletedDataSources(metaDataContexts.getMetaData(schemaName), 
dataSourceConfigurations).values());
diff --git 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
index 8385cc4..b8a2b57 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
@@ -163,7 +163,7 @@ public final class ClusterContextManagerCoordinatorTest {
     }
     
     @Test
-    public void assertRuleConfigurationsChanged() throws SQLException {
+    public void assertRuleConfigurationsChanged() {
         assertThat(contextManager.getMetaDataContexts().getMetaData("schema"), 
is(metaData));
         RuleConfigurationsChangedEvent event = new 
RuleConfigurationsChangedEvent("schema", new LinkedList<>());
         coordinator.renew(event);
@@ -177,7 +177,7 @@ public final class ClusterContextManagerCoordinatorTest {
     }
     
     @Test
-    public void assertDataSourceChanged() throws SQLException {
+    public void assertDataSourceChanged() {
         DataSourceChangedEvent event = new DataSourceChangedEvent("schema", 
getChangedDataSourceConfigurations());
         coordinator.renew(event);
         
assertTrue(contextManager.getMetaDataContexts().getMetaData("schema").getResource().getDataSources().containsKey("ds_2"));

Reply via email to