This is an automated email from the ASF dual-hosted git repository.
panjuan 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 85ea9ab041b Do not cache data source in GlobalDataSourceRegistry for
scaling (#18033)
85ea9ab041b is described below
commit 85ea9ab041bd42221cc324f954ab36df159d2fef
Author: Hongsheng Zhong <[email protected]>
AuthorDate: Sun May 29 17:02:27 2022 +0800
Do not cache data source in GlobalDataSourceRegistry for scaling (#18033)
---
.../pool/creator/DataSourcePoolCreator.java | 20 +++++++++++++++-----
.../swapper/YamlDataSourceConfigurationSwapper.java | 13 ++++++++++++-
.../ShardingSpherePipelineDataSourceCreator.java | 2 +-
3 files changed, 28 insertions(+), 7 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
index fa30fe3cc64..1aae1d2d836 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
@@ -40,7 +40,6 @@ import java.util.stream.Collectors;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DataSourcePoolCreator {
- // TODO pipeline doesn't need cache even if cache is enabled, since there
might be some temp data sources
// TODO when all data source configurations of instance are dropped by
DistSQL, cached data source should be closed
/**
@@ -50,7 +49,18 @@ public final class DataSourcePoolCreator {
* @return created data sources
*/
public static Map<String, DataSource> create(final Map<String,
DataSourceProperties> dataSourcePropsMap) {
- return
dataSourcePropsMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> create(entry.getKey(), entry.getValue()), (oldValue, currentValue) ->
oldValue,
+ return create(dataSourcePropsMap, true);
+ }
+
+ /**
+ * Create data sources.
+ *
+ * @param dataSourcePropsMap data source properties map
+ * @param cacheEnabled cache enabled
+ * @return created data sources
+ */
+ public static Map<String, DataSource> create(final Map<String,
DataSourceProperties> dataSourcePropsMap, final boolean cacheEnabled) {
+ return
dataSourcePropsMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> create(entry.getKey(), entry.getValue(), cacheEnabled), (oldValue,
currentValue) -> oldValue,
LinkedHashMap::new));
}
@@ -61,7 +71,6 @@ public final class DataSourcePoolCreator {
* @return created data source
*/
public static DataSource create(final DataSourceProperties
dataSourceProps) {
- // TODO when aggregation is enabled, some data source properties
should be changed, e.g. maxPoolSize
DataSource result =
createDataSource(dataSourceProps.getDataSourceClassName());
Optional<DataSourcePoolMetaData> poolMetaData =
DataSourcePoolMetaDataFactory.findInstance(dataSourceProps.getDataSourceClassName());
DataSourceReflection dataSourceReflection = new
DataSourceReflection(result);
@@ -81,11 +90,12 @@ public final class DataSourcePoolCreator {
*
* @param dataSourceName data source name
* @param dataSourceProps data source properties
+ * @param cacheEnabled cache enabled
* @return created data source
*/
- public static DataSource create(final String dataSourceName, final
DataSourceProperties dataSourceProps) {
+ public static DataSource create(final String dataSourceName, final
DataSourceProperties dataSourceProps, final boolean cacheEnabled) {
DataSource result = create(dataSourceProps);
- if
(!GlobalDataSourceRegistry.getInstance().getCachedDataSourceDataSources().containsKey(dataSourceName))
{
+ if (cacheEnabled &&
!GlobalDataSourceRegistry.getInstance().getCachedDataSourceDataSources().containsKey(dataSourceName))
{
GlobalDataSourceRegistry.getInstance().getCachedDataSourceDataSources().put(dataSourceName,
result);
}
return result;
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/YamlDataSourceConfigurationSwapper.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/YamlDataSourceConfigurationSwapper.java
index b05dd09ae78..329861dcc74 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/YamlDataSourceConfigurationSwapper.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/YamlDataSourceConfigurationSwapper.java
@@ -45,7 +45,18 @@ public final class YamlDataSourceConfigurationSwapper {
* @return data sources
*/
public Map<String, DataSource> swapToDataSources(final Map<String,
Map<String, Object>> yamlDataSources) {
- return
DataSourcePoolCreator.create(yamlDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> swapToDataSourceProperties(entry.getValue()))));
+ return swapToDataSources(yamlDataSources, true);
+ }
+
+ /**
+ * Swap to data sources from YAML data sources.
+ *
+ * @param yamlDataSources YAML data sources map
+ * @param cacheEnabled cache enabled
+ * @return data sources
+ */
+ public Map<String, DataSource> swapToDataSources(final Map<String,
Map<String, Object>> yamlDataSources, final boolean cacheEnabled) {
+ return
DataSourcePoolCreator.create(yamlDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> swapToDataSourceProperties(entry.getValue()))), cacheEnabled);
}
/**
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/data/pipeline/datasource/creator/ShardingSpherePipelineDataSourceCreator.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/data/pipeline/datasource/creator/ShardingSpherePipelineDataSourceCreator.java
index d19667e2480..509ec160873 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/data/pipeline/datasource/creator/ShardingSpherePipelineDataSourceCreator.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/data/pipeline/datasource/creator/ShardingSpherePipelineDataSourceCreator.java
@@ -45,7 +45,7 @@ public final class ShardingSpherePipelineDataSourceCreator
implements PipelineDa
YamlShardingRuleConfiguration shardingRuleConfig =
ShardingRuleConfigurationConverter.findYamlShardingRuleConfiguration(rootConfig.getRules());
enableRangeQueryForInline(shardingRuleConfig);
String databaseName = null;
- Map<String, DataSource> dataSourceMap = new
YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources());
+ Map<String, DataSource> dataSourceMap = new
YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources(),
false);
Collection<RuleConfiguration> ruleConfigs = new
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rootConfig.getRules());
try {
return
ShardingSphereDataSourceFactory.createDataSource(databaseName, dataSourceMap,
ruleConfigs, null);