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 2020/07/05 11:35:15 UTC

[shardingsphere] branch master updated: Persist part of configurations to registry center (#6268)

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

zhangliang 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 7591956  Persist  part of configurations to registry center (#6268)
7591956 is described below

commit 7591956ea19bc2740c602ba5195d12e539bb2b89
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Sun Jul 5 19:34:57 2020 +0800

    Persist  part of configurations to registry center (#6268)
    
    * Persist  part of configurations to registry center
    
    * fix unit tests
    
    * rename function
---
 .../core/configcenter/ConfigCenter.java            | 72 +++++++++-------------
 .../core/configcenter/ConfigCenterTest.java        | 29 +++------
 .../OrchestrationShardingSphereDataSourceTest.java | 12 +++-
 .../org/apache/shardingsphere/proxy/Bootstrap.java |  6 +-
 4 files changed, 53 insertions(+), 66 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 ef1ff12..3c14bb5 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
@@ -98,7 +98,7 @@ public final class ConfigCenter {
     }
     
     private void persistDataSourceConfiguration(final String shardingSchemaName, final Map<String, DataSourceConfiguration> dataSourceConfigurations, final boolean isOverwrite) {
-        if (isOverwrite || !hasDataSourceConfiguration(shardingSchemaName)) {
+        if (isOverwrite) {
             Preconditions.checkState(null != dataSourceConfigurations && !dataSourceConfigurations.isEmpty(), "No available data source in `%s` for orchestration.", shardingSchemaName);
             Map<String, YamlDataSourceConfiguration> yamlDataSourceConfigurations = dataSourceConfigurations.entrySet().stream()
                     .collect(Collectors.toMap(Entry::getKey, entry -> new DataSourceConfigurationYamlSwapper().swapToYamlConfiguration(entry.getValue())));
@@ -106,21 +106,11 @@ public final class ConfigCenter {
         }
     }
     
-    /**
-     * Judge whether schema has data source configuration.
-     *
-     * @param shardingSchemaName shading schema name
-     * @return has data source configuration or not
-     */
-    public boolean hasDataSourceConfiguration(final String shardingSchemaName) {
-        return !Strings.isNullOrEmpty(repository.get(node.getDataSourcePath(shardingSchemaName)));
-    }
-    
     private void persistRuleConfigurations(final String shardingSchemaName, final Collection<RuleConfiguration> ruleConfigurations, final boolean isOverwrite) {
         if (ruleConfigurations.isEmpty()) {
             return;
         }
-        if (isOverwrite || !hasRuleConfiguration(shardingSchemaName)) {
+        if (isOverwrite) {
             persistRuleConfigurations(shardingSchemaName, ruleConfigurations);
         }
     }
@@ -168,23 +158,13 @@ public final class ConfigCenter {
     }
     
     /**
-     * Judge whether schema has rule configuration.
-     *
-     * @param shardingSchemaName sharding schema name
-     * @return has rule configuration or not
-     */
-    public boolean hasRuleConfiguration(final String shardingSchemaName) {
-        return !Strings.isNullOrEmpty(repository.get(node.getRulePath(shardingSchemaName)));
-    }
-    
-    /**
      * Persist metrics configuration.
      *
      * @param metricsConfiguration  metrics configuration.
      * @param isOverwrite is overwrite config center's configuration
      */
     public void persistMetricsConfiguration(final MetricsConfiguration metricsConfiguration, final boolean isOverwrite) {
-        if (null != metricsConfiguration && (isOverwrite || !hasMetricsConfiguration())) {
+        if (null != metricsConfiguration && isOverwrite) {
             repository.persist(node.getMetricsPath(), YamlEngine.marshal(new MetricsConfigurationYamlSwapper().swapToYamlConfiguration(metricsConfiguration)));
         }
     }
@@ -196,39 +176,23 @@ public final class ConfigCenter {
      * @param isOverwrite is overwrite config center's configuration
      */
     public void persistClusterConfiguration(final ClusterConfiguration clusterConfiguration, final boolean isOverwrite) {
-        if (null != clusterConfiguration && (isOverwrite || !hasClusterConfiguration())) {
+        if (null != clusterConfiguration && isOverwrite) {
             repository.persist(node.getClusterPath(), YamlEngine.marshal(new ClusterConfigurationYamlSwapper().swapToYamlConfiguration(clusterConfiguration)));
         }
     }
     
-    private boolean hasClusterConfiguration() {
-        return !Strings.isNullOrEmpty(repository.get(node.getClusterPath()));
-    }
-    
-    private boolean hasMetricsConfiguration() {
-        return !Strings.isNullOrEmpty(repository.get(node.getMetricsPath()));
-    }
-    
     private void persistAuthentication(final Authentication authentication, final boolean isOverwrite) {
-        if (null != authentication && (isOverwrite || !hasAuthentication())) {
+        if (null != authentication && isOverwrite) {
             repository.persist(node.getAuthenticationPath(), YamlEngine.marshal(new AuthenticationYamlSwapper().swapToYamlConfiguration(authentication)));
         }
     }
     
-    private boolean hasAuthentication() {
-        return !Strings.isNullOrEmpty(repository.get(node.getAuthenticationPath()));
-    }
-    
     private void persistProperties(final Properties props, final boolean isOverwrite) {
-        if (isOverwrite || !hasProperties()) {
+        if (!props.isEmpty() && isOverwrite) {
             repository.persist(node.getPropsPath(), YamlEngine.marshal(props));
         }
     }
     
-    private boolean hasProperties() {
-        return !Strings.isNullOrEmpty(repository.get(node.getPropsPath()));
-    }
-    
     private void persistShardingSchemaName(final String shardingSchemaName) {
         String shardingSchemaNames = repository.get(node.getSchemaPath());
         if (Strings.isNullOrEmpty(shardingSchemaNames)) {
@@ -319,4 +283,28 @@ public final class ConfigCenter {
         String shardingSchemaNames = repository.get(node.getSchemaPath());
         return Strings.isNullOrEmpty(shardingSchemaNames) ? new LinkedList<>() : node.splitShardingSchemaName(shardingSchemaNames);
     }
+    
+    /**
+     * Judge whether schema has rule configuration.
+     *
+     * @param shardingSchemaName sharding schema name
+     * @return has rule configuration or not
+     */
+    public boolean hasRuleConfiguration(final String shardingSchemaName) {
+        return !Strings.isNullOrEmpty(repository.get(node.getRulePath(shardingSchemaName)));
+    }
+    
+    /**
+     * Judge whether schema has data source configuration.
+     *
+     * @param shardingSchemaName shading schema name
+     * @return has data source configuration or not
+     */
+    public boolean hasDataSourceConfiguration(final String shardingSchemaName) {
+        return !Strings.isNullOrEmpty(repository.get(node.getDataSourcePath(shardingSchemaName)));
+    }
+    
+    private boolean hasAuthentication() {
+        return !Strings.isNullOrEmpty(repository.get(node.getAuthenticationPath()));
+    }
 }
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/test/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenterTest.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/test/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenterTest.java
index 9d3b197..19c35ef 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/test/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenterTest.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/test/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenterTest.java
@@ -232,8 +232,6 @@ public final class ConfigCenterTest {
     
     @Test
     public void assertPersistConfigurationForShardingRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() {
-        when(configCenterRepository.get("/test/config/schema/sharding_db/datasource")).thenReturn(DATA_SOURCE_YAML);
-        when(configCenterRepository.get("/test/config/schema/sharding_db/rule")).thenReturn(SHARDING_RULE_YAML);
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false);
         verify(configCenterRepository, times(0)).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
@@ -242,8 +240,6 @@ public final class ConfigCenterTest {
     
     @Test
     public void assertMoreShardingSchema() {
-        when(configCenterRepository.get("/test/config/schema/sharding_db/datasource")).thenReturn(DATA_SOURCE_YAML);
-        when(configCenterRepository.get("/test/config/schema/sharding_db/rule")).thenReturn(SHARDING_RULE_YAML);
         when(configCenterRepository.get("/test/config/schema")).thenReturn("myTest1,myTest2");
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false);
@@ -252,8 +248,6 @@ public final class ConfigCenterTest {
     
     @Test
     public void assertMoreAndContainsShardingSchema() {
-        when(configCenterRepository.get("/test/config/schema/sharding_db/datasource")).thenReturn(DATA_SOURCE_YAML);
-        when(configCenterRepository.get("/test/config/schema/sharding_db/rule")).thenReturn(SHARDING_RULE_YAML);
         when(configCenterRepository.get("/test/config/schema")).thenReturn("myTest1,sharding_db");
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false);
@@ -264,8 +258,9 @@ public final class ConfigCenterTest {
     public void assertPersistConfigurationForShardingRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsNotExisted() {
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false);
-        verify(configCenterRepository).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
-        verify(configCenterRepository).persist("/test/config/schema/sharding_db/rule", SHARDING_RULE_YAML);
+        verify(configCenterRepository, times(0)).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
+        verify(configCenterRepository, times(0)).persist("/test/config/schema/sharding_db/rule", SHARDING_RULE_YAML);
+        
     }
     
     @Test
@@ -278,8 +273,6 @@ public final class ConfigCenterTest {
     
     @Test
     public void assertPersistConfigurationForMasterSlaveRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() {
-        when(configCenterRepository.get("/test/config/schema/sharding_db/datasource")).thenReturn(DATA_SOURCE_YAML);
-        when(configCenterRepository.get("/test/config/schema/sharding_db/rule")).thenReturn(MASTER_SLAVE_RULE_YAML);
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createMasterSlaveRuleConfiguration(), false);
         verify(configCenterRepository, times(0)).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
@@ -290,8 +283,8 @@ public final class ConfigCenterTest {
     public void assertPersistConfigurationForMasterSlaveRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsNotExisted() {
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createMasterSlaveRuleConfiguration(), false);
-        verify(configCenterRepository).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
-        verify(configCenterRepository).persist("/test/config/schema/sharding_db/rule", MASTER_SLAVE_RULE_YAML);
+        verify(configCenterRepository, times(0)).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
+        verify(configCenterRepository, times(0)).persist("/test/config/schema/sharding_db/rule", MASTER_SLAVE_RULE_YAML);
     }
     
     @Test
@@ -304,8 +297,6 @@ public final class ConfigCenterTest {
     
     @Test
     public void assertPersistConfigurationForShardingRuleWithAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() {
-        when(configCenterRepository.get("/test/config/schema/sharding_db/datasource")).thenReturn(DATA_SOURCE_PARAMETER_YAML);
-        when(configCenterRepository.get("/test/config/schema/sharding_db/rule")).thenReturn(SHARDING_RULE_YAML);
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false);
         verify(configCenterRepository, times(0)).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
@@ -316,8 +307,8 @@ public final class ConfigCenterTest {
     public void assertPersistConfigurationForShardingRuleWithAuthenticationAndIsNotOverwriteAndConfigurationIsNotExisted() {
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false);
-        verify(configCenterRepository).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
-        verify(configCenterRepository).persist("/test/config/schema/sharding_db/rule", SHARDING_RULE_YAML);
+        verify(configCenterRepository, times(0)).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
+        verify(configCenterRepository, times(0)).persist("/test/config/schema/sharding_db/rule", SHARDING_RULE_YAML);
     }
     
     @Test
@@ -330,8 +321,6 @@ public final class ConfigCenterTest {
     
     @Test
     public void assertPersistConfigurationForMasterSlaveRuleWithAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() {
-        when(configCenterRepository.get("/test/config/schema/sharding_db/datasource")).thenReturn(DATA_SOURCE_PARAMETER_YAML);
-        when(configCenterRepository.get("/test/config/schema/sharding_db/rule")).thenReturn(MASTER_SLAVE_RULE_YAML);
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db", createDataSourceConfigurations(), createMasterSlaveRuleConfiguration(), false);
         verify(configCenterRepository, times(0)).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
@@ -343,8 +332,8 @@ public final class ConfigCenterTest {
         ConfigCenter configurationService = new ConfigCenter("test", configCenterRepository);
         configurationService.persistConfigurations("sharding_db",
                 createDataSourceConfigurations(), createMasterSlaveRuleConfiguration(), false);
-        verify(configCenterRepository).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
-        verify(configCenterRepository).persist("/test/config/schema/sharding_db/rule", MASTER_SLAVE_RULE_YAML);
+        verify(configCenterRepository, times(0)).persist(eq("/test/config/schema/sharding_db/datasource"), ArgumentMatchers.any());
+        verify(configCenterRepository, times(0)).persist("/test/config/schema/sharding_db/rule", MASTER_SLAVE_RULE_YAML);
     }
     
     @Test
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSourceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSourceTest.java
index 51ed490..deab71a 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSourceTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSourceTest.java
@@ -79,7 +79,9 @@ public final class OrchestrationShardingSphereDataSourceTest {
     }
     
     private static CenterConfiguration getRegistryCenterConfiguration() {
-        CenterConfiguration result = new CenterConfiguration("FourthTestRegistryCenter", new Properties());
+        Properties properties = new Properties();
+        properties.setProperty("overwrite", "true");
+        CenterConfiguration result = new CenterConfiguration("FourthTestRegistryCenter", properties);
         result.setOrchestrationType(CenterType.REGISTRY_CENTER.getValue());
         result.setNamespace("test_sharding_registry");
         result.setServerLists("localhost:3181");
@@ -87,7 +89,9 @@ public final class OrchestrationShardingSphereDataSourceTest {
     }
     
     private static CenterConfiguration getConfigCenterConfiguration() {
-        CenterConfiguration result = new CenterConfiguration("FourthTestConfigCenter", new Properties());
+        Properties properties = new Properties();
+        properties.setProperty("overwrite", "true");
+        CenterConfiguration result = new CenterConfiguration("FourthTestConfigCenter", properties);
         result.setOrchestrationType(CenterType.CONFIG_CENTER.getValue());
         result.setNamespace("test_sharding_config");
         result.setServerLists("localhost:3181");
@@ -95,7 +99,9 @@ public final class OrchestrationShardingSphereDataSourceTest {
     }
     
     private static CenterConfiguration getMetaDataCenterConfiguration() {
-        CenterConfiguration result = new CenterConfiguration("FirstTestMetaDataCenter", new Properties());
+        Properties properties = new Properties();
+        properties.setProperty("overwrite", "true");
+        CenterConfiguration result = new CenterConfiguration("FirstTestMetaDataCenter", properties);
         result.setOrchestrationType(CenterType.METADATA_CENTER.getValue());
         result.setNamespace("test_encrypt_metadata");
         result.setServerLists("localhost:3181");
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 146f458..4b8df01 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
@@ -216,7 +216,7 @@ public final class Bootstrap {
     
     private static void initShardingOrchestrationFacade(
             final YamlProxyServerConfiguration serverConfig, final Map<String, YamlProxyRuleConfiguration> ruleConfigs, final ShardingOrchestrationFacade shardingOrchestrationFacade) {
-        if (ruleConfigs.isEmpty()) {
+        if (isEmptyLocalConfiguration(serverConfig, ruleConfigs)) {
             shardingOrchestrationFacade.init();
         } else {
             shardingOrchestrationFacade.init(getDataSourceConfigurationMap(ruleConfigs),
@@ -226,6 +226,10 @@ public final class Bootstrap {
         shardingOrchestrationFacade.initClusterConfiguration(Optional.ofNullable(serverConfig.getCluster()).map(new ClusterConfigurationYamlSwapper()::swapToObject).orElse(null));
     }
     
+    private static boolean isEmptyLocalConfiguration(final YamlProxyServerConfiguration serverConfig, final Map<String, YamlProxyRuleConfiguration> ruleConfigs) {
+        return ruleConfigs.isEmpty() && null == serverConfig.getAuthentication() && serverConfig.getProps().isEmpty();
+    }
+    
     private static void initCluster(final ClusterConfiguration clusterConfiguration) {
         if (null != ClusterFacade.getInstance()
                 && ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_CLUSTER_ENABLED)) {