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 386d6817581 Add RulePersistDecorator.canBeRestored() (#32361)
386d6817581 is described below

commit 386d6817581990376c269d32bffa24cd92f58f70
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Aug 1 20:52:49 2024 +0800

    Add RulePersistDecorator.canBeRestored() (#32361)
    
    * Add RulePersistDecorator.canBeRestored()
    
    * Add RulePersistDecorator.canBeRestored()
---
 .../mode/spi/RulePersistDecorator.java              |  8 ++++++++
 .../mode/metadata/MetaDataContextsFactory.java      | 21 ++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git 
a/mode/api/src/main/java/org/apache/shardingsphere/mode/spi/RulePersistDecorator.java
 
b/mode/api/src/main/java/org/apache/shardingsphere/mode/spi/RulePersistDecorator.java
index f77ef058135..edcf200a6ae 100644
--- 
a/mode/api/src/main/java/org/apache/shardingsphere/mode/spi/RulePersistDecorator.java
+++ 
b/mode/api/src/main/java/org/apache/shardingsphere/mode/spi/RulePersistDecorator.java
@@ -28,6 +28,14 @@ import 
org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
 @SingletonSPI
 public interface RulePersistDecorator extends TypedSPI {
     
+    /**
+     * Check whether the rule configuration can be restored.
+     *
+     * @param ruleConfig rule configuration to be checked
+     * @return can be restored or not
+     */
+    boolean canBeRestored(RuleConfiguration ruleConfig);
+    
     /**
      * Restore rule configuration.
      *
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
index ce176f6ad51..62f66c290d6 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
@@ -61,6 +61,7 @@ import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
@@ -89,7 +90,16 @@ public final class MetaDataContextsFactory {
                 : param.getDatabaseConfigs();
         // TODO load global data sources from persist service
         Map<String, DataSource> globalDataSources = 
param.getGlobalDataSources();
-        Collection<RuleConfiguration> globalRuleConfigs = 
isDatabaseMetaDataExisted ? persistService.getGlobalRuleService().load() : 
param.getGlobalRuleConfigs();
+        Collection<RuleConfiguration> globalRuleConfigs;
+        if (isDatabaseMetaDataExisted) {
+            globalRuleConfigs = persistService.getGlobalRuleService().load();
+        } else if (computeNodeInstanceContext.isCluster()) {
+            globalRuleConfigs = 
getGlobalRuleConfigurations(param.getGlobalRuleConfigs());
+            param.getGlobalRuleConfigs().clear();
+            param.getGlobalRuleConfigs().addAll(globalRuleConfigs);
+        } else {
+            globalRuleConfigs = param.getGlobalRuleConfigs();
+        }
         ConfigurationProperties props = isDatabaseMetaDataExisted ? new 
ConfigurationProperties(persistService.getPropsService().load()) : new 
ConfigurationProperties(param.getProps());
         Map<String, ShardingSphereDatabase> databases = 
isDatabaseMetaDataExisted
                 ? InternalMetaDataFactory.create(persistService, 
effectiveDatabaseConfigs, props, computeNodeInstanceContext)
@@ -143,6 +153,15 @@ public final class MetaDataContextsFactory {
         }
     }
     
+    private static Collection<RuleConfiguration> 
getGlobalRuleConfigurations(final Collection<RuleConfiguration> 
globalRuleConfigs) {
+        Collection<RuleConfiguration> result = new LinkedList<>();
+        for (RuleConfiguration each : globalRuleConfigs) {
+            Optional<RulePersistDecorator> rulePersistDecorator = 
TypedSPILoader.findService(RulePersistDecorator.class, each.getClass());
+            result.add(rulePersistDecorator.isPresent() && 
rulePersistDecorator.get().canBeRestored(each) ? 
rulePersistDecorator.get().restore(each) : each);
+        }
+        return result;
+    }
+    
     private static ShardingSphereStatistics initStatistics(final 
MetaDataPersistService persistService, final ShardingSphereMetaData metaData) {
         if (metaData.getDatabases().isEmpty()) {
             return new ShardingSphereStatistics();

Reply via email to