You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/08/09 07:51:48 UTC
[shardingsphere] branch master updated: fixed #11643 Custom pool
properties are lost when restarted in governance mode (#11649)
This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 db2e39b fixed #11643 Custom pool properties are lost when restarted in governance mode (#11649)
db2e39b is described below
commit db2e39be5d87b8f2aafbb888b4fba5cd25b17f08
Author: lanchengx <52...@users.noreply.github.com>
AuthorDate: Mon Aug 9 15:51:17 2021 +0800
fixed #11643 Custom pool properties are lost when restarted in governance mode (#11649)
* fix : #11643 Custom pool properties are lost when restarted in governance mode
* doc : #11643 modify doc
* refactor : flip '.equals()'
* fix : #11643 Custom pool properties are lost when restarted in governance mode
* Use .isEmpty() to replace the hasCustomPoolProps()
* Move CUSTOM_POOL_PROPS_KEY to the DataSourceConfiguration class
* Modify the judgment condition
---
.../infra/config/datasource/DataSourceConfiguration.java | 10 +++++++++-
.../config/swapper/YamlDataSourceConfigurationSwapper.java | 7 +++++++
.../proxy/config/util/DataSourceParameterConverter.java | 12 +++++++-----
3 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
index 48c141b..ae722e3 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
@@ -32,12 +32,14 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
+import java.util.Properties;
/**
* Data source configuration.
@@ -46,6 +48,8 @@ import java.util.Optional;
@Getter
public final class DataSourceConfiguration {
+ public static final String CUSTOM_POOL_PROPS_KEY = "customPoolProps";
+
private static final String GETTER_PREFIX = "get";
private static final String SETTER_PREFIX = "set";
@@ -64,6 +68,8 @@ public final class DataSourceConfiguration {
private final Map<String, Object> props = new LinkedHashMap<>();
+ private final Properties customPoolProps = new Properties();
+
/**
* Get data source configuration.
*
@@ -110,7 +116,9 @@ public final class DataSourceConfiguration {
public DataSource createDataSource() {
DataSource result = (DataSource) Class.forName(dataSourceClassName).getConstructor().newInstance();
Method[] methods = result.getClass().getMethods();
- for (Entry<String, Object> entry : props.entrySet()) {
+ Map<String, Object> allProps = new HashMap<>(props);
+ allProps.putAll((Map) customPoolProps);
+ for (Entry<String, Object> entry : allProps.entrySet()) {
if (SKIPPED_PROPERTY_NAMES.contains(entry.getKey())) {
continue;
}
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 542b687..8b2962e 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
@@ -55,6 +55,10 @@ public final class YamlDataSourceConfigurationSwapper {
Map<String, Object> newDataSourceMap = new HashMap<>(yamlConfig);
newDataSourceMap.remove(DATA_SOURCE_CLASS_NAME_KEY);
DataSourceConfiguration result = new DataSourceConfiguration(yamlConfig.get(DATA_SOURCE_CLASS_NAME_KEY).toString());
+ if (null != newDataSourceMap.get(DataSourceConfiguration.CUSTOM_POOL_PROPS_KEY)) {
+ result.getCustomPoolProps().putAll((Map) newDataSourceMap.get(DataSourceConfiguration.CUSTOM_POOL_PROPS_KEY));
+ newDataSourceMap.remove(DataSourceConfiguration.CUSTOM_POOL_PROPS_KEY);
+ }
result.getProps().putAll(newDataSourceMap);
return result;
}
@@ -67,6 +71,9 @@ public final class YamlDataSourceConfigurationSwapper {
*/
public Map<String, Object> swapToMap(final DataSourceConfiguration dataSourceConfig) {
Map<String, Object> result = new HashMap<>(dataSourceConfig.getProps());
+ if (!dataSourceConfig.getCustomPoolProps().isEmpty()) {
+ result.put(DataSourceConfiguration.CUSTOM_POOL_PROPS_KEY, dataSourceConfig.getCustomPoolProps());
+ }
result.put(DATA_SOURCE_CLASS_NAME_KEY, dataSourceConfig.getDataSourceClassName());
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/util/DataSourceParameterConverter.java b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/util/DataSourceParameterConverter.java
index 8e533b2..35eff17 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/util/DataSourceParameterConverter.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/util/DataSourceParameterConverter.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
import java.lang.reflect.Field;
-import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -64,10 +63,13 @@ public final class DataSourceParameterConverter {
DataSourceParameter result = new DataSourceParameter();
for (Field each : result.getClass().getDeclaredFields()) {
try {
- each.setAccessible(true);
- if (null != dataSourceConfig.getProps().get(each.getName())) {
- setDataSourceParameterField(each, result, dataSourceConfig.getProps().get(each.getName()));
+ Object dataSourceConfigProp =
+ DataSourceConfiguration.CUSTOM_POOL_PROPS_KEY.equals(each.getName()) ? dataSourceConfig.getCustomPoolProps() : dataSourceConfig.getProps().get(each.getName());
+ if (null == dataSourceConfigProp) {
+ continue;
}
+ each.setAccessible(true);
+ setDataSourceParameterField(each, result, dataSourceConfigProp);
} catch (final ReflectiveOperationException ignored) {
}
}
@@ -139,7 +141,7 @@ public final class DataSourceParameterConverter {
result.getProps().put("minimumIdle", dataSourceParameter.getMinPoolSize());
result.getProps().put("readOnly", dataSourceParameter.isReadOnly());
if (null != dataSourceParameter.getCustomPoolProps()) {
- result.getProps().putAll(new HashMap(dataSourceParameter.getCustomPoolProps()));
+ result.getCustomPoolProps().putAll(dataSourceParameter.getCustomPoolProps());
}
return result;
}