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

zhangliang 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 136ad3db996 Refactor database rule operators to use 
ShardingSphereDatabase for rule configuration decoration (#37163)
136ad3db996 is described below

commit 136ad3db996f599579f8d1c44563b22f2ebe96cf
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Nov 23 12:41:39 2025 +0800

    Refactor database rule operators to use ShardingSphereDatabase for rule 
configuration decoration (#37163)
    
    * Refactor database rule operators to use ShardingSphereDatabase for rule 
configuration decoration
    
    * Refactor database rule operators to use ShardingSphereDatabase for rule 
configuration decoration
    
    * Refactor database rule operators to use ShardingSphereDatabase for rule 
configuration decoration
---
 .../update/CreateBroadcastTableRuleExecutorTest.java |  6 +++---
 .../metadata/database/ShardingSphereDatabase.java    | 20 ++++++++++++++++++++
 .../database/type/AlterDatabaseRuleOperator.java     | 15 +--------------
 .../database/type/CreateDatabaseRuleOperator.java    | 16 +---------------
 ...SetDefaultSingleTableStorageUnitExecutorTest.java |  9 +++++----
 5 files changed, 30 insertions(+), 36 deletions(-)

diff --git 
a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutorTest.java
 
b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutorTest.java
index c2cdb801635..0a0c5735700 100644
--- 
a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutorTest.java
+++ 
b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutorTest.java
@@ -50,8 +50,7 @@ class CreateBroadcastTableRuleExecutorTest {
         CreateBroadcastTableRuleStatement sqlStatement = new 
CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address"));
         ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, 
RETURNS_DEEP_STUBS);
         
when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap());
-        BroadcastRule rule = mock(BroadcastRule.class);
-        assertThrows(EmptyStorageUnitException.class, () -> new 
DistSQLUpdateExecuteEngine(sqlStatement, "foo_db", mockContextManager(database, 
rule)).executeUpdate());
+        assertThrows(EmptyStorageUnitException.class, () -> new 
DistSQLUpdateExecuteEngine(sqlStatement, "foo_db", mockContextManager(database, 
mock(BroadcastRule.class))).executeUpdate());
     }
     
     @Test
@@ -101,9 +100,10 @@ class CreateBroadcastTableRuleExecutorTest {
     }
     
     private ContextManager mockContextManager(final ShardingSphereDatabase 
database, final BroadcastRule rule) {
-        ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
         when(database.getName()).thenReturn("foo_db");
         when(database.getRuleMetaData()).thenReturn(new RuleMetaData(null == 
rule ? Collections.emptyList() : Collections.singleton(rule)));
+        when(database.decorateRuleConfiguration(any())).thenReturn(new 
BroadcastRuleConfiguration(new HashSet<>(Arrays.asList("foo_tbl", "bar_tbl"))));
+        ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
         when(result.getDatabase("foo_db")).thenReturn(database);
         return result;
     }
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
index ed864dad6f8..05f0048cd62 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
@@ -21,6 +21,7 @@ import lombok.AccessLevel;
 import lombok.Getter;
 import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
 import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
 import 
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
 import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
@@ -30,6 +31,7 @@ import 
org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentif
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import 
org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute;
 import 
org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 
 import javax.sql.DataSource;
 import java.util.Collection;
@@ -37,6 +39,7 @@ import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
@@ -161,4 +164,21 @@ public final class ShardingSphereDatabase {
         notExistedDataSources.removeIf(logicDataSources::contains);
         ShardingSpherePreconditions.checkMustEmpty(notExistedDataSources, () 
-> new MissingRequiredStorageUnitsException(name, notExistedDataSources));
     }
+    
+    /**
+     * Decorate rule configuration.
+     *
+     * @param ruleConfig rule configuration
+     * @return decorated rule configuration
+     */
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public RuleConfiguration decorateRuleConfiguration(final RuleConfiguration 
ruleConfig) {
+        Optional<RuleConfigurationDecorator> decorator = 
TypedSPILoader.findService(RuleConfigurationDecorator.class, 
ruleConfig.getClass());
+        if (!decorator.isPresent()) {
+            return ruleConfig;
+        }
+        Map<String, DataSource> dataSources = 
resourceMetaData.getStorageUnits().entrySet().stream()
+                .collect(Collectors.toMap(Entry::getKey, entry -> 
entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, 
LinkedHashMap::new));
+        return decorator.get().decorate(name, dataSources, 
ruleMetaData.getRules(), ruleConfig);
+    }
 }
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/AlterDatabaseRuleOperator.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/AlterDatabaseRuleOperator.java
index 97ab4d09140..85c7b5489ec 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/AlterDatabaseRuleOperator.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/AlterDatabaseRuleOperator.java
@@ -23,17 +23,12 @@ import 
org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.data
 import 
