This is an automated email from the ASF dual-hosted git repository.

zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 5047e9115c2 Fix global rule persist and renew in new metadata 
structure (#26589)
5047e9115c2 is described below

commit 5047e9115c22e42d68f7d276ddee93745545b998
Author: ChenJiaHao <[email protected]>
AuthorDate: Mon Jun 26 19:35:30 2023 +0800

    Fix global rule persist and renew in new metadata structure (#26589)
---
 .../rule/builder/global/GlobalRulesBuilder.java    | 19 ++++++++++++++++
 ... => NewYamlGlobalRuleConfigurationSwapper.java} |  2 +-
 ...ewYamlGlobalRuleConfigurationSwapperEngine.java | 24 +++++++++++++++++---
 .../NewYamlAuthorityRuleConfigurationSwapper.java  |  4 ++--
 ...per.rule.NewYamlGlobalRuleConfigurationSwapper} |  0
 ...NewYamlGlobalClockRuleConfigurationSwapper.java |  4 ++--
 ...per.rule.NewYamlGlobalRuleConfigurationSwapper} |  0
 .../NewYamlLoggingRuleConfigurationSwapper.java    |  4 ++--
 ...per.rule.NewYamlGlobalRuleConfigurationSwapper} |  0
 .../metadata/persist/node/NewGlobalNode.java       | 10 +++++++++
 .../config/global/GlobalPersistService.java        |  4 ++--
 .../config/global/NewGlobalRulePersistService.java | 26 ++++++++++------------
 ...wYamlSQLFederationRuleConfigurationSwapper.java |  4 ++--
 ...per.rule.NewYamlGlobalRuleConfigurationSwapper} |  0
 .../NewYamlSQLParserRuleConfigurationSwapper.java  |  4 ++--
 ...per.rule.NewYamlGlobalRuleConfigurationSwapper} |  0
 ...wYamlSQLTranslatorRuleConfigurationSwapper.java |  4 ++--
 ...per.rule.NewYamlGlobalRuleConfigurationSwapper} |  0
 .../NewYamlTrafficRuleConfigurationSwapper.java    |  4 ++--
 ...per.rule.NewYamlGlobalRuleConfigurationSwapper} |  0
 ...NewYamlTransactionRuleConfigurationSwapper.java |  4 ++--
 ...per.rule.NewYamlGlobalRuleConfigurationSwapper} |  0
 .../mode/manager/ContextManager.java               | 17 +++++++++-----
 .../NewConfigurationChangedSubscriber.java         |  3 +--
 .../NewUpdatableGlobalRuleRALBackendHandler.java   |  3 +--
 25 files changed, 95 insertions(+), 45 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
index 74fbd1c49da..fb83e47c515 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
@@ -26,6 +26,7 @@ import 
org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.util.spi.type.ordered.OrderedSPILoader;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
@@ -80,4 +81,22 @@ public final class GlobalRulesBuilder {
         Collection<Class<GlobalRuleBuilder>> configuredBuilderClasses = 
configuredBuilders.stream().map(each -> (Class<GlobalRuleBuilder>) 
each.getClass()).collect(Collectors.toSet());
         return 
OrderedSPILoader.getServices(GlobalRuleBuilder.class).stream().filter(each -> 
!configuredBuilderClasses.contains(each.getClass())).collect(Collectors.toList());
     }
+    
+    /**
+     * Build single rule.
+     *
+     * @param globalRuleConfig global rule configuration
+     * @param databases databases
+     * @param props props
+     * @return built rule
+     */
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public static Collection<ShardingSphereRule> buildSingleRules(final 
RuleConfiguration globalRuleConfig,
+                                                                  final 
Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties 
props) {
+        Collection<ShardingSphereRule> result = new LinkedList<>();
+        for (Entry<RuleConfiguration, GlobalRuleBuilder> each : 
OrderedSPILoader.getServices(GlobalRuleBuilder.class, 
Collections.singleton(globalRuleConfig)).entrySet()) {
+            result.add(each.getValue().build(each.getKey(), databases, props));
+        }
+        return result;
+    }
 }
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamGlobalRuleConfigurationSwapper.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamlGlobalRuleConfigurationSwapper.java
similarity index 91%
rename from 
infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamGlobalRuleConfigurationSwapper.java
rename to 
infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamlGlobalRuleConfigurationSwapper.java
index 98ea2230228..c3e1a1537a7 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamGlobalRuleConfigurationSwapper.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamlGlobalRuleConfigurationSwapper.java
@@ -29,7 +29,7 @@ import 
org.apache.shardingsphere.infra.util.yaml.swapper.NewYamlConfigurationSwa
  * @param <T> type of rule configuration
  */
 @SingletonSPI
