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 2021/08/19 08:06:12 UTC

[shardingsphere] branch master updated: Only create data sources that are different from local when building ContextManager (#11885)

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 27a4dcc  Only create data sources that are different from local when building ContextManager (#11885)
27a4dcc is described below

commit 27a4dcca0ac9034a647077ce4f15332287947394
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Thu Aug 19 16:05:34 2021 +0800

    Only create data sources that are different from local when building ContextManager (#11885)
    
    * Only create data sources that are different from local when building ContextManager
    
    * Only create data sources that are different from local when building ContextManager
    
    * Only create data sources that are different from local when building ContextManager
    
    * Fix unit test
    
    * Fix unit test
    
    * Fix unit test
    
    * Use new HashMap to instead of emptyMap
    
    * Only create data sources that are different from local when building ContextManager
    
    * For check style
---
 .../context/ClusterContextManagerBuilder.java      | 34 +++++++++++++++++++---
 .../impl/StandaloneContextManagerBuilder.java      | 33 ++++++++++++++++++---
 .../GovernanceShardingSphereDataSource.java        |  3 +-
 .../fixture/TestRegistryCenterRepository.java      | 14 ++++-----
 4 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/ClusterContextManagerBuilder.java b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/ClusterContextManagerBuilder.java
index 02b11f8..c6c24e5 100644
--- a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/ClusterContextManagerBuilder.java
+++ b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/ClusterContextManagerBuilder.java
@@ -38,7 +38,6 @@ import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -61,6 +60,7 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
         DistMetaDataPersistService persistService = new DistMetaDataPersistService(persistRepository.get());
         RegistryCenter registryCenter = new RegistryCenter((RegistryCenterRepository) persistRepository.get());
         persistConfigurations(persistService, dataSourcesMap, schemaRuleConfigs, globalRuleConfigs, props, isOverwrite);
+        // TODO Here may be some problems to load all schemaNames for JDBC
         Collection<String> schemaNames = persistService.getSchemaMetaDataService().loadAllNames();
         MetaDataContexts metaDataContexts;
         // TODO isEmpty for test reg center fixture, will remove after local memory reg center fixture finished
@@ -111,7 +111,13 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
         Map<String, Map<String, DataSourceConfiguration>> loadedDataSourceConfigs = loadDataSourceConfigurations(persistService, schemaNames);
         Map<String, Map<String, DataSourceConfiguration>> changedDataSourceConfigs = getChangedDataSourceConfigurations(dataSourcesMap, loadedDataSourceConfigs);
         Map<String, Map<String, DataSource>> result = new LinkedHashMap<>(dataSourcesMap);
-        result.putAll(getChangedDataSources(changedDataSourceConfigs));
+        getChangedDataSources(changedDataSourceConfigs).entrySet().forEach(entry -> {
+            if (result.containsKey(entry.getKey())) {
+                result.get(entry.getKey()).putAll(entry.getValue());
+            } else {
+                result.put(entry.getKey(), entry.getValue());
+            }
+        });
         return result;
     }
     
@@ -123,10 +129,30 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
         return result;
     }
     
-    // TODO finish this method
     private Map<String, Map<String, DataSourceConfiguration>> getChangedDataSourceConfigurations(final Map<String, Map<String, DataSource>> configuredDataSourcesMap, 
                                                                                                  final Map<String, Map<String, DataSourceConfiguration>> loadedDataSourceConfigs) {
-        return isEmptyLocalDataSourcesMap(configuredDataSourcesMap) ? loadedDataSourceConfigs : Collections.emptyMap();
+        if (isEmptyLocalDataSourcesMap(configuredDataSourcesMap)) {
+            return loadedDataSourceConfigs;
+        }
+        Map<String, Map<String, DataSourceConfiguration>> result = new HashMap<>(loadedDataSourceConfigs.size(), 1);
+        for (Entry<String, Map<String, DataSourceConfiguration>> entry : loadedDataSourceConfigs.entrySet()) {
+            if (configuredDataSourcesMap.containsKey(entry.getKey())) {
+                Map<String, DataSourceConfiguration> changedDataSources = getChangedDataSourcesConfigurations(configuredDataSourcesMap.get(entry.getKey()), entry.getValue());
+                if (!changedDataSources.isEmpty()) {
+                    result.put(entry.getKey(), changedDataSources);
+                }
+            } else {
+                result.put(entry.getKey(), entry.getValue());
+            }
+        }
+        return result;
+    }
+    
+    private Map<String, DataSourceConfiguration> getChangedDataSourcesConfigurations(final Map<String, DataSource> dataSourceMap, 
+                                                                                     final Map<String, DataSourceConfiguration> loadedDataSourceConfigurationMap) {
+        Map<String, DataSourceConfiguration> dataSourceConfigurationMap = DataSourceConverter.getDataSourceConfigurationMap(dataSourceMap);
+        return loadedDataSourceConfigurationMap.entrySet().stream().filter(entry -> !dataSourceConfigurationMap.containsKey(entry.getKey()) 
+                || !dataSourceConfigurationMap.get(entry.getKey()).equals(entry.getValue())).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
     }
     
     private Map<String, Map<String, DataSource>> getChangedDataSources(final Map<String, Map<String, DataSourceConfiguration>> changedDataSourceConfigurations) {
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/StandaloneContextManagerBuilder.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/StandaloneContextManagerBuilder.java
index a4ce02a..78dc6fa 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/StandaloneContextManagerBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/manager/impl/StandaloneContextManagerBuilder.java
@@ -36,7 +36,6 @@ import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -108,7 +107,13 @@ public final class StandaloneContextManagerBuilder implements ContextManagerBuil
         Map<String, Map<String, DataSourceConfiguration>> loadedDataSourceConfigs = loadDataSourceConfigurations(persistService, schemaNames);
         Map<String, Map<String, DataSourceConfiguration>> changedDataSourceConfigs = getChangedDataSourceConfigurations(dataSourcesMap, loadedDataSourceConfigs);
         Map<String, Map<String, DataSource>> result = new LinkedHashMap<>(dataSourcesMap);
-        result.putAll(getChangedDataSources(changedDataSourceConfigs));
+        getChangedDataSources(changedDataSourceConfigs).entrySet().forEach(entry -> {
+            if (result.containsKey(entry.getKey())) {
+                result.get(entry.getKey()).putAll(entry.getValue());
+            } else {
+                result.put(entry.getKey(), entry.getValue());
+            }
+        });
         return result;
     }
     
@@ -120,10 +125,30 @@ public final class StandaloneContextManagerBuilder implements ContextManagerBuil
         return result;
     }
     
