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 d69e02e9a4d Refactor GlobalRuleChangedHandler (#34912) d69e02e9a4d is described below commit d69e02e9a4d61a77a6f0d10781c7ccaa82c1fa3b Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Fri Mar 7 13:58:50 2025 +0800 Refactor GlobalRuleChangedHandler (#34912) * Refactor GlobalRuleChangedHandler * Refactor GlobalRuleChangedHandler * Refactor GlobalRuleChangedHandler --- .../mode/node/path/type/global/config/GlobalRuleNodePath.java | 11 +++++++++++ .../node/path/type/global/config/GlobalRuleNodePathTest.java | 9 +++++++++ .../handler/global/config/GlobalRuleChangedHandler.java | 8 ++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/global/config/GlobalRuleNodePath.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/global/config/GlobalRuleNodePath.java index ccfc0708e7d..d9850b2e230 100644 --- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/global/config/GlobalRuleNodePath.java +++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/global/config/GlobalRuleNodePath.java @@ -21,6 +21,8 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.mode.node.path.NodePath; import org.apache.shardingsphere.mode.node.path.NodePathEntity; +import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern; +import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria; /** * Global rule node path. @@ -31,4 +33,13 @@ import org.apache.shardingsphere.mode.node.path.NodePathEntity; public final class GlobalRuleNodePath implements NodePath { private final String ruleType; + + /** + * Create rule type search criteria. + * + * @return created search criteria + */ + public static NodePathSearchCriteria createRuleTypeSearchCriteria() { + return new NodePathSearchCriteria(new GlobalRuleNodePath(NodePathPattern.IDENTIFIER), false, true, 1); + } } diff --git a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/type/global/config/GlobalRuleNodePathTest.java b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/type/global/config/GlobalRuleNodePathTest.java index 8767d9b4e0a..b0cd7048347 100644 --- a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/type/global/config/GlobalRuleNodePathTest.java +++ b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/type/global/config/GlobalRuleNodePathTest.java @@ -18,11 +18,13 @@ package org.apache.shardingsphere.mode.node.path.type.global.config; import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator; +import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearcher; import org.apache.shardingsphere.mode.node.path.version.VersionNodePathParser; import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; class GlobalRuleNodePathTest { @@ -38,4 +40,11 @@ class GlobalRuleNodePathTest { VersionNodePathParser versionNodePathParser = new VersionNodePathParser(new GlobalRuleNodePath("foo_rule")); assertTrue(versionNodePathParser.isActiveVersionPath("/rules/foo_rule/active_version")); } + + @Test + void assertCreateRuleTypeSearchCriteria() { + assertThat(NodePathSearcher.get("/rules/foo_rule", GlobalRuleNodePath.createRuleTypeSearchCriteria()), is("foo_rule")); + assertThat(NodePathSearcher.get("/rules/foo_rule/active_version", GlobalRuleNodePath.createRuleTypeSearchCriteria()), is("foo_rule")); + assertFalse(NodePathSearcher.find("/xxx/foo_rule/active_version", GlobalRuleNodePath.createRuleTypeSearchCriteria()).isPresent()); + } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/config/GlobalRuleChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/config/GlobalRuleChangedHandler.java index bf1b4ed6155..59d3d1b0d60 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/config/GlobalRuleChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/config/GlobalRuleChangedHandler.java @@ -25,12 +25,12 @@ import org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global.Gl import org.apache.shardingsphere.mode.metadata.manager.ActiveVersionChecker; import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator; import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern; +import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearcher; import org.apache.shardingsphere.mode.node.path.type.global.config.GlobalRuleNodePath; import org.apache.shardingsphere.mode.node.path.version.VersionNodePathParser; import java.util.Arrays; import java.util.Collection; -import java.util.Optional; /** * Global rule changed handler. @@ -49,14 +49,14 @@ public final class GlobalRuleChangedHandler implements GlobalDataChangedEventHan @Override public void handle(final ContextManager contextManager, final DataChangedEvent event) { - Optional<String> ruleType = new VersionNodePathParser(new GlobalRuleNodePath(NodePathPattern.IDENTIFIER)).findIdentifierByActiveVersionPath(event.getKey(), 1); - if (!ruleType.isPresent()) { + if (!new VersionNodePathParser(new GlobalRuleNodePath(NodePathPattern.IDENTIFIER)).isActiveVersionPath(event.getKey())) { return; } if (!new ActiveVersionChecker(contextManager.getPersistServiceFacade().getRepository()).checkSame(event)) { return; } - RuleConfiguration ruleConfig = contextManager.getPersistServiceFacade().getMetaDataPersistFacade().getGlobalRuleService().load(ruleType.get()); + String ruleType = NodePathSearcher.get(event.getKey(), GlobalRuleNodePath.createRuleTypeSearchCriteria()); + RuleConfiguration ruleConfig = contextManager.getPersistServiceFacade().getMetaDataPersistFacade().getGlobalRuleService().load(ruleType); contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterGlobalRuleConfiguration(ruleConfig); } }