You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/08/05 06:04:56 UTC

[shardingsphere] branch master updated: Decouple MasterSlaveRule (#6627)

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

zhangliang 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 c96f06d  Decouple MasterSlaveRule (#6627)
c96f06d is described below

commit c96f06d5732c345adc7f119477bffdd44fd05126
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Wed Aug 5 14:04:44 2020 +0800

    Decouple MasterSlaveRule (#6627)
---
 .../core/schema/OrchestrationSchemaContexts.java      | 19 +++++++++----------
 .../masterslave/rule/MasterSlaveRule.java             |  4 ++++
 .../masterslave/rule/MasterSlaveRuleTest.java         | 15 +++++++++++++++
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java
index 63974ef..8d67325 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.orchestration.core.schema;
 
-import com.google.common.base.Joiner;
 import com.google.common.eventbus.Subscribe;
 import org.apache.shardingsphere.cluster.facade.ClusterFacade;
 import org.apache.shardingsphere.cluster.facade.init.ClusterInitFacade;
@@ -42,7 +41,6 @@ import org.apache.shardingsphere.kernel.context.StandardSchemaContexts;
 import org.apache.shardingsphere.kernel.context.runtime.RuntimeContext;
 import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter;
 import org.apache.shardingsphere.kernel.context.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule;
 import org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;
 import org.apache.shardingsphere.metrics.facade.MetricsTrackerManagerFacade;
 import org.apache.shardingsphere.orchestration.core.common.event.AuthenticationChangedEvent;
@@ -88,21 +86,22 @@ public abstract class OrchestrationSchemaContexts implements SchemaContexts {
         persistMetaData();
     }
     
-    // TODO decouple masterslave rule
     private void disableDataSources() {
         Collection<String> disabledDataSources = orchestrationFacade.getRegistryCenter().loadDisabledDataSources();
         if (!disabledDataSources.isEmpty()) {
             schemaContexts.getSchemaContexts().forEach((key, value)
-                -> value.getSchema().getRules().stream().filter(each -> each instanceof MasterSlaveRule).forEach(each -> disableDataSources((MasterSlaveRule) each, disabledDataSources, key)));
+                -> value.getSchema().getRules().stream().filter(each -> each instanceof StatusContainedRule).forEach(each -> disableDataSources((StatusContainedRule) each, disabledDataSources, key)));
         }
     }
     
-    private void disableDataSources(final MasterSlaveRule masterSlaveRule, final Collection<String> disabledDataSources, final String schemaName) {
-        masterSlaveRule.getSingleDataSourceRule().getSlaveDataSourceNames().forEach(each -> {
-            if (disabledDataSources.contains(Joiner.on(".").join(schemaName, each))) {
-                masterSlaveRule.updateRuleStatus(new DataSourceNameDisabledEvent(each, true));
-            }
-        });
+    private void disableDataSources(final StatusContainedRule statusContainedRule, final Collection<String> disabledDataSources, final String schemaName) {
+        disabledDataSources.stream().filter(each -> each.startsWith(schemaName)).map(this::getDataSourceName).forEach(each ->
+                statusContainedRule.updateRuleStatus(new DataSourceNameDisabledEvent(each, true))
+        );
+    }
+    
+    private String getDataSourceName(final String disableDataSource) {
+        return new OrchestrationSchema(disableDataSource).getDataSourceName();
     }
     
     private void persistMetaData() {
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/rule/MasterSlaveRule.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/rule/MasterSlaveRule.java
index 0b626e3..b0f4606 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/rule/MasterSlaveRule.java
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/rule/MasterSlaveRule.java
@@ -106,7 +106,11 @@ public final class MasterSlaveRule implements DataSourceRoutedRule, StatusContai
     @Override
     public void updateRuleStatus(final RuleChangedEvent event) {
         if (event instanceof DataSourceNameDisabledEvent) {
+            String dataSourceName = ((DataSourceNameDisabledEvent) event).getDataSourceName();
             for (Entry<String, MasterSlaveDataSourceRule> entry : dataSourceRules.entrySet()) {
+                if (!entry.getValue().getSlaveDataSourceNames().contains(dataSourceName)) {
+                    continue;
+                }
                 entry.getValue().updateDisabledDataSourceNames(((DataSourceNameDisabledEvent) event).getDataSourceName(), ((DataSourceNameDisabledEvent) event).isDisabled());
             }
         }
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/test/java/org/apache/shardingsphere/masterslave/rule/MasterSlaveRuleTest.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/test/java/org/apache/shardingsphere/masterslave/rule/MasterSlaveRuleTest.java
index 6b47e01..7802a95 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/test/java/org/apache/shardingsphere/masterslave/rule/MasterSlaveRuleTest.java
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/test/java/org/apache/shardingsphere/masterslave/rule/MasterSlaveRuleTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.masterslave.rule;
 
 import com.google.common.collect.ImmutableMap;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.rule.event.impl.DataSourceNameDisabledEvent;
 import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
 import org.apache.shardingsphere.masterslave.api.config.rule.MasterSlaveDataSourceRuleConfiguration;
 import org.junit.Test;
@@ -63,4 +64,18 @@ public final class MasterSlaveRuleTest {
         assertThat(actual.getSlaveDataSourceNames(), is(Arrays.asList("slave_db_0", "slave_db_1")));
         assertThat(actual.getLoadBalancer().getType(), is("RANDOM"));
     }
+    
+    @Test
+    public void assertUpdateRuleStatusWithNotExistDataSource() {
+        MasterSlaveRule masterSlaveRule = createMasterSlaveRule();
+        masterSlaveRule.updateRuleStatus(new DataSourceNameDisabledEvent("slave_db", true));
+        assertThat(masterSlaveRule.getSingleDataSourceRule().getSlaveDataSourceNames(), is(Arrays.asList("slave_db_0", "slave_db_1")));
+    }
+    
+    @Test
+    public void assertUpdateRuleStatus() {
+        MasterSlaveRule masterSlaveRule = createMasterSlaveRule();
+        masterSlaveRule.updateRuleStatus(new DataSourceNameDisabledEvent("slave_db_0", true));
+        assertThat(masterSlaveRule.getSingleDataSourceRule().getSlaveDataSourceNames(), is(Arrays.asList("slave_db_1")));
+    }
 }