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 f80f894 Add BootstrapArguments (#6555)
f80f894 is described below
commit f80f8944e1cd92f19c18b1efdcbf2701973e1dfc
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jul 31 18:21:40 2020 +0800
Add BootstrapArguments (#6555)
* Refactor ProxyConfigurationConverterFactory
* Refactor ProxyConfigurationLoader
* Adjust private method order of Bootstrap
* Add BootstrapArguments
---
.../shardingsphere/infra/constant/Constants.java | 2 +
.../org/apache/shardingsphere/proxy/Bootstrap.java | 115 ++++++++-------------
.../proxy/arg/BootstrapArguments.java | 60 +++++++++++
.../proxy/config/ProxyConfigurationLoader.java | 15 +--
.../ProxyConfigurationConverterFactory.java | 15 ++-
.../proxy/config/ProxyConfigurationLoaderTest.java | 2 +-
6 files changed, 122 insertions(+), 87 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/constant/Constants.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/constant/Constants.java
index dd4858b..1d9805c 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/constant/Constants.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/constant/Constants.java
@@ -20,6 +20,8 @@ package org.apache.shardingsphere.infra.constant;
/**
* ShardingSphere of constants.
*/
+@Deprecated
+// TODO remove the class, ref #6546
public final class Constants {
/**
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 e5d9de9..10be97a 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
@@ -39,12 +39,13 @@ import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
import org.apache.shardingsphere.kernel.context.SchemaContextsBuilder;
import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter;
import
org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;
+import org.apache.shardingsphere.proxy.arg.BootstrapArguments;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.JDBCRawBackendDataSourceFactory;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.JDBCDriverURLRecognizerEngine;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
-import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
import org.apache.shardingsphere.proxy.config.ProxyConfigurationLoader;
+import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
import
org.apache.shardingsphere.proxy.config.converter.ProxyConfigurationConverter;
import
org.apache.shardingsphere.proxy.config.converter.ProxyConfigurationConverterFactory;
import org.apache.shardingsphere.proxy.config.yaml.YamlProxyRuleConfiguration;
@@ -71,8 +72,6 @@ import java.util.stream.Collectors;
@Slf4j
public final class Bootstrap {
- private static final String DEFAULT_CONFIG_PATH = "/conf/";
-
/**
* Main entrance.
*
@@ -80,35 +79,26 @@ public final class Bootstrap {
* @throws Exception exception
*/
public static void main(final String[] args) throws Exception {
- int port = getPort(args);
+ BootstrapArguments bootstrapArgs = new BootstrapArguments(args);
+ int port = bootstrapArgs.getPort();
System.setProperty(Constants.PORT_KEY, String.valueOf(port));
- YamlProxyConfiguration yamlConfig = new
ProxyConfigurationLoader().load(getConfigurationPath(args));
-
logRuleConfigurationMap(getRuleConfigurations(yamlConfig.getRuleConfigurations()).values());
- boolean isOrchestration = null !=
yamlConfig.getServerConfiguration().getOrchestration();
- try (ProxyConfigurationConverter converter =
ProxyConfigurationConverterFactory.newInstances(isOrchestration)) {
+ YamlProxyConfiguration yamlConfig =
ProxyConfigurationLoader.load(bootstrapArgs.getConfigurationPath());
+
logRuleConfigurations(getRuleConfigurations(yamlConfig.getRuleConfigurations()).values());
+ try (ProxyConfigurationConverter converter =
ProxyConfigurationConverterFactory.newInstances(null !=
yamlConfig.getServerConfiguration().getOrchestration())) {
ProxyConfiguration proxyConfiguration =
converter.convert(yamlConfig);
initialize(proxyConfiguration, port, converter);
}
}
- private static int getPort(final String[] args) {
- if (0 == args.length) {
- return Constants.DEFAULT_PORT;
- }
- try {
- return Integer.parseInt(args[0]);
- } catch (final NumberFormatException ex) {
- throw new IllegalArgumentException(String.format("Invalid port
`%s`.", args[0]));
+ private static void logRuleConfigurations(final
Collection<Collection<RuleConfiguration>> ruleConfigurations) {
+ if (CollectionUtils.isNotEmpty(ruleConfigurations)) {
+ ruleConfigurations.forEach(ConfigurationLogger::log);
}
}
- private static String getConfigurationPath(final String[] args) {
- return args.length < 2 ? DEFAULT_CONFIG_PATH :
paddingWithSlash(args[1]);
- }
-
- private static String paddingWithSlash(final String arg) {
- String path = arg.endsWith("/") ? arg : (arg + "/");
- return path.startsWith("/") ? path : ("/" + path);
+ private static Map<String, Collection<RuleConfiguration>>
getRuleConfigurations(final Map<String, YamlProxyRuleConfiguration>
localRuleConfigs) {
+ YamlRuleConfigurationSwapperEngine swapperEngine = new
YamlRuleConfigurationSwapperEngine();
+ return
localRuleConfigs.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> swapperEngine.swapToRuleConfigurations(entry.getValue().getRules())));
}
private static void initialize(final ProxyConfiguration
proxyConfiguration, final int port, final ProxyConfigurationConverter
converter) throws SQLException {
@@ -120,43 +110,15 @@ public final class Bootstrap {
updateServerInfo();
ShardingSphereProxy.getInstance().start(port);
}
-
- private static void updateServerInfo() {
- List<String> schemaNames =
ProxySchemaContexts.getInstance().getSchemaNames();
- if (CollectionUtils.isEmpty(schemaNames)) {
- return;
- }
- Map<String, DataSource> dataSources =
Objects.requireNonNull(ProxySchemaContexts.getInstance().getSchema(schemaNames.get(0))).getSchema().getDataSources();
- DataSource singleDataSource = dataSources.values().iterator().next();
- try (Connection connection = singleDataSource.getConnection()) {
- DatabaseMetaData databaseMetaData = connection.getMetaData();
- String databaseName = databaseMetaData.getDatabaseProductName();
- String databaseVersion =
databaseMetaData.getDatabaseProductVersion();
- log.info("database name {} , database version {}", databaseName,
databaseVersion);
- MySQLServerInfo.setServerVersion(databaseVersion);
- } catch (final SQLException ex) {
- throw new ShardingSphereException("Get database server info
failed", ex);
- }
- }
-
- private static void initControlPanelFacade(final MetricsConfiguration
metricsConfiguration, final ClusterConfiguration clusterConfiguration) {
- List<FacadeConfiguration> facadeConfigurations = new LinkedList<>();
- if (null != metricsConfiguration && metricsConfiguration.getEnable()) {
- facadeConfigurations.add(metricsConfiguration);
- }
- if
(ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_OPENTRACING_ENABLED))
{
- facadeConfigurations.add(new OpenTracingConfiguration());
- }
- if
(ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_CLUSTER_ENABLED))
{
- facadeConfigurations.add(clusterConfiguration);
- }
- new ControlPanelFacadeEngine().init(facadeConfigurations);
+ private static void log(final Authentication authentication, final
Properties properties) {
+ ConfigurationLogger.log(authentication);
+ ConfigurationLogger.log(properties);
}
private static void initProxySchemaContexts(final Map<String, Map<String,
DataSourceParameter>> schemaDataSources, final Map<String,
Collection<RuleConfiguration>> schemaRules,
final Authentication
authentication, final Properties properties, final ProxyConfigurationConverter
converter) throws SQLException {
// TODO Consider loading from configuration.
- DatabaseType databaseType = schemaDataSources.isEmpty() ? new
MySQLDatabaseType()
+ DatabaseType databaseType = schemaDataSources.isEmpty() ? new
MySQLDatabaseType()
: DatabaseTypes.getActualDatabaseType(
JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType());
SchemaContextsBuilder schemaContextsBuilder =
@@ -182,24 +144,35 @@ public final class Bootstrap {
return result;
}
- private static void log(final Authentication authentication, final
Properties properties) {
- ConfigurationLogger.log(authentication);
- ConfigurationLogger.log(properties);
- }
-
- private static Map<String, Collection<RuleConfiguration>>
getRuleConfigurations(final Map<String, YamlProxyRuleConfiguration>
localRuleConfigs) {
- YamlRuleConfigurationSwapperEngine swapperEngine = new
YamlRuleConfigurationSwapperEngine();
- return
localRuleConfigs.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> swapperEngine.swapToRuleConfigurations(entry.getValue().getRules())));
+ private static void initControlPanelFacade(final MetricsConfiguration
metricsConfiguration, final ClusterConfiguration clusterConfiguration) {
+ List<FacadeConfiguration> facadeConfigurations = new LinkedList<>();
+ if (null != metricsConfiguration && metricsConfiguration.getEnable()) {
+ facadeConfigurations.add(metricsConfiguration);
+ }
+ if
(ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_OPENTRACING_ENABLED))
{
+ facadeConfigurations.add(new OpenTracingConfiguration());
+ }
+ if
(ProxySchemaContexts.getInstance().getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_CLUSTER_ENABLED))
{
+ facadeConfigurations.add(clusterConfiguration);
+ }
+ new ControlPanelFacadeEngine().init(facadeConfigurations);
}
- /**
- * Log rule configurations.
- *
- * @param ruleConfigurations log rule configurations
- */
- private static void logRuleConfigurationMap(final
Collection<Collection<RuleConfiguration>> ruleConfigurations) {
- if (CollectionUtils.isNotEmpty(ruleConfigurations)) {
- ruleConfigurations.forEach(ConfigurationLogger::log);
+ private static void updateServerInfo() {
+ List<String> schemaNames =
ProxySchemaContexts.getInstance().getSchemaNames();
+ if (CollectionUtils.isEmpty(schemaNames)) {
+ return;
+ }
+ Map<String, DataSource> dataSources =
Objects.requireNonNull(ProxySchemaContexts.getInstance().getSchema(schemaNames.get(0))).getSchema().getDataSources();
+ DataSource singleDataSource = dataSources.values().iterator().next();
+ try (Connection connection = singleDataSource.getConnection()) {
+ DatabaseMetaData databaseMetaData = connection.getMetaData();
+ String databaseName = databaseMetaData.getDatabaseProductName();
+ String databaseVersion =
databaseMetaData.getDatabaseProductVersion();
+ log.info("database name {} , database version {}", databaseName,
databaseVersion);
+ MySQLServerInfo.setServerVersion(databaseVersion);
+ } catch (final SQLException ex) {
+ throw new ShardingSphereException("Get database server info
failed", ex);
}
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arg/BootstrapArguments.java
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arg/BootstrapArguments.java
new file mode 100644
index 0000000..798f0ed
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arg/BootstrapArguments.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.arg;
+
+import lombok.Getter;
+import org.apache.shardingsphere.infra.constant.Constants;
+
+/**
+ * Bootstrap arguments.
+ */
+@Getter
+public final class BootstrapArguments {
+
+ private static final String DEFAULT_CONFIG_PATH = "/conf/";
+
+ private final int port;
+
+ private final String configurationPath;
+
+ public BootstrapArguments(final String[] args) {
+ port = getPort(args);
+ configurationPath = getConfigurationPath(args);
+ }
+
+ private int getPort(final String[] args) {
+ if (0 == args.length) {
+ // TODO move Constants.DEFAULT_PORT to private attributes of
BootstrapArguments
+ return Constants.DEFAULT_PORT;
+ }
+ try {
+ return Integer.parseInt(args[0]);
+ } catch (final NumberFormatException ex) {
+ throw new IllegalArgumentException(String.format("Invalid port
`%s`.", args[0]));
+ }
+ }
+
+ private String getConfigurationPath(final String[] args) {
+ return args.length < 2 ? DEFAULT_CONFIG_PATH :
paddingWithSlash(args[1]);
+ }
+
+ private String paddingWithSlash(final String arg) {
+ String path = arg.endsWith("/") ? arg : (arg + "/");
+ return path.startsWith("/") ? path : ("/" + path);
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/ProxyConfigurationLoader.java
b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/ProxyConfigurationLoader.java
index bfc6759..63e096b 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/ProxyConfigurationLoader.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/ProxyConfigurationLoader.java
@@ -18,6 +18,8 @@
package org.apache.shardingsphere.proxy.config;
import com.google.common.base.Preconditions;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import
org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameterMerger;
import org.apache.shardingsphere.proxy.config.yaml.YamlProxyRuleConfiguration;
@@ -36,6 +38,7 @@ import java.util.stream.Collectors;
/**
* Proxy configuration loader.
*/
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ProxyConfigurationLoader {
private static final String DEFAULT_DATASOURCE_NAME = "dataSource";
@@ -51,7 +54,7 @@ public final class ProxyConfigurationLoader {
* @return configuration of ShardingSphere-Proxy
* @throws IOException IO exception
*/
- public YamlProxyConfiguration load(final String path) throws IOException {
+ public static YamlProxyConfiguration load(final String path) throws
IOException {
Collection<String> schemaNames = new HashSet<>();
YamlProxyServerConfiguration serverConfig =
loadServerConfiguration(getResourceFile(path + "/" + SERVER_CONFIG_FILE));
File configPath = getResourceFile(path);
@@ -60,7 +63,7 @@ public final class ProxyConfigurationLoader {
return new YamlProxyConfiguration(serverConfig,
ruleConfigurations.stream().collect(Collectors.toMap(YamlProxyRuleConfiguration::getSchemaName,
each -> each)));
}
- private File getResourceFile(final String path) {
+ private static File getResourceFile(final String path) {
URL url = ProxyConfigurationLoader.class.getResource(path);
if (null != url) {
return new File(url.getFile());
@@ -68,14 +71,14 @@ public final class ProxyConfigurationLoader {
return new File(path);
}
- private YamlProxyServerConfiguration loadServerConfiguration(final File
yamlFile) throws IOException {
+ private static YamlProxyServerConfiguration loadServerConfiguration(final
File yamlFile) throws IOException {
YamlProxyServerConfiguration result = YamlEngine.unmarshal(yamlFile,
YamlProxyServerConfiguration.class);
Preconditions.checkNotNull(result, "Server configuration file `%s` is
invalid.", yamlFile.getName());
Preconditions.checkState(null != result.getAuthentication() || null !=
result.getOrchestration(), "Authority configuration is invalid.");
return result;
}
- private Collection<YamlProxyRuleConfiguration>
loadRuleConfigurations(final Collection<String> schemaNames, final File
configPath) throws IOException {
+ private static Collection<YamlProxyRuleConfiguration>
loadRuleConfigurations(final Collection<String> schemaNames, final File
configPath) throws IOException {
Collection<YamlProxyRuleConfiguration> result = new LinkedList<>();
for (File each : findRuleConfigurationFiles(configPath)) {
loadRuleConfiguration(each).ifPresent(yamlProxyRuleConfig -> {
@@ -86,7 +89,7 @@ public final class ProxyConfigurationLoader {
return result;
}
- private Optional<YamlProxyRuleConfiguration> loadRuleConfiguration(final
File yamlFile) throws IOException {
+ private static Optional<YamlProxyRuleConfiguration>
loadRuleConfiguration(final File yamlFile) throws IOException {
YamlProxyRuleConfiguration result = YamlEngine.unmarshal(yamlFile,
YamlProxyRuleConfiguration.class);
if (null == result) {
return Optional.empty();
@@ -101,7 +104,7 @@ public final class ProxyConfigurationLoader {
return Optional.of(result);
}
- private File[] findRuleConfigurationFiles(final File path) {
+ private static File[] findRuleConfigurationFiles(final File path) {
return path.listFiles(pathname ->
RULE_CONFIG_FILE_PATTERN.matcher(pathname.getName()).matches());
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/converter/ProxyConfigurationConverterFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/converter/ProxyConfigurationConverterFactory.java
index 015a345..39c385f 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/converter/ProxyConfigurationConverterFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/converter/ProxyConfigurationConverterFactory.java
@@ -17,27 +17,24 @@
package org.apache.shardingsphere.proxy.config.converter;
-import java.util.Optional;
import
org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
import org.apache.shardingsphere.infra.spi.singleton.SingletonServiceLoader;
+import java.util.Optional;
+
/**
* Proxy configuration converter factory.
*/
public final class ProxyConfigurationConverterFactory {
/**
- * New proxy configuration converter instances.
+ * Create new proxy configuration converter instances.
*
- * @param isOrchestration is orchestration
+ * @param enableOrchestration enable orchestration or not
* @return proxy configuration converter
*/
- public static ProxyConfigurationConverter newInstances(final boolean
isOrchestration) {
- if (isOrchestration) {
- return loadConverter();
- } else {
- return new DefaultConfigurationConverter();
- }
+ public static ProxyConfigurationConverter newInstances(final boolean
enableOrchestration) {
+ return enableOrchestration ? loadConverter() : new
DefaultConfigurationConverter();
}
private static ProxyConfigurationConverter loadConverter() {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/config/ProxyConfigurationLoaderTest.java
b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/config/ProxyConfigurationLoaderTest.java
index 60be871..b1e766c 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/config/ProxyConfigurationLoaderTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/config/ProxyConfigurationLoaderTest.java
@@ -41,7 +41,7 @@ public final class ProxyConfigurationLoaderTest {
@Test
public void assertLoad() throws IOException {
- YamlProxyConfiguration actual = new
ProxyConfigurationLoader().load("/conf/");
+ YamlProxyConfiguration actual =
ProxyConfigurationLoader.load("/conf/");
assertThat(actual.getServerConfiguration().getOrchestration().getRegistryCenter().getServerLists(),
is("localhost:2181"));
assertThat(actual.getRuleConfigurations().size(), is(3));
assertShardingRuleConfiguration(actual.getRuleConfigurations().get("sharding_db"));