29
29
import org .apache .shardingsphere .infra .instance .yaml .YamlComputeNodeDataSwapper ;
30
30
import org .apache .shardingsphere .infra .state .instance .InstanceState ;
31
31
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 ;
33
38
import org .apache .shardingsphere .mode .spi .repository .PersistRepository ;
34
39
35
40
import java .util .Arrays ;
@@ -63,7 +68,7 @@ public void registerOnline(final ComputeNodeInstance computeNodeInstance) {
63
68
private void persistOnline (final ComputeNodeInstance computeNodeInstance ) {
64
69
ComputeNodeData computeNodeData = new ComputeNodeData (
65
70
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 ()),
67
72
YamlEngine .marshal (new YamlComputeNodeDataSwapper ().swapToYamlConfiguration (computeNodeData )));
68
73
}
69
74
@@ -73,7 +78,7 @@ private void persistOnline(final ComputeNodeInstance computeNodeInstance) {
73
78
* @param computeNodeInstance compute node instance
74
79
*/
75
80
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 ()));
77
82
}
78
83
79
84
/**
@@ -87,8 +92,8 @@ public Collection<ComputeNodeInstance> loadAllInstances() {
87
92
88
93
private Collection <ComputeNodeInstance > loadInstances (final InstanceType instanceType ) {
89
94
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 ));
92
97
if (!Strings .isNullOrEmpty (value )) {
93
98
ComputeNodeData computeNodeData = new YamlComputeNodeDataSwapper ().swapToObject (YamlEngine .unmarshal (value , YamlComputeNodeData .class ));
94
99
ComputeNodeInstance instance = loadInstance (InstanceMetaDataFactory .create (each , instanceType , computeNodeData ));
@@ -113,12 +118,12 @@ public ComputeNodeInstance loadInstance(final InstanceMetaData instanceMetaData)
113
118
}
114
119
115
120
private String loadState (final String instanceId ) {
116
- return repository .query (ComputeNodePathGenerator . getStatePath (instanceId ));
121
+ return repository .query (new NodePathGenerator ( new StatusNodePath ()). getPath (instanceId ));
117
122
}
118
123
119
124
@ SuppressWarnings ("unchecked" )
120
125
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 ));
122
127
return Strings .isNullOrEmpty (yamlContent ) ? Collections .emptyList () : YamlEngine .unmarshal (yamlContent , Collection .class );
123
128
}
124
129
@@ -129,7 +134,7 @@ private Collection<String> loadLabels(final String instanceId) {
129
134
* @param instanceState instance state
130
135
*/
131
136
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 ());
133
138
}
134
139
135
140
/**
@@ -139,7 +144,7 @@ public void updateState(final String instanceId, final InstanceState instanceSta
139
144
* @param labels instance labels
140
145
*/
141
146
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 ));
143
148
}
144
149
145
150
/**
@@ -149,7 +154,7 @@ public void persistLabels(final String instanceId, final Collection<String> labe
149
154
* @param workerId worker ID
150
155
*/
151
156
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 ));
153
158
}
154
159
155
160
/**
@@ -160,7 +165,7 @@ public void persistWorkerId(final String instanceId, final int workerId) {
160
165
*/
161
166
public Optional <Integer > loadWorkerId (final String instanceId ) {
162
167
try {
163
- String workerId = repository .query (ComputeNodePathGenerator . getWorkerIdPath (instanceId ));
168
+ String workerId = repository .query (new NodePathGenerator ( new ComputeNodeWorkerIDNodePath ()). getPath (instanceId ));
164
169
return Strings .isNullOrEmpty (workerId ) ? Optional .empty () : Optional .of (Integer .valueOf (workerId ));
165
170
} catch (final NumberFormatException ex ) {
166
171
log .error ("Invalid worker id for instance: {}" , instanceId );
@@ -174,7 +179,8 @@ public Optional<Integer> loadWorkerId(final String instanceId) {
174
179
* @return assigned worker IDs
175
180
*/
176
181
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 ());
179
185
}
180
186
}
0 commit comments