11
11
import org .apache .logging .log4j .Logger ;
12
12
import org .elasticsearch .cluster .ClusterState ;
13
13
import org .elasticsearch .cluster .ClusterStateUpdateTask ;
14
+ import org .elasticsearch .cluster .ProjectState ;
14
15
import org .elasticsearch .cluster .metadata .IndexMetadata ;
15
16
import org .elasticsearch .cluster .metadata .LifecycleExecutionState ;
16
- import org .elasticsearch .cluster .metadata .Metadata ;
17
17
import org .elasticsearch .common .Strings ;
18
18
import org .elasticsearch .index .Index ;
19
19
import org .elasticsearch .xcontent .ToXContentObject ;
@@ -84,22 +84,22 @@ Step.StepKey getNextStepKey() {
84
84
* @throws IOException if any exceptions occur
85
85
*/
86
86
@ Override
87
- public ClusterState doExecute (final ClusterState currentState ) throws IOException {
87
+ public ClusterState doExecute (final ProjectState currentState ) throws IOException {
88
88
Step currentStep = startStep ;
89
- IndexMetadata indexMetadata = currentState .metadata ().getProject (). index (index );
89
+ IndexMetadata indexMetadata = currentState .metadata ().index (index );
90
90
if (indexMetadata == null ) {
91
91
logger .debug ("lifecycle for index [{}] executed but index no longer exists" , index .getName ());
92
92
// This index doesn't exist any more, there's nothing to execute currently
93
- return currentState ;
93
+ return currentState . cluster () ;
94
94
}
95
95
Step registeredCurrentStep = IndexLifecycleRunner .getCurrentStep (policyStepsRegistry , policy , indexMetadata );
96
96
if (currentStep .equals (registeredCurrentStep ) == false ) {
97
97
// either we are no longer the master or the step is now
98
98
// not the same as when we submitted the update task. In
99
99
// either case we don't want to do anything now
100
- return currentState ;
100
+ return currentState . cluster () ;
101
101
}
102
- ClusterState state = currentState ;
102
+ ProjectState state = currentState ;
103
103
// We can do cluster state steps all together until we
104
104
// either get to a step that isn't a cluster state step or a
105
105
// cluster state wait step returns not completed
@@ -114,7 +114,7 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
114
114
return moveToErrorStep (state , currentStep .getKey (), exception );
115
115
}
116
116
if (nextStepKey == null ) {
117
- return state ;
117
+ return state . cluster () ;
118
118
} else {
119
119
state = moveToNextStep (state );
120
120
}
@@ -123,14 +123,14 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
123
123
// loop, if we are about to go into a new phase, return so that
124
124
// other processing can occur
125
125
if (currentStep .getKey ().phase ().equals (currentStep .getNextStepKey ().phase ()) == false ) {
126
- return state ;
126
+ return state . cluster () ;
127
127
}
128
128
currentStep = policyStepsRegistry .getStep (indexMetadata , currentStep .getNextStepKey ());
129
129
}
130
- return state ;
130
+ return state . cluster () ;
131
131
}
132
132
133
- private ClusterState executeActionStep (ClusterState state , Step currentStep ) {
133
+ private ProjectState executeActionStep (ProjectState state , Step currentStep ) {
134
134
// cluster state action step so do the action and
135
135
// move the cluster state to the next step
136
136
logger .trace (
@@ -140,7 +140,7 @@ private ClusterState executeActionStep(ClusterState state, Step currentStep) {
140
140
currentStep .getKey ()
141
141
);
142
142
ClusterStateActionStep actionStep = (ClusterStateActionStep ) currentStep ;
143
- state = actionStep .performAction (index , state . projectState ()). cluster ( );
143
+ state = actionStep .performAction (index , state );
144
144
// If this step (usually a CopyExecutionStateStep step) has brought the
145
145
// index to where it needs to have async actions invoked, then add that
146
146
// index to the list so that when the new cluster state has been
@@ -153,7 +153,7 @@ private ClusterState executeActionStep(ClusterState state, Step currentStep) {
153
153
return state ;
154
154
}
155
155
156
- private ClusterState executeWaitStep (ClusterState state , Step currentStep ) {
156
+ private ProjectState executeWaitStep (ProjectState state , Step currentStep ) {
157
157
// cluster state wait step so evaluate the
158
158
// condition, if the condition is met move to the
159
159
// next step, if its not met return the current
@@ -166,7 +166,7 @@ private ClusterState executeWaitStep(ClusterState state, Step currentStep) {
166
166
currentStep .getClass ().getSimpleName (),
167
167
currentStep .getKey ()
168
168
);
169
- ClusterStateWaitStep .Result result = ((ClusterStateWaitStep ) currentStep ).isConditionMet (index , state . projectState () );
169
+ ClusterStateWaitStep .Result result = ((ClusterStateWaitStep ) currentStep ).isConditionMet (index , state );
170
170
// some steps can decide to change the next step to execute after waiting for some time for the condition
171
171
// to be met (eg. {@link LifecycleSettings#LIFECYCLE_STEP_WAIT_TIME_THRESHOLD_SETTING}, so it's important we
172
172
// re-evaluate what the next step is after we evaluate the condition
@@ -198,35 +198,23 @@ private ClusterState executeWaitStep(ClusterState state, Step currentStep) {
198
198
if (stepInfo == null ) {
199
199
return state ;
200
200
}
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 ));
204
202
}
205
203
}
206
204
207
- private ClusterState moveToNextStep (ClusterState state ) {
205
+ private ProjectState moveToNextStep (ProjectState state ) {
208
206
if (nextStepKey == null ) {
209
207
return state ;
210
208
}
211
209
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
+ );
224
213
}
225
214
226
215
@ 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 );
230
218
if (indexMetadata != null ) {
231
219
232
220
LifecycleExecutionState exState = indexMetadata .getLifecycleExecutionState ();
@@ -246,16 +234,16 @@ public void onClusterStateProcessed(ClusterState newState) {
246
234
// After the cluster state has been processed and we have moved
247
235
// to a new step, we need to conditionally execute the step iff
248
236
// 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 );
250
238
}
251
239
}
252
240
assert indexToStepKeysForAsyncActions .size () <= 1 : "we expect a maximum of one single spawned index currently" ;
253
241
for (Map .Entry <String , Step .StepKey > indexAndStepKey : indexToStepKeysForAsyncActions .entrySet ()) {
254
242
final String indexName = indexAndStepKey .getKey ();
255
243
final Step .StepKey nextStep = indexAndStepKey .getValue ();
256
- final IndexMetadata indexMeta = metadata . getProject ().index (indexName );
244
+ final IndexMetadata indexMeta = newState . metadata ().index (indexName );
257
245
if (indexMeta != null ) {
258
- if (newState .metadata ().getProject (). isIndexManagedByILM (indexMeta )) {
246
+ if (newState .metadata ().isIndexManagedByILM (indexMeta )) {
259
247
if (nextStep != null && nextStep != TerminalPolicyStep .KEY ) {
260
248
logger .trace (
261
249
"[{}] index has been spawed from a different index's ({}) "
@@ -265,7 +253,7 @@ public void onClusterStateProcessed(ClusterState newState) {
265
253
nextStep
266
254
);
267
255
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 );
269
257
}
270
258
}
271
259
}
@@ -277,7 +265,7 @@ public void handleFailure(Exception e) {
277
265
logger .warn (() -> format ("policy [%s] for index [%s] failed on step [%s]." , policy , index , startStep .getKey ()), e );
278
266
}
279
267
280
- private ClusterState moveToErrorStep (final ClusterState state , Step .StepKey currentStepKey , Exception cause ) {
268
+ private ClusterState moveToErrorStep (final ProjectState state , Step .StepKey currentStepKey , Exception cause ) {
281
269
this .failure = cause ;
282
270
logger .warn (
283
271
() -> format (
@@ -288,12 +276,9 @@ private ClusterState moveToErrorStep(final ClusterState state, Step.StepKey curr
288
276
),
289
277
cause
290
278
);
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
+ );
297
282
}
298
283
299
284
@ Override
0 commit comments