@@ -69,22 +69,25 @@ public class UnsetTemplateProcedure
69
69
private transient ByteBuffer addTemplateSetInfo ;
70
70
private transient ByteBuffer invalidateTemplateSetInfo ;
71
71
72
- public UnsetTemplateProcedure (boolean isGeneratedByPipe ) {
72
+ public UnsetTemplateProcedure (final boolean isGeneratedByPipe ) {
73
73
super (isGeneratedByPipe );
74
74
}
75
75
76
76
public UnsetTemplateProcedure (
77
- String queryId , Template template , PartialPath path , boolean isGeneratedByPipe ) {
77
+ final String queryId ,
78
+ final Template template ,
79
+ final PartialPath path ,
80
+ final boolean isGeneratedByPipe ) {
78
81
super (isGeneratedByPipe );
79
82
this .queryId = queryId ;
80
83
this .template = template ;
81
84
this .path = path ;
82
85
}
83
86
84
87
@ Override
85
- protected Flow executeFromState (ConfigNodeProcedureEnv env , UnsetTemplateState state )
88
+ protected Flow executeFromState (final ConfigNodeProcedureEnv env , final UnsetTemplateState state )
86
89
throws ProcedureSuspendedException , ProcedureYieldException , InterruptedException {
87
- long startTime = System .currentTimeMillis ();
90
+ final long startTime = System .currentTimeMillis ();
88
91
try {
89
92
switch (state ) {
90
93
case CONSTRUCT_BLACK_LIST :
@@ -127,8 +130,8 @@ protected Flow executeFromState(ConfigNodeProcedureEnv env, UnsetTemplateState s
127
130
}
128
131
}
129
132
130
- private void constructBlackList (ConfigNodeProcedureEnv env ) {
131
- TSStatus status =
133
+ private void constructBlackList (final ConfigNodeProcedureEnv env ) {
134
+ final TSStatus status =
132
135
env .getConfigManager ()
133
136
.getClusterSchemaManager ()
134
137
.preUnsetSchemaTemplate (template .getId (), path );
@@ -139,30 +142,33 @@ private void constructBlackList(ConfigNodeProcedureEnv env) {
139
142
}
140
143
}
141
144
142
- private void invalidateCache (ConfigNodeProcedureEnv env ) {
145
+ private void invalidateCache (final ConfigNodeProcedureEnv env ) {
143
146
try {
147
+ // Cannot roll back after cache invalidation
148
+ // Because we do not know whether there are time series successfully created
149
+ alreadyRollback = true ;
144
150
executeInvalidateCache (env );
145
151
setNextState (UnsetTemplateState .CHECK_DATANODE_TEMPLATE_ACTIVATION );
146
- } catch (ProcedureException e ) {
152
+ } catch (final ProcedureException e ) {
147
153
setFailure (e );
148
154
}
149
155
}
150
156
151
- private void executeInvalidateCache (ConfigNodeProcedureEnv env ) throws ProcedureException {
152
- Map <Integer , TDataNodeLocation > dataNodeLocationMap =
157
+ private void executeInvalidateCache (final ConfigNodeProcedureEnv env ) throws ProcedureException {
158
+ final Map <Integer , TDataNodeLocation > dataNodeLocationMap =
153
159
env .getConfigManager ().getNodeManager ().getRegisteredDataNodeLocations ();
154
- TUpdateTemplateReq invalidateTemplateSetInfoReq = new TUpdateTemplateReq ();
160
+ final TUpdateTemplateReq invalidateTemplateSetInfoReq = new TUpdateTemplateReq ();
155
161
invalidateTemplateSetInfoReq .setType (
156
162
TemplateInternalRPCUpdateType .INVALIDATE_TEMPLATE_SET_INFO .toByte ());
157
163
invalidateTemplateSetInfoReq .setTemplateInfo (getInvalidateTemplateSetInfo ());
158
- DataNodeAsyncRequestContext <TUpdateTemplateReq , TSStatus > clientHandler =
164
+ final DataNodeAsyncRequestContext <TUpdateTemplateReq , TSStatus > clientHandler =
159
165
new DataNodeAsyncRequestContext <>(
160
166
CnToDnAsyncRequestType .UPDATE_TEMPLATE ,
161
167
invalidateTemplateSetInfoReq ,
162
168
dataNodeLocationMap );
163
169
CnToDnInternalServiceAsyncRequestManager .getInstance ().sendAsyncRequestWithRetry (clientHandler );
164
- Map <Integer , TSStatus > statusMap = clientHandler .getResponseMap ();
165
- for (TSStatus status : statusMap .values ()) {
170
+ final Map <Integer , TSStatus > statusMap = clientHandler .getResponseMap ();
171
+ for (final TSStatus status : statusMap .values ()) {
166
172
// all dataNodes must clear the related template cache
167
173
if (status .getCode () != TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
168
174
LOGGER .error (
@@ -174,14 +180,14 @@ private void executeInvalidateCache(ConfigNodeProcedureEnv env) throws Procedure
174
180
}
175
181
}
176
182
177
- private boolean checkDataNodeTemplateActivation (ConfigNodeProcedureEnv env ) {
178
- PathPatternTree patternTree = new PathPatternTree ();
183
+ private boolean checkDataNodeTemplateActivation (final ConfigNodeProcedureEnv env ) {
184
+ final PathPatternTree patternTree = new PathPatternTree ();
179
185
patternTree .appendPathPattern (path );
180
186
patternTree .appendPathPattern (path .concatAsMeasurementPath (MULTI_LEVEL_PATH_WILDCARD ));
181
187
try {
182
188
return SchemaUtils .checkDataNodeTemplateActivation (
183
189
env .getConfigManager (), patternTree , template );
184
- } catch (MetadataException e ) {
190
+ } catch (final MetadataException e ) {
185
191
setFailure (
186
192
new ProcedureException (
187
193
new MetadataException (
@@ -192,95 +198,64 @@ private boolean checkDataNodeTemplateActivation(ConfigNodeProcedureEnv env) {
192
198
}
193
199
}
194
200
195
- private void unsetTemplate (ConfigNodeProcedureEnv env ) {
196
- TSStatus status =
201
+ private void unsetTemplate (final ConfigNodeProcedureEnv env ) {
202
+ final TSStatus status =
197
203
env .getConfigManager ()
198
204
.getClusterSchemaManager ()
199
205
.unsetSchemaTemplateInBlackList (template .getId (), path , isGeneratedByPipe );
200
- if (status .getCode () == TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
201
- setNextState (UnsetTemplateState .CLEAN_DATANODE_TEMPLATE_CACHE );
202
- } else {
206
+ if (status .getCode () != TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
203
207
setFailure (new ProcedureException (new IoTDBException (status .getMessage (), status .getCode ())));
204
208
}
205
209
}
206
210
207
211
@ Override
208
- protected void rollbackState (ConfigNodeProcedureEnv env , UnsetTemplateState unsetTemplateState )
212
+ protected void rollbackState (
213
+ final ConfigNodeProcedureEnv env , final UnsetTemplateState unsetTemplateState )
209
214
throws IOException , InterruptedException , ProcedureException {
210
215
if (alreadyRollback ) {
211
216
return ;
212
217
}
213
218
alreadyRollback = true ;
214
219
ProcedureException rollbackException ;
215
- try {
216
- executeRollbackInvalidateCache (env );
217
- TSStatus status =
218
- env .getConfigManager ()
219
- .getClusterSchemaManager ()
220
- .rollbackPreUnsetSchemaTemplate (template .getId (), path );
221
- if (status .getCode () == TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
222
- return ;
223
- } else {
224
- LOGGER .error (
225
- "Failed to rollback pre unset template operation of template {} set on {}" ,
226
- template .getName (),
227
- path );
228
- rollbackException =
229
- new ProcedureException (
230
- new MetadataException (
231
- "Rollback template pre unset failed because of" + status .getMessage ()));
232
- }
233
- } catch (ProcedureException e ) {
234
- rollbackException = e ;
220
+ final TSStatus status =
221
+ env .getConfigManager ()
222
+ .getClusterSchemaManager ()
223
+ .rollbackPreUnsetSchemaTemplate (template .getId (), path );
224
+ if (status .getCode () == TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
225
+ return ;
226
+ } else {
227
+ LOGGER .error (
228
+ "Failed to rollback pre unset template operation of template {} set on {}" ,
229
+ template .getName (),
230
+ path );
231
+ rollbackException =
232
+ new ProcedureException (
233
+ new MetadataException (
234
+ "Rollback template pre unset failed because of" + status .getMessage ()));
235
235
}
236
236
try {
237
237
executeInvalidateCache (env );
238
238
setFailure (rollbackException );
239
- } catch (ProcedureException exception ) {
239
+ } catch (final ProcedureException exception ) {
240
240
setFailure (
241
241
new ProcedureException (
242
242
new MetadataException (
243
243
"Rollback unset template failed and the cluster template info management is strictly broken. Please try unset again." )));
244
244
}
245
245
}
246
246
247
- private void executeRollbackInvalidateCache (ConfigNodeProcedureEnv env )
248
- throws ProcedureException {
249
- Map <Integer , TDataNodeLocation > dataNodeLocationMap =
250
- env .getConfigManager ().getNodeManager ().getRegisteredDataNodeLocations ();
251
- TUpdateTemplateReq rollbackTemplateSetInfoReq = new TUpdateTemplateReq ();
252
- rollbackTemplateSetInfoReq .setType (
253
- TemplateInternalRPCUpdateType .ADD_TEMPLATE_SET_INFO .toByte ());
254
- rollbackTemplateSetInfoReq .setTemplateInfo (getAddTemplateSetInfo ());
255
- DataNodeAsyncRequestContext <TUpdateTemplateReq , TSStatus > clientHandler =
256
- new DataNodeAsyncRequestContext <>(
257
- CnToDnAsyncRequestType .UPDATE_TEMPLATE ,
258
- rollbackTemplateSetInfoReq ,
259
- dataNodeLocationMap );
260
- CnToDnInternalServiceAsyncRequestManager .getInstance ().sendAsyncRequestWithRetry (clientHandler );
261
- Map <Integer , TSStatus > statusMap = clientHandler .getResponseMap ();
262
- for (TSStatus status : statusMap .values ()) {
263
- // all dataNodes must clear the related template cache
264
- if (status .getCode () != TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
265
- LOGGER .error (
266
- "Failed to rollback template cache of template {} set on {}" , template .getName (), path );
267
- throw new ProcedureException (new MetadataException ("Rollback template cache failed" ));
268
- }
269
- }
270
- }
271
-
272
247
@ Override
273
- protected boolean isRollbackSupported (UnsetTemplateState unsetTemplateState ) {
248
+ protected boolean isRollbackSupported (final UnsetTemplateState unsetTemplateState ) {
274
249
return true ;
275
250
}
276
251
277
252
@ Override
278
- protected UnsetTemplateState getState (int stateId ) {
253
+ protected UnsetTemplateState getState (final int stateId ) {
279
254
return UnsetTemplateState .values ()[stateId ];
280
255
}
281
256
282
257
@ Override
283
- protected int getStateId (UnsetTemplateState unsetTemplateState ) {
258
+ protected int getStateId (final UnsetTemplateState unsetTemplateState ) {
284
259
return unsetTemplateState .ordinal ();
285
260
}
286
261
@@ -309,17 +284,6 @@ public PartialPath getPath() {
309
284
return path ;
310
285
}
311
286
312
- private ByteBuffer getAddTemplateSetInfo () {
313
- if (this .addTemplateSetInfo == null ) {
314
- this .addTemplateSetInfo =
315
- ByteBuffer .wrap (
316
- TemplateInternalRPCUtil .generateAddTemplateSetInfoBytes (
317
- template , path .getFullPath ()));
318
- }
319
-
320
- return addTemplateSetInfo ;
321
- }
322
-
323
287
private ByteBuffer getInvalidateTemplateSetInfo () {
324
288
if (this .invalidateTemplateSetInfo == null ) {
325
289
this .invalidateTemplateSetInfo =
@@ -331,7 +295,7 @@ private ByteBuffer getInvalidateTemplateSetInfo() {
331
295
}
332
296
333
297
@ Override
334
- public void serialize (DataOutputStream stream ) throws IOException {
298
+ public void serialize (final DataOutputStream stream ) throws IOException {
335
299
stream .writeShort (
336
300
isGeneratedByPipe
337
301
? ProcedureType .PIPE_ENRICHED_UNSET_TEMPLATE_PROCEDURE .getTypeCode ()
@@ -344,7 +308,7 @@ public void serialize(DataOutputStream stream) throws IOException {
344
308
}
345
309
346
310
@ Override
347
- public void deserialize (ByteBuffer byteBuffer ) {
311
+ public void deserialize (final ByteBuffer byteBuffer ) {
348
312
super .deserialize (byteBuffer );
349
313
queryId = ReadWriteIOUtils .readString (byteBuffer );
350
314
template = new Template ();
@@ -354,10 +318,14 @@ public void deserialize(ByteBuffer byteBuffer) {
354
318
}
355
319
356
320
@ Override
357
- public boolean equals (Object o ) {
358
- if (this == o ) return true ;
359
- if (o == null || getClass () != o .getClass ()) return false ;
360
- UnsetTemplateProcedure that = (UnsetTemplateProcedure ) o ;
321
+ public boolean equals (final Object o ) {
322
+ if (this == o ) {
323
+ return true ;
324
+ }
325
+ if (o == null || getClass () != o .getClass ()) {
326
+ return false ;
327
+ }
328
+ final UnsetTemplateProcedure that = (UnsetTemplateProcedure ) o ;
361
329
return Objects .equals (getProcId (), that .getProcId ())
362
330
&& Objects .equals (getCurrentState (), that .getCurrentState ())
363
331
&& Objects .equals (getCycles (), that .getCycles ())
0 commit comments