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 2022/06/23 02:52:28 UTC

[shardingsphere] branch master updated: MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm support cascade mode (#18488)

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

zhaojinchao 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 4eae9f7b7d9 MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm support cascade mode (#18488)
4eae9f7b7d9 is described below

commit 4eae9f7b7d95e94b400f5ddb3e0becedc50c18b2
Author: natehuang <na...@tencent.com>
AuthorDate: Thu Jun 23 10:52:19 2022 +0800

    MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm support cascade mode (#18488)
    
    * MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm support cascade mode
    
    * remove extra blank line
---
 ...licationDatabaseDiscoveryProviderAlgorithm.java | 32 +++++++++++++---------
 ...tionDatabaseDiscoveryProviderAlgorithmTest.java | 14 ++++++----
 2 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProvi [...]
index f9aaa998ede..f534ef5ff4d 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm.java
@@ -48,6 +48,8 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm impl
     
     private static final String SHOW_SLAVE_HOSTS = "SHOW SLAVE HOSTS";
     
+    private static final String SHOW_VARIABLES_READ_ONLY = "SHOW VARIABLES LIKE 'read_only'";
+    
     private Properties props;
     
     @Override
@@ -58,25 +60,25 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm impl
     @Override
     public void checkEnvironment(final String databaseName, final Collection<DataSource> dataSources) {
         ExecutorService executorService = ExecutorEngine.createExecutorEngineWithCPUAndResources(dataSources.size()).getExecutorServiceManager().getExecutorService();
-        Collection<CompletableFuture<Collection<String>>> completableFutures = new LinkedList<>();
+        Collection<CompletableFuture<Boolean>> completableFutures = new LinkedList<>();
         for (DataSource dataSource : dataSources) {
             completableFutures.add(supplyAsyncCheckEnvironment(dataSource, executorService));
         }
         CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0]));
-        Iterator<CompletableFuture<Collection<String>>> replicationInstancesFuture = completableFutures.stream().iterator();
-        int replicationGroupCount = 0;
-        while (replicationInstancesFuture.hasNext()) {
-            if (!replicationInstancesFuture.next().join().isEmpty()) {
-                replicationGroupCount++;
+        Iterator<CompletableFuture<Boolean>> primaryInstancesFuture = completableFutures.stream().iterator();
+        int primaryCount = 0;
+        while (primaryInstancesFuture.hasNext()) {
+            if (primaryInstancesFuture.next().join()) {
+                primaryCount++;
             }
         }
-        Preconditions.checkState(1 == replicationGroupCount, "Check Environment are failed in database `%s`.", databaseName);
+        Preconditions.checkState(1 == primaryCount, "Check Environment are failed in database `%s`.", databaseName);
     }
     
-    private CompletableFuture<Collection<String>> supplyAsyncCheckEnvironment(final DataSource dataSource, final ExecutorService executorService) {
+    private CompletableFuture<Boolean> supplyAsyncCheckEnvironment(final DataSource dataSource, final ExecutorService executorService) {
         return CompletableFuture.supplyAsync(() -> {
             try {
-                return getReplicationInstances(dataSource);
+                return isPrimaryInstance(dataSource);
             } catch (SQLException ex) {
                 throw new ShardingSphereException(ex);
             }
@@ -101,16 +103,20 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm impl
         return result;
     }
     
-    @Override
-    public boolean isPrimaryInstance(final DataSource dataSource) throws SQLException {
+    private boolean isNotReadonlyInstance(final DataSource dataSource) throws SQLException {
         try (
                 Connection connection = dataSource.getConnection();
                 Statement statement = connection.createStatement();
-                ResultSet resultSet = statement.executeQuery(SHOW_SLAVE_STATUS)) {
-            return !resultSet.next();
+                ResultSet resultSet = statement.executeQuery(SHOW_VARIABLES_READ_ONLY)) {
+            return resultSet.next() && resultSet.getString("Value").equals("OFF");
         }
     }
     
+    @Override
+    public boolean isPrimaryInstance(final DataSource dataSource) throws SQLException {
+        return !getReplicationInstances(dataSource).isEmpty() && isNotReadonlyInstance(dataSource);
+    }
+    
     @Override
     public ReplicaDataSourceStatus loadReplicaStatus(final DataSource replicaDataSource) throws SQLException {
         try (
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryP [...]
index 3057db45515..69d08b0535f 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java
@@ -49,13 +49,17 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest
     }
     
     private DataSource mockDataSourceForReplicationInstances() throws SQLException {
-        ResultSet resultSet = mock(ResultSet.class);
-        when(resultSet.next()).thenReturn(true, false);
-        when(resultSet.getString("Host")).thenReturn("127.0.0.1");
-        when(resultSet.getString("Port")).thenReturn("3306");
+        ResultSet slaveHostsResultSet = mock(ResultSet.class);
+        when(slaveHostsResultSet.next()).thenReturn(true, false);
+        when(slaveHostsResultSet.getString("Host")).thenReturn("127.0.0.1");
+        when(slaveHostsResultSet.getString("Port")).thenReturn("3306");
+        ResultSet readonlyResultSet = mock(ResultSet.class);
+        when(readonlyResultSet.next()).thenReturn(true, false);
+        when(readonlyResultSet.getString("Value")).thenReturn("OFF");
         Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
-        when(connection.createStatement().executeQuery("SHOW SLAVE HOSTS")).thenReturn(resultSet);
+        when(connection.createStatement().executeQuery("SHOW SLAVE HOSTS")).thenReturn(slaveHostsResultSet);
         when(connection.getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1:3306/foo_ds");
+        when(connection.createStatement().executeQuery("SHOW VARIABLES LIKE 'read_only'")).thenReturn(readonlyResultSet);
         return new MockedDataSource(connection);
     }