From 6dc69150a708371d377d97f425afd5d11efe19ef Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 2 Aug 2020 14:12:51 +0800 Subject: [PATCH 1/5] Decouple ShardingSphereSchema.dataSourceParameters --- .../schema/OrchestrationSchemaContexts.java | 2 +- .../shardingsphere/proxy/Bootstrap.java | 7 +++--- .../config/util/DataSourceConverter.java | 23 +++++++++++++++++ .../ProxyOrchestrationSchemaContexts.java | 25 +++++++++---------- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java index 34a916d6514cd..072b4993669f4 100644 --- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java +++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java @@ -389,7 +389,7 @@ private Map getNewDataSources(final Map } private Collection getDeletedDataSources(final SchemaContext oldSchemaContext, final Map newDataSources) { - Collection result = new LinkedList<>(oldSchemaContext.getSchema().getDataSourceParameters().keySet()); + Collection result = new LinkedList<>(oldSchemaContext.getSchema().getDataSources().keySet()); result.removeAll(newDataSources.keySet()); return result; } 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 2b09861c2a420..561ae0a5235a5 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 @@ -80,13 +80,12 @@ public static void main(final String[] args) throws Exception { int port = bootstrapArgs.getPort(); System.setProperty(Constants.PORT_KEY, String.valueOf(port)); YamlProxyConfiguration yamlConfig = ProxyConfigurationLoader.load(bootstrapArgs.getConfigurationPath()); - boolean orchestrationEnabled = null != yamlConfig.getServerConfiguration().getOrchestration(); - if (orchestrationEnabled) { + if (null == yamlConfig.getServerConfiguration().getOrchestration()) { + init(new YamlProxyConfigurationSwapper().swap(yamlConfig), port, false); + } else { try (OrchestrationFacade orchestrationFacade = OrchestrationFacade.getInstance()) { init(new OrchestrationBootstrap(orchestrationFacade).init(yamlConfig), port, true); } - } else { - init(new YamlProxyConfigurationSwapper().swap(yamlConfig), port, false); } } 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 63971fddb6781..c187d4f1144e5 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 @@ -24,6 +24,7 @@ import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter; import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter; +import javax.sql.DataSource; import java.lang.reflect.Field; import java.util.LinkedHashMap; import java.util.Map; @@ -121,4 +122,26 @@ private static DataSourceConfiguration createDataSourceConfiguration(final DataS result.getProps().put("readOnly", dataSourceParameter.isReadOnly()); return result; } + + /** + * Get data source parameter. + * + * @param dataSource data source + * @return data source parameter + */ + public static DataSourceParameter getDataSourceParameter(final DataSource dataSource) { + DataSourceParameter result = new DataSourceParameter(); + HikariDataSource hikariDataSource = (HikariDataSource) dataSource; + result.setUrl(hikariDataSource.getJdbcUrl()); + result.setUsername(hikariDataSource.getUsername()); + result.setPassword(hikariDataSource.getPassword()); + result.setConnectionTimeoutMilliseconds(hikariDataSource.getConnectionTimeout()); + result.setIdleTimeoutMilliseconds(hikariDataSource.getIdleTimeout()); + result.setMaxLifetimeMilliseconds(hikariDataSource.getMaxLifetime()); + result.setMaxPoolSize(hikariDataSource.getMaximumPoolSize()); + result.setMinPoolSize(hikariDataSource.getMinimumIdle()); + // TODO setMaintenanceIntervalMilliseconds + result.setReadOnly(hikariDataSource.isReadOnly()); + return result; + } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-orchestration/src/main/java/org/apache/shardingsphere/proxy/orchestration/schema/ProxyOrchestrationSchemaContexts.java b/shardingsphere-proxy/shardingsphere-proxy-orchestration/src/main/java/org/apache/shardingsphere/proxy/orchestration/schema/ProxyOrchestrationSchemaContexts.java index 55a5036fff3f1..2dc37009eff86 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-orchestration/src/main/java/org/apache/shardingsphere/proxy/orchestration/schema/ProxyOrchestrationSchemaContexts.java +++ b/shardingsphere-proxy/shardingsphere-proxy-orchestration/src/main/java/org/apache/shardingsphere/proxy/orchestration/schema/ProxyOrchestrationSchemaContexts.java @@ -48,8 +48,7 @@ public ProxyOrchestrationSchemaContexts(final SchemaContexts schemaContexts) { @Override public Map getAddedDataSources(final SchemaContext oldSchemaContext, final Map newDataSources) throws Exception { Map newDataSourceParameters = DataSourceConverter.getDataSourceParameterMap(newDataSources); - Map parameters = - Maps.filterEntries(newDataSourceParameters, input -> !oldSchemaContext.getSchema().getDataSourceParameters().containsKey(input.getKey())); + Map parameters = Maps.filterKeys(newDataSourceParameters, each -> !oldSchemaContext.getSchema().getDataSources().containsKey(each)); return createDataSources(parameters); } @@ -58,15 +57,15 @@ public Map getModifiedDataSources(final SchemaContext oldSch Map newDataSourceParameters = DataSourceConverter.getDataSourceParameterMap(newDataSources); Map parameters = new LinkedHashMap<>(); for (Entry entry : newDataSourceParameters.entrySet()) { - if (isModifiedDataSource(oldSchemaContext.getSchema().getDataSourceParameters(), entry)) { + if (isModifiedDataSource(oldSchemaContext.getSchema().getDataSources(), entry)) { parameters.put(entry.getKey(), entry.getValue()); } } return createDataSources(parameters); } - private synchronized boolean isModifiedDataSource(final Map oldDataSourceParameters, final Entry target) { - return oldDataSourceParameters.containsKey(target.getKey()) && !oldDataSourceParameters.get(target.getKey()).equals(target.getValue()); + private synchronized boolean isModifiedDataSource(final Map oldDataSources, final Entry target) { + return oldDataSources.containsKey(target.getKey()) && !DataSourceConverter.getDataSourceParameter(oldDataSources.get(target.getKey())).equals(target.getValue()); } @Override @@ -78,6 +77,14 @@ public Map> createDataSourcesMap(final Map createDataSources(final Map parameters) throws Exception { + Map result = new LinkedHashMap<>(); + for (Entry entry: parameters.entrySet()) { + result.put(entry.getKey(), backendDataSourceFactory.build(entry.getKey(), entry.getValue())); + } + return result; + } + @Override public Map> createDataSourceParametersMap(final Map> dataSourcesMap) { Map> result = new LinkedHashMap<>(); @@ -86,12 +93,4 @@ public Map> createDataSourceParametersM } return result; } - - private Map createDataSources(final Map parameters) throws Exception { - Map result = new LinkedHashMap<>(); - for (Entry entry: parameters.entrySet()) { - result.put(entry.getKey(), backendDataSourceFactory.build(entry.getKey(), entry.getValue())); - } - return result; - } } From 1d28b7ab192538c9f419416567e7fc9642311b39 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 2 Aug 2020 14:38:23 +0800 Subject: [PATCH 2/5] Remove ShardingSphereSchema.dataSourceParameters --- .../core/schema/OrchestrationSchemaContexts.java | 9 ++++----- .../kernel/context/SchemaContextsBuilder.java | 11 ++--------- .../kernel/context/schema/ShardingSphereSchema.java | 9 --------- .../org/apache/shardingsphere/proxy/Bootstrap.java | 2 +- 4 files changed, 7 insertions(+), 24 deletions(-) diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java index 072b4993669f4..21837a971181a 100644 --- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java +++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java @@ -327,9 +327,9 @@ private SchemaContext getAddedSchemaContext(final SchemaAddedEvent schemaAddedEv Map> dataSourcesMap = createDataSourcesMap(Collections.singletonMap(schemaName, schemaAddedEvent.getDataSourceConfigurations())); Map> dataSourceParametersMap = createDataSourceParametersMap(Collections.singletonMap(schemaName, schemaAddedEvent.getDataSourceConfigurations())); DatabaseType databaseType = getDatabaseType(dataSourceParametersMap.values().iterator().next().values().iterator().next()); - SchemaContextsBuilder schemaContextsBuilder = new SchemaContextsBuilder(dataSourcesMap, dataSourceParametersMap, - schemaContexts.getAuthentication(), databaseType, Collections.singletonMap(schemaName, schemaAddedEvent.getRuleConfigurations()), - schemaContexts.getProps().getProps()); + SchemaContextsBuilder schemaContextsBuilder = new SchemaContextsBuilder( + dataSourcesMap, schemaContexts.getAuthentication(), + databaseType, Collections.singletonMap(schemaName, schemaAddedEvent.getRuleConfigurations()), schemaContexts.getProps().getProps()); return schemaContextsBuilder.build().getSchemaContexts().get(schemaName); } @@ -359,7 +359,6 @@ private ShardingSphereSchema getChangedShardingSphereSchema(final ShardingSphere private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaContext, final Collection configurations) throws SQLException { ShardingSphereSchema oldSchema = oldSchemaContext.getSchema(); SchemaContextsBuilder builder = new SchemaContextsBuilder(Collections.singletonMap(oldSchemaContext.getName(), oldSchema.getDataSources()), - Collections.singletonMap(oldSchemaContext.getName(), oldSchema.getDataSourceParameters()), schemaContexts.getAuthentication(), oldSchema.getDatabaseType(), Collections.singletonMap(oldSchemaContext.getName(), configurations), schemaContexts.getProps().getProps()); return builder.build().getSchemaContexts().values().iterator().next(); } @@ -373,7 +372,7 @@ private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaConte Map> dataSourcesMap = Collections.singletonMap(oldSchemaContext.getName(), getNewDataSources(oldSchemaContext.getSchema().getDataSources(), deletedDataSources, getAddedDataSources(oldSchemaContext, newDataSources), modifiedDataSources)); Map> dataSourceParametersMap = createDataSourceParametersMap(Collections.singletonMap(oldSchemaContext.getName(), newDataSources)); - return new SchemaContextsBuilder(dataSourcesMap, dataSourceParametersMap, schemaContexts.getAuthentication(), oldSchemaContext.getSchema().getDatabaseType(), + return new SchemaContextsBuilder(dataSourcesMap, schemaContexts.getAuthentication(), oldSchemaContext.getSchema().getDatabaseType(), Collections.singletonMap(oldSchemaContext.getName(), oldSchemaContext.getSchema().getConfigurations()), schemaContexts.getProps().getProps()).build().getSchemaContexts().get(oldSchemaContext.getName()); } diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java index 7876b9445e725..2b305c85b705f 100644 --- a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java +++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java @@ -35,7 +35,6 @@ import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder; import org.apache.shardingsphere.kernel.context.runtime.CachedDatabaseMetaData; import org.apache.shardingsphere.kernel.context.runtime.RuntimeContext; -import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter; import org.apache.shardingsphere.kernel.context.schema.ShardingSphereSchema; import org.apache.shardingsphere.rdl.parser.engine.ShardingSphereSQLParserEngineFactory; import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine; @@ -60,8 +59,6 @@ public final class SchemaContextsBuilder { private final Map> dataSources; - private final Map> dataSourceParameters = new LinkedHashMap<>(); - private final Map> configurations; private final ConfigurationProperties props; @@ -81,14 +78,13 @@ public SchemaContextsBuilder(final Map> dataSour log(configurations, props); } - public SchemaContextsBuilder(final Map> dataSources, final Map> dataSourceParameters, final Authentication authentication, + public SchemaContextsBuilder(final Map> dataSources, final Authentication authentication, final DatabaseType databaseType, final Map> configurations, final Properties props) { this.dataSources = dataSources; this.databaseType = databaseType; this.configurations = configurations; this.props = new ConfigurationProperties(null == props ? new Properties() : props); executorKernel = new ExecutorKernel(this.props.getValue(ConfigurationPropertyKey.EXECUTOR_SIZE)); - this.dataSourceParameters.putAll(dataSourceParameters); this.authentication = authentication; log(configurations, props); } @@ -124,10 +120,7 @@ private ShardingSphereSchema createShardingSphereSchema(final String schemaName) Map dataSources = this.dataSources.get(schemaName); Collection configurations = this.configurations.get(schemaName); Collection rules = ShardingSphereRulesBuilder.build(configurations, dataSources.keySet()); - if (dataSourceParameters.isEmpty()) { - return new ShardingSphereSchema(databaseType, configurations, rules, dataSources, createMetaData(dataSources, rules)); - } - return new ShardingSphereSchema(databaseType, configurations, rules, dataSources, dataSourceParameters.get(schemaName), createMetaData(dataSources, rules)); + return new ShardingSphereSchema(databaseType, configurations, rules, dataSources, createMetaData(dataSources, rules)); } private ShardingSphereMetaData createMetaData(final Map dataSourceMap, final Collection rules) throws SQLException { diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchema.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchema.java index c6174644694c1..514fb2e98d954 100644 --- a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchema.java +++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/schema/ShardingSphereSchema.java @@ -44,8 +44,6 @@ public final class ShardingSphereSchema { private final Map dataSources = new LinkedHashMap<>(); - private final Map dataSourceParameters = new LinkedHashMap<>(); - private final ShardingSphereMetaData metaData; public ShardingSphereSchema(final DatabaseType databaseType, final Collection configurations, final Collection rules, @@ -57,12 +55,6 @@ public ShardingSphereSchema(final DatabaseType databaseType, final Collection configurations, final Collection rules, - final Map dataSourceMap, final Map dataSourceParameters, final ShardingSphereMetaData shardingSphereMetaData) { - this(databaseType, configurations, rules, dataSourceMap, shardingSphereMetaData); - this.dataSourceParameters.putAll(dataSourceParameters); - } - /** * Close data sources. * @param dataSources data sources @@ -70,7 +62,6 @@ public ShardingSphereSchema(final DatabaseType databaseType, final Collection dataSources) { for (String each :dataSources) { close(this.dataSources.get(each)); - dataSourceParameters.remove(each); } } 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 561ae0a5235a5..c282c399493d8 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 @@ -108,7 +108,7 @@ private static void initProxySchemaContexts(final ProxyConfiguration proxyConfig Map> schemaDataSources = proxyConfig.getSchemaDataSources(); DatabaseType databaseType = schemaDataSources.isEmpty() ? new MySQLDatabaseType() : DatabaseTypes.getActualDatabaseType(getDatabaseTypeName(schemaDataSources)); SchemaContextsBuilder schemaContextsBuilder = new SchemaContextsBuilder( - createDataSourcesMap(schemaDataSources), schemaDataSources, proxyConfig.getAuthentication(), databaseType, proxyConfig.getSchemaRules(), proxyConfig.getProps()); + createDataSourcesMap(schemaDataSources), proxyConfig.getAuthentication(), databaseType, proxyConfig.getSchemaRules(), proxyConfig.getProps()); ProxySchemaContexts.getInstance().init(createSchemaContextsAware(schemaContextsBuilder, orchestrationEnabled)); } From 1e57dcf9a557d30af3f498f074cea347b4282afd Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 2 Aug 2020 14:55:09 +0800 Subject: [PATCH 3/5] Add ProxyDataSourceContext --- .../schema/ProxyDataSourceContext.java | 71 +++++++++++++++++++ .../shardingsphere/proxy/Bootstrap.java | 38 +--------- 2 files changed, 74 insertions(+), 35 deletions(-) create mode 100644 shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContext.java diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContext.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContext.java new file mode 100644 index 0000000000000..028248ac5d635 --- /dev/null +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContext.java @@ -0,0 +1,71 @@ +/* + * 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.backend.schema; + +import lombok.Getter; +import org.apache.shardingsphere.infra.database.type.DatabaseType; +import org.apache.shardingsphere.infra.database.type.DatabaseTypes; +import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType; +import org.apache.shardingsphere.infra.exception.ShardingSphereException; +import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter; +import org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.JDBCRawBackendDataSourceFactory; +import org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.JDBCDriverURLRecognizerEngine; + +import javax.sql.DataSource; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +/** + * Proxy data source context. + */ +@Getter +public final class ProxyDataSourceContext { + + private final DatabaseType databaseType; + + private final Map> dataSourcesMap; + + public ProxyDataSourceContext(final Map> schemaDataSources) { + databaseType = schemaDataSources.isEmpty() ? new MySQLDatabaseType() : DatabaseTypes.getActualDatabaseType(getDatabaseTypeName(schemaDataSources)); + dataSourcesMap = createDataSourcesMap(schemaDataSources); + } + + private static String getDatabaseTypeName(final Map> schemaDataSources) { + return JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType(); + } + + private static Map> createDataSourcesMap(final Map> schemaDataSources) { + return schemaDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> createDataSources(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + } + + private static Map createDataSources(final Map dataSourceParameters) { + Map result = new LinkedHashMap<>(dataSourceParameters.size(), 1); + for (Entry entry : dataSourceParameters.entrySet()) { + try { + result.put(entry.getKey(), JDBCRawBackendDataSourceFactory.getInstance().build(entry.getKey(), entry.getValue())); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + throw new ShardingSphereException(String.format("Can not build data source, name is `%s`.", entry.getKey()), ex); + } + } + return result; + } +} 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 c282c399493d8..73ebcf2e11052 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 @@ -28,19 +28,14 @@ import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo; import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.constant.Constants; -import org.apache.shardingsphere.infra.database.type.DatabaseType; -import org.apache.shardingsphere.infra.database.type.DatabaseTypes; -import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType; import org.apache.shardingsphere.infra.exception.ShardingSphereException; import org.apache.shardingsphere.infra.log.ConfigurationLogger; import org.apache.shardingsphere.kernel.context.SchemaContextsAware; 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.orchestration.core.facade.OrchestrationFacade; 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.ProxyDataSourceContext; import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts; import org.apache.shardingsphere.proxy.config.ProxyConfiguration; import org.apache.shardingsphere.proxy.config.ProxyConfigurationLoader; @@ -54,13 +49,10 @@ import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.SQLException; -import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Objects; -import java.util.stream.Collectors; /** * ShardingSphere-Proxy Bootstrap. @@ -104,36 +96,12 @@ private static void log(final ProxyConfiguration proxyConfig) { } private static void initProxySchemaContexts(final ProxyConfiguration proxyConfig, final boolean orchestrationEnabled) throws SQLException { - // TODO Consider loading from configuration. - Map> schemaDataSources = proxyConfig.getSchemaDataSources(); - DatabaseType databaseType = schemaDataSources.isEmpty() ? new MySQLDatabaseType() : DatabaseTypes.getActualDatabaseType(getDatabaseTypeName(schemaDataSources)); + ProxyDataSourceContext dataSourceContext = new ProxyDataSourceContext(proxyConfig.getSchemaDataSources()); SchemaContextsBuilder schemaContextsBuilder = new SchemaContextsBuilder( - createDataSourcesMap(schemaDataSources), proxyConfig.getAuthentication(), databaseType, proxyConfig.getSchemaRules(), proxyConfig.getProps()); + dataSourceContext.getDataSourcesMap(), proxyConfig.getAuthentication(), dataSourceContext.getDatabaseType(), proxyConfig.getSchemaRules(), proxyConfig.getProps()); ProxySchemaContexts.getInstance().init(createSchemaContextsAware(schemaContextsBuilder, orchestrationEnabled)); } - private static String getDatabaseTypeName(final Map> schemaDataSources) { - return JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType(); - } - - private static Map> createDataSourcesMap(final Map> schemaDataSources) { - return schemaDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> createDataSources(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - } - - private static Map createDataSources(final Map dataSourceParameters) { - Map result = new LinkedHashMap<>(dataSourceParameters.size(), 1); - for (Entry entry : dataSourceParameters.entrySet()) { - try { - result.put(entry.getKey(), JDBCRawBackendDataSourceFactory.getInstance().build(entry.getKey(), entry.getValue())); - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - throw new ShardingSphereException(String.format("Can not build data source, name is `%s`.", entry.getKey()), ex); - } - } - return result; - } - private static SchemaContextsAware createSchemaContextsAware(final SchemaContextsBuilder schemaContextsBuilder, final boolean orchestrationEnabled) throws SQLException { return orchestrationEnabled ? new ProxyOrchestrationSchemaContexts(schemaContextsBuilder.build()) : schemaContextsBuilder.build(); } From c9a35ccdf04b2ef92b3d2171d3caa7eea1741896 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 2 Aug 2020 15:49:12 +0800 Subject: [PATCH 4/5] Refactor SchemaContextsBuilder --- .../core/schema/OrchestrationSchemaContexts.java | 16 +++++++--------- .../kernel/context/SchemaContextsBuilder.java | 14 ++++---------- .../apache/shardingsphere/proxy/Bootstrap.java | 2 +- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java index 21837a971181a..a38b5e3c8bdec 100644 --- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java +++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java @@ -327,9 +327,8 @@ private SchemaContext getAddedSchemaContext(final SchemaAddedEvent schemaAddedEv Map> dataSourcesMap = createDataSourcesMap(Collections.singletonMap(schemaName, schemaAddedEvent.getDataSourceConfigurations())); Map> dataSourceParametersMap = createDataSourceParametersMap(Collections.singletonMap(schemaName, schemaAddedEvent.getDataSourceConfigurations())); DatabaseType databaseType = getDatabaseType(dataSourceParametersMap.values().iterator().next().values().iterator().next()); - SchemaContextsBuilder schemaContextsBuilder = new SchemaContextsBuilder( - dataSourcesMap, schemaContexts.getAuthentication(), - databaseType, Collections.singletonMap(schemaName, schemaAddedEvent.getRuleConfigurations()), schemaContexts.getProps().getProps()); + SchemaContextsBuilder schemaContextsBuilder = new SchemaContextsBuilder(dataSourcesMap, databaseType, + Collections.singletonMap(schemaName, schemaAddedEvent.getRuleConfigurations()), schemaContexts.getAuthentication(), schemaContexts.getProps().getProps()); return schemaContextsBuilder.build().getSchemaContexts().get(schemaName); } @@ -359,7 +358,7 @@ private ShardingSphereSchema getChangedShardingSphereSchema(final ShardingSphere private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaContext, final Collection configurations) throws SQLException { ShardingSphereSchema oldSchema = oldSchemaContext.getSchema(); SchemaContextsBuilder builder = new SchemaContextsBuilder(Collections.singletonMap(oldSchemaContext.getName(), oldSchema.getDataSources()), - schemaContexts.getAuthentication(), oldSchema.getDatabaseType(), Collections.singletonMap(oldSchemaContext.getName(), configurations), schemaContexts.getProps().getProps()); + oldSchema.getDatabaseType(), Collections.singletonMap(oldSchemaContext.getName(), configurations), schemaContexts.getAuthentication(), schemaContexts.getProps().getProps()); return builder.build().getSchemaContexts().values().iterator().next(); } @@ -371,9 +370,8 @@ private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaConte oldSchemaContext.getRuntimeContext().getTransactionManagerEngine().close(); Map> dataSourcesMap = Collections.singletonMap(oldSchemaContext.getName(), getNewDataSources(oldSchemaContext.getSchema().getDataSources(), deletedDataSources, getAddedDataSources(oldSchemaContext, newDataSources), modifiedDataSources)); - Map> dataSourceParametersMap = createDataSourceParametersMap(Collections.singletonMap(oldSchemaContext.getName(), newDataSources)); - return new SchemaContextsBuilder(dataSourcesMap, schemaContexts.getAuthentication(), oldSchemaContext.getSchema().getDatabaseType(), - Collections.singletonMap(oldSchemaContext.getName(), oldSchemaContext.getSchema().getConfigurations()), + return new SchemaContextsBuilder(dataSourcesMap, oldSchemaContext.getSchema().getDatabaseType(), + Collections.singletonMap(oldSchemaContext.getName(), oldSchemaContext.getSchema().getConfigurations()), schemaContexts.getAuthentication(), schemaContexts.getProps().getProps()).build().getSchemaContexts().get(oldSchemaContext.getName()); } @@ -394,7 +392,7 @@ private Collection getDeletedDataSources(final SchemaContext oldSchemaCo } /** - * Get added dataSources. + * Get added data sources. * * @param oldSchemaContext old schema context * @param newDataSources new data sources @@ -404,7 +402,7 @@ private Collection getDeletedDataSources(final SchemaContext oldSchemaCo public abstract Map getAddedDataSources(SchemaContext oldSchemaContext, Map newDataSources) throws Exception; /** - * Get modified dataSources. + * Get modified data sources. * * @param oldSchemaContext old schema context * @param newDataSources new data sources diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java index 2b305c85b705f..9c9c65dcadeff 100644 --- a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java +++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java @@ -69,23 +69,17 @@ public final class SchemaContextsBuilder { public SchemaContextsBuilder(final Map> dataSources, final DatabaseType databaseType, final Map> configurations, final Properties props) { - this.dataSources = dataSources; - this.databaseType = databaseType; - this.configurations = configurations; - this.props = new ConfigurationProperties(null == props ? new Properties() : props); - executorKernel = new ExecutorKernel(this.props.getValue(ConfigurationPropertyKey.EXECUTOR_SIZE)); - authentication = new Authentication(); - log(configurations, props); + this(dataSources, databaseType, configurations, new Authentication(), props); } - public SchemaContextsBuilder(final Map> dataSources, final Authentication authentication, - final DatabaseType databaseType, final Map> configurations, final Properties props) { + public SchemaContextsBuilder(final Map> dataSources, + final DatabaseType databaseType, final Map> configurations, final Authentication authentication, final Properties props) { this.dataSources = dataSources; this.databaseType = databaseType; this.configurations = configurations; + this.authentication = authentication; this.props = new ConfigurationProperties(null == props ? new Properties() : props); executorKernel = new ExecutorKernel(this.props.getValue(ConfigurationPropertyKey.EXECUTOR_SIZE)); - this.authentication = authentication; log(configurations, props); } 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 73ebcf2e11052..5ddf4153c3b5c 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 @@ -98,7 +98,7 @@ private static void log(final ProxyConfiguration proxyConfig) { private static void initProxySchemaContexts(final ProxyConfiguration proxyConfig, final boolean orchestrationEnabled) throws SQLException { ProxyDataSourceContext dataSourceContext = new ProxyDataSourceContext(proxyConfig.getSchemaDataSources()); SchemaContextsBuilder schemaContextsBuilder = new SchemaContextsBuilder( - dataSourceContext.getDataSourcesMap(), proxyConfig.getAuthentication(), dataSourceContext.getDatabaseType(), proxyConfig.getSchemaRules(), proxyConfig.getProps()); + dataSourceContext.getDataSourcesMap(), dataSourceContext.getDatabaseType(), proxyConfig.getSchemaRules(), proxyConfig.getAuthentication(), proxyConfig.getProps()); ProxySchemaContexts.getInstance().init(createSchemaContextsAware(schemaContextsBuilder, orchestrationEnabled)); } From c3650f73b2d5e40bb4db280cbfe075c80512ad6c Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 2 Aug 2020 15:51:14 +0800 Subject: [PATCH 5/5] Refactor SchemaContextsBuilder --- .../shardingsphere/kernel/context/SchemaContextsBuilder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java index 9c9c65dcadeff..7aa22dfbf2575 100644 --- a/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java +++ b/shardingsphere-kernel/shardingsphere-kernel-context/src/main/java/org/apache/shardingsphere/kernel/context/SchemaContextsBuilder.java @@ -63,10 +63,10 @@ public final class SchemaContextsBuilder { private final ConfigurationProperties props; - private final ExecutorKernel executorKernel; - private final Authentication authentication; + private final ExecutorKernel executorKernel; + public SchemaContextsBuilder(final Map> dataSources, final DatabaseType databaseType, final Map> configurations, final Properties props) { this(dataSources, databaseType, configurations, new Authentication(), props);