Skip to content

Commit

Permalink
Remove ShardingSphereSchema.dataSourceParameters (apache#6569)
Browse files Browse the repository at this point in the history
* Decouple ShardingSphereSchema.dataSourceParameters

* Remove ShardingSphereSchema.dataSourceParameters

* Add ProxyDataSourceContext

* Refactor SchemaContextsBuilder
  • Loading branch information
terrymanu authored and qiulu3 committed Aug 3, 2020
1 parent a9d5121 commit 69b6e19
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,8 @@ private SchemaContext getAddedSchemaContext(final SchemaAddedEvent schemaAddedEv
Map<String, Map<String, DataSource>> dataSourcesMap = createDataSourcesMap(Collections.singletonMap(schemaName, schemaAddedEvent.getDataSourceConfigurations()));
Map<String, Map<String, DataSourceParameter>> 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, databaseType,
Collections.singletonMap(schemaName, schemaAddedEvent.getRuleConfigurations()), schemaContexts.getAuthentication(), schemaContexts.getProps().getProps());
return schemaContextsBuilder.build().getSchemaContexts().get(schemaName);
}

Expand Down Expand Up @@ -359,8 +358,7 @@ private ShardingSphereSchema getChangedShardingSphereSchema(final ShardingSphere
private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaContext, final Collection<RuleConfiguration> 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());
oldSchema.getDatabaseType(), Collections.singletonMap(oldSchemaContext.getName(), configurations), schemaContexts.getAuthentication(), schemaContexts.getProps().getProps());
return builder.build().getSchemaContexts().values().iterator().next();
}

Expand All @@ -372,9 +370,8 @@ private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaConte
oldSchemaContext.getRuntimeContext().getTransactionManagerEngine().close();
Map<String, Map<String, DataSource>> dataSourcesMap = Collections.singletonMap(oldSchemaContext.getName(), getNewDataSources(oldSchemaContext.getSchema().getDataSources(),
deletedDataSources, getAddedDataSources(oldSchemaContext, newDataSources), modifiedDataSources));
Map<String, Map<String, DataSourceParameter>> dataSourceParametersMap = createDataSourceParametersMap(Collections.singletonMap(oldSchemaContext.getName(), newDataSources));
return new SchemaContextsBuilder(dataSourcesMap, dataSourceParametersMap, 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());
}

Expand All @@ -389,13 +386,13 @@ private Map<String, DataSource> getNewDataSources(final Map<String, DataSource>
}

private Collection<String> getDeletedDataSources(final SchemaContext oldSchemaContext, final Map<String, DataSourceConfiguration> newDataSources) {
Collection<String> result = new LinkedList<>(oldSchemaContext.getSchema().getDataSourceParameters().keySet());
Collection<String> result = new LinkedList<>(oldSchemaContext.getSchema().getDataSources().keySet());
result.removeAll(newDataSources.keySet());
return result;
}

