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 f83b30f68e3 Refactor OnlineNodePath (#34758)
f83b30f68e3 is described below
commit f83b30f68e3edcc73c16d337a18b6a8beaf05b9b
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Feb 23 16:59:56 2025 +0800
Refactor OnlineNodePath (#34758)
* Refactor OnlineNodePath
* Refactor OnlineNodePath
---
.../path/node/compute/status/OnlineNodePath.java | 24 ++++++++++++++++++++++
.../node/compute/status/OnlineNodePathTest.java | 19 +++++++++++++++++
.../global/type/ComputeNodeOnlineHandler.java | 14 +++++--------
3 files changed, 48 insertions(+), 9 deletions(-)
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/compute/status/OnlineNodePath.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/compute/status/OnlineNodePath.java
index 49d0f29a14b..6b6e3dbe97e 100644
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/compute/status/OnlineNodePath.java
+++
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/compute/status/OnlineNodePath.java
@@ -21,6 +21,7 @@ import lombok.Getter;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
+import org.apache.shardingsphere.mode.node.path.NodePathSearchCriteria;
/**
* Online node path.
@@ -37,4 +38,27 @@ public final class OnlineNodePath implements NodePath {
this.instanceType = null == instanceType ? null :
instanceType.name().toLowerCase();
this.instanceId = instanceId;
}
+
+ public OnlineNodePath() {
+ instanceType = "(jdbc|proxy)";
+ instanceId = "([\\S]+)";
+ }
+
+ /**
+ * Create instance type search criteria.
+ *
+ * @return created search criteria
+ */
+ public static NodePathSearchCriteria createInstanceTypeSearchCriteria() {
+ return new NodePathSearchCriteria(new OnlineNodePath(), false, false,
1);
+ }
+
+ /**
+ * Create instance ID search criteria.
+ *
+ * @return created search criteria
+ */
+ public static NodePathSearchCriteria createInstanceIdSearchCriteria() {
+ return new NodePathSearchCriteria(new OnlineNodePath(), false, false,
2);
+ }
}
diff --git
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/compute/status/OnlineNodePathTest.java
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/compute/status/OnlineNodePathTest.java
index 102e0347642..fa2941496b8 100644
---
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/compute/status/OnlineNodePathTest.java
+++
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/compute/status/OnlineNodePathTest.java
@@ -19,10 +19,15 @@ package
org.apache.shardingsphere.mode.node.path.node.compute.status;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.NodePathSearcher;
import org.junit.jupiter.api.Test;
+import java.util.Optional;
+
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 OnlineNodePathTest {
@@ -32,4 +37,18 @@ class OnlineNodePathTest {
assertThat(NodePathGenerator.toPath(new
OnlineNodePath(InstanceType.JDBC, null), false),
is("/nodes/compute_nodes/online/jdbc"));
assertThat(NodePathGenerator.toPath(new
OnlineNodePath(InstanceType.PROXY, "foo_instance_id"), false),
is("/nodes/compute_nodes/online/proxy/foo_instance_id"));
}
+
+ @Test
+ void assertCreateInstanceTypeSearchCriteria() {
+
assertThat(NodePathSearcher.find("/nodes/compute_nodes/online/jdbc/foo_instance_id",
OnlineNodePath.createInstanceTypeSearchCriteria()), is(Optional.of("jdbc")));
+ assertFalse(NodePathSearcher.find("/nodes/compute_nodes/online/jdbc",
OnlineNodePath.createInstanceTypeSearchCriteria()).isPresent());
+ }
+
+ @Test
+ void assertCreateInstanceIdSearchCriteria() {
+
assertThat(NodePathSearcher.find("/nodes/compute_nodes/online/jdbc/foo_instance_id",
OnlineNodePath.createInstanceIdSearchCriteria()),
is(Optional.of("foo_instance_id")));
+ assertFalse(NodePathSearcher.find("/nodes/compute_nodes/online/jdbc",
OnlineNodePath.createInstanceIdSearchCriteria()).isPresent());
+
assertTrue(NodePathSearcher.isMatchedPath("/nodes/compute_nodes/online/proxy/foo_instance_id",
OnlineNodePath.createInstanceIdSearchCriteria()));
+
assertFalse(NodePathSearcher.isMatchedPath("/nodes/compute_nodes/online/proxy",
OnlineNodePath.createInstanceIdSearchCriteria()));
+ }
}
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/ComputeNodeOnlineHandler.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/ComputeNodeOnlineHandler.java
index 3f043295356..0dc958c9fb1 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/ComputeNodeOnlineHandler.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/ComputeNodeOnlineHandler.java
@@ -30,12 +30,11 @@ import
org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global.GlobalDataChangedEventHandler;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.NodePathSearcher;
import
org.apache.shardingsphere.mode.node.path.node.compute.status.OnlineNodePath;
import java.util.Arrays;
import java.util.Collection;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
/**
* Compute node online handler.
@@ -54,20 +53,17 @@ public final class ComputeNodeOnlineHandler implements
GlobalDataChangedEventHan
@Override
public void handle(final ContextManager contextManager, final
DataChangedEvent event) {
- Matcher matcher = getInstanceOnlinePathMatcher(event.getKey());
- if (!matcher.find()) {
+ if (!NodePathSearcher.isMatchedPath(event.getKey(),
OnlineNodePath.createInstanceIdSearchCriteria())) {
return;
}
+ InstanceType instanceType =
InstanceType.valueOf(NodePathSearcher.find(event.getKey(),
OnlineNodePath.createInstanceTypeSearchCriteria()).orElse("").toUpperCase());
+ String instanceId = NodePathSearcher.find(event.getKey(),
OnlineNodePath.createInstanceIdSearchCriteria()).orElse("");
ComputeNodeData computeNodeData = new
YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(),
YamlComputeNodeData.class));
- InstanceMetaData instanceMetaData =
InstanceMetaDataFactory.create(matcher.group(2),
InstanceType.valueOf(matcher.group(1).toUpperCase()), computeNodeData);
+ InstanceMetaData instanceMetaData =
InstanceMetaDataFactory.create(instanceId, instanceType, computeNodeData);
if (Type.ADDED == event.getType()) {
contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry().add(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadInstance(instanceMetaData));
} else if (Type.DELETED == event.getType()) {
contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry().delete(new
ComputeNodeInstance(instanceMetaData));
}
}
-
- private Matcher getInstanceOnlinePathMatcher(final String
onlineInstancePath) {
- return Pattern.compile(NodePathGenerator.toPath(new
OnlineNodePath(null, null), false) + "/([\\S]+)/([\\S]+)$",
Pattern.CASE_INSENSITIVE).matcher(onlineInstancePath);
- }
}