Skip to content

Commit

Permalink
Add orchestrationEnabled for Bootstrap (apache#6565)
Browse files Browse the repository at this point in the history
* Refactor Bootstrap

* Add orchestrationEnabled for Bootstrap
  • Loading branch information
terrymanu authored and qiulu3 committed Aug 3, 2020
1 parent 0b3997f commit 1000c2d
Showing 1 changed file with 20 additions and 23 deletions.
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

0 comments on commit 1000c2d

Please sign in to comment.