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/06/15 14:40:07 UTC

[shardingsphere] branch master updated: Persist cluster configuration to config center (#6049)

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 743682d  Persist cluster configuration to config center (#6049)
743682d is described below

commit 743682dd2f26a8d006038087e78775cebf07f05a
Author: Haoran Meng <lo...@163.com>
AuthorDate: Mon Jun 15 22:39:52 2020 +0800

    Persist cluster configuration to config center (#6049)
---
 .../core/configcenter/ConfigCenter.java            | 32 ++++++++++++++++++++--
 .../pom.xml                                        |  5 ++++
 .../core/facade/ShardingOrchestrationFacade.java   | 12 +++++++-
 .../org/apache/shardingsphere/proxy/Bootstrap.java | 15 ++++++----
 4 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
index ed8c2fd..e55a59c 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
@@ -21,6 +21,9 @@ import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
+import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
+import org.apache.shardingsphere.cluster.configuration.swapper.ClusterConfigurationYamlSwapper;
+import org.apache.shardingsphere.cluster.configuration.yaml.YamlClusterConfiguration;
 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
 import org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;
@@ -71,7 +74,7 @@ public final class ConfigCenter {
      * @param ruleConfigurations rule configurations
      * @param authentication authentication
      * @param props sharding properties
-     * @param isOverwrite is overwrite registry center's configuration
+     * @param isOverwrite is overwrite config center's configuration
      */
     public void persistConfigurations(final String shardingSchemaName, final Map<String, DataSourceConfiguration> dataSourceConfigs, final Collection<RuleConfiguration> ruleConfigurations,
                                       final Authentication authentication, final Properties props, final boolean isOverwrite) {
@@ -152,7 +155,7 @@ public final class ConfigCenter {
      * Persist metrics configuration.
      *
      * @param metricsConfiguration  metrics configuration.
-     * @param isOverwrite is overwrite registry center's configuration
+     * @param isOverwrite is overwrite config center's configuration
      */
     public void persistMetricsConfiguration(final MetricsConfiguration metricsConfiguration, final boolean isOverwrite) {
         if (null != metricsConfiguration && (isOverwrite || !hasMetricsConfiguration())) {
@@ -160,6 +163,22 @@ public final class ConfigCenter {
         }
     }
     
+    /**
+     * Persist cluster configuration.
+     *
+     * @param clusterConfiguration cluster configuration
+     * @param isOverwrite is overwrite config center's configuration
+     */
+    public void persistClusterConfiguration(final ClusterConfiguration clusterConfiguration, final boolean isOverwrite) {
+        if (null != clusterConfiguration && (isOverwrite || !hasClusterConfiguration())) {
+            repository.persist(node.getClusterPath(), YamlEngine.marshal(new ClusterConfigurationYamlSwapper().swapToYamlConfiguration(clusterConfiguration)));
+        }
+    }
+    
+    private boolean hasClusterConfiguration() {
+        return !Strings.isNullOrEmpty(repository.get(node.getMetricsPath()));
+    }
+    
     private boolean hasMetricsConfiguration() {
         return !Strings.isNullOrEmpty(repository.get(node.getMetricsPath()));
     }
@@ -251,6 +270,15 @@ public final class ConfigCenter {
     }
     
     /**
+     * Load cluster configuration.
+     *
+     * @return cluster configuration
+     */
+    public ClusterConfiguration loadClusterConfiguration() {
+        return new ClusterConfigurationYamlSwapper().swapToObject(YamlEngine.unmarshal(repository.get(node.getClusterPath()), YamlClusterConfiguration.class));
+    }
+    
+    /**
      * Get all sharding schema names.
      * 
      * @return all sharding schema names
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configuration/pom.xml b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configuration/pom.xml
index 54af448..5c666bc 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configuration/pom.xml
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configuration/pom.xml
@@ -43,5 +43,10 @@
             <artifactId>shardingsphere-metrics-configuration</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-cluster-configuration</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 </project>
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-facade/src/main/java/org/apache/shardingsphere/orchestration/core/facade/ShardingOrchestrationFacade.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-facade/src/main/java/org/apache/shardingsphere/orchestration/core/facade/ShardingOrchestrationFacade.java
index 210bb61..7fa79db 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-facade/src/main/java/org/apache/shardingsphere/orchestration/core/facade/ShardingOrchestrationFacade.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-facade/src/main/java/org/apache/shardingsphere/orchestration/core/facade/ShardingOrchestrationFacade.java
@@ -21,6 +21,7 @@ import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
 import org.apache.shardingsphere.infra.auth.Authentication;
 import org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;
 import org.apache.shardingsphere.orchestration.center.ConfigCenterRepository;
@@ -141,12 +142,21 @@ public final class ShardingOrchestrationFacade implements AutoCloseable {
     /**
      * Init metrics configuration to config center.
      *
-     * @param metricsConfiguration metrics configuration.
+     * @param metricsConfiguration metrics configuration
      */
     public void initMetricsConfiguration(final MetricsConfiguration metricsConfiguration) {
         configCenter.persistMetricsConfiguration(metricsConfiguration, isOverwrite);
     }
     
+    /**
+     * Init cluster configuration to config center.
+     *
+     * @param clusterConfiguration cluster configuration
+     */
+    public void initClusterConfiguration(final ClusterConfiguration clusterConfiguration) {
+        configCenter.persistClusterConfiguration(clusterConfiguration, isOverwrite);
+    }
+    
     @Override
     public void close() {
         try {
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 264933d..f97d04a 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
@@ -21,6 +21,7 @@ import com.google.common.primitives.Ints;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
 import org.apache.shardingsphere.cluster.configuration.swapper.ClusterConfigurationYamlSwapper;
 import org.apache.shardingsphere.cluster.configuration.yaml.YamlClusterConfiguration;
 import org.apache.shardingsphere.cluster.facade.ClusterFacade;
@@ -124,7 +125,8 @@ public final class Bootstrap {
         Authentication authentication = new AuthenticationYamlSwapper().swapToObject(yamlAuthenticationConfig);
         Map<String, Map<String, DataSourceParameter>> schemaDataSources = getDataSourceParametersMap(ruleConfigs);
         Map<String, Collection<RuleConfiguration>> schemaRules = getRuleConfigurations(ruleConfigs);
-        initialize(authentication, properties, schemaDataSources, schemaRules, new MetricsConfigurationYamlSwapper().swapToObject(metricsConfiguration), clusterConfiguration, false);
+        initialize(authentication, properties, schemaDataSources, schemaRules, new MetricsConfigurationYamlSwapper().swapToObject(metricsConfiguration),
+                new ClusterConfigurationYamlSwapper().swapToObject(clusterConfiguration), false);
         ShardingSphereProxy.getInstance().start(port);
     }
     
@@ -138,14 +140,15 @@ public final class Bootstrap {
             Map<String, Map<String, DataSourceParameter>> schemaDataSources = getDataSourceParametersMap(shardingOrchestrationFacade);
             Map<String, Collection<RuleConfiguration>> schemaRules = getSchemaRules(shardingOrchestrationFacade);
             MetricsConfiguration metricsConfiguration = shardingOrchestrationFacade.getConfigCenter().loadMetricsConfiguration();
-            initialize(authentication, properties, schemaDataSources, schemaRules, metricsConfiguration, serverConfig.getCluster(), true);
+            ClusterConfiguration clusterConfiguration = shardingOrchestrationFacade.getConfigCenter().loadClusterConfiguration();
+            initialize(authentication, properties, schemaDataSources, schemaRules, metricsConfiguration, clusterConfiguration, true);
             ShardingSphereProxy.getInstance().start(port);
         }
     }
     
     private static void initialize(final Authentication authentication, final Properties properties, final Map<String, Map<String, DataSourceParameter>> schemaDataSources,
                                    final Map<String, Collection<RuleConfiguration>> schemaRules, final MetricsConfiguration metricsConfiguration,
-                                   final YamlClusterConfiguration cluster, final boolean isOrchestration) throws SQLException {
+                                   final ClusterConfiguration cluster, final boolean isOrchestration) throws SQLException {
         initProxySchemaContexts(schemaDataSources, schemaRules, authentication, properties, isOrchestration);
         log(authentication, properties);
         initMetrics(metricsConfiguration);
@@ -211,6 +214,8 @@ public final class Bootstrap {
                     getRuleConfigurations(ruleConfigs), new AuthenticationYamlSwapper().swapToObject(serverConfig.getAuthentication()), serverConfig.getProps());
         }
         shardingOrchestrationFacade.initMetricsConfiguration(new MetricsConfigurationYamlSwapper().swapToObject(serverConfig.getMetrics()));
+        shardingOrchestrationFacade.initClusterConfiguration(null == serverConfig.getCluster() ? null
+                : new ClusterConfigurationYamlSwapper().swapToObject(serverConfig.getCluster()));
     }
     
     private static void initOpenTracing() {
@@ -225,9 +230,9 @@ public final class Bootstrap {
         }
     }
     
-    private static void initCluster(final YamlClusterConfiguration clusterConfiguration) {
+    private static void initCluster(final ClusterConfiguration clusterConfiguration) {
         if (ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_CLUSTER_ENABLED)) {
-            ClusterFacade.getInstance().init(new ClusterConfigurationYamlSwapper().swapToObject(clusterConfiguration));
+            ClusterFacade.getInstance().init(clusterConfiguration);
         }
     }