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/28 06:31:57 UTC

[shardingsphere] branch master updated: MySQLNormalReplicationDatabaseDiscovery support min-enabled-replicas (#18465)

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 6da3318d8e4 MySQLNormalReplicationDatabaseDiscovery support min-enabled-replicas (#18465)
6da3318d8e4 is described below

commit 6da3318d8e451ad5a3c0820ad895c8c71952d38c
Author: natehuang <na...@tencent.com>
AuthorDate: Tue Jun 28 14:31:49 2022 +0800

    MySQLNormalReplicationDatabaseDiscovery support min-enabled-replicas (#18465)
    
    * MySQLNormalReplicationDatabaseDiscovery support min-enabled-replicas
    
    * remove extra blank line
---
 .../algorithm/DatabaseDiscoveryEngine.java          | 21 +++++++++++++++++----
 .../mode/metadata/storage/StorageNodeStatus.java    | 10 ++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)

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 0c82cd02759..a999ea23d6d 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
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.dbdiscovery.algorithm;
 
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.dbdiscovery.mysql.type.MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm;
 import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
 import org.apache.shardingsphere.dbdiscovery.spi.ReplicaDataSourceStatus;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
@@ -73,7 +74,7 @@ public final class DatabaseDiscoveryEngine {
             ShardingSphereEventBus.getInstance().post(new PrimaryDataSourceChangedEvent(new QualifiedDatabase(databaseName, groupName, newPrimaryDataSourceName.get())));
         }
         String result = newPrimaryDataSourceName.orElse(originalPrimaryDataSourceName);
-        postReplicaDataSourceDisabledEvent(databaseName, groupName, result, dataSourceMap);
+        postReplicaDataSourceDisabledEvent(databaseName, groupName, result, dataSourceMap, disabledDataSourceNames);
         return result;
     }
     
@@ -97,11 +98,23 @@ public final class DatabaseDiscoveryEngine {
         }
         return result;
     }
-    
-    private void postReplicaDataSourceDisabledEvent(final String databaseName, final String groupName, final String primaryDataSourceName, final Map<String, DataSource> dataSourceMap) {
+
+    private void postReplicaDataSourceDisabledEvent(final String databaseName, final String groupName, final String primaryDataSourceName,
+            final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
+        int enabledReplicasCount = dataSourceMap.size() - disabledDataSourceNames.size() - 1;
         for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
             if (!entry.getKey().equals(primaryDataSourceName)) {
-                ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(databaseName, groupName, entry.getKey(), createStorageNodeDataSource(loadReplicaStatus(entry.getValue()))));
+                StorageNodeDataSource storageNodeDataSource = createStorageNodeDataSource(loadReplicaStatus(entry.getValue()));
+                if (StorageNodeStatus.isEnable(storageNodeDataSource.getStatus())) {
+                    enabledReplicasCount += disabledDataSourceNames.contains(entry.getKey()) ? 1 : 0;
+                    ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(databaseName, groupName, entry.getKey(), storageNodeDataSource));
+                    continue;
+                }
+                if (!(databaseDiscoveryProviderAlgorithm instanceof MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm)
+                        || enabledReplicasCount > Integer.parseInt(databaseDiscoveryProviderAlgorithm.getProps().getProperty("min-enabled-replicas", "0"))) {
+                    enabledReplicasCount -= disabledDataSourceNames.contains(entry.getKey()) ? 0 : 1;
+                    ShardingSphereEventBus.getInstance().post(new DataSourceDisabledEvent(databaseName, groupName, entry.getKey(), storageNodeDataSource));
+                }
             }
         }
     }
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/storage/StorageNodeStatus.java b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/storage/StorageNodeStatus.java
index 66b7b71ec95..cdbc2af99a3 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/storage/StorageNodeStatus.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/storage/StorageNodeStatus.java
@@ -33,4 +33,14 @@ public enum StorageNodeStatus {
     public static boolean isDisable(final String status) {
         return DISABLED.name().toLowerCase().equals(status);
     }
+    
+    /**
+     * Storage node disable or enable.
+     *
+     * @param status storage node status
+     * @return disable or enable
+     */
+    public static boolean isEnable(final String status) {
+        return ENABLED.name().toLowerCase().equals(status);
+    }
 }