Skip to content

Commit 74a9407

Browse files
authored
Don't halt policy execution on policy trigger exception (#49128)
When triggered either by becoming master, a new cluster state, or a periodic schedule, an ILM policy execution through `maybeRunAsyncAction`, `runPolicyAfterStateChange`, or `runPeriodicStep` throwing an exception will cause the loop the terminate. This means that any indices that would have been processed after the index where the exception was thrown will not be processed by ILM. For most execution this is not a problem because the actual running of steps is protected by a try/catch that moves the index to the ERROR step in the event of a problem. If an exception occurs prior to step execution (for example, in fetching and parsing the current policy/step) however, it causes the loop termination previously mentioned. This commit wraps the invocation of the methods specified above in a try/catch block that provides better logging and does not bubble the exception up.
1 parent 49be465 commit 74a9407

File tree

3 files changed

+164
-25
lines changed

3 files changed

+164
-25
lines changed

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

+55-23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.carrotsearch.hppc.cursors.ObjectCursor;
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
11+
import org.apache.logging.log4j.message.ParameterizedMessage;
1112
import org.apache.lucene.util.SetOnce;
1213
import org.elasticsearch.client.Client;
1314
import org.elasticsearch.cluster.ClusterChangedEvent;
@@ -119,19 +120,35 @@ public void onMaster() {
119120
final LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMeta);
120121
StepKey stepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState);
121122

122-
if (OperationMode.STOPPING == currentMode) {
123-
if (stepKey != null && IGNORE_STEPS_MAINTENANCE_REQUESTED.contains(stepKey.getName())) {
124-
logger.info("waiting to stop ILM because index [{}] with policy [{}] is currently in step [{}]",
125-
idxMeta.getIndex().getName(), policyName, stepKey.getName());
123+
try {
124+
if (OperationMode.STOPPING == currentMode) {
125+
if (stepKey != null && IGNORE_STEPS_MAINTENANCE_REQUESTED.contains(stepKey.getName())) {
126+
logger.info("waiting to stop ILM because index [{}] with policy [{}] is currently in step [{}]",
127+
idxMeta.getIndex().getName(), policyName, stepKey.getName());
128+
lifecycleRunner.maybeRunAsyncAction(clusterState, idxMeta, policyName, stepKey);
129+
// ILM is trying to stop, but this index is in a Shrink step (or other dangerous step) so we can't stop
130+
safeToStop = false;
131+
} else {
132+
logger.info("skipping policy execution of step [{}] for index [{}] with policy [{}]" +
133+
" because ILM is stopping",
134+
stepKey == null ? "n/a" : stepKey.getName(), idxMeta.getIndex().getName(), policyName);
135+
}
136+
} else {
126137
lifecycleRunner.maybeRunAsyncAction(clusterState, idxMeta, policyName, stepKey);
127-
// ILM is trying to stop, but this index is in a Shrink step (or other dangerous step) so we can't stop
128-
safeToStop = false;
138+
}
139+
} catch (Exception e) {
140+
if (logger.isTraceEnabled()) {
141+
logger.warn(new ParameterizedMessage("async action execution failed during master election trigger" +
142+
" for index [{}] with policy [{}] in step [{}], lifecycle state: [{}]",
143+
idxMeta.getIndex().getName(), policyName, stepKey, lifecycleState.asMap()), e);
129144
} else {
130-
logger.info("skipping policy execution of step [{}] for index [{}] with policy [{}] because ILM is stopping",
131-
stepKey == null ? "n/a" : stepKey.getName(), idxMeta.getIndex().getName(), policyName);
145+
logger.warn(new ParameterizedMessage("async action execution failed during master election trigger" +
146+
" for index [{}] with policy [{}] in step [{}]",
147+
idxMeta.getIndex().getName(), policyName, stepKey), e);
148+
132149
}
133-
} else {
134-
lifecycleRunner.maybeRunAsyncAction(clusterState, idxMeta, policyName, stepKey);
150+
// Don't rethrow the exception, we don't want a failure for one index to be
151+
// called to cause actions not to be triggered for further indices
135152
}
136153
}
137154
}
@@ -264,27 +281,42 @@ void triggerPolicies(ClusterState clusterState, boolean fromClusterStateChange)
264281
final LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMeta);
265282
StepKey stepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState);
266283

