@@ -43,6 +43,8 @@ public class LifecycleExecutionState {
43
43
private static final String SHRINK_INDEX_NAME ="shrink_index_name" ;
44
44
private static final String ROLLUP_INDEX_NAME = "rollup_index_name" ;
45
45
46
+ public static final LifecycleExecutionState EMPTY_STATE = LifecycleExecutionState .builder ().build ();
47
+
46
48
private final String phase ;
47
49
private final String action ;
48
50
private final String step ;
@@ -93,8 +95,11 @@ private LifecycleExecutionState(String phase, String action, String step, String
93
95
*/
94
96
public static LifecycleExecutionState fromIndexMetadata (IndexMetadata indexMetadata ) {
95
97
Map <String , String > customData = indexMetadata .getCustomData (ILM_CUSTOM_METADATA_KEY );
96
- customData = customData == null ? new HashMap <>() : customData ;
97
- return fromCustomMetadata (customData );
98
+ if (customData != null && customData .isEmpty () == false ) {
99
+ return fromCustomMetadata (customData );
100
+ } else {
101
+ return EMPTY_STATE ;
102
+ }
98
103
}
99
104
100
105
/**
@@ -160,76 +165,93 @@ public static Builder builder(LifecycleExecutionState state) {
160
165
161
166
static LifecycleExecutionState fromCustomMetadata (Map <String , String > customData ) {
162
167
Builder builder = builder ();
163
- if (customData .containsKey (PHASE )) {
164
- builder .setPhase (customData .get (PHASE ));
168
+ String phase = customData .get (PHASE );
169
+ if (phase != null ) {
170
+ builder .setPhase (phase );
165
171
}
166
- if (customData .containsKey (ACTION )) {
167
- builder .setAction (customData .get (ACTION ));
172
+ String action = customData .get (ACTION );
173
+ if (action != null ) {
174
+ builder .setAction (action );
168
175
}
169
- if (customData .containsKey (STEP )) {
170
- builder .setStep (customData .get (STEP ));
176
+ String step = customData .get (STEP );
177
+ if (step != null ) {
178
+ builder .setStep (step );
171
179
}
172
- if (customData .containsKey (FAILED_STEP )) {
173
- builder .setFailedStep (customData .get (FAILED_STEP ));
180
+ String failedStep = customData .get (FAILED_STEP );
181
+ if (failedStep != null ) {
182
+ builder .setFailedStep (failedStep );
174
183
}
175
- if (customData .containsKey (IS_AUTO_RETRYABLE_ERROR )) {
176
- builder .setIsAutoRetryableError (Boolean .parseBoolean (customData .get (IS_AUTO_RETRYABLE_ERROR )));
184
+ String isAutoRetryableError = customData .get (IS_AUTO_RETRYABLE_ERROR );
185
+ if (isAutoRetryableError != null ) {
186
+ builder .setIsAutoRetryableError (Boolean .parseBoolean (isAutoRetryableError ));
177
187
}
178
- if (customData .containsKey (FAILED_STEP_RETRY_COUNT )) {
179
- builder .setFailedStepRetryCount (Integer .parseInt (customData .get (FAILED_STEP_RETRY_COUNT )));
188
+ String failedStepRetryCount = customData .get (FAILED_STEP_RETRY_COUNT );
189
+ if (failedStepRetryCount != null ) {
190
+ builder .setFailedStepRetryCount (Integer .parseInt (failedStepRetryCount ));
180
191
}
181
- if (customData .containsKey (STEP_INFO )) {
182
- builder .setStepInfo (customData .get (STEP_INFO ));
192
+ String stepInfo = customData .get (STEP_INFO );
193
+ if (stepInfo != null ) {
194
+ builder .setStepInfo (stepInfo );
183
195
}
184
- if (customData .containsKey (PHASE_DEFINITION )) {
185
- builder .setPhaseDefinition (customData .get (PHASE_DEFINITION ));
196
+ String phaseDefinition = customData .get (PHASE_DEFINITION );
197
+ if (phaseDefinition != null ) {
198
+ builder .setPhaseDefinition (phaseDefinition );
186
199
}
187
- if (customData .containsKey (SNAPSHOT_REPOSITORY )) {
188
- builder .setSnapshotRepository (customData .get (SNAPSHOT_REPOSITORY ));
200
+ String snapShotRepository = customData .get (SNAPSHOT_REPOSITORY );
201
+ if (snapShotRepository != null ) {
202
+ builder .setSnapshotRepository (snapShotRepository );
189
203
}
190
- if (customData .containsKey (SNAPSHOT_NAME )) {
191
- builder .setSnapshotName (customData .get (SNAPSHOT_NAME ));
204
+ String snapshotName = customData .get (SNAPSHOT_NAME );
205
+ if (snapshotName != null ) {
206
+ builder .setSnapshotName (snapshotName );
192
207
}
193
- if (customData .containsKey (SHRINK_INDEX_NAME )) {
194
- builder .setShrinkIndexName (customData .get (SHRINK_INDEX_NAME ));
208
+ String shrinkIndexName = customData .get (SHRINK_INDEX_NAME );
209
+ if (shrinkIndexName != null ) {
210
+ builder .setShrinkIndexName (shrinkIndexName );
195
211
}
196
- if (customData .containsKey (INDEX_CREATION_DATE )) {
212
+ String indexCreationDate = customData .get (INDEX_CREATION_DATE );
213
+ if (indexCreationDate != null ) {
197
214
try {
198
- builder .setIndexCreationDate (Long .parseLong (customData . get ( INDEX_CREATION_DATE ) ));
215
+ builder .setIndexCreationDate (Long .parseLong (indexCreationDate ));
199
216
} catch (NumberFormatException e ) {
200
217
throw new ElasticsearchException ("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]" ,
201
218
e , INDEX_CREATION_DATE , customData .get (INDEX_CREATION_DATE ));
202
219
}
203
220
}
204
- if (customData .containsKey (PHASE_TIME )) {
221
+ String phaseTime = customData .get (PHASE_TIME );
222
+ if (phaseTime != null ) {
205
223
try {
206
- builder .setPhaseTime (Long .parseLong (customData . get ( PHASE_TIME ) ));
224
+ builder .setPhaseTime (Long .parseLong (phaseTime ));
207
225
} catch (NumberFormatException e ) {
208
226
throw new ElasticsearchException ("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]" ,
209
227
e , PHASE_TIME , customData .get (PHASE_TIME ));
210
228
}
211
229
}
212
- if (customData .containsKey (ACTION_TIME )) {
230
+ String actionTime = customData .get (ACTION_TIME );
231
+ if (actionTime != null ) {
213
232
try {
214
- builder .setActionTime (Long .parseLong (customData . get ( ACTION_TIME ) ));
233
+ builder .setActionTime (Long .parseLong (actionTime ));
215
234
} catch (NumberFormatException e ) {
216
235
throw new ElasticsearchException ("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]" ,
217
236
e , ACTION_TIME , customData .get (ACTION_TIME ));
218
237
}
219
238
}
220
- if (customData .containsKey (STEP_TIME )) {
239
+ String stepTime = customData .get (STEP_TIME );
240
+ if (stepTime != null ) {
221
241
try {
222
- builder .setStepTime (Long .parseLong (customData . get ( STEP_TIME ) ));
242
+ builder .setStepTime (Long .parseLong (stepTime ));
223
243
} catch (NumberFormatException e ) {
224
244
throw new ElasticsearchException ("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]" ,
225
245
e , STEP_TIME , customData .get (STEP_TIME ));
226
246
}
227
247
}
228
- if (customData .containsKey (SNAPSHOT_INDEX_NAME )) {
229
- builder .setSnapshotIndexName (customData .get (SNAPSHOT_INDEX_NAME ));
248
+ String snapshotIndexName = customData .get (SNAPSHOT_INDEX_NAME );
249
+ if (snapshotIndexName != null ) {
250
+ builder .setSnapshotIndexName (snapshotIndexName );
230
251
}
231
- if (customData .containsKey (ROLLUP_INDEX_NAME )) {
232
- builder .setRollupIndexName (customData .get (ROLLUP_INDEX_NAME ));
252
+ String rollupIndexName = customData .get (ROLLUP_INDEX_NAME );
253
+ if (rollupIndexName != null ) {
254
+ builder .setRollupIndexName (rollupIndexName );
233
255
}
234
256
return builder .build ();
235
257
}
0 commit comments