You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2025/02/19 05:31:03 UTC

(shardingsphere) branch master updated: Refactor ProcessNodePath (#34710)

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 dafd274363b Refactor ProcessNodePath (#34710)
dafd274363b is described below

commit dafd274363b37c2e2eef45522107304eaf34d2e5
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Feb 19 13:30:56 2025 +0800

    Refactor ProcessNodePath (#34710)
---
 ...NodePathGenerator.java => ProcessNodePath.java} | 30 +++++-------------
 .../mode/node/path/state/ProcessNodePathTest.java  |  7 +---
 .../mode/node/path/state/ProcessNodeTest.java      | 37 ----------------------
 .../process/ClusterProcessPersistCoordinator.java  |  5 +--
 .../service/ClusterProcessPersistService.java      |  9 +++---
 5 files changed, 17 insertions(+), 71 deletions(-)

diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePathGenerator.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePath.java
similarity index 60%
rename from mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePathGenerator.java
rename to mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePath.java
index 2a1d59976a0..17fb5a245a0 100644
--- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePathGenerator.java
+++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePath.java
@@ -17,35 +17,21 @@
 
 package org.apache.shardingsphere.mode.node.path.state;
 
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.mode.node.path.NodePath;
 
 /**
  * Process node path generator.
  */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ProcessNodePathGenerator {
+@RequiredArgsConstructor
+public final class ProcessNodePath implements NodePath {
     
     private static final String ROOT_NODE = "/execution_nodes";
     
-    /**
-     * Get process ID root path.
-     *
-     * @param processId process ID
-     * @return process ID root path
-     */
-    public static String getRootPath(final String processId) {
-        return String.join("/", ROOT_NODE, processId);
-    }
+    private final String processId;
     
-    /**
-     * Get instance process list.
-     *
-     * @param processId process ID
-     * @param instanceId instance ID
-     * @return instance process list
-     */
-    public static String getInstanceProcessList(final String processId, final String instanceId) {
-        return String.join("/", getRootPath(processId), instanceId);
+    @Override
+    public String getRootPath() {
+        return String.join("/", ROOT_NODE, processId);
     }
 }
diff --git a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePathTest.java b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePathTest.java
index a1dd7546577..79502d4a3db 100644
--- a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePathTest.java
+++ b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodePathTest.java
@@ -26,11 +26,6 @@ class ProcessNodePathTest {
     
     @Test
     void assertGetRootPath() {
-        assertThat(ProcessNodePathGenerator.getRootPath("foo_process_id"), is("/execution_nodes/foo_process_id"));
-    }
-    
-    @Test
-    void assertGetInstanceProcessList() {
-        assertThat(ProcessNodePathGenerator.getInstanceProcessList("foo_process_id", "foo_instance_id"), is("/execution_nodes/foo_process_id/foo_instance_id"));
+        assertThat(new ProcessNodePath("foo_process_id").getRootPath(), is("/execution_nodes/foo_process_id"));
     }
 }
diff --git a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodeTest.java b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodeTest.java
deleted file mode 100644
index cdb033008cc..00000000000
--- a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ProcessNodeTest.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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.node.path.state;
-
-import org.junit.jupiter.api.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-class ProcessNodeTest {
-    
-    @Test
-    void assertGetProcessIdPath() {
-        assertThat(ProcessNodePathGenerator.getRootPath("ae7d352a-ee1f-3cd6-8631-cd9e93b70a30"), is("/execution_nodes/ae7d352a-ee1f-3cd6-8631-cd9e93b70a30"));
-    }
-    
-    @Test
-    void assertGetProcessListInstancePath() {
-        assertThat(ProcessNodePathGenerator.getInstanceProcessList("ae7d352a-ee1f-3cd6-8631-cd9e93b70a30", "proxy_127.0.0.1@983481"),
-                is("/execution_nodes/ae7d352a-ee1f-3cd6-8631-cd9e93b70a30/proxy_127.0.0.1@983481"));
-    }
-}
diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/coordinator/process/ClusterProcessPersistCoordinator.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/coordinator/process/ClusterProcessPersistCoordinator.java
index 169871f5c29..c53bd6ada42 100644
--- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/coordinator/process/ClusterProcessPersistCoordinator.java
+++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/coordinator/process/ClusterProcessPersistCoordinator.java
@@ -22,8 +22,9 @@ import org.apache.shardingsphere.infra.executor.sql.process.Process;
 import org.apache.shardingsphere.infra.executor.sql.process.ProcessRegistry;
 import org.apache.shardingsphere.infra.executor.sql.process.yaml.swapper.YamlProcessListSwapper;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
 import org.apache.shardingsphere.mode.node.path.state.ComputeNodePathGenerator;
-import org.apache.shardingsphere.mode.node.path.state.ProcessNodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.state.ProcessNodePath;
 import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
 
 import java.util.Collection;
@@ -47,7 +48,7 @@ public final class ClusterProcessPersistCoordinator {
     public void reportLocalProcesses(final String instanceId, final String taskId) {
         Collection<Process> processes = ProcessRegistry.getInstance().listAll();
         if (!processes.isEmpty()) {
-            repository.persist(ProcessNodePathGenerator.getInstanceProcessList(taskId, instanceId), YamlEngine.marshal(swapper.swapToYamlConfiguration(processes)));
+            repository.persist(new NodePathGenerator(new ProcessNodePath(taskId)).getPath(instanceId), YamlEngine.marshal(swapper.swapToYamlConfiguration(processes)));
         }
         repository.delete(ComputeNodePathGenerator.getShowProcessListTriggerPath(instanceId, taskId));
     }
diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/ClusterProcessPersistService.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/ClusterProcessPersistService.java
index ac2f57a17a1..aaed35c4f6d 100644
--- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/ClusterProcessPersistService.java
+++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/ClusterProcessPersistService.java
@@ -24,8 +24,9 @@ import org.apache.shardingsphere.infra.executor.sql.process.yaml.YamlProcessList
 import org.apache.shardingsphere.infra.executor.sql.process.yaml.swapper.YamlProcessListSwapper;
 import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
 import org.apache.shardingsphere.mode.node.path.state.ComputeNodePathGenerator;
-import org.apache.shardingsphere.mode.node.path.state.ProcessNodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.state.ProcessNodePath;
 import org.apache.shardingsphere.mode.persist.service.ProcessPersistService;
 import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
 
@@ -53,7 +54,7 @@ public final class ClusterProcessPersistService implements ProcessPersistService
             isCompleted = ProcessOperationLockRegistry.getInstance().waitUntilReleaseReady(taskId, () -> isReady(triggerPaths));
             return getShowProcessListData(taskId);
         } finally {
-            repository.delete(ProcessNodePathGenerator.getRootPath(taskId));
+            repository.delete(new ProcessNodePath(taskId).getRootPath());
             if (!isCompleted) {
                 triggerPaths.forEach(repository::delete);
             }
@@ -69,8 +70,8 @@ public final class ClusterProcessPersistService implements ProcessPersistService
     
     private Collection<Process> getShowProcessListData(final String taskId) {
         YamlProcessList yamlProcessList = new YamlProcessList();
-        for (String each : repository.getChildrenKeys(ProcessNodePathGenerator.getRootPath(taskId)).stream()
-                .map(each -> repository.query(ProcessNodePathGenerator.getInstanceProcessList(taskId, each))).collect(Collectors.toList())) {
+        for (String each : repository.getChildrenKeys(new ProcessNodePath(taskId).getRootPath()).stream()
+                .map(each -> repository.query(new NodePathGenerator(new ProcessNodePath(taskId)).getPath(each))).collect(Collectors.toList())) {
             yamlProcessList.getProcesses().addAll(YamlEngine.unmarshal(each, YamlProcessList.class).getProcesses());
         }
         return new YamlProcessListSwapper().swapToObject(yamlProcessList);