1111import org .apache .logging .log4j .Logger ;
1212import org .elasticsearch .cluster .ClusterState ;
1313import org .elasticsearch .cluster .ClusterStateUpdateTask ;
14+ import org .elasticsearch .cluster .ProjectState ;
1415import org .elasticsearch .cluster .metadata .IndexMetadata ;
1516import org .elasticsearch .cluster .metadata .LifecycleExecutionState ;
16- import org .elasticsearch .cluster .metadata .Metadata ;
1717import org .elasticsearch .common .Strings ;
1818import org .elasticsearch .index .Index ;
1919import org .elasticsearch .xcontent .ToXContentObject ;
@@ -84,22 +84,22 @@ Step.StepKey getNextStepKey() {
8484 * @throws IOException if any exceptions occur
8585 */
8686 @ Override
87- public ClusterState doExecute (final ClusterState currentState ) throws IOException {
87+ public ClusterState doExecute (final ProjectState currentState ) throws IOException {
8888 Step currentStep = startStep ;
89- IndexMetadata indexMetadata = currentState .metadata ().getProject (). index (index );
89+ IndexMetadata indexMetadata = currentState .metadata ().index (index );
9090 if (indexMetadata == null ) {
9191 logger .debug ("lifecycle for index [{}] executed but index no longer exists" , index .getName ());
9292 // This index doesn't exist any more, there's nothing to execute currently
93- return currentState ;
93+ return currentState . cluster () ;
9494 }
9595 Step registeredCurrentStep = IndexLifecycleRunner .getCurrentStep (policyStepsRegistry , policy , indexMetadata );
9696 if (currentStep .equals (registeredCurrentStep ) == false ) {
9797 // either we are no longer the master or the step is now
9898 // not the same as when we submitted the update task. In
9999 // either case we don't want to do anything now
100- return currentState ;
100+ return currentState . cluster () ;
101101 }
102- ClusterState state = currentState ;
102+ ProjectState state = currentState ;
103103 // We can do cluster state steps all together until we
104104 // either get to a step that isn't a cluster state step or a
105105 // cluster state wait step returns not completed
@@ -114,7 +114,7 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
114114 return moveToErrorStep (state , currentStep .getKey (), exception );
115115 }
116116 if (nextStepKey == null ) {
117- return state ;
117+ return state . cluster () ;
118118 } else {
119119 state = moveToNextStep (state );
120120 }
@@ -123,14 +123,14 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
123123 // loop, if we are about to go into a new phase, return so that
124124 // other processing can occur
125125 if (currentStep .getKey ().phase ().equals (currentStep .getNextStepKey ().phase ()) == false ) {
126- return state ;
126+ return state . cluster () ;
127127 }
128128 currentStep = policyStepsRegistry .getStep (indexMetadata , currentStep .getNextStepKey ());
129129 }
130- return state ;
130+ return state . cluster () ;
131131 }
132132
133- private ClusterState executeActionStep (ClusterState state , Step currentStep ) {
133+ private ProjectState executeActionStep (ProjectState state , Step currentStep ) {
134134 // cluster state action step so do the action and
135135 // move the cluster state to the next step
136136 logger .trace (
@@ -140,7 +140,7 @@ private ClusterState executeActionStep(ClusterState state, Step currentStep) {
140140 currentStep .getKey ()
141141 );
142142 ClusterStateActionStep actionStep = (ClusterStateActionStep ) currentStep ;
143- state = actionStep .performAction (index , state . projectState ()). cluster ( );
143+ state = actionStep .performAction (index , state );
144144 // If this step (usually a CopyExecutionStateStep step) has brought the
145145 // index to where it needs to have async actions invoked, then add that
146146 // index to the list so that when the new cluster state has been
@@ -153,7 +153,7 @@ private ClusterState executeActionStep(ClusterState state, Step currentStep) {
153153 return state ;
154154 }
155155
156- private ClusterState executeWaitStep (ClusterState state , Step currentStep ) {
156+ private ProjectState executeWaitStep (ProjectState state , Step currentStep ) {
157157 // cluster state wait step so evaluate the
158158 // condition, if the condition is met move to the
159159 // next step, if its not met return the current
@@ -166,7 +166,7 @@ private ClusterState executeWaitStep(ClusterState state, Step currentStep) {
166166 currentStep .getClass ().getSimpleName (),
167167 currentStep .getKey ()
168168 );
169- ClusterStateWaitStep .Result result = ((ClusterStateWaitStep ) currentStep ).isConditionMet (index , state . projectState () );
169+ ClusterStateWaitStep .Result result = ((ClusterStateWaitStep ) currentStep ).isConditionMet (index , state );
170170 // some steps can decide to change the next step to execute after waiting for some time for the condition
171171 // to be met (eg. {@link LifecycleSettings#LIFECYCLE_STEP_WAIT_TIME_THRESHOLD_SETTING}, so it's important we
172172 // re-evaluate what the next step is after we evaluate the condition
@@ -198,35 +198,23 @@ private ClusterState executeWaitStep(ClusterState state, Step currentStep) {
198198 if (stepInfo == null ) {
199199 return state ;
200200 }
201- return ClusterState .builder (state )
202- .putProjectMetadata (IndexLifecycleTransition .addStepInfoToProject (index , state .metadata ().getProject (), stepInfo ))
203- .build ();
201+ return state .updateProject (IndexLifecycleTransition .addStepInfoToProject (index , state .metadata (), stepInfo ));
204202 }
205203 }
206204
207- private ClusterState moveToNextStep (ClusterState state ) {
205+ private ProjectState moveToNextStep (ProjectState state ) {
208206 if (nextStepKey == null ) {
209207 return state ;
210208 }
211209 logger .trace ("[{}] moving cluster state to next step [{}]" , index .getName (), nextStepKey );
212- return ClusterState .builder (state )
213- .putProjectMetadata (
214- IndexLifecycleTransition .moveIndexToStep (
215- index ,
216- state .metadata ().getProject (),
217- nextStepKey ,
218- nowSupplier ,
219- policyStepsRegistry ,
220- false
221- )
222- )
223- .build ();
210+ return state .updateProject (
211+ IndexLifecycleTransition .moveIndexToStep (index , state .metadata (), nextStepKey , nowSupplier , policyStepsRegistry , false )
212+ );
224213 }
225214
226215 @ Override
227- public void onClusterStateProcessed (ClusterState newState ) {
228- final Metadata metadata = newState .metadata ();
229- final IndexMetadata indexMetadata = metadata .getProject ().index (index );
216+ public void onClusterStateProcessed (ProjectState newState ) {
217+ final IndexMetadata indexMetadata = newState .metadata ().index (index );
230218 if (indexMetadata != null ) {
231219
232220 LifecycleExecutionState exState = indexMetadata .getLifecycleExecutionState ();
@@ -246,16 +234,16 @@ public void onClusterStateProcessed(ClusterState newState) {
246234 // After the cluster state has been processed and we have moved
247235 // to a new step, we need to conditionally execute the step iff
248236 // it is an `AsyncAction` so that it is executed exactly once.
249- lifecycleRunner .maybeRunAsyncAction (newState , indexMetadata , policy , nextStepKey );
237+ lifecycleRunner .maybeRunAsyncAction (newState . cluster () , indexMetadata , policy , nextStepKey );
250238 }
251239 }
252240 assert indexToStepKeysForAsyncActions .size () <= 1 : "we expect a maximum of one single spawned index currently" ;
253241 for (Map .Entry <String , Step .StepKey > indexAndStepKey : indexToStepKeysForAsyncActions .entrySet ()) {
254242 final String indexName = indexAndStepKey .getKey ();
255243 final Step .StepKey nextStep = indexAndStepKey .getValue ();
256- final IndexMetadata indexMeta = metadata . getProject ().index (indexName );
244+ final IndexMetadata indexMeta = newState . metadata ().index (indexName );
257245 if (indexMeta != null ) {
258- if (newState .metadata ().getProject (). isIndexManagedByILM (indexMeta )) {
246+ if (newState .metadata ().isIndexManagedByILM (indexMeta )) {
259247 if (nextStep != null && nextStep != TerminalPolicyStep .KEY ) {
260248 logger .trace (
261249 "[{}] index has been spawed from a different index's ({}) "
@@ -265,7 +253,7 @@ public void onClusterStateProcessed(ClusterState newState) {
265253 nextStep
266254 );
267255 final String policyName = LifecycleSettings .LIFECYCLE_NAME_SETTING .get (indexMeta .getSettings ());
268- lifecycleRunner .maybeRunAsyncAction (newState , indexMeta , policyName , nextStep );
256+ lifecycleRunner .maybeRunAsyncAction (newState . cluster () , indexMeta , policyName , nextStep );
269257 }
270258 }
271259 }
@@ -277,7 +265,7 @@ public void handleFailure(Exception e) {
277265 logger .warn (() -> format ("policy [%s] for index [%s] failed on step [%s]." , policy , index , startStep .getKey ()), e );
278266 }
279267
280- private ClusterState moveToErrorStep (final ClusterState state , Step .StepKey currentStepKey , Exception cause ) {
268+ private ClusterState moveToErrorStep (final ProjectState state , Step .StepKey currentStepKey , Exception cause ) {
281269 this .failure = cause ;
282270 logger .warn (
283271 () -> format (
@@ -288,12 +276,9 @@ private ClusterState moveToErrorStep(final ClusterState state, Step.StepKey curr
288276 ),
289277 cause
290278 );
291- final var project = state .metadata ().getProject ();
292- return ClusterState .builder (state )
293- .putProjectMetadata (
294- IndexLifecycleTransition .moveIndexToErrorStep (index , project , cause , nowSupplier , policyStepsRegistry ::getStep )
295- )
296- .build ();
279+ return state .updatedState (
280+ IndexLifecycleTransition .moveIndexToErrorStep (index , state .metadata (), cause , nowSupplier , policyStepsRegistry ::getStep )
281+ );
297282 }
298283
299284 @ Override
0 commit comments