Skip to content

Commit

Permalink
Use ConfigurationProperties in ContextManager (#17706)
Browse files Browse the repository at this point in the history
* Use ConfigurationProperties in MetaDataContextsBuilder

* Use ConfigurationProperties in ContextManager
  • Loading branch information
terrymanu authored May 16, 2022
1 parent 12e8d13 commit d514176
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,10 @@ private MetaDataContexts buildChangedMetaDataContextWithAddedDataSource(final Sh
final Map<String, DataSourceProperties> addedDataSourceProps) throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(originalMetaData.getResource().getDataSources());
dataSourceMap.putAll(DataSourcePoolCreator.create(addedDataSourceProps));
Properties props = metaDataContexts.getProps().getProps();
DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(dataSourceMap, originalMetaData.getRuleMetaData().getConfigurations());
Optional<MetaDataPersistService> metaDataPersistService = metaDataContexts.getMetaDataPersistService();
metaDataPersistService.ifPresent(optional -> persistTransactionConfiguration(databaseConfig, optional));
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), props);
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
metaDataContextsBuilder.addDatabase(originalMetaData.getDatabaseName(), originalMetaData.getFrontendDatabaseType(), originalMetaData.getResource().getDatabaseType(), databaseConfig);
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> optional.getSchemaMetaDataService()
.persistTables(originalMetaData.getDatabaseName(), originalMetaData.getDatabaseName(), metaDataContextsBuilder.getSchemaMap(originalMetaData.getDatabaseName())));
Expand All @@ -546,8 +545,7 @@ private void persistTransactionConfiguration(final DatabaseConfiguration databas
}

private MetaDataContexts buildChangedMetaDataContext(final ShardingSphereMetaData originalMetaData, final Collection<RuleConfiguration> ruleConfigs) throws SQLException {
Properties props = metaDataContexts.getProps().getProps();
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), props);
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
metaDataContextsBuilder.addDatabase(originalMetaData.getDatabaseName(), originalMetaData.getFrontendDatabaseType(), originalMetaData.getResource().getDatabaseType(),
new DataSourceProvidedDatabaseConfiguration(originalMetaData.getResource().getDataSources(), ruleConfigs));
metaDataContexts.getMetaDataPersistService().ifPresent(optional -> optional.getSchemaMetaDataService()
Expand All @@ -559,8 +557,7 @@ private MetaDataContexts buildChangedMetaDataContextWithChangedDataSource(final
final Map<String, DataSourceProperties> newDataSourceProps) throws SQLException {
Collection<String> deletedDataSources = getDeletedDataSources(originalMetaData, newDataSourceProps).keySet();
Map<String, DataSource> changedDataSources = buildChangedDataSources(originalMetaData, newDataSourceProps);
Properties props = metaDataContexts.getProps().getProps();
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), props);
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
metaDataContextsBuilder.addDatabase(originalMetaData.getDatabaseName(), originalMetaData.getFrontendDatabaseType(), originalMetaData.getResource().getDatabaseType(),
new DataSourceProvidedDatabaseConfiguration(
getNewDataSources(originalMetaData.getResource().getDataSources(), getAddedDataSources(originalMetaData, newDataSourceProps), changedDataSources, deletedDataSources),
Expand All @@ -574,8 +571,7 @@ private MetaDataContexts buildChangedMetaDataContextWithChangedDataSourceAndRule
final Collection<RuleConfiguration> ruleConfigs) throws SQLException {
Collection<String> deletedDataSources = getDeletedDataSources(originalMetaData, newDataSourceProps).keySet();
Map<String, DataSource> changedDataSources = buildChangedDataSources(originalMetaData, newDataSourceProps);
Properties props = metaDataContexts.getProps().getProps();
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), props);
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
metaDataContextsBuilder.addDatabase(originalMetaData.getDatabaseName(), originalMetaData.getFrontendDatabaseType(), originalMetaData.getResource().getDatabaseType(),
new DataSourceProvidedDatabaseConfiguration(getNewDataSources(originalMetaData.getResource().getDataSources(),
getAddedDataSources(originalMetaData, newDataSourceProps), changedDataSources, deletedDataSources), ruleConfigs));
Expand Down Expand Up @@ -633,9 +629,8 @@ private TransactionRule getTransactionRule() {
}

