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

yx9o 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 4d2346037e4 Add RulePersistDecorator (#32351)
4d2346037e4 is described below

commit 4d2346037e44535084ddee31415aa13174b3198b
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Aug 1 13:33:20 2024 +0800

    Add RulePersistDecorator (#32351)
    
    * Add RulePersistDecorator
    
    * Add RulePersistDecorator
---
 .../mode/spi/RulePersistDecorator.java             | 40 ++++++++++++++++++++++
 .../GlobalRuleConfigurationEventSubscriber.java    | 14 ++++++--
 2 files changed, 51 insertions(+), 3 deletions(-)

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
new file mode 100644
index 00000000000..e9a248c7a22
--- /dev/null
+++ 
b/mode/api/src/main/java/org/apache/shardingsphere/mode/spi/RulePersistDecorator.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mode.spi;
+
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
+
+/**
+ * Rule persist decorator.
+ */
+@SingletonSPI
+public interface RulePersistDecorator extends TypedSPI {
+    
+    /**
+     * Restore rule configuration.
+     *
+     * @param ruleConfig rule configuration to be restored
+     * @return restored rule configuration
+     */
+    RuleConfiguration restore(RuleConfiguration ruleConfig);
+    
+    @Override
+    Class<? extends RuleConfiguration> getType();
+}
diff --git 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
index 5f657f4c6e7..0c29749677a 100644
--- 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
+++ 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
@@ -19,9 +19,14 @@ package 
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.dispatch
 
 import com.google.common.eventbus.Subscribe;
 import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber;
 import 
org.apache.shardingsphere.mode.event.dispatch.config.AlterGlobalRuleConfigurationEvent;
 import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.mode.spi.RulePersistDecorator;
+
+import java.util.Optional;
 
 /**
  * Global rule configuration event subscriber.
@@ -42,8 +47,11 @@ public final class GlobalRuleConfigurationEventSubscriber 
implements EventSubscr
                 
contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())))
 {
             return;
         }
-        
contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(event.getRuleSimpleName())
-                .ifPresent(optional -> 
contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterGlobalRuleConfiguration(optional));
-        
+        Optional<RuleConfiguration> ruleConfig = 
contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(event.getRuleSimpleName());
+        if (!ruleConfig.isPresent()) {
+            return;
+        }
+        
contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterGlobalRuleConfiguration(
+                TypedSPILoader.findService(RulePersistDecorator.class, 
ruleConfig.getClass()).map(optional -> 
optional.restore(ruleConfig.get())).orElse(ruleConfig.get()));
     }
 }

Reply via email to