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:48:58 UTC
(shardingsphere) branch master updated: Refactor ReservationNodePath (#34712)
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 dcd62723d9d Refactor ReservationNodePath (#34712)
dcd62723d9d is described below
commit dcd62723d9dd546924cb8b2ee21772e5f5486fc9
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Feb 19 13:48:53 2025 +0800
Refactor ReservationNodePath (#34712)
---
...PathGenerator.java => ReservationNodePath.java} | 23 +++++++---------------
.../node/path/state/ReservationNodePathTest.java | 4 ++--
.../workerid/ReservationPersistService.java | 6 ++++--
3 files changed, 13 insertions(+), 20 deletions(-)
diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePathGenerator.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePath.java
similarity index 58%
rename from mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePathGenerator.java
rename to mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePath.java
index 9987cac3308..6ca219384f8 100644
--- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePathGenerator.java
+++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePath.java
@@ -17,26 +17,17 @@
package org.apache.shardingsphere.mode.node.path.state;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.mode.node.path.NodePath;
/**
- * Reservation node path generator.
+ * Reservation node path.
*/
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ReservationNodePathGenerator {
+public final class ReservationNodePath implements NodePath {
- private static final String ROOT_NODE = "/reservation";
+ private static final String ROOT_NODE = "/reservation/worker_id";
- private static final String WORKER_ID_NODE = "worker_id";
-
- /**
- * Get worker id reservation path.
- *
- * @param workerId worker id
- * @return worker id reservation path
- */
- public static String getWorkerIdReservationPath(final int workerId) {
- return String.join("/", ROOT_NODE, WORKER_ID_NODE, String.valueOf(workerId));
+ @Override
+ public String getRootPath() {
+ return ROOT_NODE;
}
}
diff --git a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePathTest.java b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePathTest.java
index 1b8dc04efee..bb0b28ed59d 100644
--- a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePathTest.java
+++ b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/ReservationNodePathTest.java
@@ -17,15 +17,15 @@
package org.apache.shardingsphere.mode.node.path.state;
-import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
class ReservationNodePathTest {
@Test
void assertGetWorkerIdReservationPath() {
- MatcherAssert.assertThat(ReservationNodePathGenerator.getWorkerIdReservationPath(1), is("/reservation/worker_id/1"));
+ assertThat(new ReservationNodePath().getRootPath(), is("/reservation/worker_id"));
}
}
diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/workerid/ReservationPersistService.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/workerid/ReservationPersistService.java
index 6ca13d50205..671d9f37231 100644
--- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/workerid/ReservationPersistService.java
+++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/workerid/ReservationPersistService.java
@@ -18,7 +18,8 @@
package org.apache.shardingsphere.mode.manager.cluster.workerid;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.mode.node.path.state.ReservationNodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.state.ReservationNodePath;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.apache.shardingsphere.mode.repository.cluster.exception.ClusterRepositoryPersistException;
@@ -41,7 +42,8 @@ public final class ReservationPersistService {
*/
public Optional<Integer> reserveWorkerId(final Integer preselectedWorkerId, final String instanceId) {
try {
- return repository.persistExclusiveEphemeral(ReservationNodePathGenerator.getWorkerIdReservationPath(preselectedWorkerId), instanceId) ? Optional.of(preselectedWorkerId) : Optional.empty();
+ return repository.persistExclusiveEphemeral(
+ new NodePathGenerator(new ReservationNodePath()).getPath(preselectedWorkerId), instanceId) ? Optional.of(preselectedWorkerId) : Optional.empty();
} catch (final ClusterRepositoryPersistException ignore) {
return Optional.empty();
}