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 MetaDataContextsInitFactory #34400

Merged
merged 3 commits into from
Jan 19, 2025
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 @@ -36,8 +36,9 @@
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.type.LocalConfigurationMetaDataContextsFactory;
import org.apache.shardingsphere.mode.metadata.factory.type.RegisterCenterMetaDataContextsFactory;
import org.apache.shardingsphere.mode.metadata.factory.init.MetaDataContextsInitFactory;
import org.apache.shardingsphere.mode.metadata.factory.init.type.LocalConfigurationMetaDataContextsInitFactory;
import org.apache.shardingsphere.mode.metadata.factory.init.type.RegisterCenterMetaDataContextsInitFactory;
import org.apache.shardingsphere.mode.metadata.manager.SwitchingResource;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;

Expand Down Expand Up @@ -67,9 +68,10 @@ public final class MetaDataContextsFactory {
* @throws SQLException SQL exception
*/
public MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException {
return containsRegisteredDatabases()
? new RegisterCenterMetaDataContextsFactory(persistService, instanceContext).create(param)
: new LocalConfigurationMetaDataContextsFactory(persistService, instanceContext).create(param);
MetaDataContextsInitFactory initFactory = containsRegisteredDatabases()
? new RegisterCenterMetaDataContextsInitFactory(persistService, instanceContext)
: new LocalConfigurationMetaDataContextsInitFactory(persistService, instanceContext);
return initFactory.create(param);
}

private boolean containsRegisteredDatabases() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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 abstract class MetaDataContextsInitFactory {

/**
* Create meta data contexts.
*
* @param param context manager builder parameter
* @return created meta data contexts
* @throws SQLException SQL exception
*/
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 @@ -15,26 +15,20 @@
* limitations under the License.
*/

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

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
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;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;

import javax.sql.DataSource;
Expand All @@ -46,39 +40,25 @@
import java.util.stream.Collectors;

/**
* Local configuration meta data contexts factory.
* Local configuration meta data contexts init factory.
*/
@RequiredArgsConstructor
public final class LocalConfigurationMetaDataContextsFactory {
public final class LocalConfigurationMetaDataContextsInitFactory extends MetaDataContextsInitFactory {

private final MetaDataPersistService persistService;

private final ComputeNodeInstanceContext instanceContext;

/**
* Create meta data contexts.
*
* @param param context manager builder parameter
* @return meta data contexts
* @throws SQLException SQL exception
*/
@Override
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 @@ -15,7 +15,7 @@
* limitations under the License.
*/

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

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
Expand All @@ -26,18 +26,12 @@
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;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;

import javax.sql.DataSource;
Expand All @@ -47,30 +41,24 @@
import java.util.stream.Collectors;

/**
* Register center meta data contexts factory.
* Register center meta data contexts init factory.
*/
@RequiredArgsConstructor
public final class RegisterCenterMetaDataContextsFactory {
public final class RegisterCenterMetaDataContextsInitFactory extends MetaDataContextsInitFactory {

private final MetaDataPersistService persistService;

private final ComputeNodeInstanceContext instanceContext;

/**
* Create meta data contexts.
*
* @param param context manager builder parameter
* @return meta data contexts
* @throws SQLException SQL exception
*/
@Override
public MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException {
Map<String, DatabaseConfiguration> effectiveDatabaseConfigs = createEffectiveDatabaseConfigurations(getDatabaseNames(param.getDatabaseConfigs()), param.getDatabaseConfigs());
Collection<RuleConfiguration> globalRuleConfigs = persistService.getGlobalRuleService().load();
// TODO load global data sources from persist service
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 @@ -99,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);
}
}
Loading