-    // TODO finish this method
     private Map<String, Map<String, DataSourceConfiguration>> getChangedDataSourceConfigurations(final Map<String, Map<String, DataSource>> configuredDataSourcesMap,
                                                                                                  final Map<String, Map<String, DataSourceConfiguration>> loadedDataSourceConfigs) {
-        return isEmptyLocalDataSourcesMap(configuredDataSourcesMap) ? loadedDataSourceConfigs : Collections.emptyMap();
+        if (isEmptyLocalDataSourcesMap(configuredDataSourcesMap)) {
+            return loadedDataSourceConfigs;
+        }
+        Map<String, Map<String, DataSourceConfiguration>> result = new HashMap<>(loadedDataSourceConfigs.size(), 1);
+        for (Entry<String, Map<String, DataSourceConfiguration>> entry : loadedDataSourceConfigs.entrySet()) {
+            if (configuredDataSourcesMap.containsKey(entry.getKey())) {
+                Map<String, DataSourceConfiguration> changedDataSources = getChangedDataSourcesConfigurations(configuredDataSourcesMap.get(entry.getKey()), entry.getValue());
+                if (!changedDataSources.isEmpty()) {
+                    result.put(entry.getKey(), changedDataSources);
+                }
+            } else {
+                result.put(entry.getKey(), entry.getValue());
+            }
+        }
+        return result;
+    }
+    
+    private Map<String, DataSourceConfiguration> getChangedDataSourcesConfigurations(final Map<String, DataSource> dataSourceMap,
+                                                                                     final Map<String, DataSourceConfiguration> loadedDataSourceConfigurationMap) {
+        Map<String, DataSourceConfiguration> dataSourceConfigurationMap = DataSourceConverter.getDataSourceConfigurationMap(dataSourceMap);
+        return loadedDataSourceConfigurationMap.entrySet().stream().filter(entry -> !dataSourceConfigurationMap.containsKey(entry.getKey())
+                || !dataSourceConfigurationMap.get(entry.getKey()).equals(entry.getValue())).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
     }
     
     private Map<String, Map<String, DataSource>> getChangedDataSources(final Map<String, Map<String, DataSourceConfiguration>> changedDataSourceConfigurations) {
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
index 68b130a..35cda90 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
@@ -38,6 +38,7 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 import java.util.stream.Collectors;
@@ -62,7 +63,7 @@ public final class GovernanceShardingSphereDataSource extends AbstractUnsupporte
         this.schemaName = schemaName;
         mode = ModeBuilderEngine.build(modeConfig);
         contextManager = TypedSPIRegistry.getRegisteredService(ContextManagerBuilder.class, modeConfig.getType(), new Properties()).build(
-                mode, Collections.singletonMap(schemaName, Collections.emptyMap()), Collections.singletonMap(schemaName, Collections.emptyList()), Collections.emptyList(), new Properties(), false);
+                mode, Collections.singletonMap(schemaName, new HashMap<>()), Collections.singletonMap(schemaName, Collections.emptyList()), Collections.emptyList(), new Properties(), false);
     }
     
     public GovernanceShardingSphereDataSource(final String schemaName, final ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap, 
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/test/java/org/apache/shardingsphere/driver/governance/fixture/TestRegistryCenterRepository.java b/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/test/java/org/apache/shardingsphere/driver/governance/fixture/TestRegistryCenterRepository.java
index c4a68da..be4ab46 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/test/java/org/apache/shardingsphere/driver/governance/fixture/TestRegistryCenterRepository.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/test/java/org/apache/shardingsphere/driver/governance/fixture/TestRegistryCenterRepository.java
@@ -30,31 +30,31 @@ import java.util.concurrent.TimeUnit;
 
 public final class TestRegistryCenterRepository implements RegistryCenterRepository {
     
-    private static final Map<String, String> REGISTRY_DATA = new LinkedHashMap<>();
+    private final Map<String, String> registryData = new LinkedHashMap<>();
     
     @Override
     public void init(final RegistryCenterConfiguration config) {
-        REGISTRY_DATA.put("/metadata", DefaultSchema.LOGIC_NAME);
+        registryData.put("/metadata", DefaultSchema.LOGIC_NAME);
     }
     
     @Override
     public String get(final String key) {
-        return REGISTRY_DATA.get(key);
+        return registryData.get(key);
     }
     
     @Override
     public List<String> getChildrenKeys(final String key) {
-        return REGISTRY_DATA.containsKey(key) ? Collections.singletonList(REGISTRY_DATA.get(key)) : Collections.emptyList();
+        return registryData.containsKey(key) ? Collections.singletonList(registryData.get(key)) : Collections.emptyList();
     }
     
     @Override
     public void persist(final String key, final String value) {
-        REGISTRY_DATA.put(key, value);
+        registryData.put(key, value);
     }
     
     @Override
     public void persistEphemeral(final String key, final String value) {
-        REGISTRY_DATA.put(key, value);
+        registryData.put(key, value);
     }
     
     @Override
@@ -76,7 +76,7 @@ public final class TestRegistryCenterRepository implements RegistryCenterReposit
     
     @Override
     public void close() {
-        REGISTRY_DATA.clear();
+        registryData.clear();
     }
     
     @Override