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

[shardingsphere] branch master updated: Refactor DatabaseDiscoveryRule (#12225)

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 4098502  Refactor DatabaseDiscoveryRule (#12225)
4098502 is described below

commit 40985027228e50418fb4d06cc09054ea55e05332
Author: Liang Zhang <te...@163.com>
AuthorDate: Sun Sep 5 14:20:37 2021 +0800

    Refactor DatabaseDiscoveryRule (#12225)
---
 .../dbdiscovery/rule/DatabaseDiscoveryRule.java    | 95 ++++++++++------------
 ...orithmProvidedDatabaseDiscoveryRuleBuilder.java |  2 +-
 .../rule/builder/DatabaseDiscoveryRuleBuilder.java |  2 +-
 .../route/DatabaseDiscoverySQLRouterTest.java      |  2 +-
 .../rule/DatabaseDiscoveryRuleTest.java            |  5 +-
 5 files changed, 50 insertions(+), 56 deletions(-)

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 d738d3c..cedf26d 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
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDa
 import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
 import org.apache.shardingsphere.infra.aware.DataSourceNameAware;
 import org.apache.shardingsphere.infra.aware.DataSourceNameAwareFactory;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
@@ -55,53 +56,57 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
         ShardingSphereServiceLoader.register(DataSourceNameAware.class);
     }
     
-    private final Map<String, DatabaseDiscoveryType> discoveryTypes = new LinkedHashMap<>();
+    private final Map<String, DatabaseDiscoveryType> discoveryTypes;
     
     @Getter
     private final Map<String, DatabaseDiscoveryDataSourceRule> dataSourceRules;
     
-    public DatabaseDiscoveryRule(final DatabaseDiscoveryRuleConfiguration config, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final String schemaName) {
-        Preconditions.checkArgument(!config.getDataSources().isEmpty(), "database discovery rules can not be empty.");
-        Preconditions.checkArgument(null != dataSourceMap && !dataSourceMap.isEmpty(), "Data sources of cannot be empty.");
-        Preconditions.checkNotNull(databaseType, "Database type cannot be null.");
-        config.getDiscoveryTypes().forEach((key, value) -> discoveryTypes.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, DatabaseDiscoveryType.class)));
-        dataSourceRules = new HashMap<>(config.getDataSources().size(), 1);
-        for (DatabaseDiscoveryDataSourceRuleConfiguration each : config.getDataSources()) {
-            Preconditions.checkNotNull(each.getDiscoveryTypeName(), "Discovery type cannot be null of rule name `%s`.", each.getName());
-            Preconditions.checkArgument(discoveryTypes.containsKey(each.getDiscoveryTypeName()), "Can not find discovery type of rule name `%s`.", each.getName());
-            dataSourceRules.put(each.getName(), new DatabaseDiscoveryDataSourceRule(each, discoveryTypes.get(each.getDiscoveryTypeName())));
-        }
-        for (Entry<String, DatabaseDiscoveryDataSourceRule> entry : dataSourceRules.entrySet()) {
-            String groupName = entry.getKey();
-            DatabaseDiscoveryDataSourceRule dbDiscoveryDataSourceRule = entry.getValue();
-            DatabaseDiscoveryType databaseDiscoveryType = dbDiscoveryDataSourceRule.getDatabaseDiscoveryType();
-            Map<String, DataSource> originalDataSourceMap = new HashMap<>(dataSourceMap);
-            Collection<String> disabledDataSourceNames = dbDiscoveryDataSourceRule.getDisabledDataSourceNames();
-            String primaryDataSourceName = dbDiscoveryDataSourceRule.getPrimaryDataSourceName();
-            databaseDiscoveryType.updatePrimaryDataSource(originalDataSourceMap, schemaName, disabledDataSourceNames, groupName, primaryDataSourceName);
-            dbDiscoveryDataSourceRule.updatePrimaryDataSourceName(databaseDiscoveryType.getPrimaryDataSource());
-            databaseDiscoveryType.updateMemberState(originalDataSourceMap, schemaName, disabledDataSourceNames);
-            try {
-                databaseDiscoveryType.checkDatabaseDiscoveryConfig(dataSourceMap, schemaName);
-                databaseDiscoveryType.startPeriodicalUpdate(originalDataSourceMap, schemaName, disabledDataSourceNames, groupName, primaryDataSourceName);
-            } catch (final SQLException ex) {
-                throw new ShardingSphereException(ex);
-            }
-        }
+    public DatabaseDiscoveryRule(final DatabaseDiscoveryRuleConfiguration config, final String schemaName, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
+        checkDataSourcesArguments(config.getDataSources(), databaseType, dataSourceMap);
+        discoveryTypes = getDiscoveryTypes(config.getDiscoveryTypes());
+        dataSourceRules = getDataSourceRules(config.getDataSources());
+        startMonitor(schemaName, dataSourceMap);
+        initAware();
+    }
+    
+    public DatabaseDiscoveryRule(final AlgorithmProvidedDatabaseDiscoveryRuleConfiguration config, 
+                                 final String schemaName, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
+        checkDataSourcesArguments(config.getDataSources(), databaseType, dataSourceMap);
+        discoveryTypes = config.getDiscoveryTypes();
+        dataSourceRules = getDataSourceRules(config.getDataSources());
+        startMonitor(schemaName, dataSourceMap);
         initAware();
     }
     
-    public DatabaseDiscoveryRule(final AlgorithmProvidedDatabaseDiscoveryRuleConfiguration config, final DatabaseType databaseType, 
-                                 final Map<String, DataSource> dataSourceMap, final String schemaName) {
-        Preconditions.checkArgument(!config.getDataSources().isEmpty(), "HA data source rules can not be empty.");
+    private void checkDataSourcesArguments(final Collection<DatabaseDiscoveryDataSourceRuleConfiguration> dataSources, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
+        Preconditions.checkArgument(!dataSources.isEmpty(), "Database discovery rules can not be empty.");
         Preconditions.checkArgument(null != dataSourceMap && !dataSourceMap.isEmpty(), "Data sources cannot be empty.");
-        Preconditions.checkArgument(null != databaseType, "Database type cannot be null.");
-        dataSourceRules = new HashMap<>(config.getDataSources().size(), 1);
-        for (DatabaseDiscoveryDataSourceRuleConfiguration each : config.getDataSources()) {
-            Preconditions.checkNotNull(each.getDiscoveryTypeName(), "Discovery type cannot be null of rule name `%s`.", each.getName());
-            Preconditions.checkArgument(discoveryTypes.containsKey(each.getDiscoveryTypeName()), "Can not find discovery type of rule name `%s`.", each.getName());
-            dataSourceRules.put(each.getName(), new DatabaseDiscoveryDataSourceRule(each, discoveryTypes.get(each.getDiscoveryTypeName())));
+        Preconditions.checkNotNull(databaseType, "Database type cannot be null.");
+    }
+    
+    private Map<String, DatabaseDiscoveryType> getDiscoveryTypes(final Map<String, ShardingSphereAlgorithmConfiguration> discoveryTypesConfig) {
+        Map<String, DatabaseDiscoveryType> result = new LinkedHashMap<>();
+        for (Entry<String, ShardingSphereAlgorithmConfiguration> entry : discoveryTypesConfig.entrySet()) {
+            result.put(entry.getKey(), ShardingSphereAlgorithmFactory.createAlgorithm(entry.getValue(), DatabaseDiscoveryType.class));
         }
+        return result;
+    }
+    
+    private Map<String, DatabaseDiscoveryDataSourceRule> getDataSourceRules(final Collection<DatabaseDiscoveryDataSourceRuleConfiguration> dataSources) {
+        Map<String, DatabaseDiscoveryDataSourceRule> result = new HashMap<>(dataSources.size(), 1);
+        for (DatabaseDiscoveryDataSourceRuleConfiguration each : dataSources) {
+            checkDatabaseDiscoveryDataSourceRuleConfigurationArguments(each);
+            result.put(each.getName(), new DatabaseDiscoveryDataSourceRule(each, discoveryTypes.get(each.getDiscoveryTypeName())));
+        }
+        return result;
+    }
+    
+    private void checkDatabaseDiscoveryDataSourceRuleConfigurationArguments(final DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig) {
+        Preconditions.checkNotNull(dataSourceRuleConfig.getDiscoveryTypeName(), "Discovery type cannot be null of rule name `%s`.", dataSourceRuleConfig.getName());
+        Preconditions.checkArgument(discoveryTypes.containsKey(dataSourceRuleConfig.getDiscoveryTypeName()), "Can not find discovery type of rule name `%s`.", dataSourceRuleConfig.getName());
+    }
+    
+    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();
@@ -119,7 +124,6 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
                 throw new ShardingSphereException(ex);
             }
         }
-        initAware();
     }
     
     private void initAware() {
@@ -127,18 +131,9 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
     }
     
     /**
-     * Get all logic data source names.
-     *
-     * @return all logic data source names
-     */
-    public Collection<String> getAllLogicDataSourceNames() {
-        return dataSourceRules.keySet();
-    }
-    
-    /**
      * Get single data source rule.
      *
-     * @return HA data source rule
+     * @return data source rule
      */
     public DatabaseDiscoveryDataSourceRule getSingleDataSourceRule() {
         return dataSourceRules.values().iterator().next();
@@ -148,7 +143,7 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
      * Find data source rule.
      * 
      * @param dataSourceName data source name
-     * @return HA data source rule
+     * @return found data source rule
      */
     public Optional<DatabaseDiscoveryDataSourceRule> findDataSourceRule(final String dataSourceName) {
         return Optional.ofNullable(dataSourceRules.get(dataSourceName));
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/builder/AlgorithmProvidedDatabaseDiscoveryRuleBuilder.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/builder/AlgorithmProvidedDatabaseDiscoveryRuleBuilder.java
index f673bbf..85b6d3d 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/builder/AlgorithmProvidedDatabaseDiscoveryRuleBuilder.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/builder/AlgorithmProvidedDatabaseDiscoveryRuleBuilder.java
@@ -34,7 +34,7 @@ public final class AlgorithmProvidedDatabaseDiscoveryRuleBuilder implements Sche
     @Override
     public DatabaseDiscoveryRule build(final SchemaRulesBuilderMaterials materials, final AlgorithmProvidedDatabaseDiscoveryRuleConfiguration config,
                                        final Collection<ShardingSphereRule> rules) {
-        return new DatabaseDiscoveryRule(config, materials.getDatabaseType(), materials.getDataSourceMap(), materials.getSchemaName());
+        return new DatabaseDiscoveryRule(config, materials.getSchemaName(), materials.getDatabaseType(), materials.getDataSourceMap());
     }
     
     @Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilder.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilder.java
index dbf1928..f3da959 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilder.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilder.java
@@ -43,7 +43,7 @@ public final class DatabaseDiscoveryRuleBuilder implements SchemaRuleBuilder<Dat
                 realDataSourceMap.put(datasourceName, materials.getDataSourceMap().get(datasourceName));
             }
         }
