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

Add orchestrationEnabled for Bootstrap #6565

Merged
merged 2 commits into from
Aug 1, 2020
Merged
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 @@ -26,8 +26,6 @@
import org.apache.shardingsphere.control.panel.spi.engine.ControlPanelFacadeEngine;
import org.apache.shardingsphere.control.panel.spi.opentracing.OpenTracingConfiguration;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.constant.Constants;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
Expand Down Expand Up @@ -56,14 +54,12 @@
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collection;
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.Properties;
import java.util.stream.Collectors;

/**
Expand All @@ -84,20 +80,19 @@ 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());
if (null == yamlConfig.getServerConfiguration().getOrchestration()) {
init(new YamlProxyConfigurationSwapper().swap(yamlConfig), port);
} else {
boolean orchestrationEnabled = null != yamlConfig.getServerConfiguration().getOrchestration();
if (orchestrationEnabled) {
try (OrchestrationFacade orchestrationFacade = OrchestrationFacade.getInstance()) {
init(new OrchestrationBootstrap(orchestrationFacade).init(yamlConfig), port);
init(new OrchestrationBootstrap(orchestrationFacade).init(yamlConfig), port, true);
}
} else {
init(new YamlProxyConfigurationSwapper().swap(yamlConfig), port, false);
}
}

private static void init(final ProxyConfiguration proxyConfig, final int port) throws SQLException {
private static void init(final ProxyConfiguration proxyConfig, final int port, final boolean orchestrationEnabled) throws SQLException {
log(proxyConfig);
Authentication authentication = proxyConfig.getAuthentication();
Properties props = proxyConfig.getProps();
initProxySchemaContexts(proxyConfig.getSchemaDataSources(), proxyConfig.getSchemaRules(), authentication, props);
initProxySchemaContexts(proxyConfig, orchestrationEnabled);
initControlPanelFacade(proxyConfig.getMetrics(), proxyConfig.getCluster());
updateServerInfo();
ShardingSphereProxy.getInstance().start(port);
Expand All @@ -109,19 +104,21 @@ private static void log(final ProxyConfiguration proxyConfig) {
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) throws SQLException {
private static void initProxySchemaContexts(final ProxyConfiguration proxyConfig, final boolean orchestrationEnabled) 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(createSchemaContextsAware(schemaContextsBuilder));
Map<String, Map<String, DataSourceParameter>> 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());
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()), (oldVal, currVal) -> oldVal, LinkedHashMap::new));
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) {
Expand All @@ -138,8 +135,8 @@ 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 SchemaContextsAware createSchemaContextsAware(final SchemaContextsBuilder schemaContextsBuilder, final boolean orchestrationEnabled) throws SQLException {
return orchestrationEnabled ? new ProxyOrchestrationSchemaContexts(schemaContextsBuilder.build()) : schemaContextsBuilder.build();
}

private static void initControlPanelFacade(final MetricsConfiguration metricsConfiguration, final ClusterConfiguration clusterConfiguration) {
Expand Down