You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ki...@apache.org on 2020/07/01 06:44:37 UTC

[shardingsphere] branch master updated: Dynamic init or stop heartbeat detection (#6227)

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

kimmking 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 91cee9b  Dynamic init or stop heartbeat detection (#6227)
91cee9b is described below

commit 91cee9bc26db146ba0f090d053e2bebf82bb9bd5
Author: Haoran Meng <lo...@163.com>
AuthorDate: Wed Jul 1 14:44:23 2020 +0800

    Dynamic init or stop heartbeat detection (#6227)
    
    * dynamic stop or init heartbeat detection
    
    * remove useless method
---
 .../cluster/facade/ClusterFacade.java              | 42 +++++++++++++++++++---
 .../cluster/state/ClusterStateInstance.java        | 18 ----------
 .../org/apache/shardingsphere/proxy/Bootstrap.java |  3 +-
 3 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-facade/src/main/java/org/apache/shardingsphere/cluster/facade/ClusterFacade.java b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-facade/src/main/java/org/apache/shardingsphere/cluster/facade/ClusterFacade.java
index 9477e17..75236bc 100644
--- a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-facade/src/main/java/org/apache/shardingsphere/cluster/facade/ClusterFacade.java
+++ b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-facade/src/main/java/org/apache/shardingsphere/cluster/facade/ClusterFacade.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.cluster.facade;
 import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.eventbus.Subscribe;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
 import org.apache.shardingsphere.cluster.heartbeat.ClusterHeartbeatInstance;
 import org.apache.shardingsphere.cluster.heartbeat.response.HeartbeatResponse;
@@ -28,9 +29,13 @@ import org.apache.shardingsphere.cluster.state.ClusterStateInstance;
 import org.apache.shardingsphere.cluster.state.DataSourceState;
 import org.apache.shardingsphere.cluster.state.InstanceState;
 import org.apache.shardingsphere.cluster.state.enums.NodeState;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
 import org.apache.shardingsphere.kernel.context.SchemaContext;
 import org.apache.shardingsphere.orchestration.core.common.event.ClusterConfigurationChangedEvent;
+import org.apache.shardingsphere.orchestration.core.common.event.PropertiesChangedEvent;
 import org.apache.shardingsphere.orchestration.core.common.eventbus.ShardingOrchestrationEventBus;
+import org.apache.shardingsphere.orchestration.core.facade.ShardingOrchestrationFacade;
 
 import java.util.Collection;
 import java.util.HashMap;
@@ -39,12 +44,15 @@ import java.util.Map;
 /**
  * Cluster facade.
  */
+@Slf4j
 public final class ClusterFacade {
     
     private ClusterHeartbeatInstance clusterHeartbeatInstance;
     
     private ClusterStateInstance clusterStateInstance;
     
+    private volatile boolean enabled;
+    
     private ClusterFacade() {
         ShardingOrchestrationEventBus.getInstance().register(this);
     }
@@ -55,10 +63,13 @@ public final class ClusterFacade {
      * @param clusterConfiguration cluster configuration
      */
     public void init(final ClusterConfiguration clusterConfiguration) {
-        Preconditions.checkNotNull(clusterConfiguration, "cluster configuration can not be null.");
-        clusterHeartbeatInstance = ClusterHeartbeatInstance.getInstance();
-        clusterHeartbeatInstance.init(clusterConfiguration.getHeartbeat());
-        clusterStateInstance = ClusterStateInstance.getInstance();
+        if (!enabled) {
+            Preconditions.checkNotNull(clusterConfiguration, "cluster configuration can not be null.");
+            clusterHeartbeatInstance = ClusterHeartbeatInstance.getInstance();
+            clusterHeartbeatInstance.init(clusterConfiguration.getHeartbeat());
+            clusterStateInstance = ClusterStateInstance.getInstance();
+            enabled = true;
+        }
     }
     
     /**
@@ -91,8 +102,29 @@ public final class ClusterFacade {
         init(event.getClusterConfiguration());
     }
     
+    /**
+     *  Renew cluster facade after properties changed.
+     *
+     * @param event properties changed event
+     */
+    @Subscribe
+    public void renew(final PropertiesChangedEvent event) {
+        boolean clusterEnabled = new ConfigurationProperties(event.getProps()).<Boolean>getValue(ConfigurationPropertyKey.PROXY_CLUSTER_ENABLED);
+        if (enabled != clusterEnabled) {
+            if (clusterEnabled) {
+                init(ShardingOrchestrationFacade.getInstance().getConfigCenter().loadClusterConfiguration());
+            } else {
+                stop();
+            }
+        }
+    }
+    
     private void stop() {
-        clusterHeartbeatInstance.close();
+        if (enabled) {
+            clusterHeartbeatInstance.close();
+            enabled = false;
+            log.info("heart beat detect stopped");
+        }
     }
     
     private InstanceState buildInstanceState(final HeartbeatResponse heartbeatResponse) {
diff --git a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/main/java/org/apache/shardingsphere/cluster/state/ClusterStateInstance.java b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/main/java/org/apache/shardingsphere/cluster/state/ClusterStateInstance.java
index 3724390..8645c6b 100644
--- a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/main/java/org/apache/shardingsphere/cluster/state/ClusterStateInstance.java
+++ b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/main/java/org/apache/shardingsphere/cluster/state/ClusterStateInstance.java
@@ -27,10 +27,6 @@ import org.apache.shardingsphere.orchestration.core.common.eventbus.ShardingOrch
 import org.apache.shardingsphere.orchestration.core.facade.ShardingOrchestrationFacade;
 import org.apache.shardingsphere.orchestration.core.registrycenter.event.DisabledStateChangedEvent;
 
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-
 /**
  * Cluster state instance.
  */
@@ -96,20 +92,6 @@ public final class ClusterStateInstance {
         persistInstanceState(instanceState);
     }
     
-    /**
-     * Load all instance states.
-     *
-     * @return all instance states
-     */
-    public Map<String, InstanceState> loadAllInstanceStates() {
-        Collection<String> instances = ShardingOrchestrationFacade.getInstance().getRegistryCenter().loadAllInstances();
-        Map<String, InstanceState> instanceStateMap = new HashMap<>();
-        instances.forEach(each -> {
-            instanceStateMap.put(each, loadInstanceState(each));
-        });
-        return instanceStateMap;
-    }
-    
     private static class ClusterStateInstanceHolder {
         
         private static final ClusterStateInstance INSTANCE = new ClusterStateInstance();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
index 809314c..67d575d 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
@@ -251,7 +251,8 @@ public final class Bootstrap {
     }
     
     private static void initCluster(final ClusterConfiguration clusterConfiguration) {
-        if (ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_CLUSTER_ENABLED)) {
+        if (null != ClusterFacade.getInstance()
+                && ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_CLUSTER_ENABLED)) {
             ClusterFacade.getInstance().init(clusterConfiguration);
         }
     }