You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by to...@apache.org on 2024/08/02 07:55:50 UTC

(shardingsphere) branch master updated: Add RuleConfigurationPersistDecorateEngine (#32370)

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

totalo 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 84eec86c915 Add RuleConfigurationPersistDecorateEngine (#32370)
84eec86c915 is described below

commit 84eec86c91549e9bfe52c700ec1aaba5be028fcf
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Aug 2 15:55:44 2024 +0800

    Add RuleConfigurationPersistDecorateEngine (#32370)
---
 .../mode/metadata/MetaDataContextManager.java      | 21 ++----
 .../mode/metadata/MetaDataContextsFactory.java     | 33 ++-------
 .../RuleConfigurationPersistDecorateEngine.java    | 78 ++++++++++++++++++++++
 .../ral/queryable/ExportMetaDataExecutor.java      | 20 ++----
 .../ral/updatable/SetDistVariableExecutor.java     | 15 ++---
 5 files changed, 99 insertions(+), 68 deletions(-)

diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java
index 2277e574e6d..5c0c1848653 100644
--- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java
+++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java
@@ -28,8 +28,8 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
 import org.apache.shardingsphere.infra.metadata.database.schema.manager.GenericSchemaManager;
 import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.mode.metadata.decorator.RuleConfigurationPersistDecorateEngine;
 import org.apache.shardingsphere.mode.metadata.manager.DatabaseRuleConfigurationManager;
 import org.apache.shardingsphere.mode.metadata.manager.GlobalConfigurationManager;
 import org.apache.shardingsphere.mode.metadata.manager.ResourceSwitchManager;
@@ -39,13 +39,10 @@ import org.apache.shardingsphere.mode.metadata.manager.ShardingSphereDatabaseDat
 import org.apache.shardingsphere.mode.metadata.manager.StorageUnitManager;
 import org.apache.shardingsphere.mode.metadata.manager.SwitchingResource;
 import org.apache.shardingsphere.mode.spi.PersistRepository;
-import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator;
 
 import java.sql.SQLException;
 import java.util.Collection;
-import java.util.LinkedList;
 import java.util.Map;
-import java.util.Optional;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -75,6 +72,8 @@ public class MetaDataContextManager {
     
     private final GlobalConfigurationManager globalConfigurationManager;
     
+    private final RuleConfigurationPersistDecorateEngine ruleConfigPersistDecorateEngine;
+    
     public MetaDataContextManager(final AtomicReference<MetaDataContexts> metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext,
                                   final PersistRepository repository) {
         this.metaDataContexts = metaDataContexts;
@@ -87,6 +86,7 @@ public class MetaDataContextManager {
         ruleItemManager = new RuleItemManager(metaDataContexts, repository, databaseRuleConfigurationManager);
         globalConfigurationManager = new GlobalConfigurationManager(metaDataContexts, repository);
         metaDataPersistService = new MetaDataPersistService(repository);
+        ruleConfigPersistDecorateEngine = new RuleConfigurationPersistDecorateEngine(computeNodeInstanceContext);
     }
     
     /**
@@ -155,20 +155,11 @@ public class MetaDataContextManager {
         Map<String, ShardingSphereDatabase> changedDatabases = MetaDataContextsFactory
                 .createChangedDatabases(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext);
         ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
-        RuleMetaData changedGlobalMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(getGlobalRuleConfigurations(), changedDatabases, props));
+        RuleMetaData changedGlobalMetaData = new RuleMetaData(
+                GlobalRulesBuilder.buildRules(ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), changedDatabases, props));
         MetaDataContexts result = MetaDataContextsFactory.create(metaDataPersistService,
                 new ShardingSphereMetaData(changedDatabases, metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props));
         switchingResource.closeStaleDataSources();
         return result;
     }
-    
-    @SuppressWarnings({"rawtypes", "unchecked"})
-    private Collection<RuleConfiguration> getGlobalRuleConfigurations() {
-        Collection<RuleConfiguration> result = new LinkedList<>();
-        for (RuleConfiguration each : metaDataPersistService.getGlobalRuleService().load()) {
-            Optional<RuleConfigurationPersistDecorator> rulePersistDecorator = TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, each);
-            result.add(rulePersistDecorator.isPresent() && computeNodeInstanceContext.isCluster() ? rulePersistDecorator.get().restore(each) : each);
-        }
-        return result;
-    }
 }
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 c3dec5325ac..215599b6708 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
@@ -46,13 +46,13 @@ import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableDa
 import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
-import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.metadata.factory.ExternalMetaDataFactory;
 import org.apache.shardingsphere.metadata.factory.InternalMetaDataFactory;
 import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
+import org.apache.shardingsphere.mode.metadata.decorator.RuleConfigurationPersistDecorateEngine;
 import org.apache.shardingsphere.mode.metadata.manager.SwitchingResource;
 import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator;
 
@@ -61,7 +61,6 @@ 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;
@@ -94,7 +93,7 @@ public final class MetaDataContextsFactory {
         if (isDatabaseMetaDataExisted) {
             globalRuleConfigs = persistService.getGlobalRuleService().load();
         } else if (computeNodeInstanceContext.isCluster()) {
-            globalRuleConfigs = getGlobalRuleConfigurations(param.getGlobalRuleConfigs());
+            globalRuleConfigs = new RuleConfigurationPersistDecorateEngine(computeNodeInstanceContext).tryRestore(param.getGlobalRuleConfigs());
             param.getGlobalRuleConfigs().clear();
             param.getGlobalRuleConfigs().addAll(globalRuleConfigs);
         } else {
@@ -153,16 +152,6 @@ public final class MetaDataContextsFactory {
         }
     }
     
-    @SuppressWarnings({"unchecked", "rawtypes"})
-    private static Collection<RuleConfiguration> getGlobalRuleConfigurations(final Collection<RuleConfiguration> globalRuleConfigs) {
-        Collection<RuleConfiguration> result = new LinkedList<>();
-        for (RuleConfiguration each : globalRuleConfigs) {
-            Optional<RuleConfigurationPersistDecorator> rulePersistDecorator = TypedSPILoader.findService(RuleConfigurationPersistDecorator.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();
@@ -212,10 +201,7 @@ public final class MetaDataContextsFactory {
         }
         for (RuleConfigurationPersistDecorator each : ShardingSphereServiceLoader.getServiceInstances(RuleConfigurationPersistDecorator.class)) {
             ShardingSphereRule rule = metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(each.getRuleType());
-            if (!(rule instanceof GlobalRule)) {
-                continue;
-            }
-            metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().removeIf(eachRule -> each.getRuleType().isAssignableFrom(rule.getClass()));
+            metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().removeIf(eachRule -> each.getRuleType() == eachRule.getClass());
             RuleConfiguration restoredRuleConfig = each.restore(rule.getConfiguration());
             ShardingSphereRule rebuiltRule = GlobalRulesBuilder.buildRules(
                     Collections.singleton(restoredRuleConfig), metaDataContexts.getMetaData().getDatabases(), metaDataContexts.getMetaData().getProps()).iterator().next();
@@ -225,7 +211,8 @@ public final class MetaDataContextsFactory {
     
     private static void persistDatabaseConfigurations(final MetaDataContexts metadataContexts, final ContextManagerBuilderParameter param, final MetaDataPersistService persistService,
                                                       final ComputeNodeInstanceContext computeNodeInstanceContext) {
-        persistService.persistGlobalRuleConfiguration(decorateGlobalRuleConfigurations(param.getGlobalRuleConfigs(), computeNodeInstanceContext), param.getProps());
+        RuleConfigurationPersistDecorateEngine ruleConfigPersistDecorateEngine = new RuleConfigurationPersistDecorateEngine(computeNodeInstanceContext);
+        persistService.persistGlobalRuleConfiguration(ruleConfigPersistDecorateEngine.decorate(param.getGlobalRuleConfigs()), param.getProps());
         for (Entry<String, ? extends DatabaseConfiguration> entry : param.getDatabaseConfigs().entrySet()) {
             String databaseName = entry.getKey();
             persistService.persistConfigurations(entry.getKey(), entry.getValue(),
@@ -235,16 +222,6 @@ public final class MetaDataContextsFactory {
         }
     }
     
-    @SuppressWarnings({"rawtypes", "unchecked"})
-    private static Collection<RuleConfiguration> decorateGlobalRuleConfigurations(final Collection<RuleConfiguration> globalRuleConfigs, final ComputeNodeInstanceContext computeNodeInstanceContext) {
-        Collection<RuleConfiguration> result = new LinkedList<>();
-        for (RuleConfiguration each : globalRuleConfigs) {
-            Optional<RuleConfigurationPersistDecorator> rulePersistDecorator = TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, each.getClass());
-            result.add(rulePersistDecorator.isPresent() && computeNodeInstanceContext.isCluster() ? rulePersistDecorator.get().decorate(each) : each);
-        }
-        return result;
-    }
-    
     private static void persistMetaData(final MetaDataContexts metaDataContexts, final MetaDataPersistService persistService) {
         metaDataContexts.getMetaData().getDatabases().values().forEach(each -> each.getSchemas().forEach((schemaName, schema) -> {
             if (schema.isEmpty()) {
diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/decorator/RuleConfigurationPersistDecorateEngine.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/decorator/RuleConfigurationPersistDecorateEngine.java
new file mode 100644
index 00000000000..bb7dea44a60
--- /dev/null
+++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/decorator/RuleConfigurationPersistDecorateEngine.java
@@ -0,0 +1,78 @@
+/*
+ * 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.metadata.decorator;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator;
+
+import java.util.Collection;
+import java.util.stream.Collectors;
+
+/**
+ * Rule configuration persist decorate engine.
+ */
+@RequiredArgsConstructor
+public final class RuleConfigurationPersistDecorateEngine {
+    
+    private final ComputeNodeInstanceContext computeNodeInstanceContext;
+    
+    /**
+     * Decorate rule configurations.
+     *
+     * @param ruleConfigs to be decorated rule configurations
+     * @return decorated rule configurations
+     */
+    @SuppressWarnings("unchecked")
+    public Collection<RuleConfiguration> decorate(final Collection<RuleConfiguration> ruleConfigs) {
+        if (!computeNodeInstanceContext.isCluster()) {
+            return ruleConfigs;
+        }
+        return ruleConfigs.stream()
+                .map(each -> TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, each).map(optional -> optional.decorate(each)).orElse(each)).collect(Collectors.toList());
+    }
+    
+    /**
+     * Restore rule configurations.
+     *
+     * @param ruleConfigs to be restored rule configurations
+     * @return restored rule configurations
+     */
+    @SuppressWarnings("unchecked")
+    public Collection<RuleConfiguration> restore(final Collection<RuleConfiguration> ruleConfigs) {
+        if (!computeNodeInstanceContext.isCluster()) {
+            return ruleConfigs;
+        }
+        return ruleConfigs.stream()
+                .map(each -> TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, each).map(optional -> optional.restore(each)).orElse(each)).collect(Collectors.toList());
+    }
+    
+    /**
+     * Try to restore rule configurations.
+     *
+     * @param ruleConfigs to be restored rule configurations
+     * @return restored rule configurations
+     */
+    @SuppressWarnings("unchecked")
+    public Collection<RuleConfiguration> tryRestore(final Collection<RuleConfiguration> ruleConfigs) {
+        return ruleConfigs.stream().map(each -> TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, each)
+                .filter(optional -> optional.canBeRestored(each)).map(optional -> optional.restore(each)).orElse(each)).collect(Collectors.toList());
+    }
+}
diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutor.java
index 4b33cdd352e..326d65e10fd 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutor.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutor.java
@@ -32,7 +32,7 @@ import org.apache.shardingsphere.infra.util.json.JsonUtils;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
 import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator;
+import org.apache.shardingsphere.mode.metadata.decorator.RuleConfigurationPersistDecorateEngine;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.distsql.export.ExportedClusterInfo;
 import org.apache.shardingsphere.proxy.backend.distsql.export.ExportedMetaData;
@@ -44,10 +44,8 @@ import java.util.Arrays;
 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;
 import java.util.Properties;
 
 /**
@@ -78,7 +76,10 @@ public final class ExportMetaDataExecutor implements DistSQLQueryExecutor<Export
         ExportedMetaData exportedMetaData = new ExportedMetaData();
         exportedMetaData.setDatabases(getDatabases(proxyContext));
         exportedMetaData.setProps(generatePropsData(metaData.getProps().getProps()));
-        exportedMetaData.setRules(generateRulesData(decorateGlobalRuleConfigurations(metaData.getGlobalRuleMetaData().getConfigurations())));
+        RuleConfigurationPersistDecorateEngine ruleConfigPersistDecorateEngine =
+                new RuleConfigurationPersistDecorateEngine(ProxyContext.getInstance().getContextManager().getComputeNodeInstanceContext());
+        Collection<RuleConfiguration> ruleConfigs = ruleConfigPersistDecorateEngine.decorate(metaData.getGlobalRuleMetaData().getConfigurations());
+        exportedMetaData.setRules(generateRulesData(ruleConfigs));
         ExportedClusterInfo exportedClusterInfo = new ExportedClusterInfo();
         exportedClusterInfo.setMetaData(exportedMetaData);
         generateSnapshotInfo(metaData, exportedClusterInfo);
@@ -125,17 +126,6 @@ public final class ExportMetaDataExecutor implements DistSQLQueryExecutor<Export
         return result.toString();
     }
     
-    @SuppressWarnings({"rawtypes", "unchecked"})
-    private Collection<RuleConfiguration> decorateGlobalRuleConfigurations(final Collection<RuleConfiguration> ruleConfigs) {
-        Collection<RuleConfiguration> result = new LinkedList<>();
-        for (RuleConfiguration each : ruleConfigs) {
-            Optional<RuleConfigurationPersistDecorator> rulePersistDecorator = TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, each);
-            result.add(
-                    rulePersistDecorator.isPresent() && ProxyContext.getInstance().getContextManager().getComputeNodeInstanceContext().isCluster() ? rulePersistDecorator.get().decorate(each) : each);
-        }
-        return result;
-    }
-    
     private void generateSnapshotInfo(final ShardingSphereMetaData metaData, final ExportedClusterInfo exportedClusterInfo) {
         GlobalClockRule globalClockRule = metaData.getGlobalRuleMetaData().getSingleRule(GlobalClockRule.class);
         if (globalClockRule.getConfiguration().isEnabled()) {
diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutor.java
index 52a8f6e9590..6d53c4da8f9 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutor.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableExecutor.java
@@ -32,18 +32,16 @@ import org.apache.shardingsphere.infra.props.TypedPropertyKey;
 import org.apache.shardingsphere.infra.props.TypedPropertyValue;
 import org.apache.shardingsphere.infra.props.exception.TypedPropertyValueException;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.logging.constant.LoggingConstants;
 import org.apache.shardingsphere.logging.util.LoggingUtils;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator;
+import org.apache.shardingsphere.mode.metadata.decorator.RuleConfigurationPersistDecorateEngine;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.slf4j.LoggerFactory;
 
 import java.sql.SQLException;
 import java.util.Collection;
-import java.util.LinkedList;
-import java.util.Optional;
 import java.util.Properties;
 
 /**
@@ -121,13 +119,10 @@ public final class SetDistVariableExecutor implements DistSQLUpdateExecutor<SetD
         }
     }
     
-    @SuppressWarnings({"rawtypes", "unchecked"})
     private void decorateGlobalRuleConfiguration(final ContextManager contextManager) {
-        Collection<RuleConfiguration> globalRuleConfigs = new LinkedList<>();
-        for (RuleConfiguration each : contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations()) {
-            Optional<RuleConfigurationPersistDecorator> rulePersistDecorator = TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, each);
-            globalRuleConfigs.add(rulePersistDecorator.isPresent() && contextManager.getComputeNodeInstanceContext().isCluster() ? rulePersistDecorator.get().decorate(each) : each);
-        }
+        RuleConfigurationPersistDecorateEngine ruleConfigPersistDecorateEngine = new RuleConfigurationPersistDecorateEngine(
+                ProxyContext.getInstance().getContextManager().getComputeNodeInstanceContext());
+        Collection<RuleConfiguration> globalRuleConfigs = ruleConfigPersistDecorateEngine.decorate(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations());
         contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().persist(globalRuleConfigs);
     }