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

menghaoran 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 7199d81  Adjust checkDatabaseDiscoveryConfiguration method to check 
first. (#14514)
7199d81 is described below

commit 7199d812947935056f1f5faf00621afb84d8427a
Author: zhaojinchao <[email protected]>
AuthorDate: Wed Jan 5 11:30:51 2022 +0800

    Adjust checkDatabaseDiscoveryConfiguration method to check first. (#14514)
---
 .../dbdiscovery/spi/DatabaseDiscoveryType.java          |  4 ++--
 .../dbdiscovery/rule/DatabaseDiscoveryRule.java         | 10 ++++++----
 .../dbdiscovery/fixture/TestDatabaseDiscoveryType.java  |  2 +-
 ...gorithmProvidedDatabaseDiscoveryRuleBuilderTest.java |  4 +++-
 .../rule/builder/DatabaseDiscoveryRuleBuilderTest.java  |  2 +-
 .../fixture/MGRDatabaseDiscoveryTypeFixture.java        |  3 +--
 .../dbdiscovery/mgr/MGRDatabaseDiscoveryType.java       |  4 ++--
 .../dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java   |  4 +---
 .../opengauss/OpenGaussDatabaseDiscoveryType.java       | 17 +----------------
 .../opengauss/OpenGaussDatabaseDiscoveryTypeTest.java   |  4 +---
 .../text/distsql/fixture/TestDatabaseDiscoveryType.java |  2 +-
 11 files changed, 20 insertions(+), 36 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 4eb65d7..4d4b30b 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
@@ -33,10 +33,10 @@ public interface DatabaseDiscoveryType extends 
ShardingSphereAlgorithm {
      * Check database discovery configuration.
      *
      * @param schemaName schema name
-     * @param dataSourceMap data source map
+     * @param dataSource data source
      * @throws SQLException SQL exception
      */
-    void checkDatabaseDiscoveryConfiguration(String schemaName, Map<String, 
DataSource> dataSourceMap) throws SQLException;
+    void checkDatabaseDiscoveryConfiguration(String schemaName, DataSource 
dataSource) 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/DatabaseDiscoveryRule.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
index ca09db3..172f667 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
@@ -45,6 +45,7 @@ import 
org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -111,14 +112,15 @@ public final class DatabaseDiscoveryRule implements 
SchemaRule, DataSourceContai
             DatabaseDiscoveryType databaseDiscoveryType = 
dataSourceRule.getDatabaseDiscoveryType();
             Map<String, DataSource> originalDataSourceMap = new 
HashMap<>(dataSourceMap);
             Collection<String> disabledDataSourceNames = 
dataSourceRule.getDisabledDataSourceNames();
-            databaseDiscoveryType.updatePrimaryDataSource(schemaName, 
originalDataSourceMap, disabledDataSourceNames, groupName);
-            
dataSourceRule.updatePrimaryDataSourceName(databaseDiscoveryType.getPrimaryDataSource());
-            databaseDiscoveryType.updateMemberState(schemaName, 
originalDataSourceMap, disabledDataSourceNames);
+            DataSource dataSource = new 
ArrayList<>(originalDataSourceMap.values()).stream().findFirst().get();
             try {
-                
databaseDiscoveryType.checkDatabaseDiscoveryConfiguration(schemaName, 
dataSourceMap);
+                
databaseDiscoveryType.checkDatabaseDiscoveryConfiguration(schemaName, 
dataSource);
             } catch (final SQLException ex) {
                 throw new ShardingSphereException(ex);
             }
+            databaseDiscoveryType.updatePrimaryDataSource(schemaName, 
originalDataSourceMap, disabledDataSourceNames, groupName);
+            
dataSourceRule.updatePrimaryDataSourceName(databaseDiscoveryType.getPrimaryDataSource());
+            databaseDiscoveryType.updateMemberState(schemaName, 
originalDataSourceMap, disabledDataSourceNames);
         }
     }
     
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 7c8b997..4ebacb2 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 checkDatabaseDiscoveryConfiguration(final String schemaName, 
final Map<String, DataSource> dataSourceMap) {
+    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final DataSource dataSource) {
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/AlgorithmProvidedDatabaseDiscoveryRuleBuilderTest.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/AlgorithmProvidedDatabaseDiscoveryRuleBuilderTest.java
index fada0d1..0d79ea3 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/AlgorithmProvidedDatabaseDiscoveryRuleBuilderTest.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/AlgorithmProvidedDatabaseDiscoveryRuleBuilderTest.java
@@ -30,6 +30,7 @@ import 
org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.spi.ordered.OrderedSPIRegistry;
 import org.junit.Test;
 
+import javax.sql.DataSource;
 import java.util.Collections;
 import java.util.Properties;
 
@@ -55,6 +56,7 @@ public final class 
AlgorithmProvidedDatabaseDiscoveryRuleBuilderTest {
         SchemaRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(
                 SchemaRuleBuilder.class, 
Collections.singletonList(algorithmProvidedRuleConfig)).get(algorithmProvidedRuleConfig);
         assertThat(builder.build(new SchemaRulesBuilderMaterials("", 
Collections.emptyList(), mock(DatabaseType.class),
-                Collections.emptyMap(), new ConfigurationProperties(new 
Properties())), algorithmProvidedRuleConfig, Collections.emptyList()), 
instanceOf(DatabaseDiscoveryRule.class));
+                Collections.singletonMap("name", mock(DataSource.class)), new 
ConfigurationProperties(new Properties())),
+                algorithmProvidedRuleConfig, Collections.emptyList()), 
instanceOf(DatabaseDiscoveryRule.class));
     }
 }
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilderTest.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilderTest.java
index 098c565..c19867a 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilderTest.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/builder/DatabaseDiscoveryRuleBuilderTest.java
@@ -56,7 +56,7 @@ public final class DatabaseDiscoveryRuleBuilderTest {
                 Collections.singletonMap("TEST", new 
ShardingSphereAlgorithmConfiguration("TEST", new Properties())));
         SchemaRuleBuilder builder = 
