Skip to content

Commit 89f0a92

Browse files
authored
Refactor NodeNodePath (#34722)
* Refactor NodeNodePath * Refactor NodeNodePath * Refactor NodeNodePath * Refactor NodeNodePath
1 parent acc16fc commit 89f0a92

File tree

40 files changed

+693
-319
lines changed

40 files changed

+693
-319
lines changed

features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/deliver/ReadwriteSplittingQualifiedDataSourceChangedSubscriber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import lombok.Setter;
2222
import org.apache.shardingsphere.mode.deliver.DeliverEventSubscriber;
2323
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
24-
import org.apache.shardingsphere.mode.node.path.node.QualifiedDataSourceNodePath;
24+
import org.apache.shardingsphere.mode.node.path.node.storage.QualifiedDataSourceNodePath;
2525
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
2626

2727
/**

mode/core/src/main/java/org/apache/shardingsphere/mode/state/node/ComputeNodePersistService.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
import org.apache.shardingsphere.infra.instance.yaml.YamlComputeNodeDataSwapper;
3030
import org.apache.shardingsphere.infra.state.instance.InstanceState;
3131
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
32-
import org.apache.shardingsphere.mode.node.path.node.ComputeNodePathGenerator;
32+
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
33+
import org.apache.shardingsphere.mode.node.path.node.compute.label.LabelNodePath;
34+
import org.apache.shardingsphere.mode.node.path.node.compute.status.OnlineInstanceNodePath;
35+
import org.apache.shardingsphere.mode.node.path.node.compute.status.OnlineTypeNodePath;
36+
import org.apache.shardingsphere.mode.node.path.node.compute.status.StatusNodePath;
37+
import org.apache.shardingsphere.mode.node.path.node.compute.workerid.ComputeNodeWorkerIDNodePath;
3338
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
3439

3540
import java.util.Arrays;
@@ -63,7 +68,7 @@ public void registerOnline(final ComputeNodeInstance computeNodeInstance) {
6368
private void persistOnline(final ComputeNodeInstance computeNodeInstance) {
6469
ComputeNodeData computeNodeData = new ComputeNodeData(
6570
computeNodeInstance.getMetaData().getDatabaseName(), computeNodeInstance.getMetaData().getAttributes(), computeNodeInstance.getMetaData().getVersion());
66-
repository.persistEphemeral(ComputeNodePathGenerator.getOnlinePath(computeNodeInstance.getMetaData().getId(), computeNodeInstance.getMetaData().getType()),
71+
repository.persistEphemeral(new NodePathGenerator(new OnlineInstanceNodePath(computeNodeInstance.getMetaData().getType())).getPath(computeNodeInstance.getMetaData().getId()),
6772
YamlEngine.marshal(new YamlComputeNodeDataSwapper().swapToYamlConfiguration(computeNodeData)));
6873
}
6974

@@ -73,7 +78,7 @@ private void persistOnline(final ComputeNodeInstance computeNodeInstance) {
7378
* @param computeNodeInstance compute node instance
7479
*/
7580
public void offline(final ComputeNodeInstance computeNodeInstance) {
76-
repository.delete(ComputeNodePathGenerator.getOnlinePath(computeNodeInstance.getMetaData().getId(), computeNodeInstance.getMetaData().getType()));
81+
repository.delete(new NodePathGenerator(new OnlineInstanceNodePath(computeNodeInstance.getMetaData().getType())).getPath(computeNodeInstance.getMetaData().getId()));
7782
}
7883

7984
/**
@@ -87,8 +92,8 @@ public Collection<ComputeNodeInstance> loadAllInstances() {
8792

8893
private Collection<ComputeNodeInstance> loadInstances(final InstanceType instanceType) {
8994
Collection<ComputeNodeInstance> result = new LinkedList<>();
90-
for (String each : repository.getChildrenKeys(ComputeNodePathGenerator.getOnlinePath(instanceType))) {
91-
String value = repository.query(ComputeNodePathGenerator.getOnlinePath(each, instanceType));
95+
for (String each : repository.getChildrenKeys(new NodePathGenerator(new OnlineTypeNodePath()).getPath(instanceType.name().toLowerCase()))) {
96+
String value = repository.query(new NodePathGenerator(new OnlineInstanceNodePath(instanceType)).getPath(each));
9297
if (!Strings.isNullOrEmpty(value)) {
9398
ComputeNodeData computeNodeData = new YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(value, YamlComputeNodeData.class));
9499
ComputeNodeInstance instance = loadInstance(InstanceMetaDataFactory.create(each, instanceType, computeNodeData));
@@ -113,12 +118,12 @@ public ComputeNodeInstance loadInstance(final InstanceMetaData instanceMetaData)
113118
}
114119

115120
private String loadState(final String instanceId) {
116-
return repository.query(ComputeNodePathGenerator.getStatePath(instanceId));
121+
return repository.query(new NodePathGenerator(new StatusNodePath()).getPath(instanceId));
117122
}
118123

119124
@SuppressWarnings("unchecked")
120125
private Collection<String> loadLabels(final String instanceId) {
121-
String yamlContent = repository.query(ComputeNodePathGenerator.getLabelsPath(instanceId));
126+
String yamlContent = repository.query(new NodePathGenerator(new LabelNodePath()).getPath(instanceId));
122127
return Strings.isNullOrEmpty(yamlContent) ? Collections.emptyList() : YamlEngine.unmarshal(yamlContent, Collection.class);
123128
}
124129

@@ -129,7 +134,7 @@ private Collection<String> loadLabels(final String instanceId) {
129134
* @param instanceState instance state
130135
*/
131136
public void updateState(final String instanceId, final InstanceState instanceState) {
132-
repository.persistEphemeral(ComputeNodePathGenerator.getStatePath(instanceId), instanceState.name());
137+
repository.persistEphemeral(new NodePathGenerator(new StatusNodePath()).getPath(instanceId), instanceState.name());
133138
}
134139

135140
/**
@@ -139,7 +144,7 @@ public void updateState(final String instanceId, final InstanceState instanceSta
139144
* @param labels instance labels
140145
*/
141146
public void persistLabels(final String instanceId, final Collection<String> labels) {
142-
repository.persistEphemeral(ComputeNodePathGenerator.getLabelsPath(instanceId), YamlEngine.marshal(labels));
147+
repository.persistEphemeral(new NodePathGenerator(new LabelNodePath()).getPath(instanceId), YamlEngine.marshal(labels));
143148
}
144149

145150
/**
@@ -149,7 +154,7 @@ public void persistLabels(final String instanceId, final Collection<String> labe
149154
* @param workerId worker ID
150155
*/
151156
public void persistWorkerId(final String instanceId, final int workerId) {
152-
repository.persistEphemeral(ComputeNodePathGenerator.getWorkerIdPath(instanceId), String.valueOf(workerId));
157+
repository.persistEphemeral(new NodePathGenerator(new ComputeNodeWorkerIDNodePath()).getPath(instanceId), String.valueOf(workerId));
153158
}
154159

155160
/**
@@ -160,7 +165,7 @@ public void persistWorkerId(final String instanceId, final int workerId) {
160165
*/
161166
public Optional<Integer> loadWorkerId(final String instanceId) {
162167
try {
163-
String workerId = repository.query(ComputeNodePathGenerator.getWorkerIdPath(instanceId));
168+
String workerId = repository.query(new NodePathGenerator(new ComputeNodeWorkerIDNodePath()).getPath(instanceId));
164169
return Strings.isNullOrEmpty(workerId) ? Optional.empty() : Optional.of(Integer.valueOf(workerId));
165170
} catch (final NumberFormatException ex) {
166171
log.error("Invalid worker id for instance: {}", instanceId);
@@ -174,7 +179,8 @@ public Optional<Integer> loadWorkerId(final String instanceId) {
174179
* @return assigned worker IDs
175180
*/
176181
public Collection<Integer> getAssignedWorkerIds() {
177-
Collection<String> instanceIds = repository.getChildrenKeys(ComputeNodePathGenerator.getWorkerIdRootPath());
178-
return instanceIds.stream().map(each -> repository.query(ComputeNodePathGenerator.getWorkerIdPath(each))).filter(Objects::nonNull).map(Integer::parseInt).collect(Collectors.toSet());
182+
Collection<String> instanceIds = repository.getChildrenKeys(new ComputeNodeWorkerIDNodePath().getRootPath());
183+
return instanceIds.stream()
184+
.map(each -> repository.query(new NodePathGenerator(new ComputeNodeWorkerIDNodePath()).getPath(each))).filter(Objects::nonNull).map(Integer::parseInt).collect(Collectors.toSet());
179185
}
180186
}

mode/core/src/main/java/org/apache/shardingsphere/mode/state/node/QualifiedDataSourceStatePersistService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.apache.shardingsphere.infra.state.datasource.qualified.yaml.YamlQualifiedDataSourceStateSwapper;
2727
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
2828
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
29-
import org.apache.shardingsphere.mode.node.path.node.QualifiedDataSourceNodePath;
29+
import org.apache.shardingsphere.mode.node.path.node.storage.QualifiedDataSourceNodePath;
3030
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
3131

3232
import java.util.Collection;

mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/ComputeNodePathGenerator.java

Lines changed: 0 additions & 161 deletions
This file was deleted.
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@
2020
import org.apache.shardingsphere.mode.node.path.NodePath;
2121

2222
/**
23-
* Qualified data source node path.
23+
* Node node path.
2424
*/
25-
public final class QualifiedDataSourceNodePath implements NodePath {
25+
public final class NodeNodePath implements NodePath {
2626

2727
private static final String ROOT_NODE = "/nodes";
2828

29-
private static final String QUALIFIED_DATA_SOURCES_NODE = "qualified_data_sources";
30-
3129
@Override
3230
public String getRootPath() {
33-
return String.join("/", ROOT_NODE, QUALIFIED_DATA_SOURCES_NODE);
31+
return ROOT_NODE;
3432
}
3533
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.mode.node.path.node.compute;
19+
20+
import org.apache.shardingsphere.mode.node.path.NodePath;
21+
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
22+
import org.apache.shardingsphere.mode.node.path.node.NodeNodePath;
23+
24+
/**
25+
* Compute node path.
26+
*/
27+
public final class ComputeNodePath implements NodePath {
28+
29+
private static final String ROOT_NODE = "compute_nodes";
30+
31+
private final NodePathGenerator nodePathGenerator = new NodePathGenerator(new NodeNodePath());
32+
33+
@Override
34+
public String getRootPath() {
35+
return nodePathGenerator.getPath(ROOT_NODE);
36+
}
37+
}

mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/ComputeNodePathParser.java renamed to mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/compute/ComputeNodePathParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.shardingsphere.mode.node.path.node;
18+
package org.apache.shardingsphere.mode.node.path.node.compute;
1919

2020
import lombok.AccessLevel;
2121
import lombok.NoArgsConstructor;
@@ -39,7 +39,7 @@ public final class ComputeNodePathParser {
3939
* @return found instance ID
4040
*/
4141
public static Optional<String> findInstanceId(final String computeNodePath) {
42-
Pattern pattern = Pattern.compile(ComputeNodePathGenerator.getRootPath() + "(/status|/worker_id|/labels)" + "/" + INSTANCE_ID_PATTERN + "$", Pattern.CASE_INSENSITIVE);
42+
Pattern pattern = Pattern.compile(new ComputeNodePath().getRootPath() + "(/status|/worker_id|/labels)" + "/" + INSTANCE_ID_PATTERN + "$", Pattern.CASE_INSENSITIVE);
4343
Matcher matcher = pattern.matcher(computeNodePath);
4444
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
4545
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.mode.node.path.node.compute.label;
19+
20+
import org.apache.shardingsphere.mode.node.path.NodePath;
21+
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
22+
import org.apache.shardingsphere.mode.node.path.node.compute.ComputeNodePath;
23+
24+
/**
25+
* Label node path.
26+
*/
27+
public final class LabelNodePath implements NodePath {
28+
29+
private static final String ROOT_NODE = "labels";
30+
31+
private final NodePathGenerator nodePathGenerator = new NodePathGenerator(new ComputeNodePath());
32+
33+
@Override
34+
public String getRootPath() {
35+
return nodePathGenerator.getPath(ROOT_NODE);
36+
}
37+
}

0 commit comments

Comments
 (0)