Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ProxyConfigurationConverter #6564

Merged
merged 17 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import lombok.Setter;

/**
* Data source parameters for YAML.
* Data source parameters.
*/
@Getter
@Setter
Expand All @@ -35,17 +35,17 @@ public final class DataSourceParameter {

private String password;

private long connectionTimeoutMilliseconds = 30 * 1000;
private long connectionTimeoutMilliseconds = 30 * 1000L;

private long idleTimeoutMilliseconds = 60 * 1000;
private long idleTimeoutMilliseconds = 60 * 1000L;

private long maxLifetimeMilliseconds;

private int maxPoolSize = 50;

private int minPoolSize = 1;

private long maintenanceIntervalMilliseconds = 30 * 1000;

private long maintenanceIntervalMilliseconds = 30 * 1000L;
private boolean readOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@
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.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
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.swapper.YamlProxyConfigurationSwapper;
import org.apache.shardingsphere.proxy.frontend.bootstrap.ShardingSphereProxy;
import org.apache.shardingsphere.proxy.orchestration.OrchestrationBootstrap;
import org.apache.shardingsphere.proxy.orchestration.schema.ProxyOrchestrationSchemaContexts;

import javax.sql.DataSource;
import java.sql.Connection;
Expand Down Expand Up @@ -81,40 +84,40 @@ 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());
try (ProxyConfigurationConverter converter = ProxyConfigurationConverterFactory.newInstances(null != yamlConfig.getServerConfiguration().getOrchestration())) {
ProxyConfiguration proxyConfiguration = converter.convert(yamlConfig);
log(proxyConfiguration);
initialize(proxyConfiguration, port, converter);
}
}