org.apache.shardingsphere.distsql.statement.type.rdl.rule.database.DatabaseRuleDefinitionStatement;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.config.rule.checker.DatabaseRuleConfigurationEmptyChecker;
-import 
org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
 import 
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import 
org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
 
-import java.util.LinkedHashMap;
-import java.util.Map.Entry;
-import java.util.stream.Collectors;
-
 /**
  * Alter database rule operator.
  */
@@ -50,19 +45,11 @@ public final class AlterDatabaseRuleOperator implements 
DatabaseRuleOperator {
     public void operate(final DatabaseRuleDefinitionStatement sqlStatement, 
final ShardingSphereDatabase database, final RuleConfiguration 
currentRuleConfig) {
         RuleConfiguration toBeAlteredRuleConfig = 
executor.buildToBeAlteredRuleConfiguration(sqlStatement);
         MetaDataManagerPersistService metaDataManagerPersistService = 
contextManager.getPersistServiceFacade().getModeFacade().getMetaDataManagerService();
-        metaDataManagerPersistService.alterRuleConfiguration(database, 
decorateRuleConfiguration(database, toBeAlteredRuleConfig));
+        metaDataManagerPersistService.alterRuleConfiguration(database, 
database.decorateRuleConfiguration(toBeAlteredRuleConfig));
         RuleConfiguration toBeDroppedRuleConfig = 
executor.buildToBeDroppedRuleConfiguration(toBeAlteredRuleConfig);
         if (null != toBeDroppedRuleConfig
                 && 
!TypedSPILoader.getService(DatabaseRuleConfigurationEmptyChecker.class, 
toBeAlteredRuleConfig.getClass()).isEmpty((DatabaseRuleConfiguration) 
toBeDroppedRuleConfig)) {
             
metaDataManagerPersistService.removeRuleConfigurationItem(database, 
toBeDroppedRuleConfig);
         }
     }
-    
-    @SuppressWarnings("unchecked")
-    private RuleConfiguration decorateRuleConfiguration(final 
ShardingSphereDatabase database, final RuleConfiguration ruleConfig) {
-        return TypedSPILoader.findService(RuleConfigurationDecorator.class, 
ruleConfig.getClass()).map(optional -> optional.decorate(database.getName(),
-                
database.getResourceMetaData().getStorageUnits().entrySet().stream()
-                        .collect(Collectors.toMap(Entry::getKey, entry -> 
entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, 
LinkedHashMap::new)),
-                database.getRuleMetaData().getRules(), 
ruleConfig)).orElse(ruleConfig);
-    }
 }
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/CreateDatabaseRuleOperator.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/CreateDatabaseRuleOperator.java
index 23c77edd79b..d3f7f01d2bd 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/CreateDatabaseRuleOperator.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/CreateDatabaseRuleOperator.java
@@ -22,16 +22,10 @@ import 
org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.engine.d
 import 
org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.type.DatabaseRuleCreateExecutor;
 import 
org.apache.shardingsphere.distsql.statement.type.rdl.rule.database.DatabaseRuleDefinitionStatement;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import 
org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import 
org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
 
-import java.util.LinkedHashMap;
-import java.util.Map.Entry;
-import java.util.stream.Collectors;
-
 /**
  * Create database rule operator.
  */
@@ -48,14 +42,6 @@ public final class CreateDatabaseRuleOperator implements 
DatabaseRuleOperator {
     public void operate(final DatabaseRuleDefinitionStatement sqlStatement, 
final ShardingSphereDatabase database, final RuleConfiguration 
currentRuleConfig) {
         RuleConfiguration toBeCreatedRuleConfig = 
executor.buildToBeCreatedRuleConfiguration(sqlStatement);
         MetaDataManagerPersistService metaDataManagerPersistService = 
contextManager.getPersistServiceFacade().getModeFacade().getMetaDataManagerService();
-        metaDataManagerPersistService.alterRuleConfiguration(database, 
decorateRuleConfiguration(database, toBeCreatedRuleConfig));
-    }
-    
-    @SuppressWarnings("unchecked")
-    private RuleConfiguration decorateRuleConfiguration(final 
ShardingSphereDatabase database, final RuleConfiguration ruleConfig) {
-        return TypedSPILoader.findService(RuleConfigurationDecorator.class, 
ruleConfig.getClass()).map(optional -> optional.decorate(database.getName(),
-                
database.getResourceMetaData().getStorageUnits().entrySet().stream()
-                        .collect(Collectors.toMap(Entry::getKey, entry -> 
entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, 
LinkedHashMap::new)),
-                database.getRuleMetaData().getRules(), 
ruleConfig)).orElse(ruleConfig);
+        metaDataManagerPersistService.alterRuleConfiguration(database, 
database.decorateRuleConfiguration(toBeCreatedRuleConfig));
     }
 }
