Skip to content

Commit

Permalink
Add MetaDataContextsInitFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 19, 2025
1 parent 779b217 commit b5f0308
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@

package org.apache.shardingsphere.mode.metadata.factory.init;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsFactory;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;

/**
* Meta data contexts init factory.
*/
public interface MetaDataContextsInitFactory {
public abstract class MetaDataContextsInitFactory {

/**
* Create meta data contexts.
Expand All @@ -34,5 +48,13 @@ public interface MetaDataContextsInitFactory {
* @return created meta data contexts
* @throws SQLException SQL exception
*/
MetaDataContexts create(ContextManagerBuilderParameter param) throws SQLException;
public abstract MetaDataContexts create(ContextManagerBuilderParameter param) throws SQLException;

protected final MetaDataContexts create(final Collection<RuleConfiguration> globalRuleConfigs, final Map<String, DataSource> globalDataSources,
final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props, final MetaDataPersistService persistService) {
Collection<ShardingSphereRule> globalRules = GlobalRulesBuilder.buildRules(globalRuleConfigs, databases, props);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, new ResourceMetaData(globalDataSources), new RuleMetaData(globalRules), props);
ShardingSphereStatistics statistics = ShardingSphereStatisticsFactory.create(metaData, persistService.getShardingSphereDataPersistService().load(metaData));
return new MetaDataContexts(metaData, statistics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabasesFactory;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsFactory;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.factory.init.MetaDataContextsInitFactory;
Expand All @@ -50,7 +43,7 @@
* Local configuration meta data contexts init factory.
*/
@RequiredArgsConstructor
public final class LocalConfigurationMetaDataContextsInitFactory implements MetaDataContextsInitFactory {
public final class LocalConfigurationMetaDataContextsInitFactory extends MetaDataContextsInitFactory {

private final MetaDataPersistService persistService;

Expand All @@ -60,20 +53,12 @@ public final class LocalConfigurationMetaDataContextsInitFactory implements Meta
public MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException {
ConfigurationProperties props = new ConfigurationProperties(param.getProps());
Collection<ShardingSphereDatabase> databases = ShardingSphereDatabasesFactory.create(param.getDatabaseConfigs(), props, instanceContext);
MetaDataContexts result = createMetaDataContexts(param.getGlobalRuleConfigs(), param.getGlobalDataSources(), databases, props);
MetaDataContexts result = create(param.getGlobalRuleConfigs(), param.getGlobalDataSources(), databases, props, persistService);
persistDatabaseConfigurations(result, param);
persistMetaData(result);
return result;
}

private MetaDataContexts createMetaDataContexts(final Collection<RuleConfiguration> globalRuleConfigs, final Map<String, DataSource> globalDataSources,
final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
Collection<ShardingSphereRule> globalRules = GlobalRulesBuilder.buildRules(globalRuleConfigs, databases, props);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, new ResourceMetaData(globalDataSources), new RuleMetaData(globalRules), props);
ShardingSphereStatistics statistics = ShardingSphereStatisticsFactory.create(metaData, persistService.getShardingSphereDataPersistService().load(metaData));
return new MetaDataContexts(metaData, statistics);
}

private void persistDatabaseConfigurations(final MetaDataContexts metadataContexts, final ContextManagerBuilderParameter param) {
Collection<RuleConfiguration> globalRuleConfigs = metadataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations();
persistService.persistGlobalRuleConfiguration(globalRuleConfigs, param.getProps());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@
import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabasesFactory;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsFactory;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.factory.init.MetaDataContextsInitFactory;
Expand All @@ -51,7 +44,7 @@
* Register center meta data contexts init factory.
*/
@RequiredArgsConstructor
public final class RegisterCenterMetaDataContextsInitFactory implements MetaDataContextsInitFactory {
public final class RegisterCenterMetaDataContextsInitFactory extends MetaDataContextsInitFactory {

private final MetaDataPersistService persistService;

Expand All @@ -65,7 +58,7 @@ public MetaDataContexts create(final ContextManagerBuilderParameter param) throw
Map<String, DataSource> globalDataSources = param.getGlobalDataSources();
ConfigurationProperties props = new ConfigurationProperties(persistService.getPropsService().load());
Collection<ShardingSphereDatabase> databases = ShardingSphereDatabasesFactory.create(effectiveDatabaseConfigs, loadSchemas(effectiveDatabaseConfigs.keySet()), props, instanceContext);
return createMetaDataContexts(globalRuleConfigs, globalDataSources, databases, props);
return create(globalRuleConfigs, globalDataSources, databases, props, persistService);
}

private Collection<String> getDatabaseNames(final Map<String, DatabaseConfiguration> databaseConfigs) {
Expand Down Expand Up @@ -94,12 +87,4 @@ private void closeGeneratedDataSources(final String databaseName, final Map<Stri
private Map<String, Collection<ShardingSphereSchema>> loadSchemas(final Collection<String> databaseNames) {
return databaseNames.stream().collect(Collectors.toMap(each -> each, each -> persistService.getDatabaseMetaDataFacade().getSchema().load(each)));
}

private MetaDataContexts createMetaDataContexts(final Collection<RuleConfiguration> globalRuleConfigs, final Map<String, DataSource> globalDataSources,
final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
Collection<ShardingSphereRule> globalRules = GlobalRulesBuilder.buildRules(globalRuleConfigs, databases, props);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, new ResourceMetaData(globalDataSources), new RuleMetaData(globalRules), props);
ShardingSphereStatistics statistics = ShardingSphereStatisticsFactory.create(metaData, persistService.getShardingSphereDataPersistService().load(metaData));
return new MetaDataContexts(metaData, statistics);
}
}

0 comments on commit b5f0308

Please sign in to comment.