Skip to content

Commit

Permalink
Remove useless RuleConfigurationPersistDecorator (#34322)
Browse files Browse the repository at this point in the history
* Simplify MetaDataContextsFactory

* Remove useless RuleConfigurationPersistDecorator
  • Loading branch information
terrymanu authored Jan 12, 2025
1 parent 0f36656 commit e78d0bd
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 287 deletions.

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 Down Expand Up @@ -98,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 @@ -139,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.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@

import com.google.common.base.Preconditions;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.metadata.persist.node.GlobalNodePath;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.cluster.dispatch.checker.ActiveVersionChecker;
import org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.DataChangedEventHandler;
import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator;
import org.apache.shardingsphere.mode.metadata.persist.node.GlobalNodePath;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -47,7 +45,6 @@ public Collection<Type> getSubscribedTypes() {
return Arrays.asList(Type.ADDED, Type.UPDATED);
}

@SuppressWarnings("unchecked")
@Override
public void handle(final ContextManager contextManager, final DataChangedEvent event) {
if (!org.apache.shardingsphere.mode.path.GlobalNodePath.isRuleActiveVersionPath(event.getKey())) {
Expand All @@ -60,7 +57,6 @@ public void handle(final ContextManager contextManager, final DataChangedEvent e
ActiveVersionChecker.checkActiveVersion(contextManager, event);
Optional<RuleConfiguration> ruleConfig = contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(ruleName.get());
Preconditions.checkArgument(ruleConfig.isPresent(), "Can not find rule configuration with name: %s", ruleName.get());
contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterGlobalRuleConfiguration(
TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, ruleConfig.get().getClass()).map(optional -> optional.restore(ruleConfig.get())).orElse(ruleConfig.get()));
contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterGlobalRuleConfiguration(ruleConfig.get());
}
}
Loading

0 comments on commit e78d0bd

Please sign in to comment.