Skip to content

Commit fe5da7e

Browse files
authored
Improve LifecycleExecutionState parsing. (#77855) (#77860)
This change improves the parsing of LifecycleExecutionState from IndexMetadata custom data by avoiding containsKey(...) call and in case there is no custom data then return a blank LifecycleExecutionState instance. Relates to #77466
1 parent 39d7bbb commit fe5da7e

File tree

1 file changed

+58
-36
lines changed

1 file changed

+58
-36
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionState.java

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class LifecycleExecutionState {
4343
private static final String SHRINK_INDEX_NAME ="shrink_index_name";
4444
private static final String ROLLUP_INDEX_NAME = "rollup_index_name";
4545

46+
public static final LifecycleExecutionState EMPTY_STATE = LifecycleExecutionState.builder().build();
47+
4648
private final String phase;
4749
private final String action;
4850
private final String step;
@@ -93,8 +95,11 @@ private LifecycleExecutionState(String phase, String action, String step, String
9395
*/
9496
public static LifecycleExecutionState fromIndexMetadata(IndexMetadata indexMetadata) {
9597
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+
}
98103
}
99104

100105
/**
@@ -160,76 +165,93 @@ public static Builder builder(LifecycleExecutionState state) {
160165

161166
static LifecycleExecutionState fromCustomMetadata(Map<String, String> customData) {
162167
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);
165171
}
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);
168175
}
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);
171179
}
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);
174183
}
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));
177187
}
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));
180191
}
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);
183195
}
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);
186199
}
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);
189203
}
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);
192207
}
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);
195211
}
196-
if (customData.containsKey(INDEX_CREATION_DATE)) {
212+
String indexCreationDate = customData.get(INDEX_CREATION_DATE);
213+
if (indexCreationDate != null) {
197214
try {
198-
builder.setIndexCreationDate(Long.parseLong(customData.get(INDEX_CREATION_DATE)));
215+
builder.setIndexCreationDate(Long.parseLong(indexCreationDate));
199216
} catch (NumberFormatException e) {
200217
throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]",
201218
e, INDEX_CREATION_DATE, customData.get(INDEX_CREATION_DATE));
202219
}
203220
}
204-
if (customData.containsKey(PHASE_TIME)) {
221+
String phaseTime = customData.get(PHASE_TIME);
222+
if (phaseTime != null) {
205223
try {
206-
builder.setPhaseTime(Long.parseLong(customData.get(PHASE_TIME)));
224+
builder.setPhaseTime(Long.parseLong(phaseTime));
207225
} catch (NumberFormatException e) {
208226
throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]",
209227
e, PHASE_TIME, customData.get(PHASE_TIME));
210228
}
211229
}
212-
if (customData.containsKey(ACTION_TIME)) {
230+
String actionTime = customData.get(ACTION_TIME);
231+
if (actionTime != null) {
213232
try {
214-
builder.setActionTime(Long.parseLong(customData.get(ACTION_TIME)));
233+
builder.setActionTime(Long.parseLong(actionTime));
215234
} catch (NumberFormatException e) {
216235
throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]",
217236
e, ACTION_TIME, customData.get(ACTION_TIME));
218237
}
219238
}
220-
if (customData.containsKey(STEP_TIME)) {
239+
String stepTime = customData.get(STEP_TIME);
240+
if (stepTime != null) {
221241
try {
222-
builder.setStepTime(Long.parseLong(customData.get(STEP_TIME)));
242+
builder.setStepTime(Long.parseLong(stepTime));
223243
} catch (NumberFormatException e) {
224244
throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]",
225245
e, STEP_TIME, customData.get(STEP_TIME));
226246
}
227247
}
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);
230251
}
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);
233255
}
234256
return builder.build();
235257
}

0 commit comments

Comments
 (0)