-        return new DatabaseDiscoveryRule(config, materials.getDatabaseType(), realDataSourceMap, materials.getSchemaName());
+        return new DatabaseDiscoveryRule(config, materials.getSchemaName(), materials.getDatabaseType(), realDataSourceMap);
     }
     
     @Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
index ec69fc8..1d8a48b 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
@@ -78,7 +78,7 @@ public final class DatabaseDiscoverySQLRouterTest {
                 = new DatabaseDiscoveryDataSourceRuleConfiguration(DATA_SOURCE_NAME, Collections.singletonList(PRIMARY_DATA_SOURCE), "TEST");
         ShardingSphereAlgorithmConfiguration algorithmConfig = new ShardingSphereAlgorithmConfiguration("TEST", new Properties());
         DatabaseDiscoveryRuleConfiguration config = new DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceConfig), Collections.singletonMap("TEST", algorithmConfig));
-        rule = new DatabaseDiscoveryRule(config, mock(DatabaseType.class), Collections.singletonMap("ds", mock(DataSource.class)), "TEST");
+        rule = new DatabaseDiscoveryRule(config, "TEST", mock(DatabaseType.class), Collections.singletonMap("ds", mock(DataSource.class)));
         sqlRouter = (DatabaseDiscoverySQLRouter) OrderedSPIRegistry.getRegisteredServices(SQLRouter.class, Collections.singleton(rule)).get(rule);
     }
     
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
index 9d72fb3..6d64d27 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
@@ -44,7 +44,7 @@ public final class DatabaseDiscoveryRuleTest {
     
     @Test(expected = IllegalArgumentException.class)
     public void assertNewWithEmptyDataSourceRule() {
-        new DatabaseDiscoveryRule(new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Collections.emptyMap()), mock(DatabaseType.class), dataSourceMap, "ha_db");
+        new DatabaseDiscoveryRule(new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Collections.emptyMap()), "ha_db", mock(DatabaseType.class), dataSourceMap);
     }
     
     @Test
@@ -98,7 +98,6 @@ public final class DatabaseDiscoveryRuleTest {
     private DatabaseDiscoveryRule createRule() {
         DatabaseDiscoveryDataSourceRuleConfiguration config = new DatabaseDiscoveryDataSourceRuleConfiguration("test_pr", Arrays.asList("ds_0", "ds_1"), "TEST");
         return new DatabaseDiscoveryRule(new DatabaseDiscoveryRuleConfiguration(
-                Collections.singleton(config), ImmutableMap.of("TEST", new ShardingSphereAlgorithmConfiguration("TEST", new Properties()))),
-                mock(DatabaseType.class), dataSourceMap, "ha_db");
+                Collections.singleton(config), ImmutableMap.of("TEST", new ShardingSphereAlgorithmConfiguration("TEST", new Properties()))), "ha_db", mock(DatabaseType.class), dataSourceMap);
     }
 }