OrderedSPIRegistry.getRegisteredServices(SchemaRuleBuilder.class, 
Collections.singletonList(config)).get(config);
         Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
-        dataSourceMap.put("primaryDataSourceName", mock(DataSource.class));
+        dataSourceMap.put("name", mock(DataSource.class));
         assertThat(builder.build(new 
SchemaRulesBuilderMaterials("test_schema", Collections.emptyList(), 
mock(DatabaseType.class),
                 dataSourceMap, new ConfigurationProperties(new Properties())), 
config, Collections.emptyList()), instanceOf(DatabaseDiscoveryRule.class));
     }
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/fixture/MGRDatabaseDiscoveryTypeFixture.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/fixture/MGRDatabaseDiscoveryTypeF
 [...]
index 782b95c..b06ae6f 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/fixture/MGRDatabaseDiscoveryTypeFixture.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/fixture/MGRDatabaseDiscoveryTypeFixture.java
@@ -21,7 +21,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
 
 import javax.sql.DataSource;
-import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Map;
 
@@ -37,7 +36,7 @@ public final class MGRDatabaseDiscoveryTypeFixture implements 
DatabaseDiscoveryT
     }
     
     @Override
-    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final Map<String, DataSource> dataSourceMap) throws SQLException {
+    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final DataSource dataSource) {
         
     }
     
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 af8b591..7de8e64 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
@@ -62,8 +62,8 @@ public final class MGRDatabaseDiscoveryType implements 
DatabaseDiscoveryType {
     private Properties props = new Properties();
     
     @Override
-    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final Map<String, DataSource> dataSourceMap) throws SQLException {
-        try (Connection connection = 
dataSourceMap.get(oldPrimaryDataSource).getConnection();
+    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final DataSource dataSource) throws SQLException {
+        try (Connection connection = dataSource.getConnection();
              Statement statement = connection.createStatement()) {
             checkPluginIsActive(statement);
             checkMemberCount(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 b961ddb..16b2b71 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
@@ -72,10 +72,8 @@ public final class MGRDatabaseDiscoveryTypeTest {
         when(resultSet.getString("PLUGIN_STATUS")).thenReturn("ACTIVE");
         when(resultSet.getInt(1)).thenReturn(3);
         when(resultSet.getString("VARIABLE_VALUE")).thenReturn("group_name", 
"ON");
-        Map<String, DataSource> dataSourceMap = mock(HashMap.class);
-        when(dataSourceMap.get(null)).thenReturn(dataSource);
         mgrHaType.getProps().setProperty("group-name", "group_name");
-        mgrHaType.checkDatabaseDiscoveryConfiguration("discovery_db", 
dataSourceMap);
+        mgrHaType.checkDatabaseDiscoveryConfiguration("discovery_db", 
dataSource);
     }
     
     @Test
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
index 7e04e91..7dbef05 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
@@ -21,7 +21,6 @@ import lombok.Getter;
 import lombok.Setter;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
-import 
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
 import 
org.apache.shardingsphere.infra.rule.event.impl.PrimaryDataSourceChangedEvent;
@@ -52,21 +51,7 @@ public final class OpenGaussDatabaseDiscoveryType implements 
DatabaseDiscoveryTy
     private Properties props = new Properties();
     
     @Override
-    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final Map<String, DataSource> dataSourceMap) throws SQLException {
-        try (Connection connection = 
dataSourceMap.get(oldPrimaryDataSource).getConnection();
-                Statement statement = connection.createStatement()) {
-            checkRolePrimary(statement);
-        }
-    }
-    
-    private void checkRolePrimary(final Statement statement) throws 
SQLException {
-        try (ResultSet resultSet = statement.executeQuery(DB_ROLE)) {
-            while (resultSet.next()) {
-                if (!"Primary".equals(resultSet.getString("local_role"))) {
-                    throw new ShardingSphereConfigurationException("Instance 
is not Primary.");
-                }
-            }
-        }
+    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final DataSource dataSource) {
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
index 5f65d3c..d720847 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
@@ -58,9 +58,7 @@ public final class OpenGaussDatabaseDiscoveryTypeTest {
         when(resultSet.getString("local_role")).thenReturn("Primary");
         when(resultSet.getString("db_state")).thenReturn("Normal");
         when(resultSet.getString("db_state")).thenReturn("Sync");
-        Map<String, DataSource> dataSourceMap = mock(HashMap.class);
-        when(dataSourceMap.get(null)).thenReturn(dataSource);
-        ogHaType.checkDatabaseDiscoveryConfiguration("discovery_db", 
dataSourceMap);
+        ogHaType.checkDatabaseDiscoveryConfiguration("discovery_db", 
dataSource);
     }
     
     @Test
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 91c80f2..d279287 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
@@ -26,7 +26,7 @@ import java.util.Map;
 public final class TestDatabaseDiscoveryType implements DatabaseDiscoveryType {
     
     @Override
-    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final Map<String, DataSource> dataSourceMap) {
+    public void checkDatabaseDiscoveryConfiguration(final String schemaName, 
final DataSource dataSource) {
     }
     
     @Override

Reply via email to