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

panjuan 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 aab226b  refactor refresh table meta data (#14170)
aab226b is described below

commit aab226b72ba574061748d8f94c461ea469f9168f
Author: Haoran Meng <[email protected]>
AuthorDate: Mon Dec 20 16:14:12 2021 +0800

    refactor refresh table meta data (#14170)
---
 .../mode/manager/ContextManager.java               | 12 ++++++++++
 .../metadata/persist/node/SchemaMetaDataNode.java  | 15 +++++++++++-
 .../persist/node/SchemaMetaDataNodeTest.java       |  5 ++++
 .../ClusterContextManagerCoordinator.java          |  2 +-
 .../config/event/schema/SchemaChangedEvent.java    |  6 +++--
 .../metadata/watcher/MetaDataChangedWatcher.java   | 18 ++++++--------
 .../ClusterContextManagerCoordinatorTest.java      | 28 ++++++++++++----------
 7 files changed, 59 insertions(+), 27 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 1def3dc..e373c82 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
@@ -239,6 +239,18 @@ public final class ContextManager implements AutoCloseable 
{
     }
     
     /**
+     * Alter schema.
+     *
+     * @param schemaName schema name
+     * @param table table                  
+     * @param tableMetaData table meta data
+     */
+    public void alterSchema(final String schemaName, final String table, final 
TableMetaData tableMetaData) {
+        metaDataContexts.getMetaData(schemaName).getSchema().put(table, 
tableMetaData);
+        
metaDataContexts.getOptimizerContext().getFederationMetaData().getSchemas().get(schemaName).put(tableMetaData);
+    }
+    
+    /**
      * Alter global rule configuration.
      * 
      * @param ruleConfigurations global rule configuration
diff --git 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNode.java
 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNode.java
index 0a068cb..691123e 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNode.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNode.java
@@ -108,7 +108,7 @@ public final class SchemaMetaDataNode {
      * @return schema name
      */
     public static String getSchemaName(final String configurationNodeFullPath) 
{
-        Pattern pattern = Pattern.compile(getMetaDataNodePath() + 
"/([\\w\\-]+)" + "(/datasources|/rules|/schema)?", Pattern.CASE_INSENSITIVE);
+        Pattern pattern = Pattern.compile(getMetaDataNodePath() + 
"/([\\w\\-]+)" + "(/datasources|/rules|/tables)?", Pattern.CASE_INSENSITIVE);
         Matcher matcher = pattern.matcher(configurationNodeFullPath);
         return matcher.find() ? matcher.group(1) : "";
     }
@@ -124,4 +124,17 @@ public final class SchemaMetaDataNode {
         Matcher matcher = pattern.matcher(schemaPath);
         return matcher.find() ? matcher.group(1) : "";
     }
+    
+    /**
+     * Get table meta data path.
+     * 
+     * @param schemaName schema name
+     * @param tableMetaDataPath table meta data path
+     * @return table name
+     */
+    public static String getTableName(final String schemaName, final String 
tableMetaDataPath) {
+        Pattern pattern = Pattern.compile(getMetaDataTablesPath(schemaName) + 
"/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
+        Matcher matcher = pattern.matcher(tableMetaDataPath);
+        return matcher.find() ? matcher.group(1) : "";
+    }
 }
diff --git 
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNodeTest.java
 
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNodeTest.java
index ee3bc9e..ac0e0f8 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNodeTest.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/SchemaMetaDataNodeTest.java
@@ -54,4 +54,9 @@ public class SchemaMetaDataNodeTest {
     public void assertGetSchemaNameBySchemaPath() {
         
assertThat(SchemaMetaDataNode.getSchemaNameBySchemaPath("/metadata/logic_db"), 
is(DefaultSchema.LOGIC_NAME));
     }
+    
+    @Test
+    public void assertGetTableName() {
+        assertThat(SchemaMetaDataNode.getTableName("logic_db", 
"/metadata/logic_db/tables/t_order"), is("t_order"));
+    }
 }
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 1006b0f..ad706d7 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
@@ -108,7 +108,7 @@ public final class ClusterContextManagerCoordinator {
      */
     @Subscribe
     public synchronized void renew(final SchemaChangedEvent event) {
-        contextManager.alterSchema(event.getSchemaName(), event.getSchema());
+        contextManager.alterSchema(event.getSchemaName(), event.getTable(), 
event.getTableMetaData());
     }
     
     /**
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/registry/config/event/schema/SchemaChangedEvent.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/event/schema/SchemaChangedEvent.java
index 13093a9..c5fe108 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/event/schema/SchemaChangedEvent.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/event/schema/SchemaChangedEvent.java
@@ -19,8 +19,8 @@ package 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.conf
 
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceEvent;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 
 /**
  * Schema changed event.
@@ -30,6 +30,8 @@ import 
org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 public final class SchemaChangedEvent implements GovernanceEvent {
 
     private final String schemaName;
+    
+    private final String table;
 
-    private final ShardingSphereSchema schema;
+    private final TableMetaData tableMetaData;
 }
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/registry/metadata/watcher/MetaDataChangedWatcher.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java
index 55f05fb..cc98447 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java
@@ -24,8 +24,8 @@ import 
org.apache.shardingsphere.infra.yaml.config.pojo.YamlRuleConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.YamlDataSourceConfigurationSwapper;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapperEngine;
 import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlSchema;
-import org.apache.shardingsphere.infra.yaml.schema.swapper.SchemaYamlSwapper;
+import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlTableMetaData;
+import 
org.apache.shardingsphere.infra.yaml.schema.swapper.TableMetaDataYamlSwapper;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceEvent;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent;
@@ -103,8 +103,9 @@ public final class MetaDataChangedWatcher implements 
GovernanceWatcher<Governanc
         if (isRuleChangedEvent(schemaName, event.getKey())) {
             return Optional.of(createRuleChangedEvent(schemaName, event));
         }
-        if (isSchemaChangedEvent(schemaName, event.getKey())) {
-            return Optional.of(createSchemaChangedEvent(schemaName, event));
+        String table = SchemaMetaDataNode.getTableName(schemaName, 
event.getKey());
+        if (!Strings.isNullOrEmpty(table)) {
+            return Optional.of(createSchemaChangedEvent(schemaName, table, 
event));
         }
         return Optional.empty();
     }
@@ -141,12 +142,7 @@ public final class MetaDataChangedWatcher implements 
GovernanceWatcher<Governanc
         return new 
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rules);
     }
     
-    private boolean isSchemaChangedEvent(final String schemaName, final String 
key) {
-        // TODO refresh meta data
-        return false;
-    }
-    
-    private GovernanceEvent createSchemaChangedEvent(final String schemaName, 
final DataChangedEvent event) {
-        return new SchemaChangedEvent(schemaName, new 
SchemaYamlSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), 
YamlSchema.class)));
+    private GovernanceEvent createSchemaChangedEvent(final String schemaName, 
final String table, final DataChangedEvent event) {
+        return new SchemaChangedEvent(schemaName, table, new 
TableMetaDataYamlSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), 
YamlTableMetaData.class)));
     }
 }
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 72ae990..d147e3a 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
@@ -34,6 +34,7 @@ import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import 
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.infra.metadata.schema.QualifiedSchema;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import 
org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContext;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -78,8 +79,10 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -149,18 +152,11 @@ public final class ClusterContextManagerCoordinatorTest {
     
     @Test
     public void assertSchemaChanged() {
-        SchemaChangedEvent event = new SchemaChangedEvent("schema", 
mock(ShardingSphereSchema.class));
-        ShardingSphereMetaData metaData = 
contextManager.getMetaDataContexts().getMetaData("schema");
+        SchemaChangedEvent event = new SchemaChangedEvent("schema", "t_order", 
mock(TableMetaData.class));
+        TableMetaData tableMetaData = 
contextManager.getMetaDataContexts().getMetaData("schema").getSchema().get("t_order");
         coordinator.renew(event);
         
assertTrue(contextManager.getMetaDataContexts().getAllSchemaNames().contains("schema"));
-        assertThat(contextManager.getMetaDataContexts().getMetaData("schema"), 
not(metaData));
-    }
-    
-    @Test
-    public void assertSchemaChangedWithExistSchema() {
-        SchemaChangedEvent event = new SchemaChangedEvent("schema", 
mock(ShardingSphereSchema.class));
-        coordinator.renew(event);
-        assertThat(contextManager.getMetaDataContexts().getMetaData("schema"), 
not(metaData));
+        
verify(contextManager.getMetaDataContexts().getMetaData("schema").getSchema()).put(eq("t_order"),
 eq(event.getTableMetaData()));
     }
     
     @Test
@@ -237,9 +233,17 @@ public final class ClusterContextManagerCoordinatorTest {
         when(metaData.getName()).thenReturn("schema");
         ShardingSphereResource resource = mock(ShardingSphereResource.class);
         when(metaData.getResource()).thenReturn(resource);
-        
when(metaData.getSchema()).thenReturn(mock(ShardingSphereSchema.class));
+        ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+        when(schema.get("t_order")).thenReturn(mock(TableMetaData.class));
+        when(metaData.getSchema()).thenReturn(schema);
         
when(metaData.getRuleMetaData().getRules()).thenReturn(Collections.emptyList());
         
when(metaData.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList());
         return Collections.singletonMap("schema", metaData);
     }
-}
+    
+    private Map<String, TableMetaData> createTableMetaData() {
+        Map<String, TableMetaData> result = new HashMap<>(1, 1);
+        result.put("t_order", mock(TableMetaData.class));
+        return result;
+    }
+} 

Reply via email to