/**
* Get added dataSources.
* Get added data sources.
*
* @param oldSchemaContext old schema context
* @param newDataSources new data sources
Expand All @@ -405,7 +402,7 @@ private Collection<String> getDeletedDataSources(final SchemaContext oldSchemaCo
public abstract Map<String, DataSource> getAddedDataSources(SchemaContext oldSchemaContext, Map<String, DataSourceConfiguration> newDataSources) throws Exception;

/**
* Get modified dataSources.
* Get modified data sources.
*
* @param oldSchemaContext old schema context
* @param newDataSources new data sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -60,36 +59,27 @@ public final class SchemaContextsBuilder {

private final Map<String, Map<String, DataSource>> dataSources;

private final Map<String, Map<String, DataSourceParameter>> dataSourceParameters = new LinkedHashMap<>();

private final Map<String, Collection<RuleConfiguration>> configurations;

private final ConfigurationProperties props;

private final ExecutorKernel executorKernel;

private final Authentication authentication;

private final ExecutorKernel executorKernel;

public SchemaContextsBuilder(final Map<String, Map<String, DataSource>> dataSources,
final DatabaseType databaseType, final Map<String, Collection<RuleConfiguration>> 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.<Integer>getValue(ConfigurationPropertyKey.EXECUTOR_SIZE));
authentication = new Authentication();
log(configurations, props);
this(dataSources, databaseType, configurations, new Authentication(), props);
}

public SchemaContextsBuilder(final Map<String, Map<String, DataSource>> dataSources, final Map<String, Map<String, DataSourceParameter>> dataSourceParameters, final Authentication authentication,
final DatabaseType databaseType, final Map<String, Collection<RuleConfiguration>> configurations, final Properties props) {
public SchemaContextsBuilder(final Map<String, Map<String, DataSource>> dataSources,
final DatabaseType databaseType, final Map<String, Collection<RuleConfiguration>> 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.<Integer>getValue(ConfigurationPropertyKey.EXECUTOR_SIZE));
this.dataSourceParameters.putAll(dataSourceParameters);
this.authentication = authentication;
log(configurations, props);
}

Expand Down Expand Up @@ -124,10 +114,7 @@ private ShardingSphereSchema createShardingSphereSchema(final String schemaName)
Map<String, DataSource> dataSources = this.dataSources.get(schemaName);
Collection<RuleConfiguration> configurations = this.configurations.get(schemaName);
Collection<ShardingSphereRule> 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<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> rules) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public final class ShardingSphereSchema {

private final Map<String, DataSource> dataSources = new LinkedHashMap<>();

private final Map<String, DataSourceParameter> dataSourceParameters = new LinkedHashMap<>();

private final ShardingSphereMetaData metaData;

public ShardingSphereSchema(final DatabaseType databaseType, final Collection<RuleConfiguration> configurations, final Collection<ShardingSphereRule> rules,
Expand All @@ -57,20 +55,13 @@ public ShardingSphereSchema(final DatabaseType databaseType, final Collection<Ru
metaData = shardingSphereMetaData;
}

public ShardingSphereSchema(final DatabaseType databaseType, final Collection<RuleConfiguration> configurations, final Collection<ShardingSphereRule> rules,
final Map<String, DataSource> dataSourceMap, final Map<String, DataSourceParameter> dataSourceParameters, final ShardingSphereMetaData shardingSphereMetaData) {
this(databaseType, configurations, rules, dataSourceMap, shardingSphereMetaData);
this.dataSourceParameters.putAll(dataSourceParameters);
}

/**
* Close data sources.
* @param dataSources data sources
*/
public void closeDataSources(final Collection<String> dataSources) {
for (String each :dataSources) {
close(this.dataSources.get(each));
dataSourceParameters.remove(each);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Map<String, DataSource>> dataSourcesMap;

public ProxyDataSourceContext(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) {
databaseType = schemaDataSources.isEmpty() ? new MySQLDatabaseType() : DatabaseTypes.getActualDatabaseType(getDatabaseTypeName(schemaDataSources));
dataSourcesMap = createDataSourcesMap(schemaDataSources);
}

private static String getDatabaseTypeName(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) {
return JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType();
}

private static Map<String, Map<String, DataSource>> createDataSourcesMap(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) {
return schemaDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> createDataSources(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

private static Map<String, DataSource> createDataSources(final Map<String, DataSourceParameter> dataSourceParameters) {
Map<String, DataSource> result = new LinkedHashMap<>(dataSourceParameters.size(), 1);
for (Entry<String, DataSourceParameter> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -80,13 +72,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);
}
}

Expand All @@ -105,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<String, Map<String, DataSourceParameter>> 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), schemaDataSources, proxyConfig.getAuthentication(), databaseType, proxyConfig.getSchemaRules(), proxyConfig.getProps());
dataSourceContext.getDataSourcesMap(), dataSourceContext.getDatabaseType(), proxyConfig.getSchemaRules(), proxyConfig.getAuthentication(), proxyConfig.getProps());
ProxySchemaContexts.getInstance().init(createSchemaContextsAware(schemaContextsBuilder, orchestrationEnabled));
}

private static String getDatabaseTypeName(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) {
return JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType();
}

private static Map<String, Map<String, DataSource>> createDataSourcesMap(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) {
return schemaDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> createDataSources(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

private static Map<String, DataSource> createDataSources(final Map<String, DataSourceParameter> dataSourceParameters) {
Map<String, DataSource> result = new LinkedHashMap<>(dataSourceParameters.size(), 1);
for (Entry<String, DataSourceParameter> 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();
}
Expand Down
Loading

0 comments on commit 69b6e19

Please sign in to comment.