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

zhaojinchao 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 1bd68a49168 fix add resource cannot be synchronized with cluster mode 
(#18479)
1bd68a49168 is described below

commit 1bd68a4916864bbdafe041383567664b0ed6dc66
Author: Haoran Meng <[email protected]>
AuthorDate: Tue Jun 21 11:57:42 2022 +0800

    fix add resource cannot be synchronized with cluster mode (#18479)
---
 .../metadata/watcher/MetaDataChangedWatcher.java        | 17 +++++++++--------
 .../metadata/watcher/MetaDataChangedWatcherTest.java    | 15 +++++++++++++++
 2 files changed, 24 insertions(+), 8 deletions(-)

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 f3826e806a1..90fd7734704 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
@@ -80,10 +80,7 @@ public final class MetaDataChangedWatcher implements 
GovernanceWatcher<Governanc
         if (isTableMetaDataChanged(event)) {
             return createSchemaChangedEvent(event);
         }
-        if (Type.UPDATED == event.getType()) {
-            return createUpdateGovernanceEvent(event);
-        }
-        return Optional.empty();
+        return createRuleAndDataSourceChangedEvent(event);
     }
     
     private boolean isLogicDatabaseChanged(final DataChangedEvent event) {
@@ -128,19 +125,23 @@ public final class MetaDataChangedWatcher implements 
GovernanceWatcher<Governanc
         return Optional.empty();
     }
     
-    private Optional<GovernanceEvent> createUpdateGovernanceEvent(final 
DataChangedEvent event) {
+    private Optional<GovernanceEvent> 
createRuleAndDataSourceChangedEvent(final DataChangedEvent event) {
         Optional<String> databaseName = 
DatabaseMetaDataNode.getDatabaseNameByDatabasePath(event.getKey());
         if (!databaseName.isPresent() || 
Strings.isNullOrEmpty(event.getValue())) {
             return Optional.empty();
         }
-        if 
(event.getKey().equals(DatabaseMetaDataNode.getActiveVersionPath(databaseName.get())))
 {
+        if (event.getType() == Type.UPDATED && 
event.getKey().equals(DatabaseMetaDataNode.getActiveVersionPath(databaseName.get())))
 {
             return Optional.of(new 
DatabaseVersionChangedEvent(databaseName.get(), event.getValue()));
         }
         Optional<String> databaseVersion = 
DatabaseMetaDataNode.getVersionByDataSourcesPath(event.getKey());
-        if (databaseVersion.isPresent()) {
+        if (databaseVersion.isPresent() && event.getType() != Type.DELETED) {
             return 
Optional.of(createDataSourceChangedEvent(databaseName.get(), 
databaseVersion.get(), event));
         }
-        return 
DatabaseMetaDataNode.getVersionByRulesPath(event.getKey()).map(optional -> new 
RuleConfigurationsChangedEvent(databaseName.get(), optional, 
getRuleConfigurations(event.getValue())));
+        databaseVersion = 
DatabaseMetaDataNode.getVersionByRulesPath(event.getKey());
+        if (databaseVersion.isPresent() && event.getType() != Type.DELETED) {
+            return Optional.of(new 
RuleConfigurationsChangedEvent(databaseName.get(), databaseVersion.get(), 
getRuleConfigurations(event.getValue())));
+        }
+        return Optional.empty();
     }
     
     @SuppressWarnings("unchecked")
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/registry/metadata/watcher/MetaDataChangedWatcherTest.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcherTest.java
index 4407b02707b..3fda06df10f 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcherTest.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcherTest.java
@@ -81,6 +81,14 @@ public final class MetaDataChangedWatcherTest {
         assertTrue(actual.isPresent());
     }
     
+    @Test
+    public void assertCreateDataSourceChangedEventWithAddEvent() {
+        String key = "/metadata/sharding_db/versions/0/dataSources";
+        String value = "{}";
+        Optional<GovernanceEvent> actual = createEvent(key, value, Type.ADDED);
+        assertTrue(actual.isPresent());
+    }
+    
     @Test
     public void assertCreateRuleChangedEvent() {
         String key = "/metadata/sharding_db/versions/0/rules";
@@ -88,6 +96,13 @@ public final class MetaDataChangedWatcherTest {
         assertTrue(actual.isPresent());
     }
     
+    @Test
+    public void assertCreateRuleChangedEventWithAddEvent() {
+        String key = "/metadata/sharding_db/versions/0/rules";
+        Optional<GovernanceEvent> actual = createEvent(key, "[]", Type.ADDED);
+        assertTrue(actual.isPresent());
+    }
+    
     @Test
     public void assertCreateTableSchemaChangedEvent() {
         String key = 
"/metadata/sharding_db/schemas/sharding_schema/tables/t_order";

Reply via email to