1212import org .elasticsearch .ElasticsearchException ;
1313import org .elasticsearch .action .support .TransportAction ;
1414import org .elasticsearch .client .internal .Client ;
15- import org .elasticsearch .cluster .ClusterState ;
1615import org .elasticsearch .cluster .metadata .IndexMetadata ;
1716import org .elasticsearch .cluster .metadata .LifecycleExecutionState ;
1817import org .elasticsearch .cluster .metadata .ProjectMetadata ;
1918import org .elasticsearch .common .Strings ;
2019import org .elasticsearch .common .settings .Settings ;
21- import org .elasticsearch .core .Nullable ;
2220import org .elasticsearch .index .Index ;
2321import org .elasticsearch .license .XPackLicenseState ;
2422import org .elasticsearch .xcontent .ToXContentObject ;
2523import org .elasticsearch .xpack .core .ilm .ErrorStep ;
2624import org .elasticsearch .xpack .core .ilm .IndexLifecycleMetadata ;
2725import org .elasticsearch .xpack .core .ilm .InitializePolicyContextStep ;
2826import org .elasticsearch .xpack .core .ilm .InitializePolicyException ;
29- import org .elasticsearch .xpack .core .ilm .LifecycleExecutionStateUtils ;
3027import org .elasticsearch .xpack .core .ilm .LifecyclePolicy ;
3128import org .elasticsearch .xpack .core .ilm .LifecyclePolicyMetadata ;
3229import org .elasticsearch .xpack .core .ilm .LifecycleSettings ;
@@ -113,7 +110,7 @@ public static void validateTransition(
113110 * @param nowSupplier The current-time supplier for updating when steps changed
114111 * @param stepRegistry The steps registry to check a step-key's existence in the index's current policy
115112 * @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>
117114 */
118115 static ProjectMetadata moveIndexToStep (
119116 Index index ,
@@ -149,15 +146,15 @@ static ProjectMetadata moveIndexToStep(
149146 * Moves the given index into the ERROR step. The ERROR step will have the same phase and
150147 * action, but use the {@link ErrorStep#NAME} as the name in the lifecycle execution state.
151148 */
152- static ClusterState moveClusterStateToErrorStep (
149+ static ProjectMetadata moveIndexToErrorStep (
153150 Index index ,
154- ClusterState clusterState ,
151+ ProjectMetadata project ,
155152 Exception cause ,
156153 LongSupplier nowSupplier ,
157154 BiFunction <IndexMetadata , Step .StepKey , Step > stepLookupFunction
158155 ) {
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 );
161158 LifecyclePolicyMetadata policyMetadata = ilmMeta .getPolicyMetadatas ().get (idxMeta .getLifecyclePolicyName ());
162159 LifecycleExecutionState currentState = idxMeta .getLifecycleExecutionState ();
163160 Step .StepKey currentStep ;
@@ -204,22 +201,21 @@ static ClusterState moveClusterStateToErrorStep(
204201 );
205202 }
206203
207- return LifecycleExecutionStateUtils . newClusterStateWithLifecycleState ( clusterState , idxMeta .getIndex (), failedState .build ());
204+ return project . withLifecycleState ( idxMeta .getIndex (), failedState .build ());
208205 }
209206
210207 /**
211208 * Move the given index's execution state back to a step that had previously failed. If this is
212209 * an automatic retry ({@code isAutomaticRetry}), the retry count is incremented.
213210 */
214- static ClusterState moveClusterStateToPreviouslyFailedStep (
215- ClusterState currentState ,
211+ static ProjectMetadata moveIndexToPreviouslyFailedStep (
212+ ProjectMetadata project ,
216213 String index ,
217214 LongSupplier nowSupplier ,
218215 PolicyStepsRegistry stepRegistry ,
219216 boolean isAutomaticRetry
220217 ) {
221- ClusterState newState ;
222- IndexMetadata indexMetadata = currentState .metadata ().getProject ().index (index );
218+ IndexMetadata indexMetadata = project .index (index );
223219 if (indexMetadata == null ) {
224220 throw new IllegalArgumentException ("index [" + index + "] does not exist" );
225221 }
@@ -229,7 +225,7 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(
229225 if (currentStepKey != null && ErrorStep .NAME .equals (currentStepKey .name ()) && Strings .isNullOrEmpty (failedStep ) == false ) {
230226 Step .StepKey nextStepKey = new Step .StepKey (currentStepKey .phase (), currentStepKey .action (), failedStep );
231227 validateTransition (indexMetadata , currentStepKey , nextStepKey , stepRegistry );
232- IndexLifecycleMetadata ilmMeta = currentState . metadata (). getProject () .custom (IndexLifecycleMetadata .TYPE );
228+ IndexLifecycleMetadata ilmMeta = project .custom (IndexLifecycleMetadata .TYPE );
233229
234230 LifecyclePolicyMetadata policyMetadata = ilmMeta .getPolicyMetadatas ().get (indexMetadata .getLifecyclePolicyName ());
235231
@@ -259,17 +255,12 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(
259255 // manual retries don't update the retry count
260256 retryStepState .setFailedStepRetryCount (lifecycleState .failedStepRetryCount ());
261257 }
262- newState = LifecycleExecutionStateUtils .newClusterStateWithLifecycleState (
263- currentState ,
264- indexMetadata .getIndex (),
265- retryStepState .build ()
266- );
258+ return project .withLifecycleState (indexMetadata .getIndex (), retryStepState .build ());
267259 } else {
268260 throw new IllegalArgumentException (
269261 "cannot retry an action for an index [" + index + "] that has not encountered an error when running a Lifecycle Policy"
270262 );
271263 }
272- return newState ;
273264 }
274265
275266 /**
@@ -413,53 +404,31 @@ public static LifecycleExecutionState moveStateToNextActionAndUpdateCachedPhase(
413404 }
414405
415406 /**
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
418409 * 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
425410 */
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 );
428413 if (indexMetadata == null ) {
429414 // This index doesn't exist anymore, we can't do anything
430- return clusterState ;
415+ return project ;
431416 }
432417 LifecycleExecutionState lifecycleState = indexMetadata .getLifecycleExecutionState ();
433418 final String stepInfoString = Strings .toString (stepInfo );
434419 if (stepInfoString .equals (lifecycleState .stepInfo ())) {
435- return clusterState ;
420+ return project ;
436421 }
437422 LifecycleExecutionState .Builder newState = LifecycleExecutionState .builder (lifecycleState );
438423 newState .setStepInfo (stepInfoString );
439- return LifecycleExecutionStateUtils . newClusterStateWithLifecycleState ( clusterState , indexMetadata . getIndex () , newState .build ());
424+ return project . withLifecycleState ( index , newState .build ());
440425 }
441426
442427 /**
443428 * Remove the ILM policy from the given indices, this removes the lifecycle setting as well as
444429 * any lifecycle execution state that may be present in the index metadata
445430 */
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 (
463432 final Index [] indices ,
464433 ProjectMetadata currentProject ,
465434 List <String > failedIndexes
@@ -481,7 +450,7 @@ private static ProjectMetadata.Builder removePolicyForIndexes(
481450 }
482451 }
483452
484- return newProject ;
453+ return newProject == null ? currentProject : newProject . build () ;
485454 }
486455
487456 /**
0 commit comments