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

menghaoran 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 caf6a6762ea Use refresh instead of alter and drop for 
DatabaseRuleConfigurationManager (#34807)
caf6a6762ea is described below

commit caf6a6762ea65eb99611ea9b4a722a84f212af97
Author: Haoran Meng <menghaora...@gmail.com>
AuthorDate: Fri Feb 28 08:29:12 2025 +0800

    Use refresh instead of alter and drop for DatabaseRuleConfigurationManager 
(#34807)
    
    * Use refresh instead of alter and drop for DatabaseRuleConfigurationManager
    
    * Use refresh instead of alter and drop for DatabaseRuleConfigurationManager
    
    * Refactor DatabaseRuleConfigurationManager
---
 .../rule/DatabaseRuleConfigurationManager.java     | 32 +++-------------------
 .../manager/rule/DatabaseRuleItemManager.java      |  7 +++--
 .../StandaloneMetaDataManagerPersistService.java   |  2 +-
 ...tandaloneMetaDataManagerPersistServiceTest.java |  2 +-
 4 files changed, 11 insertions(+), 32 deletions(-)

diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleConfigurationManager.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleConfigurationManager.java
index 72a9f4928e4..55bf9cf0324 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleConfigurationManager.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleConfigurationManager.java
@@ -20,14 +20,11 @@ package 
org.apache.shardingsphere.mode.metadata.manager.rule;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import 
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
-import 
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker;
 import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.rule.PartialRuleUpdateSupported;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import 
org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.metadata.factory.MetaDataContextsFactory;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistFacade;
@@ -51,14 +48,15 @@ public final class DatabaseRuleConfigurationManager {
     private final MetaDataPersistFacade metaDataPersistFacade;
     
     /**
-     * Alter rule configuration.
+     * Refresh rule configuration.
      *
      * @param databaseName database name
      * @param ruleConfig rule configurations
+     * @param reBuildRules is need to rebuild rules
      * @throws SQLException SQL Exception
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
-    public synchronized void alter(final String databaseName, final 
RuleConfiguration ruleConfig) throws SQLException {
+    public synchronized void refresh(final String databaseName, final 
RuleConfiguration ruleConfig, final boolean reBuildRules) throws SQLException {
         ShardingSphereDatabase database = 
metaDataContexts.getMetaData().getDatabase(databaseName);
         Collection<ShardingSphereRule> rules = new 
LinkedList<>(database.getRuleMetaData().getRules());
         Optional<ShardingSphereRule> toBeChangedRule = 
rules.stream().filter(each -> 
each.getConfiguration().getClass().equals(ruleConfig.getClass())).findFirst();
@@ -68,29 +66,7 @@ public final class DatabaseRuleConfigurationManager {
         }
         Collection<ShardingSphereRule> toBeRemovedRules = 
rules.stream().filter(each -> 
each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass())).collect(Collectors.toList());
         rules.removeAll(toBeRemovedRules);
-        rules.add(DatabaseRulesBuilder.build(databaseName, 
database.getProtocolType(), database.getRuleMetaData().getRules(), ruleConfig, 
computeNodeInstanceContext, database.getResourceMetaData()));
-        refreshMetadata(databaseName, rules, toBeRemovedRules);
-    }
-    
-    /**
-     * Drop rule configuration.
-     *
-     * @param databaseName database name
-     * @param ruleConfig rule configurations
-     * @throws SQLException SQL Exception
-     */
-    @SuppressWarnings({"unchecked", "rawtypes"})
-    public synchronized void drop(final String databaseName, final 
RuleConfiguration ruleConfig) throws SQLException {
-        ShardingSphereDatabase database = 
metaDataContexts.getMetaData().getDatabase(databaseName);
-        Collection<ShardingSphereRule> rules = new 
LinkedList<>(database.getRuleMetaData().getRules());
-        Optional<ShardingSphereRule> toBeChangedRule = 
rules.stream().filter(each -> 
each.getConfiguration().getClass().equals(ruleConfig.getClass())).findFirst();
-        if (toBeChangedRule.isPresent() && toBeChangedRule.get() instanceof 
PartialRuleUpdateSupported && ((PartialRuleUpdateSupported) 
toBeChangedRule.get()).partialUpdate(ruleConfig)) {
-            ((PartialRuleUpdateSupported) 
toBeChangedRule.get()).updateConfiguration(ruleConfig);
-            return;
-        }
-        Collection<ShardingSphereRule> toBeRemovedRules = 
rules.stream().filter(each -> 
each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass())).collect(Collectors.toList());
-        rules.removeAll(toBeRemovedRules);
-        if 
(!TypedSPILoader.getService(DatabaseRuleConfigurationEmptyChecker.class, 
ruleConfig.getClass()).isEmpty((DatabaseRuleConfiguration) ruleConfig)) {
+        if (reBuildRules) {
             rules.add(DatabaseRulesBuilder.build(
                     databaseName, database.getProtocolType(), 
database.getRuleMetaData().getRules(), ruleConfig, computeNodeInstanceContext, 
database.getResourceMetaData()));
         }
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleItemManager.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleItemManager.java
index 624e47129d1..da4b12767a5 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleItemManager.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleItemManager.java
@@ -20,6 +20,8 @@ package org.apache.shardingsphere.mode.metadata.manager.rule;
 import com.google.common.base.Preconditions;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistFacade;
@@ -57,7 +59,7 @@ public final class DatabaseRuleItemManager {
         RuleConfiguration currentRuleConfig = 
processor.findRuleConfiguration(metaDataContexts.getMetaData().getDatabase(databaseName));
         synchronized (this) {
             processor.changeRuleItemConfiguration(alterRuleItem, 
currentRuleConfig, processor.swapRuleItemConfiguration(alterRuleItem, 
yamlContent));
-            databaseRuleConfigManager.alter(databaseName, currentRuleConfig);
+            databaseRuleConfigManager.refresh(databaseName, currentRuleConfig, 
true);
         }
     }
     
@@ -75,7 +77,8 @@ public final class DatabaseRuleItemManager {
         RuleConfiguration currentRuleConfig = 
processor.findRuleConfiguration(metaDataContexts.getMetaData().getDatabase(databaseName));
         synchronized (this) {
             processor.dropRuleItemConfiguration(dropRuleItem, 
currentRuleConfig);
-            databaseRuleConfigManager.drop(databaseName, currentRuleConfig);
+            databaseRuleConfigManager.refresh(databaseName, currentRuleConfig,
+                    
!TypedSPILoader.getService(DatabaseRuleConfigurationEmptyChecker.class, 
currentRuleConfig.getClass()).isEmpty((DatabaseRuleConfiguration) 
currentRuleConfig));
         }
     }
 }
diff --git 
a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java
 
b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java
index ee7add1beca..d11b91aca76 100644
--- 
a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java
+++ 
b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java
@@ -197,7 +197,7 @@ public final class StandaloneMetaDataManagerPersistService 
implements MetaDataMa
     public void alterSingleRuleConfiguration(final ShardingSphereDatabase 
database, final RuleMetaData ruleMetaData) throws SQLException {
         SingleRuleConfiguration singleRuleConfig = 
ruleMetaData.getSingleRule(SingleRule.class).getConfiguration();
         
metaDataPersistFacade.getDatabaseRuleService().persist(database.getName(), 
Collections.singleton(singleRuleConfig));
-        
metaDataContextManager.getDatabaseRuleConfigurationManager().alter(database.getName(),
 singleRuleConfig);
+        
metaDataContextManager.getDatabaseRuleConfigurationManager().refresh(database.getName(),
 singleRuleConfig, true);
         clearServiceCache();
     }
     
diff --git 
a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistServiceTest.java
 
b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistServiceTest.java
index 5b6654098be..ae678672a95 100644
--- 
a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistServiceTest.java
+++ 
b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistServiceTest.java
@@ -155,7 +155,7 @@ class StandaloneMetaDataManagerPersistServiceTest {
         when(singleRule.getConfiguration()).thenReturn(singleRuleConfig);
         metaDataManagerPersistService.alterSingleRuleConfiguration(
                 new ShardingSphereDatabase("foo_db", mock(), mock(), mock(), 
Collections.emptyList()), new RuleMetaData(Collections.singleton(singleRule)));
-        
verify(metaDataContextManager.getDatabaseRuleConfigurationManager()).alter("foo_db",
 singleRuleConfig);
+        
verify(metaDataContextManager.getDatabaseRuleConfigurationManager()).refresh("foo_db",
 singleRuleConfig, true);
     }
     
     @Test

Reply via email to