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/10 05:09:31 UTC
[shardingsphere] branch master updated: Polish process to convert
yaml configuration (#6316)
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 31356a7 Polish process to convert yaml configuration (#6316)
31356a7 is described below
commit 31356a755be779107793a308ba5d62c32465ff2e
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Fri Jul 10 13:09:13 2020 +0800
Polish process to convert yaml configuration (#6316)
* Polish process to convert yaml configuration
* check style
---
.../jdbc/execute/RegistryCenterExecuteEngine.java | 13 ++++++
.../YamlDataSourceConfigurationGenerator.java | 12 +++---
.../converter/AbstractConfigurationConverter.java | 46 ++++------------------
.../proxy/config/util/DataSourceConverter.java | 29 +++++++++++++-
.../OrchestrationConfigurationConverter.java | 17 ++++----
.../ddl/CreateDataSourcesStatementContext.java | 2 +
6 files changed, 65 insertions(+), 54 deletions(-)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RegistryCenterExecuteEngine.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RegistryCenterExecuteEngine.java
index fab7307..fc58ddb 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RegistryCenterExecuteEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RegistryCenterExecuteEngine.java
@@ -18,15 +18,21 @@
package org.apache.shardingsphere.proxy.backend.communication.jdbc.execute;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.generator.YamlDataSourceConfigurationGenerator;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
+import org.apache.shardingsphere.proxy.config.util.DataSourceConverter;
+import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateDataSourcesStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateShardingRuleStatementContext;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
+import java.util.Map;
/**
* Registry center execute engine.
@@ -52,6 +58,7 @@ public class RegistryCenterExecuteEngine implements SQLExecuteEngine {
}
private BackendResponse execute(final CreateDataSourcesStatementContext context) {
+ Map<String, YamlDataSourceParameter> parameters = new YamlDataSourceConfigurationGenerator().generate(context);
UpdateResponse result = new UpdateResponse();
result.setType("CREATE");
return result;
@@ -62,4 +69,10 @@ public class RegistryCenterExecuteEngine implements SQLExecuteEngine {
result.setType("CREATE");
return result;
}
+
+ private Map<String, DataSourceConfiguration> getDataSourceConfigurationMap(final Map<String, YamlDataSourceParameter> parameters) {
+ Map<String, DataSourceConfiguration> result = new LinkedHashMap<>();
+ DataSourceConverter.getDataSourceConfigurationMap(DataSourceConverter.getDataSourceParameterMap2(parameters));
+ return result;
+ }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlDataSourceConfigurationGenerator.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlDataSourceConfigurationGenerator.java
index 87d1359..8a2fa4e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlDataSourceConfigurationGenerator.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlDataSourceConfigurationGenerator.java
@@ -20,23 +20,23 @@ package org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.gener
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateDataSourcesStatementContext;
-import java.util.Collection;
-import java.util.LinkedList;
+import java.util.LinkedHashMap;
+import java.util.Map;
/**
* Yaml dataSource configuration generator.
*/
-public class YamlDataSourceConfigurationGenerator implements YamlConfigurationGenerator<CreateDataSourcesStatementContext, Collection<YamlDataSourceParameter>> {
+public class YamlDataSourceConfigurationGenerator implements YamlConfigurationGenerator<CreateDataSourcesStatementContext, Map<String, YamlDataSourceParameter>> {
@Override
- public Collection<YamlDataSourceParameter> generate(final CreateDataSourcesStatementContext sqlStatement) {
- Collection<YamlDataSourceParameter> result = new LinkedList<>();
+ public Map<String, YamlDataSourceParameter> generate(final CreateDataSourcesStatementContext sqlStatement) {
+ Map<String, YamlDataSourceParameter> result = new LinkedHashMap<>();
for (CreateDataSourcesStatementContext.DataSourceContext each : sqlStatement.getDataSourceContexts()) {
YamlDataSourceParameter dataSource = new YamlDataSourceParameter();
dataSource.setUrl(each.getUrl());
dataSource.setUsername(each.getUserName());
dataSource.setPassword(each.getPassword());
- result.add(dataSource);
+ result.put(each.getName(), dataSource);
}
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/converter/AbstractConfigurationConverter.java b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/converter/AbstractConfigurationConverter.java
index 50bb35a..436f9ec 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/converter/AbstractConfigurationConverter.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/converter/AbstractConfigurationConverter.java
@@ -17,20 +17,20 @@
package org.apache.shardingsphere.proxy.config.converter;
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter;
import org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;
import org.apache.shardingsphere.metrics.configuration.swapper.MetricsConfigurationYamlSwapper;
import org.apache.shardingsphere.metrics.configuration.yaml.YamlMetricsConfiguration;
-import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
+import org.apache.shardingsphere.proxy.config.util.DataSourceConverter;
import org.apache.shardingsphere.proxy.config.yaml.YamlProxyRuleConfiguration;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
/**
* Abstract configuration converter.
*/
@@ -54,39 +54,7 @@ public abstract class AbstractConfigurationConverter implements ProxyConfigurati
* @return data source parameters map
*/
protected Map<String, Map<String, DataSourceParameter>> getDataSourceParametersMap(final Map<String, YamlProxyRuleConfiguration> localRuleConfigs) {
- return localRuleConfigs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> getDataSourceParameters(entry.getValue().getDataSources())));
- }
-
- /**
- * Get data source parameters.
- *
- * @param dataSourceParameters data source parameters for YAML
- * @return data source parameters
- */
- protected Map<String, DataSourceParameter> getDataSourceParameters(final Map<String, YamlDataSourceParameter> dataSourceParameters) {
- return dataSourceParameters.entrySet().stream()
- .collect(Collectors.toMap(Map.Entry::getKey, entry -> createDataSourceParameter(entry.getValue()), (oldVal, currVal) -> oldVal, LinkedHashMap::new));
- }
-
- /**
- * Create data source parameter.
- *
- * @param yamlDataSourceParameter data source parameter for YAML
- * @return data source parameter
- */
- protected DataSourceParameter createDataSourceParameter(final YamlDataSourceParameter yamlDataSourceParameter) {
- DataSourceParameter result = new DataSourceParameter();
- result.setConnectionTimeoutMilliseconds(yamlDataSourceParameter.getConnectionTimeoutMilliseconds());
- result.setIdleTimeoutMilliseconds(yamlDataSourceParameter.getIdleTimeoutMilliseconds());
- result.setMaintenanceIntervalMilliseconds(yamlDataSourceParameter.getMaintenanceIntervalMilliseconds());
- result.setMaxLifetimeMilliseconds(yamlDataSourceParameter.getMaxLifetimeMilliseconds());
- result.setMaxPoolSize(yamlDataSourceParameter.getMaxPoolSize());
- result.setMinPoolSize(yamlDataSourceParameter.getMinPoolSize());
- result.setUsername(yamlDataSourceParameter.getUsername());
- result.setPassword(yamlDataSourceParameter.getPassword());
- result.setReadOnly(yamlDataSourceParameter.isReadOnly());
- result.setUrl(yamlDataSourceParameter.getUrl());
- return result;
+ return localRuleConfigs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> DataSourceConverter.getDataSourceParameterMap2(entry.getValue().getDataSources())));
}
/**
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/util/DataSourceConverter.java b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/util/DataSourceConverter.java
index 7cb2145..63971fd 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/util/DataSourceConverter.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/util/DataSourceConverter.java
@@ -22,6 +22,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter;
+import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
@@ -36,7 +37,7 @@ import java.util.stream.Collectors;
public final class DataSourceConverter {
/**
- * Get data source map.
+ * Get data source parameter map.
*
* @param dataSourceConfigurationMap data source configuration map
* @return data source parameter map
@@ -46,6 +47,17 @@ public final class DataSourceConverter {
.collect(Collectors.toMap(Entry::getKey, entry -> createDataSourceParameter(entry.getValue()), (oldVal, currVal) -> oldVal, LinkedHashMap::new));
}
+ /**
+ * Get data source parameter map.
+ *
+ * @param dataSourceParameters yaml data source parameters
+ * @return data source parameter map
+ */
+ public static Map<String, DataSourceParameter> getDataSourceParameterMap2(final Map<String, YamlDataSourceParameter> dataSourceParameters) {
+ return dataSourceParameters.entrySet().stream()
+ .collect(Collectors.toMap(Entry::getKey, entry -> createDataSourceParameter(entry.getValue()), (oldVal, currVal) -> oldVal, LinkedHashMap::new));
+ }
+
private static DataSourceParameter createDataSourceParameter(final DataSourceConfiguration dataSourceConfiguration) {
bindAlias(dataSourceConfiguration);
DataSourceParameter result = new DataSourceParameter();
@@ -60,6 +72,21 @@ public final class DataSourceConverter {
}
return result;
}
+
+ private static DataSourceParameter createDataSourceParameter(final YamlDataSourceParameter yamlDataSourceParameter) {
+ DataSourceParameter result = new DataSourceParameter();
+ result.setConnectionTimeoutMilliseconds(yamlDataSourceParameter.getConnectionTimeoutMilliseconds());
+ result.setIdleTimeoutMilliseconds(yamlDataSourceParameter.getIdleTimeoutMilliseconds());
+ result.setMaintenanceIntervalMilliseconds(yamlDataSourceParameter.getMaintenanceIntervalMilliseconds());
+ result.setMaxLifetimeMilliseconds(yamlDataSourceParameter.getMaxLifetimeMilliseconds());
+ result.setMaxPoolSize(yamlDataSourceParameter.getMaxPoolSize());
+ result.setMinPoolSize(yamlDataSourceParameter.getMinPoolSize());
+ result.setUsername(yamlDataSourceParameter.getUsername());
+ result.setPassword(yamlDataSourceParameter.getPassword());
+ result.setReadOnly(yamlDataSourceParameter.isReadOnly());
+ result.setUrl(yamlDataSourceParameter.getUrl());
+ return result;
+ }
private static void bindAlias(final DataSourceConfiguration dataSourceConfiguration) {
dataSourceConfiguration.addAlias("url", "jdbcUrl");
diff --git a/shardingsphere-proxy/shardingsphere-proxy-orchestration/src/main/java/org/apache/shardingsphere/proxy/orchestration/OrchestrationConfigurationConverter.java b/shardingsphere-proxy/shardingsphere-proxy-orchestration/src/main/java/org/apache/shardingsphere/proxy/orchestration/OrchestrationConfigurationConverter.java
index f31e08f..bb5d935 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-orchestration/src/main/java/org/apache/shardingsphere/proxy/orchestration/OrchestrationConfigurationConverter.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-orchestration/src/main/java/org/apache/shardingsphere/proxy/orchestration/OrchestrationConfigurationConverter.java
@@ -17,13 +17,6 @@
package org.apache.shardingsphere.proxy.orchestration;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Properties;
-import java.util.Set;
import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
import org.apache.shardingsphere.cluster.configuration.swapper.ClusterConfigurationYamlSwapper;
import org.apache.shardingsphere.infra.auth.Authentication;
@@ -46,6 +39,14 @@ import org.apache.shardingsphere.proxy.config.yaml.YamlProxyRuleConfiguration;
import org.apache.shardingsphere.proxy.config.yaml.YamlProxyServerConfiguration;
import org.apache.shardingsphere.proxy.orchestration.schema.ProxyOrchestrationSchemaContexts;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
/**
* Orchestration configuration converter.
*/
@@ -100,7 +101,7 @@ public class OrchestrationConfigurationConverter extends AbstractConfigurationCo
private Map<String, Map<String, DataSourceConfiguration>> getDataSourceConfigurationMap(final Map<String, YamlProxyRuleConfiguration> ruleConfigs) {
Map<String, Map<String, DataSourceConfiguration>> result = new LinkedHashMap<>();
for (Map.Entry<String, YamlProxyRuleConfiguration> entry : ruleConfigs.entrySet()) {
- result.put(entry.getKey(), DataSourceConverter.getDataSourceConfigurationMap(getDataSourceParameters(entry.getValue().getDataSources())));
+ result.put(entry.getKey(), DataSourceConverter.getDataSourceConfigurationMap(DataSourceConverter.getDataSourceParameterMap2(entry.getValue().getDataSources())));
}
return result;
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourcesStatementContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourcesStatementContext.java
index be64d13..66ec740 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourcesStatementContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourcesStatementContext.java
@@ -47,6 +47,8 @@ public final class CreateDataSourcesStatementContext extends CommonSQLStatementC
@Getter
public final class DataSourceContext {
+ private final String name;
+
private final String url;
private final String userName;