You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/09/06 07:42:51 UTC

[shardingsphere] branch master updated: Refactor DatabaseDiscoveryDataSourceRule (#12235)

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

duanzhengqiang 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 f4e5cad  Refactor DatabaseDiscoveryDataSourceRule (#12235)
f4e5cad is described below

commit f4e5cad98af542b3228070f8bb1bf942570c5b97
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Sep 6 15:42:23 2021 +0800

    Refactor DatabaseDiscoveryDataSourceRule (#12235)
    
    * Rename DatabaseDiscoveryType.checkDatabaseDiscoveryConfiguration
    
    * Refactor DatabaseDiscoveryDataSourceRule
---
 .../dbdiscovery/spi/DatabaseDiscoveryType.java     |  4 ++--
 .../rule/DatabaseDiscoveryDataSourceRule.java      | 22 +++++++++++++---------
 .../dbdiscovery/rule/DatabaseDiscoveryRule.java    | 18 +++++++++++-------
 .../fixture/TestDatabaseDiscoveryType.java         |  2 +-
 .../rule/DatabaseDiscoveryDataSourceRuleTest.java  |  8 ++++----
 .../dbdiscovery/mgr/MGRDatabaseDiscoveryType.java  |  2 +-
 .../mgr/MGRDatabaseDiscoveryTypeTest.java          |  2 +-
 .../distsql/fixture/TestDatabaseDiscoveryType.java |  2 +-
 8 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
index de370d2..430cff0 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
@@ -30,13 +30,13 @@ import java.util.Map;
 public interface DatabaseDiscoveryType extends ShardingSphereAlgorithm {
     
     /**
-     * Check database discovery config.
+     * Check database discovery configuration.
      *
      * @param schemaName schema name
      * @param dataSourceMap data source map
      * @throws SQLException SQL Exception
      */
-    void checkDatabaseDiscoveryConfig(String schemaName, Map<String, DataSource> dataSourceMap) throws SQLException;
+    void checkDatabaseDiscoveryConfiguration(String schemaName, Map<String, DataSource> dataSourceMap) throws SQLException;
     
     /**
      * Update primary data source.
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRule.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRule.java
index 3bc1888..74c736f 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRule.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRule.java
@@ -78,17 +78,21 @@ public final class DatabaseDiscoveryDataSourceRule {
     }
     
     /**
-     * Update disabled data source names.
+     * Disable data source.
      *
-     * @param dataSourceName data source name
-     * @param isDisabled is disabled
+     * @param dataSourceName data source name to be disabled
+     */
+    public void disableDataSource(final String dataSourceName) {
+        disabledDataSourceNames.add(dataSourceName);
+    }
+    
+    /**
+     * Enable data source.
+     *
+     * @param dataSourceName data source name to be enabled
      */
-    public void updateDisabledDataSourceNames(final String dataSourceName, final boolean isDisabled) {
-        if (isDisabled) {
-            disabledDataSourceNames.add(dataSourceName);
-        } else {
-            disabledDataSourceNames.remove(dataSourceName);
-        }
+    public void enableDataSource(final String dataSourceName) {
+        disabledDataSourceNames.remove(dataSourceName);
     }
     
     /**
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
index 7310038..2d68ecc 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
@@ -107,16 +107,16 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
     private void startMonitor(final String schemaName, final Map<String, DataSource> dataSourceMap) {
         for (Entry<String, DatabaseDiscoveryDataSourceRule> entry : dataSourceRules.entrySet()) {
             String groupName = entry.getKey();
-            DatabaseDiscoveryDataSourceRule dbDiscoveryDataSourceRule = entry.getValue();
-            DatabaseDiscoveryType databaseDiscoveryType = dbDiscoveryDataSourceRule.getDatabaseDiscoveryType();
+            DatabaseDiscoveryDataSourceRule dataSourceRule = entry.getValue();
+            DatabaseDiscoveryType databaseDiscoveryType = dataSourceRule.getDatabaseDiscoveryType();
             Map<String, DataSource> originalDataSourceMap = new HashMap<>(dataSourceMap);
-            Collection<String> disabledDataSourceNames = dbDiscoveryDataSourceRule.getDisabledDataSourceNames();
-            String primaryDataSourceName = dbDiscoveryDataSourceRule.getPrimaryDataSourceName();
+            Collection<String> disabledDataSourceNames = dataSourceRule.getDisabledDataSourceNames();
+            String primaryDataSourceName = dataSourceRule.getPrimaryDataSourceName();
             databaseDiscoveryType.updatePrimaryDataSource(schemaName, originalDataSourceMap, disabledDataSourceNames, groupName, primaryDataSourceName);
-            dbDiscoveryDataSourceRule.updatePrimaryDataSourceName(databaseDiscoveryType.getPrimaryDataSource());
+            dataSourceRule.updatePrimaryDataSourceName(databaseDiscoveryType.getPrimaryDataSource());
             databaseDiscoveryType.updateMemberState(schemaName, originalDataSourceMap, disabledDataSourceNames);
             try {
-                databaseDiscoveryType.checkDatabaseDiscoveryConfig(schemaName, dataSourceMap);
+                databaseDiscoveryType.checkDatabaseDiscoveryConfiguration(schemaName, dataSourceMap);
                 databaseDiscoveryType.startPeriodicalUpdate(schemaName, originalDataSourceMap, disabledDataSourceNames, groupName, primaryDataSourceName);
             } catch (final SQLException ex) {
                 throw new ShardingSphereException(ex);
@@ -160,7 +160,11 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
     public void updateRuleStatus(final RuleChangedEvent event) {
         if (event instanceof DataSourceNameDisabledEvent) {
             for (Entry<String, DatabaseDiscoveryDataSourceRule> entry : dataSourceRules.entrySet()) {
-                entry.getValue().updateDisabledDataSourceNames(((DataSourceNameDisabledEvent) event).getDataSourceName(), ((DataSourceNameDisabledEvent) event).isDisabled());
+                if (((DataSourceNameDisabledEvent) event).isDisabled()) {
+                    entry.getValue().disableDataSource(((DataSourceNameDisabledEvent) event).getDataSourceName());
+                } else {
+                    entry.getValue().enableDataSource(((DataSourceNameDisabledEvent) event).getDataSourceName());
+                }
             }
         } else if (event instanceof PrimaryDataSourceEvent) {
             for (Entry<String, DatabaseDiscoveryDataSourceRule> entry : dataSourceRules.entrySet()) {
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/TestDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/TestDatabaseDiscoveryType.java
index ab0a3ce..d763492 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/TestDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/TestDatabaseDiscoveryType.java
@@ -26,7 +26,7 @@ import java.util.Map;
 public final class TestDatabaseDiscoveryType implements DatabaseDiscoveryType {
     
     @Override
-    public void checkDatabaseDiscoveryConfig(final String schemaName, final Map<String, DataSource> dataSourceMap) {
+    public void checkDatabaseDiscoveryConfiguration(final String schemaName, final Map<String, DataSource> dataSourceMap) {
     }
     
     @Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRuleTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRuleTest.java
index 84c74d6..fa9f645 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRuleTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryDataSourceRuleTest.java
@@ -57,20 +57,20 @@ public final class DatabaseDiscoveryDataSourceRuleTest {
     
     @Test
     public void assertGetDataSourceNamesWithDisabledDataSourceNames() {
-        databaseDiscoveryDataSourceRule.updateDisabledDataSourceNames("ds_0", true);
+        databaseDiscoveryDataSourceRule.disableDataSource("ds_0");
         assertThat(databaseDiscoveryDataSourceRule.getDataSourceNames(), is(Collections.singletonList("ds_1")));
     }
     
     @Test
     public void assertUpdateDisabledDataSourceNamesForDisabled() {
-        databaseDiscoveryDataSourceRule.updateDisabledDataSourceNames("ds_0", true);
+        databaseDiscoveryDataSourceRule.disableDataSource("ds_0");
         assertThat(databaseDiscoveryDataSourceRule.getDataSourceNames(), is(Collections.singletonList("ds_1")));
     }
     
     @Test
     public void assertUpdateDisabledDataSourceNamesForEnabled() {
-        databaseDiscoveryDataSourceRule.updateDisabledDataSourceNames("ds_0", true);
-        databaseDiscoveryDataSourceRule.updateDisabledDataSourceNames("ds_0", false);
+        databaseDiscoveryDataSourceRule.disableDataSource("ds_0");
+        databaseDiscoveryDataSourceRule.enableDataSource("ds_0");
         assertThat(databaseDiscoveryDataSourceRule.getDataSourceNames(), is(Arrays.asList("ds_0", "ds_1")));
     }
     
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryType.java
index 79c57ef..8e2f0bd 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/main/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryType.java
@@ -71,7 +71,7 @@ public final class MGRDatabaseDiscoveryType implements DatabaseDiscoveryType {
     private Properties props = new Properties();
     
     @Override
-    public void checkDatabaseDiscoveryConfig(final String schemaName, final Map<String, DataSource> dataSourceMap) throws SQLException {
+    public void checkDatabaseDiscoveryConfiguration(final String schemaName, final Map<String, DataSource> dataSourceMap) throws SQLException {
         try (Connection connection = dataSourceMap.get(oldPrimaryDataSource).getConnection();
              Statement statement = connection.createStatement()) {
             checkPluginIsActive(statement);
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
index 3bfb40c..1b7d6d5 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
@@ -73,7 +73,7 @@ public final class MGRDatabaseDiscoveryTypeTest {
         when(dataSourceMap.get(null)).thenReturn(dataSource);
         try {
             mgrHaType.getProps().setProperty("groupName", "group_name");
-            mgrHaType.checkDatabaseDiscoveryConfig("discovery_db", dataSourceMap);
+            mgrHaType.checkDatabaseDiscoveryConfiguration("discovery_db", dataSourceMap);
         } catch (final SQLException ex) {
             throw new ShardingSphereException(ex);
         }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/TestDatabaseDiscoveryType.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/TestDatabaseDiscoveryType.java
index 46cddac..652a448 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/TestDatabaseDiscoveryType.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/TestDatabaseDiscoveryType.java
@@ -29,7 +29,7 @@ import java.util.Map;
 public final class TestDatabaseDiscoveryType implements DatabaseDiscoveryType {
     
     @Override
-    public void checkDatabaseDiscoveryConfig(final String schemaName, final Map<String, DataSource> dataSourceMap) {
+    public void checkDatabaseDiscoveryConfiguration(final String schemaName, final Map<String, DataSource> dataSourceMap) {
     }
     
     @Override