Skip to content

Commit 8ea999e

Browse files
authored
Include stack trace with ILM error in explain output (#35512)
This changes the stacktrace to be included with the ILM explain error when the index is an on ERROR step. Before: ```json { "indices" : { "foo" : { "index" : "foo", "managed" : true, "policy" : "bad", "lifecycle_date_millis" : 1542131670601, "phase" : "warm", "phase_time_millis" : 1542131676335, "action" : "shrink", "action_time_millis" : 1542131676335, "step" : "ERROR", "step_time_millis" : 1542131676451, "failed_step" : "shrink", "step_info" : { "type" : "illegal_argument_exception", "reason" : "the number of target shards [13] must be less that the number of source shards [2]" }, "phase_execution" : { "policy" : "bad", "phase_definition" : { "min_age" : "5s", "actions" : { "shrink" : { "number_of_shards" : 13 } } }, "version" : 1, "modified_date_in_millis" : 1542131669839 } } } } ``` After ``` { "indices" : { "foo" : { "index" : "foo", "managed" : true, "policy" : "bad", "lifecycle_date_millis" : 1542131670601, "phase" : "warm", "phase_time_millis" : 1542131676335, "action" : "shrink", "action_time_millis" : 1542131676335, "step" : "ERROR", "step_time_millis" : 1542131676451, "failed_step" : "shrink", "step_info" : { "type" : "illegal_argument_exception", "reason" : "the number of target shards [13] must be less that the number of source shards [2]", "stack_trace" : "java.lang.IllegalArgumentException: the number of target shards [13] must be less that the number of source shards [2]\n\tat org.elasticsearch.cluster.metadata.IndexMetaData.selectShrinkShards(IndexMetaData.java:1509)\n\tat org.elasticsearch.action.admin.indices.shrink.TransportResizeAction.prepareCreateIndexRequest(TransportResizeAction.java:146)\n\tat org.elasticsearch.action.admin.indices.shrink.TransportResizeAction$1.onResponse(TransportResizeAction.java:104)\n\tat org.elasticsearch.action.admin.indices.shrink.TransportResizeAction$1.onResponse(TransportResizeAction.java:101)\n\tat org.elasticsearch.action.support.TransportAction$1.onResponse(TransportAction.java:64)\n\tat org.elasticsearch.action.support.TransportAction$1.onResponse(TransportAction.java:60)\n\tat org.elasticsearch.action.support.broadcast.node.TransportBroadcastByNodeAction$AsyncAction.onCompletion(TransportBroadcastByNodeAction.java:383)\n\tat org.elasticsearch.action.support.broadcast.node.TransportBroadcastByNodeAction$AsyncAction.onNodeResponse(TransportBroadcastByNodeAction.java:352)\n\tat org.elasticsearch.action.support.broadcast.node.TransportBroadcastByNodeAction$AsyncAction$1.handleResponse(TransportBroadcastByNodeAction.java:324)\n\tat org.elasticsearch.action.support.broadcast.node.TransportBroadcastByNodeAction$AsyncAction$1.handleResponse(TransportBroadcastByNodeAction.java:314)\n\tat org.elasticsearch.transport.TransportService$ContextRestoreResponseHandler.handleResponse(TransportService.java:1117)\n\tat org.elasticsearch.transport.TransportService$DirectResponseChannel.processResponse(TransportService.java:1198)\n\tat org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1178)\n\tat org.elasticsearch.transport.TaskTransportChannel.sendResponse(TaskTransportChannel.java:54)\n\tat org.elasticsearch.action.support.broadcast.node.TransportBroadcastByNodeAction$BroadcastByNodeTransportRequestHandler.messageReceived(TransportBroadcastByNodeAction.java:417)\n\tat org.elasticsearch.action.support.broadcast.node.TransportBroadcastByNodeAction$BroadcastByNodeTransportRequestHandler.messageReceived(TransportBroadcastByNodeAction.java:391)\n\tat org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler$1.doRun(SecurityServerTransportInterceptor.java:251)\n\tat org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)\n\tat org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler.messageReceived(SecurityServerTransportInterceptor.java:309)\n\tat org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:63)\n\tat org.elasticsearch.transport.TransportService$7.doRun(TransportService.java:714)\n\tat org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:726)\n\tat org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat java.base/java.lang.Thread.run(Thread.java:834)\n" }, "phase_execution" : { "policy" : "bad", "phase_definition" : { "min_age" : "5s", "actions" : { "shrink" : { "number_of_shards" : 13 } } }, "version" : 1, "modified_date_in_millis" : 1542131669839 } } } } ``` Resolves #35498
1 parent fc774a3 commit 8ea999e

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@
4242
import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep;
4343

4444
import java.io.IOException;
45+
import java.util.Collections;
4546
import java.util.List;
4647
import java.util.function.LongSupplier;
4748

49+
import static org.elasticsearch.ElasticsearchException.REST_EXCEPTION_SKIP_STACK_TRACE;
4850
import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
4951

5052
public class IndexLifecycleRunner {
5153
private static final Logger logger = LogManager.getLogger(IndexLifecycleRunner.class);
54+
private static final ToXContent.Params STACKTRACE_PARAMS =
55+
new ToXContent.MapParams(Collections.singletonMap(REST_EXCEPTION_SKIP_STACK_TRACE, "false"));
5256
private PolicyStepsRegistry stepRegistry;
5357
private ClusterService clusterService;
5458
private LongSupplier nowSupplier;
@@ -323,7 +327,7 @@ static ClusterState moveClusterStateToErrorStep(Index index, ClusterState cluste
323327
.get(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxMeta.getSettings()));
324328
XContentBuilder causeXContentBuilder = JsonXContent.contentBuilder();
325329
causeXContentBuilder.startObject();
326-
ElasticsearchException.generateThrowableXContent(causeXContentBuilder, ToXContent.EMPTY_PARAMS, cause);
330+
ElasticsearchException.generateThrowableXContent(causeXContentBuilder, STACKTRACE_PARAMS, cause);
327331
causeXContentBuilder.endObject();
328332
LifecycleExecutionState nextStepState = moveExecutionStateToNextStep(policyMetadata,
329333
LifecycleExecutionState.fromIndexMetadata(idxMeta), currentStep, new StepKey(currentStep.getPhase(),

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
5050
import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTestsUtils.newTestLifecyclePolicy;
51+
import static org.hamcrest.Matchers.containsString;
5152
import static org.hamcrest.Matchers.equalTo;
5253
import static org.hamcrest.Matchers.nullValue;
5354
import static org.hamcrest.Matchers.sameInstance;
@@ -270,7 +271,8 @@ public void testClusterActionStepThrowsException() throws IOException {
270271
assertThat(task.getNextStepKey(), equalTo(secondStep.getKey()));
271272
assertThat(lifecycleState.getPhaseTime(), nullValue());
272273
assertThat(lifecycleState.getActionTime(), nullValue());
273-
assertThat(lifecycleState.getStepInfo(), equalTo("{\"type\":\"runtime_exception\",\"reason\":\"error\"}"));
274+
assertThat(lifecycleState.getStepInfo(),
275+
containsString("{\"type\":\"runtime_exception\",\"reason\":\"error\",\"stack_trace\":\""));
274276
}
275277

276278
public void testClusterWaitStepThrowsException() throws IOException {
@@ -289,7 +291,8 @@ public void testClusterWaitStepThrowsException() throws IOException {
289291
assertThat(task.getNextStepKey(), equalTo(thirdStepKey));
290292
assertThat(lifecycleState.getPhaseTime(), nullValue());
291293
assertThat(lifecycleState.getActionTime(), nullValue());
292-
assertThat(lifecycleState.getStepInfo(), equalTo("{\"type\":\"runtime_exception\",\"reason\":\"error\"}"));
294+
assertThat(lifecycleState.getStepInfo(),
295+
containsString("{\"type\":\"runtime_exception\",\"reason\":\"error\",\"stack_trace\":\""));
293296
}
294297

295298
private void setStateToKey(StepKey stepKey) throws IOException {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373

7474
import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
7575
import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTestsUtils.newTestLifecyclePolicy;
76+
import static org.hamcrest.Matchers.containsString;
7677
import static org.hamcrest.Matchers.equalTo;
7778
import static org.mockito.Mockito.mock;
7879

@@ -853,12 +854,12 @@ public void testMoveClusterStateToErrorStep() throws IOException {
853854

854855
ClusterState newClusterState = IndexLifecycleRunner.moveClusterStateToErrorStep(index, clusterState, currentStep, cause, () -> now);
855856
assertClusterStateOnErrorStep(clusterState, index, currentStep, newClusterState, now,
856-
"{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\"}");
857+
"{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\"");
857858

858859
cause = new IllegalArgumentException("non elasticsearch-exception");
859860
newClusterState = IndexLifecycleRunner.moveClusterStateToErrorStep(index, clusterState, currentStep, cause, () -> now);
860861
assertClusterStateOnErrorStep(clusterState, index, currentStep, newClusterState, now,
861-
"{\"type\":\"illegal_argument_exception\",\"reason\":\"non elasticsearch-exception\"}");
862+
"{\"type\":\"illegal_argument_exception\",\"reason\":\"non elasticsearch-exception\",\"stack_trace\":\"");
862863
}
863864

864865
public void testMoveClusterStateToFailedStep() {
@@ -1267,7 +1268,7 @@ private void assertClusterStateOnErrorStep(ClusterState oldClusterState, Index i
12671268
assertEquals(currentStep.getAction(), newLifecycleState.getAction());
12681269
assertEquals(ErrorStep.NAME, newLifecycleState.getStep());
12691270
assertEquals(currentStep.getName(), newLifecycleState.getFailedStep());
1270-
assertEquals(expectedCauseValue, newLifecycleState.getStepInfo());
1271+
assertThat(newLifecycleState.getStepInfo(), containsString(expectedCauseValue));
12711272
assertEquals(oldLifecycleState.getPhaseTime(), newLifecycleState.getPhaseTime());
12721273
assertEquals(oldLifecycleState.getActionTime(), newLifecycleState.getActionTime());
12731274
assertEquals(now, newLifecycleState.getStepTime().longValue());

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Collections;
3434

3535
import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
36+
import static org.hamcrest.Matchers.containsString;
3637
import static org.hamcrest.Matchers.equalTo;
3738
import static org.hamcrest.Matchers.nullValue;
3839
import static org.hamcrest.Matchers.sameInstance;
@@ -86,7 +87,8 @@ public void testExecuteSuccessfullyMoved() throws IOException {
8687
ElasticsearchException.generateThrowableXContent(causeXContentBuilder, ToXContent.EMPTY_PARAMS, cause);
8788
causeXContentBuilder.endObject();
8889
String expectedCauseValue = BytesReference.bytes(causeXContentBuilder).utf8ToString();
89-
assertThat(lifecycleState.getStepInfo(), equalTo(expectedCauseValue));
90+
assertThat(lifecycleState.getStepInfo(),
91+
containsString("{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\",\"stack_trace\":\""));
9092
}
9193

9294
public void testExecuteNoopDifferentStep() throws IOException {

0 commit comments

Comments
 (0)