From 90b38d9b3d383017cc7a34853d3f28018899f4a9 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Thu, 15 Nov 2018 09:29:26 -0700 Subject: [PATCH] [ILM] Avoid NullPointerException in CopyExecutionStateStep (#35568) In the event that the target index does not exist when `CopyExecutionStateStep` executes, this avoids a `NullPointerException` and provides a more helpful error to the ILM user. Resolves #35567 --- .../CopyExecutionStateStep.java | 9 +++++++- .../CopyExecutionStateStepTests.java | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java index b8192dd7e43..bce5431ac07 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java @@ -22,7 +22,7 @@ import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionStat * new index has been created. Useful for actions such as shrink. */ public class CopyExecutionStateStep extends ClusterStateActionStep { - public static final String NAME = "copy_execution_state"; + public static final String NAME = "copy-execution-state"; private static final Logger logger = LogManager.getLogger(CopyExecutionStateStep.class); @@ -52,6 +52,13 @@ public class CopyExecutionStateStep extends ClusterStateActionStep { String targetIndexName = shrunkIndexPrefix + indexName; IndexMetaData targetIndexMetaData = clusterState.metaData().index(targetIndexName); + if (targetIndexMetaData == null) { + logger.warn("[{}] index [{}] unable to copy execution state to target index [{}] as target index does not exist", + getKey().getAction(), index.getName(), targetIndexName); + throw new IllegalStateException("unable to copy execution state from [" + index.getName() + + "] to [" + targetIndexName + "] as target index does not exist"); + } + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); String phase = lifecycleState.getPhase(); String action = lifecycleState.getAction(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java index 40dd022c05d..ef1655d99bc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java @@ -17,6 +17,7 @@ import java.util.Map; import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionStateTests.createCustomMetadata; +import static org.hamcrest.Matchers.equalTo; public class CopyExecutionStateStepTests extends AbstractStepTestCase { @Override @@ -86,4 +87,25 @@ public class CopyExecutionStateStepTests extends AbstractStepTestCase customMetadata = createCustomMetadata(); + + IndexMetaData originalIndexMetaData = IndexMetaData.builder(indexName) + .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5)) + .numberOfReplicas(randomIntBetween(1,5)) + .putCustom(ILM_CUSTOM_METADATA_KEY, customMetadata) + .build(); + ClusterState originalClusterState = ClusterState.builder(ClusterName.DEFAULT) + .metaData(MetaData.builder() + .put(originalIndexMetaData, false)) + .build(); + + IllegalStateException e = expectThrows(IllegalStateException.class, + () -> step.performAction(originalIndexMetaData.getIndex(), originalClusterState)); + + assertThat(e.getMessage(), equalTo("unable to copy execution state from [" + + indexName + "] to [" + step.getShrunkIndexPrefix() + indexName + "] as target index does not exist")); + } }