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 2022/06/13 04:15:40 UTC

[shardingsphere] branch master updated: Use DatabaseDiscoveryRule in DatabaseDiscoveryRuleQueryResultSet (#18321)

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 16ff508eeef Use DatabaseDiscoveryRule in DatabaseDiscoveryRuleQueryResultSet (#18321)
16ff508eeef is described below

commit 16ff508eeefe992f83bed2d1d1ab7c529cb6d111
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Jun 13 12:15:33 2022 +0800

    Use DatabaseDiscoveryRule in DatabaseDiscoveryRuleQueryResultSet (#18321)
    
    * Use DatabaseDiscoveryRule in DatabaseDiscoveryRuleQueryResultSet
    
    * Use DatabaseDiscoveryRule in DatabaseDiscoveryRuleQueryResultSet
---
 .../dbdiscovery/rule/DatabaseDiscoveryRule.java    |  4 +-
 .../pom.xml                                        |  2 +-
 .../query/DatabaseDiscoveryRuleQueryResultSet.java | 38 +++++++++---------
 .../DatabaseDiscoveryRuleQueryResultSetTest.java   | 46 +++++++++++++---------
 4 files changed, 49 insertions(+), 41 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 7e4b5feab39..79e1e75321f 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
@@ -83,7 +83,7 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
         this.databaseName = databaseName;
         this.dataSourceMap = dataSourceMap;
         dataSourceRules = getDataSourceRules(dataSourceRuleConfigs, heartBeatConfig);
-        findMasterSlaveRelation(databaseName, dataSourceMap);
+        findPrimaryReplicaRelationship(databaseName, dataSourceMap);
         initAware();
     }
     
@@ -105,7 +105,7 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
         return result;
     }
     
-    private void findMasterSlaveRelation(final String databaseName, final Map<String, DataSource> dataSourceMap) {
+    private void findPrimaryReplicaRelationship(final String databaseName, final Map<String, DataSource> dataSourceMap) {
         for (Entry<String, DatabaseDiscoveryDataSourceRule> entry : dataSourceRules.entrySet()) {
             String groupName = entry.getKey();
             DatabaseDiscoveryDataSourceRule dataSourceRule = entry.getValue();
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/pom.xml b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/pom.xml
index f02077fb842..f963d5f3dbb 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/pom.xml
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/pom.xml
@@ -30,7 +30,7 @@
     <dependencies>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
-            <artifactId>shardingsphere-db-discovery-api</artifactId>
+            <artifactId>shardingsphere-db-discovery-core</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSet.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQuer [...]
index 0e7982aa982..85d56d2f45b 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSet.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSet.java
@@ -17,17 +17,16 @@
 
 package org.apache.shardingsphere.dbdiscovery.distsql.handler.query;
 
+import com.google.common.base.Preconditions;
 import com.google.gson.Gson;
 import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
 import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
 import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
 import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.ShowDatabaseDiscoveryRulesStatement;
+import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryRule;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
 import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.rule.identifier.type.exportable.ExportableRule;
-import org.apache.shardingsphere.infra.rule.identifier.type.exportable.RuleExportEngine;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.Arrays;
@@ -36,7 +35,9 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 /**
  * Result set for show database discovery rule.
@@ -55,7 +56,7 @@ public final class DatabaseDiscoveryRuleQueryResultSet implements DistSQLResultS
     
     private static final String HEARTBEAT = "discovery_heartbeat";
     
-    private Iterator<DatabaseDiscoveryDataSourceRuleConfiguration> data;
+    private Iterator<DatabaseDiscoveryDataSourceRuleConfiguration> dataSourceRules;
     
     private Map<String, ShardingSphereAlgorithmConfiguration> discoveryTypes;
     
@@ -63,21 +64,20 @@ public final class DatabaseDiscoveryRuleQueryResultSet implements DistSQLResultS
     
     private Map<String, String> primaryDataSources;
     
-    @SuppressWarnings("unchecked")
     @Override
     public void init(final ShardingSphereDatabase database, final SQLStatement sqlStatement) {
-        Optional<DatabaseDiscoveryRuleConfiguration> ruleConfig = database.getRuleMetaData().getConfigurations()
-                .stream().filter(each -> each instanceof DatabaseDiscoveryRuleConfiguration).map(each -> (DatabaseDiscoveryRuleConfiguration) each).findAny();
-        data = ruleConfig.map(optional -> optional.getDataSources().iterator()).orElseGet(Collections::emptyIterator);
-        discoveryTypes = ruleConfig.map(DatabaseDiscoveryRuleConfiguration::getDiscoveryTypes).orElseGet(Collections::emptyMap);
-        discoveryHeartbeats = ruleConfig.map(DatabaseDiscoveryRuleConfiguration::getDiscoveryHeartbeats).orElseGet(Collections::emptyMap);
-        Optional<ExportableRule> exportableRule = database.getRuleMetaData().getRules()
-                .stream().filter(each -> each instanceof ExportableRule)
-                .filter(each -> new RuleExportEngine((ExportableRule) each).containExportableKey(Collections.singleton(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES)))
-                .map(each -> (ExportableRule) each).findAny();
-        primaryDataSources = (Map<String, String>) (exportableRule.map(optional -> new RuleExportEngine(optional)
-                .export(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES).orElseGet(Collections::emptyMap))
-                .orElseGet(Collections::emptyMap));
+        Optional<DatabaseDiscoveryRuleConfiguration> ruleConfig = database.getRuleMetaData().findSingleRuleConfiguration(DatabaseDiscoveryRuleConfiguration.class);
+        Preconditions.checkState(ruleConfig.isPresent());
+        dataSourceRules = ruleConfig.get().getDataSources().iterator();
+        discoveryTypes = ruleConfig.get().getDiscoveryTypes();
+        discoveryHeartbeats = ruleConfig.get().getDiscoveryHeartbeats();
+        primaryDataSources = getPrimaryDataSources(database);
+    }
+    
+    private Map<String, String> getPrimaryDataSources(final ShardingSphereDatabase database) {
+        Optional<DatabaseDiscoveryRule> rule = database.getRuleMetaData().findSingleRule(DatabaseDiscoveryRule.class);
+        Preconditions.checkState(rule.isPresent());
+        return rule.get().getDataSourceRules().entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getPrimaryDataSourceName(), (a, b) -> b));
     }
     
     @Override
@@ -87,12 +87,12 @@ public final class DatabaseDiscoveryRuleQueryResultSet implements DistSQLResultS
     
     @Override
     public boolean next() {
-        return data.hasNext();
+        return dataSourceRules.hasNext();
     }
     
     @Override
     public Collection<Object> getRowData() {
-        DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig = data.next();
+        DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig = dataSourceRules.next();
         Map<String, String> typeMap = new LinkedHashMap<>();
         typeMap.put(NAME, dataSourceRuleConfig.getDiscoveryTypeName());
         typeMap.putAll(convertToMap(discoveryTypes.get(dataSourceRuleConfig.getDiscoveryTypeName())));
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRule [...]
index 6249520bd2d..ed8a8a5784f 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/query/DatabaseDiscoveryRuleQueryResultSetTest.java
@@ -21,12 +21,11 @@ import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleCon
 import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
 import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
 import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.ShowDatabaseDiscoveryRulesStatement;
-import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryDataSourceRule;
+import org.apache.shardingsphere.dbdiscovery.rule.DatabaseDiscoveryRule;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
 import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.rule.identifier.type.exportable.ExportableRule;
 import org.junit.Test;
 
 import java.util.ArrayList;
@@ -35,6 +34,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -47,27 +47,21 @@ import static org.mockito.Mockito.when;
 public final class DatabaseDiscoveryRuleQueryResultSetTest {
     
     @Test
-    public void assertGetRowData() {
+    public void assertInit() {
         ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
-        when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.singleton(createRuleConfiguration()));
-        ExportableRule exportableRule = mock(ExportableRule.class);
-        when(exportableRule.getExportData()).thenReturn(Collections.singletonMap(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES, Collections.singletonMap("ms_group", "ds_0")));
-        when(database.getRuleMetaData().getRules()).thenReturn(Collections.singleton(exportableRule));
+        when(database.getRuleMetaData().findSingleRuleConfiguration(DatabaseDiscoveryRuleConfiguration.class)).thenReturn(Optional.of(createRuleConfiguration()));
+        DatabaseDiscoveryRule databaseDiscoveryRule = mock(DatabaseDiscoveryRule.class, RETURNS_DEEP_STUBS);
+        DatabaseDiscoveryDataSourceRule dataSourceRule = mock(DatabaseDiscoveryDataSourceRule.class);
+        when(dataSourceRule.getPrimaryDataSourceName()).thenReturn("ds_0");
+        when(databaseDiscoveryRule.getDataSourceRules()).thenReturn(Collections.singletonMap("ms_group", dataSourceRule));
+        when(database.getRuleMetaData().findSingleRule(DatabaseDiscoveryRule.class)).thenReturn(Optional.of(databaseDiscoveryRule));
         DistSQLResultSet resultSet = new DatabaseDiscoveryRuleQueryResultSet();
         resultSet.init(database, mock(ShowDatabaseDiscoveryRulesStatement.class));
-        Collection<String> columnNames = resultSet.getColumnNames();
-        List<Object> actual = new ArrayList<>(resultSet.getRowData());
-        assertThat(columnNames.size(), is(5));
-        assertTrue(columnNames.containsAll(Arrays.asList("group_name", "data_source_names", "primary_data_source_name", "discovery_type", "discovery_heartbeat")));
-        assertThat(actual.size(), is(5));
-        assertThat(actual.get(0), is("ms_group"));
-        assertThat(actual.get(1), is("ds_0,ds_1"));
-        assertThat(actual.get(2), is("ds_0"));
-        assertThat(actual.get(3).toString(), is("{name=type_test, type=MySQL.MGR, props={}}"));
-        assertThat(actual.get(4).toString(), is("{name=heartbeat_test, props={}}"));
+        assertColumns(resultSet.getColumnNames());
+        assertRowData(new ArrayList<>(resultSet.getRowData()));
     }
     
-    private RuleConfiguration createRuleConfiguration() {
+    private DatabaseDiscoveryRuleConfiguration createRuleConfiguration() {
         DatabaseDiscoveryDataSourceRuleConfiguration databaseDiscoveryDataSourceRuleConfig = new DatabaseDiscoveryDataSourceRuleConfiguration(
                 "ms_group", Arrays.asList("ds_0", "ds_1"), "heartbeat_test", "type_test");
         ShardingSphereAlgorithmConfiguration shardingSphereAlgorithmConfig = new ShardingSphereAlgorithmConfiguration("MySQL.MGR", new Properties());
@@ -75,4 +69,18 @@ public final class DatabaseDiscoveryRuleQueryResultSetTest {
         Map<String, ShardingSphereAlgorithmConfiguration> discoverTypes = Collections.singletonMap("type_test", shardingSphereAlgorithmConfig);
         return new DatabaseDiscoveryRuleConfiguration(Collections.singleton(databaseDiscoveryDataSourceRuleConfig), discoveryHeartbeat, discoverTypes);
     }
+    
+    private void assertColumns(final Collection<String> actual) {
+        assertThat(actual.size(), is(5));
+        assertTrue(actual.containsAll(Arrays.asList("group_name", "data_source_names", "primary_data_source_name", "discovery_type", "discovery_heartbeat")));
+    }
+    
+    private void assertRowData(final List<Object> actual) {
+        assertThat(actual.size(), is(5));
+        assertThat(actual.get(0), is("ms_group"));
+        assertThat(actual.get(1), is("ds_0,ds_1"));
+        assertThat(actual.get(2), is("ds_0"));
+        assertThat(actual.get(3).toString(), is("{name=type_test, type=MySQL.MGR, props={}}"));
+        assertThat(actual.get(4).toString(), is("{name=heartbeat_test, props={}}"));
+    }
 }