12
12
import org .elasticsearch .ElasticsearchException ;
13
13
import org .elasticsearch .action .support .TransportAction ;
14
14
import org .elasticsearch .client .internal .Client ;
15
- import org .elasticsearch .cluster .ClusterState ;
16
15
import org .elasticsearch .cluster .metadata .IndexMetadata ;
17
16
import org .elasticsearch .cluster .metadata .LifecycleExecutionState ;
18
17
import org .elasticsearch .cluster .metadata .ProjectMetadata ;
19
18
import org .elasticsearch .common .Strings ;
20
19
import org .elasticsearch .common .settings .Settings ;
21
- import org .elasticsearch .core .Nullable ;
22
20
import org .elasticsearch .index .Index ;
23
21
import org .elasticsearch .license .XPackLicenseState ;
24
22
import org .elasticsearch .xcontent .ToXContentObject ;
25
23
import org .elasticsearch .xpack .core .ilm .ErrorStep ;
26
24
import org .elasticsearch .xpack .core .ilm .IndexLifecycleMetadata ;
27
25
import org .elasticsearch .xpack .core .ilm .InitializePolicyContextStep ;
28
26
import org .elasticsearch .xpack .core .ilm .InitializePolicyException ;
29
- import org .elasticsearch .xpack .core .ilm .LifecycleExecutionStateUtils ;
30
27
import org .elasticsearch .xpack .core .ilm .LifecyclePolicy ;
31
28
import org .elasticsearch .xpack .core .ilm .LifecyclePolicyMetadata ;
32
29
import org .elasticsearch .xpack .core .ilm .LifecycleSettings ;
@@ -113,7 +110,7 @@ public static void validateTransition(
113
110
* @param nowSupplier The current-time supplier for updating when steps changed
114
111
* @param stepRegistry The steps registry to check a step-key's existence in the index's current policy
115
112
* @param forcePhaseDefinitionRefresh Whether to force the phase JSON to be reread or not
116
- * @return The updated cluster state where the index moved to <code>newStepKey</code>
113
+ * @return The updated project metadata where the index moved to <code>newStepKey</code>
117
114
*/
118
115
static ProjectMetadata moveIndexToStep (
119
116
Index index ,
@@ -149,15 +146,15 @@ static ProjectMetadata moveIndexToStep(
149
146
* Moves the given index into the ERROR step. The ERROR step will have the same phase and
150
147
* action, but use the {@link ErrorStep#NAME} as the name in the lifecycle execution state.
151
148
*/
152
- static ClusterState moveClusterStateToErrorStep (
149
+ static ProjectMetadata moveIndexToErrorStep (
153
150
Index index ,
154
- ClusterState clusterState ,
151
+ ProjectMetadata project ,
155
152
Exception cause ,
156
153
LongSupplier nowSupplier ,
157
154
BiFunction <IndexMetadata , Step .StepKey , Step > stepLookupFunction
158
155
) {
159
- IndexMetadata idxMeta = clusterState . getMetadata (). getProject () .index (index );
160
- IndexLifecycleMetadata ilmMeta = clusterState . metadata (). getProject () .custom (IndexLifecycleMetadata .TYPE );
156
+ IndexMetadata idxMeta = project .index (index );
157
+ IndexLifecycleMetadata ilmMeta = project .custom (IndexLifecycleMetadata .TYPE );
161
158
LifecyclePolicyMetadata policyMetadata = ilmMeta .getPolicyMetadatas ().get (idxMeta .getLifecyclePolicyName ());
162
159
LifecycleExecutionState currentState = idxMeta .getLifecycleExecutionState ();
163
160
Step .StepKey currentStep ;
@@ -204,22 +201,21 @@ static ClusterState moveClusterStateToErrorStep(
204
201
);
205
202
}
206
203
207
- return LifecycleExecutionStateUtils . newClusterStateWithLifecycleState ( clusterState , idxMeta .getIndex (), failedState .build ());
204
+ return project . withLifecycleState ( idxMeta .getIndex (), failedState .build ());
208
205
}
209
206
210
207
/**
211
208
* Move the given index's execution state back to a step that had previously failed. If this is
212
209
* an automatic retry ({@code isAutomaticRetry}), the retry count is incremented.
213
210
*/
214
- static ClusterState moveClusterStateToPreviouslyFailedStep (
215
- ClusterState currentState ,
211
+ static ProjectMetadata moveIndexToPreviouslyFailedStep (
212
+ ProjectMetadata project ,
216
213
String index ,
217
214
LongSupplier nowSupplier ,
218
215
PolicyStepsRegistry stepRegistry ,
219
216
boolean isAutomaticRetry
220
217
) {
221
- ClusterState newState ;
222
- IndexMetadata indexMetadata = currentState .metadata ().getProject ().index (index );
218
+ IndexMetadata indexMetadata = project .index (index );
223
219
if (indexMetadata == null ) {
224
220
throw new IllegalArgumentException ("index [" + index + "] does not exist" );
225
221
}
@@ -229,7 +225,7 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(
229
225
if (currentStepKey != null && ErrorStep .NAME .equals (currentStepKey .name ()) && Strings .isNullOrEmpty (failedStep ) == false ) {
230
226
Step .StepKey nextStepKey = new Step .StepKey (currentStepKey .phase (), currentStepKey .action (), failedStep );
231
227
validateTransition (indexMetadata , currentStepKey , nextStepKey , stepRegistry );
232
- IndexLifecycleMetadata ilmMeta = currentState . metadata (). getProject () .custom (IndexLifecycleMetadata .TYPE );
228
+ IndexLifecycleMetadata ilmMeta = project .custom (IndexLifecycleMetadata .TYPE );
233
229
234
230
LifecyclePolicyMetadata policyMetadata = ilmMeta .getPolicyMetadatas ().get (indexMetadata .getLifecyclePolicyName ());
235
231
@@ -259,17 +255,12 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(
259
255
// manual retries don't update the retry count
260
256
retryStepState .setFailedStepRetryCount (lifecycleState .failedStepRetryCount ());
261
257
}
262
- newState = LifecycleExecutionStateUtils .newClusterStateWithLifecycleState (
263
- currentState ,
264
- indexMetadata .getIndex (),
265
- retryStepState .build ()
266
- );
258
+ return project .withLifecycleState (indexMetadata .getIndex (), retryStepState .build ());
267
259
} else {
268
260
throw new IllegalArgumentException (
269
261
"cannot retry an action for an index [" + index + "] that has not encountered an error when running a Lifecycle Policy"
270
262
);
271
263
}
272
- return newState ;
273
264
}
274
265
275
266
/**
@@ -413,53 +404,31 @@ public static LifecycleExecutionState moveStateToNextActionAndUpdateCachedPhase(
413
404
}
414
405
415
406
/**
416
- * Conditionally updates cluster state with new step info. The new cluster state is only
417
- * built if the step info has changed, otherwise the same old <code>clusterState </code> is
407
+ * Conditionally updates project metadata with new step info. The new project metadata is only
408
+ * built if the step info has changed, otherwise the same old <code>project </code> is
418
409
* returned
419
- *
420
- * @param index the index to modify
421
- * @param clusterState the cluster state to modify
422
- * @param stepInfo the new step info to update
423
- * @return Updated cluster state with <code>stepInfo</code> if changed, otherwise the same cluster state
424
- * if no changes to step info exist
425
410
*/
426
- static ClusterState addStepInfoToClusterState (Index index , ClusterState clusterState , ToXContentObject stepInfo ) {
427
- IndexMetadata indexMetadata = clusterState . getMetadata (). getProject () .index (index );
411
+ static ProjectMetadata addStepInfoToProject (Index index , ProjectMetadata project , ToXContentObject stepInfo ) {
412
+ IndexMetadata indexMetadata = project .index (index );
428
413
if (indexMetadata == null ) {
429
414
// This index doesn't exist anymore, we can't do anything
430
- return clusterState ;
415
+ return project ;
431
416
}
432
417
LifecycleExecutionState lifecycleState = indexMetadata .getLifecycleExecutionState ();
433
418
final String stepInfoString = Strings .toString (stepInfo );
434
419
if (stepInfoString .equals (lifecycleState .stepInfo ())) {
435
- return clusterState ;
420
+ return project ;
436
421
}
437
422
LifecycleExecutionState .Builder newState = LifecycleExecutionState .builder (lifecycleState );
438
423
newState .setStepInfo (stepInfoString );
439
- return LifecycleExecutionStateUtils . newClusterStateWithLifecycleState ( clusterState , indexMetadata . getIndex () , newState .build ());
424
+ return project . withLifecycleState ( index , newState .build ());
440
425
}
441
426
442
427
/**
443
428
* Remove the ILM policy from the given indices, this removes the lifecycle setting as well as
444
429
* any lifecycle execution state that may be present in the index metadata
445
430
*/
446
- public static ClusterState removePolicyForIndexes (final Index [] indices , ClusterState currentState , List <String > failedIndexes ) {
447
- final ProjectMetadata currentProject = currentState .metadata ().getProject ();
448
- final ProjectMetadata .Builder updatedProject = removePolicyForIndexes (indices , currentProject , failedIndexes );
449
-
450
- if (updatedProject == null ) {
451
- return currentState ;
452
- } else {
453
- return ClusterState .builder (currentState ).putProjectMetadata (updatedProject ).build ();
454
- }
455
- }
456
-
457
- /**
458
- * @return If one or more policies were removed, then a new builder representing the changed project state.
459
- * Otherwise {@code null} (if no changes were made)
460
- */
461
- @ Nullable
462
- private static ProjectMetadata .Builder removePolicyForIndexes (
431
+ public static ProjectMetadata removePolicyForIndexes (
463
432
final Index [] indices ,
464
433
ProjectMetadata currentProject ,
465
434
List <String > failedIndexes
@@ -481,7 +450,7 @@ private static ProjectMetadata.Builder removePolicyForIndexes(
481
450
}
482
451
}
483
452
484
- return newProject ;
453
+ return newProject == null ? currentProject : newProject . build () ;
485
454
}
486
455
487
456
/**
0 commit comments