private MetaDataContexts buildNewMetaDataContext(final String databaseName) throws SQLException {
ConfigurationProperties configurationProps = metaDataContexts.getProps();
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), configurationProps.getProps());
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(Collections.emptyMap(), configurationProps);
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), metaDataContexts.getProps());
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(Collections.emptyMap(), metaDataContexts.getProps());
DatabaseType backendDatabaseType = DatabaseTypeEngine.getBackendDatabaseType(Collections.emptyMap());
metaDataContextsBuilder.addDatabase(databaseName, frontendDatabaseType, backendDatabaseType, new DataSourceProvidedDatabaseConfiguration(new HashMap<>(), new LinkedList<>()));
return metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
* Meta data contexts builder.
Expand All @@ -64,9 +63,9 @@ public final class MetaDataContextsBuilder {

private final ExecutorEngine executorEngine;

public MetaDataContextsBuilder(final Collection<RuleConfiguration> globalRuleConfigs, final Properties props) {
public MetaDataContextsBuilder(final Collection<RuleConfiguration> globalRuleConfigs, final ConfigurationProperties props) {
this.globalRuleConfigs = globalRuleConfigs;
this.props = new ConfigurationProperties(props);
this.props = props;
executorEngine = ExecutorEngine.createExecutorEngineWithSize(this.props.<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void assertBuildWithAuthorityRuleConfigurations() throws SQLException {
ShardingSphereUser user = new ShardingSphereUser("root", "root", "");
AuthorityRuleConfiguration authorityRuleConfig = new AuthorityRuleConfiguration(Collections.singleton(user),
new ShardingSphereAlgorithmConfiguration("ALL_PERMITTED", new Properties()));
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(Collections.singleton(authorityRuleConfig), props);
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(Collections.singleton(authorityRuleConfig), new ConfigurationProperties(props));
builder.addDatabase("logic_db", DatabaseTypeEngine.getDatabaseType("MySQL"), DatabaseTypeEngine.getDatabaseType("MySQL"),
new DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), Collections.singletonList(new FixtureRuleConfiguration())));
MetaDataContexts actual = builder.build(mock(MetaDataPersistService.class));
Expand All @@ -67,7 +68,7 @@ public void assertBuildWithAuthorityRuleConfigurations() throws SQLException {

@Test
public void assertBuildWithoutGlobalRuleConfigurations() throws SQLException {
MetaDataContexts actual = new MetaDataContextsBuilder(Collections.emptyList(), new Properties()).build(mock(MetaDataPersistService.class));
MetaDataContexts actual = new MetaDataContextsBuilder(Collections.emptyList(), new ConfigurationProperties(new Properties())).build(mock(MetaDataPersistService.class));
assertThat(actual.getGlobalRuleMetaData().getRules().size(), is(4));
assertThat(actual.getGlobalRuleMetaData().getRules().stream().filter(each -> each instanceof AuthorityRule).count(), is(1L));
assertThat(actual.getGlobalRuleMetaData().getRules().stream().filter(each -> each instanceof TransactionRule).count(), is(1L));
Expand All @@ -77,7 +78,7 @@ public void assertBuildWithoutGlobalRuleConfigurations() throws SQLException {

@Test
public void assertBuildWithEmptyRuleConfigurations() throws SQLException {
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(Collections.emptyList(), new Properties());
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(Collections.emptyList(), new ConfigurationProperties(new Properties()));
builder.addSystemDatabases(new MySQLDatabaseType());
MetaDataContexts actual = builder.build(mock(MetaDataPersistService.class));
assertThat(actual.getMetaDataMap().size(), is(4));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ private MetaDataContextsBuilder createMetaDataContextsBuilder(final MetaDataPers
? parameter.getDatabaseConfigs().keySet()
: metaDataPersistService.getSchemaMetaDataService().loadAllDatabaseNames();
Collection<RuleConfiguration> globalRuleConfigs = metaDataPersistService.getGlobalRuleService().load();
Properties props = metaDataPersistService.getPropsService().load();
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
MetaDataContextsBuilder result = new MetaDataContextsBuilder(globalRuleConfigs, props);
Map<String, ? extends DatabaseConfiguration> databaseConfigMap = getDatabaseConfigMap(databaseNames, metaDataPersistService, parameter);
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(databaseConfigMap, new ConfigurationProperties(props));
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(databaseConfigMap, props);
DatabaseType backendDatabaseType = DatabaseTypeEngine.getBackendDatabaseType(databaseConfigMap);
for (Entry<String, ? extends DatabaseConfiguration> entry : databaseConfigMap.entrySet()) {
if (!frontendDatabaseType.getSystemSchemas().contains(entry.getKey())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public final class MemoryContextManagerBuilder implements ContextManagerBuilder

@Override
public ContextManager build(final ContextManagerBuilderParameter parameter) throws SQLException {
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(parameter.getGlobalRuleConfigs(), parameter.getProps());
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(parameter.getDatabaseConfigs(), new ConfigurationProperties(parameter.getProps()));
ConfigurationProperties props = new ConfigurationProperties(parameter.getProps());
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(parameter.getGlobalRuleConfigs(), props);
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(parameter.getDatabaseConfigs(), props);
DatabaseType backendDatabaseType = DatabaseTypeEngine.getBackendDatabaseType(parameter.getDatabaseConfigs());
for (Entry<String, ? extends DatabaseConfiguration> entry : parameter.getDatabaseConfigs().entrySet()) {
metaDataContextsBuilder.addDatabase(entry.getKey(), frontendDatabaseType, backendDatabaseType, entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;

/**
* Standalone context manager builder.
Expand All @@ -74,10 +73,10 @@ private MetaDataContexts createMetaDataContexts(final MetaDataPersistService met
? parameter.getDatabaseConfigs().keySet()
: metaDataPersistService.getSchemaMetaDataService().loadAllDatabaseNames();
Collection<RuleConfiguration> globalRuleConfigs = metaDataPersistService.getGlobalRuleService().load();
Properties props = metaDataPersistService.getPropsService().load();
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(globalRuleConfigs, props);
Map<String, ? extends DatabaseConfiguration> databaseConfigMap = getDatabaseConfigMap(databaseNames, metaDataPersistService, parameter);
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(databaseConfigMap, new ConfigurationProperties(props));
DatabaseType frontendDatabaseType = DatabaseTypeEngine.getFrontendDatabaseType(databaseConfigMap, props);
DatabaseType backendDatabaseType = DatabaseTypeEngine.getBackendDatabaseType(databaseConfigMap);
for (Entry<String, ? extends DatabaseConfiguration> entry : databaseConfigMap.entrySet()) {
if (frontendDatabaseType.getSystemSchemas().contains(entry.getKey())) {
Expand Down

0 comments on commit d514176

Please sign in to comment.