Skip to content

Commit

Permalink
Refactor SetDistVariableExecutor (#32365)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Aug 1, 2024
1 parent 314da2e commit af8b1e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static MetaDataContexts create(final MetaDataPersistService persistServic
if (isDatabaseMetaDataExisted) {
restoreRules(result, computeNodeInstanceContext);
} else {
persistDatabaseConfigurations(result, param, persistService);
persistDatabaseConfigurations(result, param, persistService, computeNodeInstanceContext);
persistMetaData(result, persistService);
}
return result;
Expand Down Expand Up @@ -223,8 +223,9 @@ private static void restoreRules(final MetaDataContexts metaDataContexts, final
}
}

private static void persistDatabaseConfigurations(final MetaDataContexts metadataContexts, final ContextManagerBuilderParameter param, final MetaDataPersistService persistService) {
persistService.persistGlobalRuleConfiguration(decorateGlobalRuleConfigurations(param.getGlobalRuleConfigs()), param.getProps());
private static void persistDatabaseConfigurations(final MetaDataContexts metadataContexts, final ContextManagerBuilderParameter param, final MetaDataPersistService persistService,
final ComputeNodeInstanceContext computeNodeInstanceContext) {
persistService.persistGlobalRuleConfiguration(decorateGlobalRuleConfigurations(param.getGlobalRuleConfigs(), computeNodeInstanceContext), param.getProps());
for (Entry<String, ? extends DatabaseConfiguration> entry : param.getDatabaseConfigs().entrySet()) {
String databaseName = entry.getKey();
persistService.persistConfigurations(entry.getKey(), entry.getValue(),
Expand All @@ -235,11 +236,11 @@ private static void persistDatabaseConfigurations(final MetaDataContexts metadat
}

@SuppressWarnings({"rawtypes", "unchecked"})
private static Collection<RuleConfiguration> decorateGlobalRuleConfigurations(final Collection<RuleConfiguration> globalRuleConfigs) {
private static Collection<RuleConfiguration> decorateGlobalRuleConfigurations(final Collection<RuleConfiguration> globalRuleConfigs, final ComputeNodeInstanceContext computeNodeInstanceContext) {
Collection<RuleConfiguration> result = new LinkedList<>();
for (RuleConfiguration each : globalRuleConfigs) {
Optional<RulePersistDecorator> rulePersistDecorator = TypedSPILoader.findService(RulePersistDecorator.class, each.getClass());
result.add(rulePersistDecorator.isPresent() ? rulePersistDecorator.get().decorate(each) : each);
result.add(rulePersistDecorator.isPresent() && computeNodeInstanceContext.isCluster() ? rulePersistDecorator.get().decorate(each) : each);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@
import org.apache.shardingsphere.distsql.statement.ral.updatable.SetDistVariableStatement;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.kernel.syntax.InvalidVariableValueException;
import org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedVariableException;
import org.apache.shardingsphere.infra.props.TypedPropertyKey;
import org.apache.shardingsphere.infra.props.TypedPropertyValue;
import org.apache.shardingsphere.infra.props.exception.TypedPropertyValueException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.logging.constant.LoggingConstants;
import org.apache.shardingsphere.logging.util.LoggingUtils;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.spi.RulePersistDecorator;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Properties;

/**
Expand Down Expand Up @@ -101,7 +107,7 @@ private void syncSQLShowToLoggingRule(final TypedPropertyKey propertyKey, final
if (LoggingConstants.SQL_SHOW.equalsIgnoreCase(propertyKey.getKey())) {
LoggingUtils.getSQLLogger(metaDataContexts.getMetaData().getGlobalRuleMetaData()).ifPresent(option -> {
option.getProps().setProperty(LoggingConstants.SQL_LOG_ENABLE, value);
contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations());
decorateGlobalRuleConfiguration(contextManager);
});
}
}
Expand All @@ -110,11 +116,21 @@ private void syncSQLSimpleToLoggingRule(final TypedPropertyKey propertyKey, fina
if (LoggingConstants.SQL_SIMPLE.equalsIgnoreCase(propertyKey.getKey())) {
LoggingUtils.getSQLLogger(metaDataContexts.getMetaData().getGlobalRuleMetaData()).ifPresent(option -> {
option.getProps().setProperty(LoggingConstants.SQL_LOG_SIMPLE, value);
contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().persist(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations());
decorateGlobalRuleConfiguration(contextManager);
});
}
}

@SuppressWarnings({"rawtypes", "unchecked"})
private void decorateGlobalRuleConfiguration(final ContextManager contextManager) {
Collection<RuleConfiguration> globalRuleConfigs = new LinkedList<>();
for (RuleConfiguration each : contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations()) {
Optional<RulePersistDecorator> rulePersistDecorator = TypedSPILoader.findService(RulePersistDecorator.class, each);
globalRuleConfigs.add(rulePersistDecorator.isPresent() && contextManager.getComputeNodeInstanceContext().isCluster() ? rulePersistDecorator.get().decorate(each) : each);
}
contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().persist(globalRuleConfigs);
}

@Override
public Class<SetDistVariableStatement> getType() {
return SetDistVariableStatement.class;
Expand Down

0 comments on commit af8b1e2

Please sign in to comment.