Skip to content

Commit ef21fa2

Browse files
committed
Improve LifecycleExecutionState parsing.
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 elastic#77466
1 parent 9275eca commit ef21fa2

File tree

1 file changed

+57
-36
lines changed

1 file changed

+57
-36
lines changed

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

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ private LifecycleExecutionState(String phase, String action, String step, String
9393
*/
9494
public static LifecycleExecutionState fromIndexMetadata(IndexMetadata indexMetadata) {
9595
Map<String, String> customData = indexMetadata.getCustomData(ILM_CUSTOM_METADATA_KEY);
96-
customData = customData == null ? new HashMap<>() : customData;
97-
return fromCustomMetadata(customData);
96+
if (customData != null && customData.isEmpty() == false) {
97+
return fromCustomMetadata(customData);
98+
} else {
99+
return LifecycleExecutionState.builder().build();
100+
}
98101
}
99102

100103
/**
@@ -159,77 +162,95 @@ public static Builder builder(LifecycleExecutionState state) {
159162
}
160163

161164
static LifecycleExecutionState fromCustomMetadata(Map<String, String> customData) {
165+
assert customData.isEmpty() == false;
162166
Builder builder = builder();
163-
if (customData.containsKey(PHASE)) {
164-
builder.setPhase(customData.get(PHASE));
167+
String phase = customData.get(PHASE);
168+
if (phase != null) {
169+
builder.setPhase(phase);
165170
}
166-
if (customData.containsKey(ACTION)) {
167-
builder.setAction(customData.get(ACTION));
171+
String action = customData.get(ACTION);
172+
if (action != null) {
173+
builder.setAction(action);
168174
}
169-
if (customData.containsKey(STEP)) {
170-
builder.setStep(customData.get(STEP));
175+
String step = customData.get(STEP);
176+
if (step != null) {
177+
builder.setStep(step);
171178
}
172-
if (customData.containsKey(FAILED_STEP)) {
173-
builder.setFailedStep(customData.get(FAILED_STEP));
179+
String failedStep = customData.get(FAILED_STEP);
180+
if (failedStep != null) {
181+
builder.setFailedStep(failedStep);
174182
}
175-
if (customData.containsKey(IS_AUTO_RETRYABLE_ERROR)) {
176-
builder.setIsAutoRetryableError(Boolean.parseBoolean(customData.get(IS_AUTO_RETRYABLE_ERROR)));
183+
String isAutoRetryableError = customData.get(IS_AUTO_RETRYABLE_ERROR);
184+
if (isAutoRetryableError != null) {
185+
builder.setIsAutoRetryableError(Boolean.parseBoolean(isAutoRetryableError));
177186
}
178-
if (customData.containsKey(FAILED_STEP_RETRY_COUNT)) {
179-
builder.setFailedStepRetryCount(Integer.parseInt(customData.get(FAILED_STEP_RETRY_COUNT)));
187+
String failedStepRetryCount = customData.get(FAILED_STEP_RETRY_COUNT);
188+
if (failedStepRetryCount != null) {
189+
builder.setFailedStepRetryCount(Integer.parseInt(failedStepRetryCount));
180190
}
181-
if (customData.containsKey(STEP_INFO)) {
182-
builder.setStepInfo(customData.get(STEP_INFO));
191+
String stepInfo = customData.get(STEP_INFO);
192+
if (stepInfo != null) {
193+
builder.setStepInfo(stepInfo);
183194
}
184-
if (customData.containsKey(PHASE_DEFINITION)) {
185-
builder.setPhaseDefinition(customData.get(PHASE_DEFINITION));
195+
String phaseDefinition = customData.get(PHASE_DEFINITION);
196+
if (phaseDefinition != null) {
197+
builder.setPhaseDefinition(phaseDefinition);
186198
}
187-
if (customData.containsKey(SNAPSHOT_REPOSITORY)) {
188-
builder.setSnapshotRepository(customData.get(SNAPSHOT_REPOSITORY));
199+
String snapShotRepository = customData.get(SNAPSHOT_REPOSITORY);
200+
if (snapShotRepository != null) {
201+
builder.setSnapshotRepository(snapShotRepository);
189202
}
190-
if (customData.containsKey(SNAPSHOT_NAME)) {
191-
builder.setSnapshotName(customData.get(SNAPSHOT_NAME));
203+
String snapshotName = customData.get(SNAPSHOT_NAME);
204+
if (snapshotName != null) {
205+
builder.setSnapshotName(snapshotName);
192206
}
193-
if (customData.containsKey(SHRINK_INDEX_NAME)) {
194-
builder.setShrinkIndexName(customData.get(SHRINK_INDEX_NAME));
207+
String shrinkIndexName = customData.get(SHRINK_INDEX_NAME);
208+
if (shrinkIndexName != null) {
209+
builder.setShrinkIndexName(shrinkIndexName);
195210
}
196-
if (customData.containsKey(INDEX_CREATION_DATE)) {
211+
String indexCreationDate = customData.get(INDEX_CREATION_DATE);
212+
if (indexCreationDate != null) {
197213
try {
198-
builder.setIndexCreationDate(Long.parseLong(customData.get(INDEX_CREATION_DATE)));
214+
builder.setIndexCreationDate(Long.parseLong(indexCreationDate));
199215
} catch (NumberFormatException e) {
200216
throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]",
201217
e, INDEX_CREATION_DATE, customData.get(INDEX_CREATION_DATE));
202218
}
203219
}
204-
if (customData.containsKey(PHASE_TIME)) {
220+
String phaseTime = customData.get(PHASE_TIME);
221+
if (phaseTime != null) {
205222
try {
206-
builder.setPhaseTime(Long.parseLong(customData.get(PHASE_TIME)));
223+
builder.setPhaseTime(Long.parseLong(phaseTime));
207224
} catch (NumberFormatException e) {
208225
throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]",
209226
e, PHASE_TIME, customData.get(PHASE_TIME));
210227
}
211228
}
212-
if (customData.containsKey(ACTION_TIME)) {
229+
String actionTime = customData.get(ACTION_TIME);
230+
if (actionTime != null) {
213231
try {
214-
builder.setActionTime(Long.parseLong(customData.get(ACTION_TIME)));
232+
builder.setActionTime(Long.parseLong(actionTime));
215233
} catch (NumberFormatException e) {
216234
throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]",
217235
e, ACTION_TIME, customData.get(ACTION_TIME));
218236
}
219237
}
220-
if (customData.containsKey(STEP_TIME)) {
238+
String stepTime = customData.get(STEP_TIME);
239+
if (stepTime != null) {
221240
try {
222-
builder.setStepTime(Long.parseLong(customData.get(STEP_TIME)));
241+
builder.setStepTime(Long.parseLong(stepTime));
223242
} catch (NumberFormatException e) {
224243
throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]",
225244
e, STEP_TIME, customData.get(STEP_TIME));
226245
}
227246
}
228-
if (customData.containsKey(SNAPSHOT_INDEX_NAME)) {
229-
builder.setSnapshotIndexName(customData.get(SNAPSHOT_INDEX_NAME));
247+
String snapshotIndexName = customData.get(SNAPSHOT_INDEX_NAME);
248+
if (snapshotIndexName != null) {
249+
builder.setSnapshotIndexName(snapshotIndexName);
230250
}
231-
if (customData.containsKey(ROLLUP_INDEX_NAME)) {
232-
builder.setRollupIndexName(customData.get(ROLLUP_INDEX_NAME));
251+
String rollupIndexName = customData.get(ROLLUP_INDEX_NAME);
252+
if (rollupIndexName != null) {
253+
builder.setRollupIndexName(rollupIndexName);
233254
}
234255
return builder.build();
235256
}

0 commit comments

Comments
 (0)