Skip to content

Commit

Permalink
Add WorkerIDNodePath and ProcessNodePath (#34715)
Browse files Browse the repository at this point in the history
* Add WorkerIDNodePath

* Add ProcessNodePath
  • Loading branch information
terrymanu authored Feb 19, 2025
1 parent d664b5c commit 0697712
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@
* limitations under the License.
*/

package org.apache.shardingsphere.mode.node.path.state;
package org.apache.shardingsphere.mode.node.path.execution;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;

/**
* Process node path generator.
* Execution node path.
*/
@RequiredArgsConstructor
public final class ProcessNodePath implements NodePath {
public final class ExecutionNodePath implements NodePath {

private static final String ROOT_NODE = "/execution_nodes";

private final String processId;

@Override
public String getRootPath() {
return String.join("/", ROOT_NODE, processId);
return ROOT_NODE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.execution.process;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.execution.ExecutionNodePath;

/**
* Process node path.
*/
@RequiredArgsConstructor
public final class ProcessNodePath implements NodePath {

private final String processId;

private final NodePathGenerator nodePathGenerator = new NodePathGenerator(new ExecutionNodePath());

@Override
public String getRootPath() {
return String.join("/", nodePathGenerator.getPath(processId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public final class ReservationNodePath implements NodePath {

private static final String ROOT_NODE = "/reservation/worker_id";
private static final String ROOT_NODE = "/reservation";

@Override
public String getRootPath() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.reservation.workerid;

import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.reservation.ReservationNodePath;

/**
* Worker ID node path.
*/
public final class WorkerIDNodePath implements NodePath {

private static final String ROOT_NODE = "worker_id";

private final NodePathGenerator nodePathGenerator = new NodePathGenerator(new ReservationNodePath());

@Override
public String getRootPath() {
return String.join("/", nodePathGenerator.getPath(ROOT_NODE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.execution;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class ExecutionNodePathTest {

@Test
void assertGetRootPath() {
assertThat(new ExecutionNodePath().getRootPath(), is("/execution_nodes"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.mode.node.path.state;
package org.apache.shardingsphere.mode.node.path.execution.process;

import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class ReservationNodePathTest {

@Test
void assertGetWorkerIdReservationPath() {
assertThat(new ReservationNodePath().getRootPath(), is("/reservation/worker_id"));
void assertGetRootPath() {
assertThat(new ReservationNodePath().getRootPath(), is("/reservation"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.reservation.workerid;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class WorkerIDNodePathTest {

@Test
void assertGetRootPath() {
assertThat(new WorkerIDNodePath().getRootPath(), is("/reservation/worker_id"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.node.ComputeNodePathGenerator;
import org.apache.shardingsphere.mode.node.path.state.ProcessNodePath;
import org.apache.shardingsphere.mode.node.path.execution.process.ProcessNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.node.ComputeNodePathGenerator;
import org.apache.shardingsphere.mode.node.path.state.ProcessNodePath;
import org.apache.shardingsphere.mode.node.path.execution.process.ProcessNodePath;
import org.apache.shardingsphere.mode.persist.service.ProcessPersistService;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.reservation.ReservationNodePath;
import org.apache.shardingsphere.mode.node.path.reservation.workerid.WorkerIDNodePath;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.apache.shardingsphere.mode.repository.cluster.exception.ClusterRepositoryPersistException;

Expand All @@ -43,7 +43,7 @@ public final class ReservationPersistService {
public Optional<Integer> reserveWorkerId(final Integer preselectedWorkerId, final String instanceId) {
try {
return repository.persistExclusiveEphemeral(
new NodePathGenerator(new ReservationNodePath()).getPath(preselectedWorkerId), instanceId) ? Optional.of(preselectedWorkerId) : Optional.empty();
new NodePathGenerator(new WorkerIDNodePath()).getPath(preselectedWorkerId), instanceId) ? Optional.of(preselectedWorkerId) : Optional.empty();
} catch (final ClusterRepositoryPersistException ignore) {
return Optional.empty();
}
Expand Down

0 comments on commit 0697712

Please sign in to comment.