You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2022/04/23 15:54:28 UTC

[shardingsphere] branch master updated: Remove DatabaseDiscoveryType.oldPrimaryDataSource (#17039)

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

yx9o 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 3a6a6f67484 Remove DatabaseDiscoveryType.oldPrimaryDataSource (#17039)
3a6a6f67484 is described below

commit 3a6a6f674847a2f23668b4ba8bba0bcbc1379a06
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Apr 23 23:54:20 2022 +0800

    Remove DatabaseDiscoveryType.oldPrimaryDataSource (#17039)
    
    * Remove DatabaseDiscoveryType.oldPrimaryDataSource
    
    * Remove DatabaseDiscoveryType.oldPrimaryDataSource
    
    * Refactor MySQLNormalReplicationMySQLDatabaseDiscoveryType
    
    * Refactor MySQLNormalReplicationMySQLDatabaseDiscoveryType
    
    * Refactor MySQLNormalReplicationMySQLDatabaseDiscoveryType
    
    * Refactor MySQLNormalReplicationMySQLDatabaseDiscoveryType
    
    * Refactor MySQLNormalReplicationMySQLDatabaseDiscoveryType
---
 .../dbdiscovery/spi/DatabaseDiscoveryType.java     | 15 ++++---------
 .../algorithm/DatabaseDiscoveryEngine.java         |  4 ++--
 .../fixture/CoreFixtureDatabaseDiscoveryType.java  |  7 +-----
 .../DistSQLFixtureDatabaseDiscoveryType.java       |  7 +-----
 .../mysql/AbstractMySQLDatabaseDiscoveryType.java  |  7 +-----
 ...ormalReplicationMySQLDatabaseDiscoveryType.java | 25 ++++++++--------------
 ...aussNormalReplicationDatabaseDiscoveryType.java |  9 ++------
 .../cases/mysql/MySQLManualScalingCase.java        |  1 -
 8 files changed, 20 insertions(+), 55 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 f0fda7cc8dd..c35dc1a2f1e 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
@@ -64,16 +64,9 @@ public interface DatabaseDiscoveryType extends ShardingSphereAlgorithm {
     String getPrimaryDataSource();
     
     /**
-     * Get old primary data source.
-     * 
-     * @return old primary data source
-     */
-    String getOldPrimaryDataSource();
-    
-    /**
-     * Get old primary data source.
-     * 
-     * @param oldPrimaryDataSource old primary data source
+     * Set primary data source.
+     *
+     * @param primaryDataSource primary data source
      */
-    void setOldPrimaryDataSource(String oldPrimaryDataSource);
+    void setPrimaryDataSource(String primaryDataSource);
 }
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
index 7b7cfbcbec8..4fd393f9a91 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
@@ -93,8 +93,8 @@ public final class DatabaseDiscoveryEngine {
      */
     public String updatePrimaryDataSource(final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames, final String groupName) {
         Optional<String> newPrimaryDataSourceName = databaseDiscoveryType.findPrimaryDataSourceName(getActiveDataSourceMap(dataSourceMap, disabledDataSourceNames));
-        if (newPrimaryDataSourceName.isPresent() && !newPrimaryDataSourceName.get().equals(databaseDiscoveryType.getOldPrimaryDataSource())) {
-            databaseDiscoveryType.setOldPrimaryDataSource(newPrimaryDataSourceName.get());
+        if (newPrimaryDataSourceName.isPresent() && !newPrimaryDataSourceName.get().equals(databaseDiscoveryType.getPrimaryDataSource())) {
+            databaseDiscoveryType.setPrimaryDataSource(newPrimaryDataSourceName.get());
             ShardingSphereEventBus.getInstance().post(new PrimaryDataSourceChangedEvent(new QualifiedDatabase(databaseName, groupName, newPrimaryDataSourceName.get())));
         }
         databaseDiscoveryType.updateMemberState(databaseName, dataSourceMap, groupName);
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryType.java
index 643ad823fc9..84a0fd823a2 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryType.java
@@ -48,12 +48,7 @@ public final class CoreFixtureDatabaseDiscoveryType implements DatabaseDiscovery
     }
     
     @Override
-    public String getOldPrimaryDataSource() {
-        return null;
-    }
-    
-    @Override
-    public void setOldPrimaryDataSource(final String oldPrimaryDataSource) {
+    public void setPrimaryDataSource(final String primaryDataSource) {
     }
     
     @Override
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/DistSQLFixtureDatabaseDiscoveryType.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/DistSQLFixtureDatabas [...]
index 5c53b7184f5..31c7f953764 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/DistSQLFixtureDatabaseDiscoveryType.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/DistSQLFixtureDatabaseDiscoveryType.java
@@ -48,12 +48,7 @@ public final class DistSQLFixtureDatabaseDiscoveryType implements DatabaseDiscov
     }
     
     @Override
-    public String getOldPrimaryDataSource() {
-        return null;
-    }
-    
-    @Override
-    public void setOldPrimaryDataSource(final String oldPrimaryDataSource) {
+    public void setPrimaryDataSource(final String primaryDataSource) {
     }
     
     @Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractMySQLDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractMySQLDatabaseDiscoveryType.java
index ef1e29dbb88..f2be8cc09d6 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractMySQLDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractMySQLDatabaseDiscoveryType.java
@@ -41,7 +41,7 @@ public abstract class AbstractMySQLDatabaseDiscoveryType implements DatabaseDisc
     
     private Properties props = new Properties();
     
-    private String oldPrimaryDataSource;
+    private String primaryDataSource;
     
     @Override
     public final Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap) {
@@ -80,9 +80,4 @@ public abstract class AbstractMySQLDatabaseDiscoveryType implements DatabaseDisc
     }
     
     protected abstract Optional<String> loadPrimaryDatabaseInstanceURL(Statement statement) throws SQLException;
-    
-    @Override
-    public final String getPrimaryDataSource() {
-        return oldPrimaryDataSource;
-    }
 }
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/replication/MySQLNormalReplicationMySQLDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalReplicationMySQLD [...]
index 9d1598c6f02..ea78d43cdf6 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalReplicationMySQLDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalReplicationMySQLDatabaseDiscoveryType.java
@@ -68,10 +68,9 @@ public final class MySQLNormalReplicationMySQLDatabaseDiscoveryType extends Abst
     @Override
     public void updateMemberState(final String databaseName, final Map<String, DataSource> dataSourceMap, final String groupName) {
         for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
-            if (entry.getKey().equals(getPrimaryDataSource())) {
-                continue;
+            if (!entry.getKey().equals(getPrimaryDataSource())) {
+                determineDatasourceState(databaseName, entry.getKey(), entry.getValue(), groupName);
             }
-            determineDatasourceState(databaseName, entry.getKey(), entry.getValue(), groupName);
         }
     }
     
@@ -79,25 +78,19 @@ public final class MySQLNormalReplicationMySQLDatabaseDiscoveryType extends Abst
         try (
                 Connection connection = dataSource.getConnection();
                 Statement statement = connection.createStatement()) {
-            long replicationDelayMilliseconds = getSecondsBehindMaster(statement) * 1000L;
-            if (replicationDelayMilliseconds < Long.parseLong(getProps().getProperty("delay-milliseconds-threshold"))) {
-                ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(databaseName, groupName, datasourceName,
-                        new StorageNodeDataSource(StorageNodeRole.MEMBER, StorageNodeStatus.ENABLED, replicationDelayMilliseconds)));
-            } else {
-                ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(databaseName, groupName, datasourceName,
-                        new StorageNodeDataSource(StorageNodeRole.MEMBER, StorageNodeStatus.DISABLED, replicationDelayMilliseconds)));
-            }
+            long replicationDelayMilliseconds = loadReplicationDelayMilliseconds(statement);
+            StorageNodeStatus storageNodeStatus = replicationDelayMilliseconds < Long.parseLong(getProps().getProperty("delay-milliseconds-threshold"))
+                    ? StorageNodeStatus.ENABLED : StorageNodeStatus.DISABLED;
+            ShardingSphereEventBus.getInstance().post(
+                    new DataSourceDisabledEvent(databaseName, groupName, datasourceName, new StorageNodeDataSource(StorageNodeRole.MEMBER, storageNodeStatus, replicationDelayMilliseconds)));
         } catch (SQLException ex) {
             log.error("An exception occurred while find member data source `Seconds_Behind_Master`", ex);
         }
     }
     
-    private long getSecondsBehindMaster(final Statement statement) throws SQLException {
+    private long loadReplicationDelayMilliseconds(final Statement statement) throws SQLException {
         try (ResultSet resultSet = statement.executeQuery(SHOW_SLAVE_STATUS)) {
-            if (resultSet.next()) {
-                return resultSet.getLong("Seconds_Behind_Master");
-            }
-            return 0L;
+            return resultSet.next() ? resultSet.getLong("Seconds_Behind_Master") * 1000L : 0L;
         }
     }
     
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicat [...]
index e7738dbe8c5..3999209281f 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationDatabaseDiscoveryType.java
@@ -47,7 +47,7 @@ public final class OpenGaussNormalReplicationDatabaseDiscoveryType implements Da
     
     private static final String QUERY_DB_ROLE = "SELECT local_role,db_state FROM pg_stat_get_stream_replications()";
     
-    private String oldPrimaryDataSource;
+    private String primaryDataSource;
     
     private Properties props = new Properties();
     
@@ -91,7 +91,7 @@ public final class OpenGaussNormalReplicationDatabaseDiscoveryType implements Da
                 Connection connection = dataSource.getConnection();
                 Statement statement = connection.createStatement();
                 ResultSet resultSet = statement.executeQuery(QUERY_DB_ROLE)) {
-            if (resultSet.next() && ((resultSet.getString("local_role").equals("Standby") && resultSet.getString("db_state").equals("Normal")) || dataSourceName.equals(oldPrimaryDataSource))) {
+            if (resultSet.next() && ((resultSet.getString("local_role").equals("Standby") && resultSet.getString("db_state").equals("Normal")) || dataSourceName.equals(primaryDataSource))) {
                 return false;
             }
         } catch (final SQLException ex) {
@@ -100,11 +100,6 @@ public final class OpenGaussNormalReplicationDatabaseDiscoveryType implements Da
         return true;
     }
     
-    @Override
-    public String getPrimaryDataSource() {
-        return oldPrimaryDataSource;
-    }
-    
     @Override
     public String getType() {
         return "openGauss.NORMAL_REPLICATION";
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipline/cases/mysql/MySQLManualScalingCase.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipline/cases/mysql/MySQLManualScalingCase.java
index 6cc9a1a610c..85c8b7c2a19 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipline/cases/mysql/MySQLManualScalingCase.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipline/cases/mysql/MySQLManualScalingCase.java
@@ -42,7 +42,6 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
-
 /**
  * MySQL auto rule scaling test case.
  */