267-
if (OperationMode.STOPPING == currentMode) {
268-
if (stepKey != null && IGNORE_STEPS_MAINTENANCE_REQUESTED.contains(stepKey.getName())) {
269-
logger.info("waiting to stop ILM because index [{}] with policy [{}] is currently in step [{}]",
270-
idxMeta.getIndex().getName(), policyName, stepKey.getName());
284+
try {
285+
if (OperationMode.STOPPING == currentMode) {
286+
if (stepKey != null && IGNORE_STEPS_MAINTENANCE_REQUESTED.contains(stepKey.getName())) {
287+
logger.info("waiting to stop ILM because index [{}] with policy [{}] is currently in step [{}]",
288+
idxMeta.getIndex().getName(), policyName, stepKey.getName());
289+
if (fromClusterStateChange) {
290+
lifecycleRunner.runPolicyAfterStateChange(policyName, idxMeta);
291+
} else {
292+
lifecycleRunner.runPeriodicStep(policyName, idxMeta);
293+
}
294+
// ILM is trying to stop, but this index is in a Shrink step (or other dangerous step) so we can't stop
295+
safeToStop = false;
296+
} else {
297+
logger.info("skipping policy execution of step [{}] for index [{}] with policy [{}] because ILM is stopping",
298+
stepKey == null ? "n/a" : stepKey.getName(), idxMeta.getIndex().getName(), policyName);
299+
}
300+
} else {
271301
if (fromClusterStateChange) {
272302
lifecycleRunner.runPolicyAfterStateChange(policyName, idxMeta);
273303
} else {
274304
lifecycleRunner.runPeriodicStep(policyName, idxMeta);
275305
}
276-
// ILM is trying to stop, but this index is in a Shrink step (or other dangerous step) so we can't stop
277-
safeToStop = false;
278-
} else {
279-
logger.info("skipping policy execution of step [{}] for index [{}] with policy [{}] because ILM is stopping",
280-
stepKey == null ? "n/a" : stepKey.getName(), idxMeta.getIndex().getName(), policyName);
281306
}
282-
} else {
283-
if (fromClusterStateChange) {
284-
lifecycleRunner.runPolicyAfterStateChange(policyName, idxMeta);
307+
} catch (Exception e) {
308+
if (logger.isTraceEnabled()) {
309+
logger.warn(new ParameterizedMessage("async action execution failed during policy trigger" +
310+
" for index [{}] with policy [{}] in step [{}], lifecycle state: [{}]",
311+
idxMeta.getIndex().getName(), policyName, stepKey, lifecycleState.asMap()), e);
285312
} else {
286-
lifecycleRunner.runPeriodicStep(policyName, idxMeta);
313+
logger.warn(new ParameterizedMessage("async action execution failed during policy trigger" +
314+
" for index [{}] with policy [{}] in step [{}]",
315+
idxMeta.getIndex().getName(), policyName, stepKey), e);
316+
287317
}
318+
// Don't rethrow the exception, we don't want a failure for one index to be
319+
// called to cause actions not to be triggered for further indices
288320
}
289321
}
290322
}

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ private void assertClusterStateStepInfo(ClusterState oldClusterState, Index inde
15271527
assertEquals(newLifecycleState.getStepTime(), newLifecycleState.getStepTime());
15281528
}
15291529

1530-
private static class MockAsyncActionStep extends AsyncActionStep {
1530+
static class MockAsyncActionStep extends AsyncActionStep {
15311531

15321532
private Exception exception;
15331533
private boolean willComplete;
@@ -1576,7 +1576,7 @@ public void performAction(IndexMetaData indexMetaData, ClusterState currentState
15761576

15771577
}
15781578

1579-
private static class MockAsyncWaitStep extends AsyncWaitStep {
1579+
static class MockAsyncWaitStep extends AsyncWaitStep {
15801580

15811581
private Exception exception;
15821582
private boolean willComplete;

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleServiceTests.java

+107
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151
import java.util.SortedMap;
5252
import java.util.TreeMap;
5353
import java.util.UUID;
54+
import java.util.concurrent.CountDownLatch;
5455
import java.util.concurrent.ExecutorService;
56+
import java.util.concurrent.TimeUnit;
5557

5658
import static org.elasticsearch.node.Node.NODE_MASTER_SETTING;
5759
import static org.elasticsearch.xpack.core.ilm.AbstractStepTestCase.randomStepKey;
@@ -300,6 +302,111 @@ public void testRequestedStopOnSafeAction() {
300302
assertTrue(moveToMaintenance.get());
301303
}
302304

305+
public void testExceptionStillProcessesOtherIndices() {
306+
doTestExceptionStillProcessesOtherIndices(false);
307+
}
308+
309+
public void testExceptionStillProcessesOtherIndicesOnMaster() {
310+
doTestExceptionStillProcessesOtherIndices(true);
311+
}
312+
313+
@SuppressWarnings("unchecked")
314+
public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
315+
String policy1 = randomAlphaOfLengthBetween(1, 20);
316+
Step.StepKey i1currentStepKey = randomStepKey();
317+
final Step i1mockStep;
318+
if (useOnMaster) {
319+
i1mockStep = new IndexLifecycleRunnerTests.MockAsyncActionStep(i1currentStepKey, randomStepKey());
320+
} else {
321+
i1mockStep = new IndexLifecycleRunnerTests.MockClusterStateActionStep(i1currentStepKey, randomStepKey());
322+
}
323+
MockAction i1mockAction = new MockAction(Collections.singletonList(i1mockStep));
324+
Phase i1phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", i1mockAction));
325+
LifecyclePolicy i1policy = newTestLifecyclePolicy(policy1, Collections.singletonMap(i1phase.getName(), i1phase));
326+
Index index1 = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
327+
LifecycleExecutionState.Builder i1lifecycleState = LifecycleExecutionState.builder();
328+
i1lifecycleState.setPhase(i1currentStepKey.getPhase());
329+
i1lifecycleState.setAction(i1currentStepKey.getAction());
330+
i1lifecycleState.setStep(i1currentStepKey.getName());
331+
332+
String policy2 = randomValueOtherThan(policy1, () -> randomAlphaOfLengthBetween(1, 20));
333+
Step.StepKey i2currentStepKey = randomStepKey();
334+
final Step i2mockStep;
335+
if (useOnMaster) {
336+
i2mockStep = new IndexLifecycleRunnerTests.MockAsyncActionStep(i2currentStepKey, randomStepKey());
337+
} else {
338+
i2mockStep = new IndexLifecycleRunnerTests.MockClusterStateActionStep(i2currentStepKey, randomStepKey());
339+
}
340+
MockAction mockAction = new MockAction(Collections.singletonList(i2mockStep));
341+
Phase i2phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction));
342+
LifecyclePolicy i2policy = newTestLifecyclePolicy(policy1, Collections.singletonMap(i2phase.getName(), i1phase));
343+
Index index2 = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
344+
LifecycleExecutionState.Builder i2lifecycleState = LifecycleExecutionState.builder();
345+
i2lifecycleState.setPhase(i2currentStepKey.getPhase());
346+
i2lifecycleState.setAction(i2currentStepKey.getAction());
347+
i2lifecycleState.setStep(i2currentStepKey.getName());
348+
349+
CountDownLatch stepLatch = new CountDownLatch(2);
350+
boolean failStep1 = randomBoolean();
351+
if (useOnMaster) {
352+
((IndexLifecycleRunnerTests.MockAsyncActionStep) i1mockStep).setLatch(stepLatch);
353+
((IndexLifecycleRunnerTests.MockAsyncActionStep) i1mockStep)
354+
.setException(failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null);
355+
((IndexLifecycleRunnerTests.MockAsyncActionStep) i2mockStep).setLatch(stepLatch);
356+
((IndexLifecycleRunnerTests.MockAsyncActionStep) i2mockStep)
357+
.setException(failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2"));
358+
} else {
359+
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setLatch(stepLatch);
360+
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep)
361+
.setException(failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null);
362+
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setLatch(stepLatch);
363+
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep)
364+
.setException(failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2"));
365+
}
366+
367+
SortedMap<String, LifecyclePolicyMetadata> policyMap = new TreeMap<>();
368+
policyMap.put(policy1, new LifecyclePolicyMetadata(i1policy, Collections.emptyMap(),
369+
randomNonNegativeLong(), randomNonNegativeLong()));
370+
policyMap.put(policy2, new LifecyclePolicyMetadata(i2policy, Collections.emptyMap(),
371+
randomNonNegativeLong(), randomNonNegativeLong()));
372+
373+
IndexMetaData i1indexMetadata = IndexMetaData.builder(index1.getName())
374+
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policy1))
375+
.putCustom(ILM_CUSTOM_METADATA_KEY, i1lifecycleState.build().asMap())
376+
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
377+
IndexMetaData i2indexMetadata = IndexMetaData.builder(index2.getName())
378+
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policy1))
379+
.putCustom(ILM_CUSTOM_METADATA_KEY, i2lifecycleState.build().asMap())
380+
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
381+
ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder()
382+
.fPut(index1.getName(), i1indexMetadata)
383+
.fPut(index2.getName(), i2indexMetadata);
384+
385+
MetaData metaData = MetaData.builder()
386+
.putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING))
387+
.indices(indices.build())
388+
.persistentSettings(settings(Version.CURRENT).build())
389+
.build();
390+
391+
ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT)
392+
.metaData(metaData)
393+
.nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build())
394+
.build();
395+
396+
if (useOnMaster) {
397+
when(clusterService.state()).thenReturn(currentState);
398+
indexLifecycleService.onMaster();
399+
} else {
400+
indexLifecycleService.triggerPolicies(currentState, randomBoolean());
401+
}
402+
try {
403+
stepLatch.await(5, TimeUnit.SECONDS);
404+
} catch (InterruptedException e) {
405+
logger.error("failure while waiting for step execution", e);
406+
fail("both steps should have been executed, even with an exception");
407+
}
408+
}
409+
303410
public void testTriggeredDifferentJob() {
304411
Mockito.reset(clusterService);
305412
SchedulerEngine.Event schedulerEvent = new SchedulerEngine.Event("foo", randomLong(), randomLong());

0 commit comments

Comments
 (0)