diff --git 
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java
 
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java
index 3bddcf8af94..9b4dd5fbecb 100644
--- 
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java
+++ 
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java
@@ -52,7 +52,7 @@ class SetDefaultSingleTableStorageUnitExecutorTest {
         
when(database.getRuleMetaData().getAttributes(DataSourceMapperRuleAttribute.class)).thenReturn(Collections.emptyList());
         SingleRule rule = mock(SingleRule.class, RETURNS_DEEP_STUBS);
         
when(rule.getAttributes().findAttribute(DataSourceMapperRuleAttribute.class)).thenReturn(Optional.empty());
-        DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(new 
SetDefaultSingleTableStorageUnitStatement("foo_ds"), "foo_db", 
mockContextManager(database, rule));
+        DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(new 
SetDefaultSingleTableStorageUnitStatement("foo_ds"), "foo_db", 
mockContextManager(database, rule, "foo_ds"));
         assertThrows(MissingRequiredStorageUnitsException.class, 
engine::executeUpdate);
     }
     
@@ -61,7 +61,7 @@ class SetDefaultSingleTableStorageUnitExecutorTest {
         ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, 
RETURNS_DEEP_STUBS);
         SingleRule rule = mock(SingleRule.class);
         when(rule.getConfiguration()).thenReturn(new 
SingleRuleConfiguration(Collections.emptyList(), "foo_ds"));
-        ContextManager contextManager = mockContextManager(database, rule);
+        ContextManager contextManager = mockContextManager(database, rule, 
null);
         new DistSQLUpdateExecuteEngine(new 
SetDefaultSingleTableStorageUnitStatement(null), "foo_db", 
contextManager).executeUpdate();
         MetaDataManagerPersistService metaDataManagerPersistService = 
contextManager.getPersistServiceFacade().getModeFacade().getMetaDataManagerService();
         
verify(metaDataManagerPersistService).removeRuleConfigurationItem(any(), 
ArgumentMatchers.<SingleRuleConfiguration>argThat(x -> 
x.getDefaultDataSource().equals(Optional.of("foo_ds"))));
@@ -77,16 +77,17 @@ class SetDefaultSingleTableStorageUnitExecutorTest {
         SingleRule rule = mock(SingleRule.class, RETURNS_DEEP_STUBS);
         when(rule.getConfiguration()).thenReturn(new 
SingleRuleConfiguration(Collections.emptyList(), "foo_ds"));
         
when(rule.getAttributes().findAttribute(DataSourceMapperRuleAttribute.class)).thenReturn(Optional.empty());
-        ContextManager contextManager = mockContextManager(database, rule);
+        ContextManager contextManager = mockContextManager(database, rule, 
"bar_ds");
         new DistSQLUpdateExecuteEngine(new 
SetDefaultSingleTableStorageUnitStatement("bar_ds"), "foo_db", 
contextManager).executeUpdate();
         MetaDataManagerPersistService metaDataManagerPersistService = 
contextManager.getPersistServiceFacade().getModeFacade().getMetaDataManagerService();
         verify(metaDataManagerPersistService).alterRuleConfiguration(any(), 
ArgumentMatchers.<SingleRuleConfiguration>argThat(x -> 
x.getDefaultDataSource().equals(Optional.of("bar_ds"))));
     }
     
-    private ContextManager mockContextManager(final ShardingSphereDatabase 
database, final SingleRule rule) {
+    private ContextManager mockContextManager(final ShardingSphereDatabase 
database, final SingleRule rule, final String defaultDataSource) {
         ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
         when(database.getName()).thenReturn("foo_db");
         when(database.getRuleMetaData()).thenReturn(new 
RuleMetaData(Collections.singleton(rule)));
+        when(database.decorateRuleConfiguration(any())).thenReturn(new 
SingleRuleConfiguration(Collections.emptyList(), defaultDataSource));
         when(result.getDatabase("foo_db")).thenReturn(database);
         return result;
     }

Reply via email to