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

Remove useless RuleConfigurationPersistDecorator #34322

Merged
merged 2 commits into from
Jan 12, 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.manager.GenericSchemaManager;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.mode.metadata.decorator.RuleConfigurationPersistDecorateEngine;
import org.apache.shardingsphere.mode.metadata.manager.DatabaseRuleConfigurationManager;
import org.apache.shardingsphere.mode.metadata.manager.GlobalConfigurationManager;
import org.apache.shardingsphere.mode.metadata.manager.ResourceSwitchManager;
Expand Down Expand Up @@ -72,8 +71,6 @@ public class MetaDataContextManager {

private final GlobalConfigurationManager globalConfigurationManager;

private final RuleConfigurationPersistDecorateEngine ruleConfigPersistDecorateEngine;

public MetaDataContextManager(final AtomicReference<MetaDataContexts> metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository) {
this.metaDataContexts = metaDataContexts;
this.computeNodeInstanceContext = computeNodeInstanceContext;
Expand All @@ -85,7 +82,6 @@ public MetaDataContextManager(final AtomicReference<MetaDataContexts> metaDataCo
ruleItemManager = new RuleItemManager(metaDataContexts, repository, databaseRuleConfigurationManager);
globalConfigurationManager = new GlobalConfigurationManager(metaDataContexts, repository);
metaDataPersistService = new MetaDataPersistService(repository);
ruleConfigPersistDecorateEngine = new RuleConfigurationPersistDecorateEngine(computeNodeInstanceContext);
}

/**
Expand Down Expand Up @@ -154,8 +150,8 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat
.createChangedDatabase(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext);
metaDataContexts.get().getMetaData().putDatabase(changedDatabase);
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
RuleMetaData changedGlobalMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(
ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), metaDataContexts.get().getMetaData().getAllDatabases(), props));
Collection<RuleConfiguration> globalRuleConfigs = metaDataPersistService.getGlobalRuleService().load();
RuleMetaData changedGlobalMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, metaDataContexts.get().getMetaData().getAllDatabases(), props));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
metaDataContexts.get().getMetaData().getAllDatabases(), metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props);
MetaDataContexts result = new MetaDataContexts(metaData, ShardingSphereStatisticsFactory.create(metaDataPersistService, metaData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,16 @@
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.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.decorator.RuleConfigurationPersistDecorateEngine;
import org.apache.shardingsphere.mode.metadata.factory.ExternalMetaDataFactory;
import org.apache.shardingsphere.mode.metadata.factory.InternalMetaDataFactory;
import org.apache.shardingsphere.mode.metadata.manager.SwitchingResource;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -72,28 +67,20 @@ public final class MetaDataContextsFactory {
* @return meta data contexts
* @throws SQLException SQL exception
*/
public static MetaDataContexts create(final MetaDataPersistService persistService, final ContextManagerBuilderParameter param,
final ComputeNodeInstanceContext instanceContext) throws SQLException {
public static MetaDataContexts create(final MetaDataPersistService persistService,
final ContextManagerBuilderParameter param, final ComputeNodeInstanceContext instanceContext) throws SQLException {
return isCreateByLocal(persistService) ? createByLocal(persistService, param, instanceContext) : createByRepository(persistService, param, instanceContext);
}

private static boolean isCreateByLocal(final MetaDataPersistService persistService) {
return persistService.getDatabaseMetaDataFacade().getDatabase().loadAllDatabaseNames().isEmpty();
}

private static MetaDataContexts createByLocal(final MetaDataPersistService persistService, final ContextManagerBuilderParameter param,
final ComputeNodeInstanceContext instanceContext) throws SQLException {
Map<String, DatabaseConfiguration> effectiveDatabaseConfigs = param.getDatabaseConfigs();
Collection<RuleConfiguration> globalRuleConfigs;
if (instanceContext.getModeConfiguration().isCluster()) {
globalRuleConfigs = new RuleConfigurationPersistDecorateEngine(instanceContext).tryRestore(param.getGlobalRuleConfigs());
param.getGlobalRuleConfigs().clear();
param.getGlobalRuleConfigs().addAll(globalRuleConfigs);
} else {
globalRuleConfigs = param.getGlobalRuleConfigs();
}
private static MetaDataContexts createByLocal(final MetaDataPersistService persistService,
final ContextManagerBuilderParameter param, final ComputeNodeInstanceContext instanceContext) throws SQLException {
Collection<RuleConfiguration> globalRuleConfigs = param.getGlobalRuleConfigs();
ConfigurationProperties props = new ConfigurationProperties(param.getProps());
Map<String, ShardingSphereDatabase> databases = ExternalMetaDataFactory.create(effectiveDatabaseConfigs, props, instanceContext);
Map<String, ShardingSphereDatabase> databases = ExternalMetaDataFactory.create(param.getDatabaseConfigs(), props, instanceContext);
MetaDataContexts result = newMetaDataContexts(persistService, param, globalRuleConfigs, databases, props);
persistDatabaseConfigurations(result, param, persistService, instanceContext);
persistMetaData(result, persistService);
Expand All @@ -106,9 +93,7 @@ private static MetaDataContexts createByRepository(final MetaDataPersistService
Collection<RuleConfiguration> globalRuleConfigs = persistService.getGlobalRuleService().load();
ConfigurationProperties props = new ConfigurationProperties(persistService.getPropsService().load());
Map<String, ShardingSphereDatabase> databases = InternalMetaDataFactory.create(persistService, effectiveDatabaseConfigs, props, instanceContext);
MetaDataContexts result = newMetaDataContexts(persistService, param, globalRuleConfigs, databases, props);
restoreRules(result, instanceContext);
return result;
return newMetaDataContexts(persistService, param, globalRuleConfigs, databases, props);
}

private static MetaDataContexts newMetaDataContexts(final MetaDataPersistService persistService, final ContextManagerBuilderParameter param,
Expand Down Expand Up @@ -147,25 +132,10 @@ private static void closeGeneratedDataSources(final String databaseName, final M
}
}

@SuppressWarnings({"unchecked", "rawtypes"})
private static void restoreRules(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext instanceContext) {
if (!instanceContext.getModeConfiguration().isCluster()) {
return;
}
for (RuleConfigurationPersistDecorator each : ShardingSphereServiceLoader.getServiceInstances(RuleConfigurationPersistDecorator.class)) {
ShardingSphereRule rule = metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(each.getRuleType());
metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().removeIf(eachRule -> each.getRuleType() == eachRule.getClass());
RuleConfiguration restoredRuleConfig = each.restore(rule.getConfiguration());
ShardingSphereRule rebuiltRule = GlobalRulesBuilder.buildRules(
Collections.singleton(restoredRuleConfig), metaDataContexts.getMetaData().getAllDatabases(), metaDataContexts.getMetaData().getProps()).iterator().next();
metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().add(rebuiltRule);
}
}

private static void persistDatabaseConfigurations(final MetaDataContexts metadataContexts, final ContextManagerBuilderParameter param, final MetaDataPersistService persistService,
final ComputeNodeInstanceContext instanceContext) {
RuleConfigurationPersistDecorateEngine ruleConfigPersistDecorateEngine = new RuleConfigurationPersistDecorateEngine(instanceContext);
persistService.persistGlobalRuleConfiguration(ruleConfigPersistDecorateEngine.decorate(metadataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations()), param.getProps());
private static void persistDatabaseConfigurations(final MetaDataContexts metadataContexts,
final ContextManagerBuilderParameter param, final MetaDataPersistService persistService, final ComputeNodeInstanceContext instanceContext) {
Collection<RuleConfiguration> globalRuleConfigs = metadataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations();
persistService.persistGlobalRuleConfiguration(globalRuleConfigs, param.getProps());
for (Entry<String, ? extends DatabaseConfiguration> entry : param.getDatabaseConfigs().entrySet()) {
String databaseName = entry.getKey();
persistService.persistConfigurations(entry.getKey(), entry.getValue(),
Expand Down

This file was deleted.

Loading
Loading