-public interface NewYamGlobalRuleConfigurationSwapper<T extends 
RuleConfiguration> extends NewYamlConfigurationSwapper<T>, OrderedSPI<T> {
+public interface NewYamlGlobalRuleConfigurationSwapper<T extends 
RuleConfiguration> extends NewYamlConfigurationSwapper<T>, OrderedSPI<T> {
     
     /**
      * Get YAML rule tag name.
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamlGlobalRuleConfigurationSwapperEngine.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamlGlobalRuleConfigurationSwapperEngine.java
index 9660ee0e1e0..455f510febe 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamlGlobalRuleConfigurationSwapperEngine.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/NewYamlGlobalRuleConfigurationSwapperEngine.java
@@ -24,6 +24,7 @@ import 
org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.Optional;
 
 /**
  * TODO Rename YamlGlobalRuleConfigurationSwapperEngine when metadata 
structure adjustment completed. #25485
@@ -38,8 +39,8 @@ public final class 
NewYamlGlobalRuleConfigurationSwapperEngine {
      * @return YAML global rule configurations
      */
     @SuppressWarnings("rawtypes")
-    public Map<RuleConfiguration, NewYamGlobalRuleConfigurationSwapper> 
swapToYamlRuleConfigurations(final Collection<RuleConfiguration> ruleConfigs) {
-        return 
OrderedSPILoader.getServices(NewYamGlobalRuleConfigurationSwapper.class, 
ruleConfigs);
+    public Map<RuleConfiguration, NewYamlGlobalRuleConfigurationSwapper> 
swapToYamlRuleConfigurations(final Collection<RuleConfiguration> ruleConfigs) {
+        return 
OrderedSPILoader.getServices(NewYamlGlobalRuleConfigurationSwapper.class, 
ruleConfigs);
     }
     
     /**
@@ -51,9 +52,26 @@ public final class 
NewYamlGlobalRuleConfigurationSwapperEngine {
     @SuppressWarnings({"unchecked", "rawtypes"})
     public Collection<RuleConfiguration> swapToRuleConfigurations(final 
Collection<YamlDataNode> dataNodes) {
         Collection<RuleConfiguration> result = new LinkedList<>();
-        for (NewYamGlobalRuleConfigurationSwapper each : 
OrderedSPILoader.getServices(NewYamGlobalRuleConfigurationSwapper.class)) {
+        for (NewYamlGlobalRuleConfigurationSwapper each : 
OrderedSPILoader.getServices(NewYamlGlobalRuleConfigurationSwapper.class)) {
             result.add((RuleConfiguration) each.swapToObject(dataNodes));
         }
         return result;
     }
+    
+    /**
+     * Swap from single YAML global rule configuration to rule configurations.
+     *
+     * @param ruleName rule name
+     * @param dataNodes YAML data nodes
+     * @return global rule configuration
+     */
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public Optional<RuleConfiguration> swapSingleRuleToRuleConfiguration(final 
String ruleName, final Collection<YamlDataNode> dataNodes) {
+        for (NewYamlGlobalRuleConfigurationSwapper each : 
OrderedSPILoader.getServices(NewYamlGlobalRuleConfigurationSwapper.class)) {
+            if (ruleName.equals(each.getRuleTagName().toLowerCase())) {
+                return Optional.of((RuleConfiguration) 
each.swapToObject(dataNodes));
+            }
+        }
+        return Optional.empty();
+    }
 }
diff --git 
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
 
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
index c02b20cd885..f28b600b8e0 100644
--- 
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
+++ 
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
@@ -28,7 +28,7 @@ import 
org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -38,7 +38,7 @@ import java.util.Optional;
  * TODO Rename YamlAuthorityRuleConfigurationSwapper when metadata structure 
adjustment completed. #25485
  * New YAML Authority rule configuration swapper.
  */
-public final class NewYamlAuthorityRuleConfigurationSwapper implements 
NewYamGlobalRuleConfigurationSwapper<AuthorityRuleConfiguration> {
+public final class NewYamlAuthorityRuleConfigurationSwapper implements 
NewYamlGlobalRuleConfigurationSwapper<AuthorityRuleConfiguration> {
     
     private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new 
YamlAlgorithmConfigurationSwapper();
     
diff --git 
a/kernel/authority/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
 
b/kernel/authority/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
similarity index 100%
rename from 
kernel/authority/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
rename to 
kernel/authority/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
diff --git 
a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/NewYamlGlobalClockRuleConfigurationSwapper.java
 
b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/NewYamlGlobalClockRuleConfigurationSwapper.java
index 844206429b6..1f9857ef45f 100644
--- 
a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/NewYamlGlobalClockRuleConfigurationSwapper.java
+++ 
b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/yaml/swapper/NewYamlGlobalClockRuleConfigurationSwapper.java
@@ -23,7 +23,7 @@ import 
org.apache.shardingsphere.globalclock.core.yaml.config.YamlGlobalClockRul
 import org.apache.shardingsphere.infra.config.converter.GlobalNodeConverter;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -34,7 +34,7 @@ import java.util.Properties;
  * TODO Rename YamlGlobalClockRuleConfigurationSwapper when metadata structure 
adjustment completed. #25485
  * YAML global clock rule configuration swapper.
  */
-public final class NewYamlGlobalClockRuleConfigurationSwapper implements 
NewYamGlobalRuleConfigurationSwapper<GlobalClockRuleConfiguration> {
+public final class NewYamlGlobalClockRuleConfigurationSwapper implements 
NewYamlGlobalRuleConfigurationSwapper<GlobalClockRuleConfiguration> {
     
     @Override
     public Collection<YamlDataNode> swapToDataNodes(final 
GlobalClockRuleConfiguration data) {
diff --git 
a/kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
 
b/kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
similarity index 100%
rename from 
kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
rename to 
kernel/global-clock/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
diff --git 
a/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/yaml/swapper/NewYamlLoggingRuleConfigurationSwapper.java
 
b/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/yaml/swapper/NewYamlLoggingRuleConfigurationSwapper.java
index fb11547affe..cab53e99fba 100644
--- 
a/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/yaml/swapper/NewYamlLoggingRuleConfigurationSwapper.java
+++ 
b/kernel/logging/core/src/main/java/org/apache/shardingsphere/logging/yaml/swapper/NewYamlLoggingRuleConfigurationSwapper.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.logging.yaml.swapper;
 import org.apache.shardingsphere.infra.config.converter.GlobalNodeConverter;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 import org.apache.shardingsphere.logging.config.LoggingRuleConfiguration;
 import org.apache.shardingsphere.logging.constant.LoggingOrder;
 import 
org.apache.shardingsphere.logging.rule.builder.DefaultLoggingRuleConfigurationBuilder;
@@ -36,7 +36,7 @@ import java.util.Optional;
  * TODO Rename YamlLoggingRuleConfigurationSwapper when metadata structure 
adjustment completed. #25485
  * YAML logging rule configuration swapper.
  */
-public final class NewYamlLoggingRuleConfigurationSwapper implements 
NewYamGlobalRuleConfigurationSwapper<LoggingRuleConfiguration> {
+public final class NewYamlLoggingRuleConfigurationSwapper implements 
NewYamlGlobalRuleConfigurationSwapper<LoggingRuleConfiguration> {
     
     @Override
     public Collection<YamlDataNode> swapToDataNodes(final 
LoggingRuleConfiguration data) {
diff --git 
a/kernel/logging/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
 
b/kernel/logging/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
similarity index 100%
rename from 
kernel/logging/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
rename to 
kernel/logging/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
diff --git 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNode.java
 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNode.java
index 317f31f2498..91b7a7bdae2 100644
--- 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNode.java
+++ 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/NewGlobalNode.java
@@ -35,6 +35,16 @@ public final class NewGlobalNode {
     
     private static final String VERSIONS = "versions";
     
+    /**
+     * Get global rule node.
+     *
+     * @param rulePath rule path
+     * @return global rule node
+     */
+    public static String getGlobalRuleNode(final String rulePath) {
+        return String.join("/", getGlobalRuleRootNode(), rulePath);
+    }
+    
     /**
      * Get global rule active version node.
      *
diff --git 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalPersistService.java
 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalPersistService.java
index 85eab13b6b1..8b543b77a4e 100644
--- 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalPersistService.java
+++ 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalPersistService.java
@@ -59,8 +59,8 @@ public interface GlobalPersistService<T> {
      * @param ruleName rule name
      * @return single rule configuration
      */
-    default Collection<RuleConfiguration> load(String ruleName) {
-        return Collections.emptyList();
+    default RuleConfiguration load(String ruleName) {
+        return null;
     }
     
     /**
diff --git 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewGlobalRulePersistService.java
 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewGlobalRulePersistService.java
index a9013bb6bdd..5a932ea426d 100644
--- 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewGlobalRulePersistService.java
+++ 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/NewGlobalRulePersistService.java
@@ -23,21 +23,19 @@ import 
org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapperEngine;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapper;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapperEngine;
 import org.apache.shardingsphere.metadata.persist.node.NewGlobalNode;
 import 
org.apache.shardingsphere.metadata.persist.service.config.AbstractPersistService;
 import org.apache.shardingsphere.mode.spi.PersistRepository;
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Optional;
-import java.util.Map;
-import java.util.List;
 import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 
 /**
  * TODO Rename GlobalRulePersistService when metadata structure adjustment 
completed. #25485
@@ -57,8 +55,8 @@ public final class NewGlobalRulePersistService extends 
AbstractPersistService im
     @SuppressWarnings({"unchecked", "rawtypes"})
     @Override
     public void persist(final Collection<RuleConfiguration> globalRuleConfigs) 
{
-        Map<RuleConfiguration, NewYamGlobalRuleConfigurationSwapper> 
yamlConfigs = new 
NewYamlGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs);
-        for (Entry<RuleConfiguration, NewYamGlobalRuleConfigurationSwapper> 
entry : yamlConfigs.entrySet()) {
+        Map<RuleConfiguration, NewYamlGlobalRuleConfigurationSwapper> 
yamlConfigs = new 
NewYamlGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs);
+        for (Entry<RuleConfiguration, NewYamlGlobalRuleConfigurationSwapper> 
entry : yamlConfigs.entrySet()) {
             Collection<YamlDataNode> dataNodes = 
entry.getValue().swapToDataNodes(entry.getKey());
             if (dataNodes.isEmpty()) {
                 continue;
@@ -71,8 +69,8 @@ public final class NewGlobalRulePersistService extends 
AbstractPersistService im
     @Override
     public Collection<MetaDataVersion> persistConfig(final 
Collection<RuleConfiguration> globalRuleConfigs) {
         Collection<MetaDataVersion> result = new LinkedList<>();
-        Map<RuleConfiguration, NewYamlRuleConfigurationSwapper> yamlConfigs = 
new 
NewYamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs);
-        for (Entry<RuleConfiguration, NewYamlRuleConfigurationSwapper> entry : 
yamlConfigs.entrySet()) {
+        Map<RuleConfiguration, NewYamlGlobalRuleConfigurationSwapper> 
yamlConfigs = new 
NewYamlGlobalRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs);
+        for (Entry<RuleConfiguration, NewYamlGlobalRuleConfigurationSwapper> 
entry : yamlConfigs.entrySet()) {
             Collection<YamlDataNode> dataNodes = 
entry.getValue().swapToDataNodes(entry.getKey());
             if (dataNodes.isEmpty()) {
                 continue;
@@ -92,7 +90,7 @@ public final class NewGlobalRulePersistService extends 
AbstractPersistService im
             if 
(Strings.isNullOrEmpty(getActiveVersion(NewGlobalNode.getGlobalRuleActiveVersionNode(each.getKey()))))
 {
                 
repository.persist(NewGlobalNode.getGlobalRuleActiveVersionNode(each.getKey()), 
DEFAULT_VERSION);
             }
-            result.add(new MetaDataVersion(persistKey, 
NewGlobalNode.getGlobalRuleActiveVersionNode(each.getKey()), 
nextActiveVersion));
+            result.add(new 
MetaDataVersion(NewGlobalNode.getGlobalRuleNode(each.getKey()), 
getActiveVersion(NewGlobalNode.getGlobalRuleActiveVersionNode(each.getKey())), 
nextActiveVersion));
         }
         return result;
     }
@@ -104,9 +102,9 @@ public final class NewGlobalRulePersistService extends 
AbstractPersistService im
     }
     
     @Override
-    public Collection<RuleConfiguration> load(final String ruleName) {
-        Collection<YamlDataNode> dataNodes = 
getDataNodes(NewGlobalNode.getGlobalRuleVersionsNode(ruleName));
-        return dataNodes.isEmpty() ? Collections.emptyList() : new 
NewYamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(dataNodes);
+    public RuleConfiguration load(final String ruleName) {
+        Collection<YamlDataNode> dataNodes = 
getDataNodes(NewGlobalNode.getGlobalRuleNode(ruleName));
+        return new 
NewYamlGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName,
 dataNodes).orElse(null);
     }
     
     /**
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/NewYamlSQLFederationRuleConfigurationSwapper.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/NewYamlSQLFederationRuleConfigurationSwapper.java
index 6852bf1888b..1226ba90398 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/NewYamlSQLFederationRuleConfigurationSwapper.java
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/yaml/swapper/NewYamlSQLFederationRuleConfigurationSwapper.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.sqlfederation.yaml.swapper;
 import org.apache.shardingsphere.infra.config.converter.GlobalNodeConverter;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 import org.apache.shardingsphere.sql.parser.api.CacheOption;
 import 
org.apache.shardingsphere.sqlfederation.api.config.SQLFederationRuleConfiguration;
 import org.apache.shardingsphere.sqlfederation.constant.SQLFederationOrder;
@@ -34,7 +34,7 @@ import java.util.Optional;
  * TODO Rename YamlSQLTranslatorRuleConfigurationSwapper when metadata 
structure adjustment completed. #25485
  * YAML SQL federation rule configuration swapper.
  */
-public final class NewYamlSQLFederationRuleConfigurationSwapper implements 
NewYamGlobalRuleConfigurationSwapper<SQLFederationRuleConfiguration> {
+public final class NewYamlSQLFederationRuleConfigurationSwapper implements 
NewYamlGlobalRuleConfigurationSwapper<SQLFederationRuleConfiguration> {
     
     private final YamlSQLFederationExecutionPlanCacheConfigurationSwapper 
executionPlanCacheConfigSwapper = new 
YamlSQLFederationExecutionPlanCacheConfigurationSwapper();
     
diff --git 
a/kernel/sql-federation/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
 
b/kernel/sql-federation/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
similarity index 100%
rename from 
kernel/sql-federation/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
rename to 
kernel/sql-federation/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
diff --git 
a/kernel/sql-parser/core/src/main/java/org/apache/shardingsphere/parser/yaml/swapper/NewYamlSQLParserRuleConfigurationSwapper.java
 
b/kernel/sql-parser/core/src/main/java/org/apache/shardingsphere/parser/yaml/swapper/NewYamlSQLParserRuleConfigurationSwapper.java
index 2613aa50a91..b10719ef808 100644
--- 
a/kernel/sql-parser/core/src/main/java/org/apache/shardingsphere/parser/yaml/swapper/NewYamlSQLParserRuleConfigurationSwapper.java
+++ 
b/kernel/sql-parser/core/src/main/java/org/apache/shardingsphere/parser/yaml/swapper/NewYamlSQLParserRuleConfigurationSwapper.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.parser.yaml.swapper;
 import org.apache.shardingsphere.infra.config.converter.GlobalNodeConverter;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
 import org.apache.shardingsphere.parser.constant.SQLParserOrder;
 import 
org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
@@ -35,7 +35,7 @@ import java.util.Optional;
  * TODO Rename YamlSQLTranslatorRuleConfigurationSwapper when metadata 
structure adjustment completed. #25485
  * YAML SQL parser rule configuration swapper.
  */
-public final class NewYamlSQLParserRuleConfigurationSwapper implements 
NewYamGlobalRuleConfigurationSwapper<SQLParserRuleConfiguration> {
+public final class NewYamlSQLParserRuleConfigurationSwapper implements 
NewYamlGlobalRuleConfigurationSwapper<SQLParserRuleConfiguration> {
     
     private final YamlSQLParserCacheOptionConfigurationSwapper 
cacheOptionSwapper = new YamlSQLParserCacheOptionConfigurationSwapper();
     
diff --git 
a/kernel/sql-parser/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
 
b/kernel/sql-parser/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
similarity index 100%
rename from 
kernel/sql-parser/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
rename to 
kernel/sql-parser/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
diff --git 
a/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapper.java
 
b/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapper.java
index 90b02ed65ba..ffa310f8b59 100644
--- 
a/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapper.java
+++ 
b/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapper.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.sqltranslator.yaml.swapper;
 import org.apache.shardingsphere.infra.config.converter.GlobalNodeConverter;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 import 
org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
 import org.apache.shardingsphere.sqltranslator.constant.SQLTranslatorOrder;
 import 
org.apache.shardingsphere.sqltranslator.yaml.config.YamlSQLTranslatorRuleConfiguration;
@@ -33,7 +33,7 @@ import java.util.Optional;
  * TODO Rename YamlSQLTranslatorRuleConfigurationSwapper when metadata 
structure adjustment completed. #25485
  * YAML SQL translator rule configuration swapper.
  */
-public final class NewYamlSQLTranslatorRuleConfigurationSwapper implements 
NewYamGlobalRuleConfigurationSwapper<SQLTranslatorRuleConfiguration> {
+public final class NewYamlSQLTranslatorRuleConfigurationSwapper implements 
NewYamlGlobalRuleConfigurationSwapper<SQLTranslatorRuleConfiguration> {
     
     @Override
     public Collection<YamlDataNode> swapToDataNodes(final 
SQLTranslatorRuleConfiguration data) {
diff --git 
a/kernel/sql-translator/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
 
b/kernel/sql-translator/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
similarity index 100%
rename from 
kernel/sql-translator/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
rename to 
kernel/sql-translator/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
diff --git 
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapper.java
 
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapper.java
index ed02c672440..650ef2b34c2 100644
--- 
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapper.java
+++ 
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapper.java
@@ -21,7 +21,7 @@ import 
org.apache.shardingsphere.infra.config.converter.GlobalNodeConverter;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
 import org.apache.shardingsphere.traffic.constant.TrafficOrder;
 import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficRuleConfiguration;
@@ -36,7 +36,7 @@ import java.util.Optional;
  * TODO Rename YamlTrafficRuleConfigurationSwapper when metadata structure 
adjustment completed. #25485
  * New YAML traffic rule configuration swapper.
  */
-public final class NewYamlTrafficRuleConfigurationSwapper implements 
NewYamGlobalRuleConfigurationSwapper<TrafficRuleConfiguration> {
+public final class NewYamlTrafficRuleConfigurationSwapper implements 
NewYamlGlobalRuleConfigurationSwapper<TrafficRuleConfiguration> {
     
     private final YamlTrafficStrategyConfigurationSwapper strategySwapper = 
new YamlTrafficStrategyConfigurationSwapper();
     
diff --git 
a/kernel/traffic/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
 
b/kernel/traffic/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
similarity index 100%
rename from 
kernel/traffic/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
rename to 
kernel/traffic/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
diff --git 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/yaml/swapper/NewYamlTransactionRuleConfigurationSwapper.java
 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/yaml/swapper/NewYamlTransactionRuleConfigurationSwapper.java
index 836299d52b4..3254d796a6d 100644
--- 
a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/yaml/swapper/NewYamlTransactionRuleConfigurationSwapper.java
+++ 
b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/yaml/swapper/NewYamlTransactionRuleConfigurationSwapper.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.transaction.yaml.swapper;
 import org.apache.shardingsphere.infra.config.converter.GlobalNodeConverter;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper;
 import org.apache.shardingsphere.transaction.api.TransactionType;
 import 
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
 import org.apache.shardingsphere.transaction.constant.TransactionOrder;
@@ -35,7 +35,7 @@ import java.util.Properties;
  * TODO Rename YamlTransactionRuleConfigurationSwapper when metadata structure 
adjustment completed. #25485
  * New YAML Transaction rule configuration swapper.
  */
-public final class NewYamlTransactionRuleConfigurationSwapper implements 
NewYamGlobalRuleConfigurationSwapper<TransactionRuleConfiguration> {
+public final class NewYamlTransactionRuleConfigurationSwapper implements 
NewYamlGlobalRuleConfigurationSwapper<TransactionRuleConfiguration> {
     
     @Override
     public Collection<YamlDataNode> swapToDataNodes(final 
TransactionRuleConfiguration data) {
diff --git 
a/kernel/transaction/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
 
b/kernel/transaction/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
similarity index 100%
rename from 
kernel/transaction/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamGlobalRuleConfigurationSwapper
rename to 
kernel/transaction/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRuleConfigurationSwapper
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index f739f64279c..68e3148643e 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -561,13 +561,14 @@ public final class ContextManager implements 
AutoCloseable {
     /**
      * Alter global rule configuration.
      *
-     * @param ruleName rule name
      * @param ruleConfig global rule configuration
      */
-    public synchronized void alterGlobalRuleConfiguration(final String 
ruleName, final RuleConfiguration ruleConfig) {
-        Collection<ShardingSphereRule> rules = 
removeSingleGlobalRule(ruleName);
-        
rules.addAll(GlobalRulesBuilder.buildRules(Collections.singletonList(ruleConfig),
 metaDataContexts.get().getMetaData().getDatabases(),
-                metaDataContexts.get().getMetaData().getProps()));
+    public synchronized void alterGlobalRuleConfiguration(final 
RuleConfiguration ruleConfig) {
+        if (null == ruleConfig) {
+            return;
+        }
+        Collection<ShardingSphereRule> rules = 
removeSingleGlobalRule(ruleConfig);
+        rules.addAll(GlobalRulesBuilder.buildSingleRules(ruleConfig, 
metaDataContexts.get().getMetaData().getDatabases(), 
metaDataContexts.get().getMetaData().getProps()));
         
metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules().clear();
         
metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules().addAll(rules);
         ShardingSphereMetaData toBeChangedMetaData = new 
ShardingSphereMetaData(
@@ -592,6 +593,12 @@ public final class ContextManager implements AutoCloseable 
{
         metaDataContexts.set(newMetaDataContexts(toBeChangedMetaData));
     }
     
+    private Collection<ShardingSphereRule> removeSingleGlobalRule(final 
RuleConfiguration ruleConfig) {
+        Collection<ShardingSphereRule> result = new 
LinkedList<>(metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules());
+        result.removeIf(each -> 
each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass()));
+        return result;
+    }
+    
     private Collection<ShardingSphereRule> removeSingleGlobalRule(final String 
ruleSimpleName) {
         Collection<ShardingSphereRule> result = new 
LinkedList<>(metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules());
         result.removeIf(each -> each.getType().equals(ruleSimpleName));
diff --git 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewConfigurationChangedSubscriber.java
 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewConfigurationChangedSubscriber.java
index 8bfa7a800b2..1341bbf6068 100644
--- 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewConfigurationChangedSubscriber.java
+++ 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/NewConfigurationChangedSubscriber.java
@@ -58,8 +58,7 @@ public final class NewConfigurationChangedSubscriber {
         if 
(!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey())))
 {
             return;
         }
-        contextManager.alterGlobalRuleConfiguration(event.getRuleSimpleName(),
-                
contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().load(event.getRuleSimpleName()).iterator().next());
+        
contextManager.alterGlobalRuleConfiguration(contextManager.getMetaDataContexts().getPersistService().getGlobalRuleService().load(event.getRuleSimpleName()));
     }
     
     /**
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/NewUpdatableGlobalRuleRALBackendHandler.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/NewUpdatableGlobalRuleRALBackendHandler.java
index b936d587092..38f9e9038e3 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/NewUpdatableGlobalRuleRALBackendHandler.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/NewUpdatableGlobalRuleRALBackendHandler.java
@@ -51,8 +51,7 @@ public final class NewUpdatableGlobalRuleRALBackendHandler 
implements DistSQLBac
         Collection<RuleConfiguration> ruleConfigurations = 
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations();
         RuleConfiguration currentRuleConfig = 
findCurrentRuleConfiguration(ruleConfigurations, ruleConfigClass);
         globalRuleUpdater.checkSQLStatement(currentRuleConfig, sqlStatement);
-        contextManager.getInstanceContext().getModeContextManager()
-                
.alterGlobalRuleConfiguration(processUpdate(ruleConfigurations, sqlStatement, 
globalRuleUpdater, currentRuleConfig));
+        
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(processUpdate(ruleConfigurations,
 sqlStatement, globalRuleUpdater, currentRuleConfig));
         return new UpdateResponseHeader(sqlStatement);
     }
     


Reply via email to