private static void log(final ProxyConfiguration proxyConfiguration) {
Collection<Collection<RuleConfiguration>> ruleConfigurations = proxyConfiguration.getSchemaRules().values();
if (CollectionUtils.isNotEmpty(ruleConfigurations)) {
ruleConfigurations.forEach(ConfigurationLogger::log);
if (null == yamlConfig.getServerConfiguration().getOrchestration()) {
init(new YamlProxyConfigurationSwapper().swap(yamlConfig), port);
} else {
try (OrchestrationFacade orchestrationFacade = OrchestrationFacade.getInstance()) {
init(new OrchestrationBootstrap(orchestrationFacade).init(yamlConfig), port);
}
}
ConfigurationLogger.log(proxyConfiguration.getAuthentication());
ConfigurationLogger.log(proxyConfiguration.getProps());
}

private static void initialize(final ProxyConfiguration proxyConfiguration, final int port, final ProxyConfigurationConverter converter) throws SQLException {
Authentication authentication = proxyConfiguration.getAuthentication();
Properties props = proxyConfiguration.getProps();
initProxySchemaContexts(proxyConfiguration.getSchemaDataSources(), proxyConfiguration.getSchemaRules(), authentication, props, converter);
initControlPanelFacade(proxyConfiguration.getMetrics(), proxyConfiguration.getCluster());
private static void init(final ProxyConfiguration proxyConfig, final int port) throws SQLException {
log(proxyConfig);
Authentication authentication = proxyConfig.getAuthentication();
Properties props = proxyConfig.getProps();
initProxySchemaContexts(proxyConfig.getSchemaDataSources(), proxyConfig.getSchemaRules(), authentication, props);
initControlPanelFacade(proxyConfig.getMetrics(), proxyConfig.getCluster());
updateServerInfo();
ShardingSphereProxy.getInstance().start(port);
}

private static void log(final ProxyConfiguration proxyConfig) {
proxyConfig.getSchemaRules().values().forEach(ConfigurationLogger::log);
ConfigurationLogger.log(proxyConfig.getAuthentication());
ConfigurationLogger.log(proxyConfig.getProps());
}

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 {
final Authentication authentication, final Properties properties) throws SQLException {
// TODO Consider loading from configuration.
DatabaseType databaseType = schemaDataSources.isEmpty() ? new MySQLDatabaseType()
: DatabaseTypes.getActualDatabaseType(
JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType());
SchemaContextsBuilder schemaContextsBuilder =
new SchemaContextsBuilder(createDataSourcesMap(schemaDataSources), schemaDataSources, authentication, databaseType, schemaRules, properties);
ProxySchemaContexts.getInstance().init(converter.contextsAware(schemaContextsBuilder));
ProxySchemaContexts.getInstance().init(createSchemaContextsAware(schemaContextsBuilder));
}

private static Map<String, Map<String, DataSource>> createDataSourcesMap(final Map<String, Map<String, DataSourceParameter>> schemaDataSources) {
Expand All @@ -135,6 +138,10 @@ private static Map<String, DataSource> createDataSources(final Map<String, DataS
return result;
}

private static SchemaContextsAware createSchemaContextsAware(final SchemaContextsBuilder schemaContextsBuilder) throws SQLException {
return null == OrchestrationFacade.getInstance() ? schemaContextsBuilder.build() : new ProxyOrchestrationSchemaContexts(schemaContextsBuilder.build());
}

private static void initControlPanelFacade(final MetricsConfiguration metricsConfiguration, final ClusterConfiguration clusterConfiguration) {
List<FacadeConfiguration> facadeConfigurations = new LinkedList<>();
if (null != metricsConfiguration && metricsConfiguration.getEnable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,56 @@

package org.apache.shardingsphere.proxy.orchestration;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
import org.apache.shardingsphere.cluster.configuration.swapper.ClusterConfigurationYamlSwapper;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.auth.yaml.swapper.AuthenticationYamlSwapper;
import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.kernel.context.SchemaContextsAware;
import org.apache.shardingsphere.kernel.context.SchemaContextsBuilder;
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.orchestration.core.facade.OrchestrationFacade;
import org.apache.shardingsphere.orchestration.core.common.yaml.config.YamlOrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.core.common.yaml.swapper.OrchestrationConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.core.facade.OrchestrationFacade;
import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
import org.apache.shardingsphere.proxy.config.converter.AbstractConfigurationConverter;
import org.apache.shardingsphere.proxy.config.util.DataSourceConverter;
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.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Orchestration configuration converter.
* Orchestration bootstrap.
*/
public final class OrchestrationConfigurationConverter extends AbstractConfigurationConverter {
@RequiredArgsConstructor
public final class OrchestrationBootstrap {

private final OrchestrationFacade orchestrationFacade = OrchestrationFacade.getInstance();
private final OrchestrationFacade orchestrationFacade;

@Override
public ProxyConfiguration convert(final YamlProxyConfiguration yamlProxyConfiguration) {
YamlOrchestrationConfiguration orchestrationConfig = yamlProxyConfiguration.getServerConfiguration().getOrchestration();
Set<String> schemaNames = yamlProxyConfiguration.getRuleConfigurations().keySet();
ProxyConfiguration result = new ProxyConfiguration();
orchestrationFacade.init(new OrchestrationConfigurationYamlSwapper().swapToObject(orchestrationConfig), schemaNames);
initOrchestrationConfigurations(yamlProxyConfiguration.getServerConfiguration(), yamlProxyConfiguration.getRuleConfigurations(), orchestrationFacade);
Authentication authentication = orchestrationFacade.getConfigCenter().loadAuthentication();
Properties properties = orchestrationFacade.getConfigCenter().loadProperties();
Map<String, Map<String, DataSourceParameter>> schemaDataSources = getDataSourceParametersMap(orchestrationFacade);
Map<String, Collection<RuleConfiguration>> schemaRules = getSchemaRules(orchestrationFacade);
ClusterConfiguration clusterConfiguration = orchestrationFacade.getConfigCenter().loadClusterConfiguration();
result.setAuthentication(authentication);
result.setProps(properties);
result.setSchemaDataSources(schemaDataSources);
result.setSchemaRules(schemaRules);
result.setCluster(clusterConfiguration);
result.setMetrics(getMetricsConfiguration(yamlProxyConfiguration.getServerConfiguration().getMetrics()));
return result;
/**
* Initialize orchestration.
*
* @param yamlConfig YAML proxy configuration
* @return proxy configuration
*/
public ProxyConfiguration init(final YamlProxyConfiguration yamlConfig) {
orchestrationFacade.init(new OrchestrationConfigurationYamlSwapper().swapToObject(yamlConfig.getServerConfiguration().getOrchestration()), yamlConfig.getRuleConfigurations().keySet());
initConfigurations(yamlConfig);
return loadProxyConfiguration();
}

@Override
public SchemaContextsAware contextsAware(final SchemaContextsBuilder builder) throws SQLException {
return new ProxyOrchestrationSchemaContexts(builder.build());
}

private void initOrchestrationConfigurations(
final YamlProxyServerConfiguration serverConfig, final Map<String, YamlProxyRuleConfiguration> ruleConfigs, final OrchestrationFacade orchestrationFacade) {
private void initConfigurations(final YamlProxyConfiguration yamlConfig) {
YamlProxyServerConfiguration serverConfig = yamlConfig.getServerConfiguration();
Map<String, YamlProxyRuleConfiguration> ruleConfigs = yamlConfig.getRuleConfigurations();
if (isEmptyLocalConfiguration(serverConfig, ruleConfigs)) {
orchestrationFacade.onlineInstance();
} else {
Expand All @@ -97,32 +83,40 @@ private boolean isEmptyLocalConfiguration(final YamlProxyServerConfiguration ser

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()) {
for (Entry<String, YamlProxyRuleConfiguration> entry : ruleConfigs.entrySet()) {
result.put(entry.getKey(), DataSourceConverter.getDataSourceConfigurationMap(DataSourceConverter.getDataSourceParameterMap2(entry.getValue().getDataSources())));
}
return result;
}

private Map<String, Map<String, DataSourceParameter>> getDataSourceParametersMap(final OrchestrationFacade orchestrationFacade) {
private Map<String, Collection<RuleConfiguration>> getRuleConfigurations(final Map<String, YamlProxyRuleConfiguration> yamlRuleConfigurations) {
YamlRuleConfigurationSwapperEngine swapperEngine = new YamlRuleConfigurationSwapperEngine();
return yamlRuleConfigurations.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> swapperEngine.swapToRuleConfigurations(entry.getValue().getRules())));
}

private ProxyConfiguration loadProxyConfiguration() {
Map<String, Map<String, DataSourceParameter>> schemaDataSources = loadDataSourceParametersMap();
Map<String, Collection<RuleConfiguration>> schemaRules = loadSchemaRules();
Authentication authentication = orchestrationFacade.getConfigCenter().loadAuthentication();
ClusterConfiguration clusterConfig = orchestrationFacade.getConfigCenter().loadClusterConfiguration();
MetricsConfiguration metricsConfig = orchestrationFacade.getConfigCenter().loadMetricsConfiguration();
Properties props = orchestrationFacade.getConfigCenter().loadProperties();
return new ProxyConfiguration(schemaDataSources, schemaRules, authentication, clusterConfig, metricsConfig, props);
}

private Map<String, Map<String, DataSourceParameter>> loadDataSourceParametersMap() {
Map<String, Map<String, DataSourceParameter>> result = new LinkedHashMap<>();
for (String each : orchestrationFacade.getConfigCenter().getAllSchemaNames()) {
result.put(each, DataSourceConverter.getDataSourceParameterMap(orchestrationFacade.getConfigCenter().loadDataSourceConfigurations(each)));
}
return result;
}

private Map<String, Collection<RuleConfiguration>> getSchemaRules(final OrchestrationFacade orchestrationFacade) {
private Map<String, Collection<RuleConfiguration>> loadSchemaRules() {
Map<String, Collection<RuleConfiguration>> result = new LinkedHashMap<>();
for (String each : orchestrationFacade.getConfigCenter().getAllSchemaNames()) {
result.put(each, orchestrationFacade.getConfigCenter().loadRuleConfigurations(each));
}
return result;
}

@Override
public void close() {
if (null != orchestrationFacade) {
orchestrationFacade.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,33 @@
package org.apache.shardingsphere.proxy.config;

import lombok.Getter;
import lombok.Setter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter;
import org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;

/**
* Proxy configuration.
*/
@RequiredArgsConstructor
@Getter
@Setter
public final class ProxyConfiguration implements Serializable {
public final class ProxyConfiguration {

private static final long serialVersionUID = 2258701685899056191L;
private final Map<String, Map<String, DataSourceParameter>> schemaDataSources;

private Authentication authentication;
private final Map<String, Collection<RuleConfiguration>> schemaRules;

private Properties props;
private final Authentication authentication;

private Map<String, Map<String, DataSourceParameter>> schemaDataSources;
private final ClusterConfiguration cluster;

private Map<String, Collection<RuleConfiguration>> schemaRules;
private final MetricsConfiguration metrics;

private MetricsConfiguration metrics;

private ClusterConfiguration cluster;
private final Properties props;
}
Loading