Sfoglia il codice sorgente

[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
Lee Hinman 7 anni fa
parent
commit
90b38d9b3d

+ 8 - 1
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();

+ 22 - 0
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<CopyExecutionStateStep> {
     @Override
@@ -86,4 +87,25 @@ public class CopyExecutionStateStepTests extends AbstractStepTestCase<CopyExecut
         assertEquals(oldIndexData.getAction(), newIndexData.getAction());
         assertEquals(ShrunkenIndexCheckStep.NAME, newIndexData.getStep());
     }
+    public void testPerformActionWithNoTarget() {
+        CopyExecutionStateStep step = createRandomInstance();
+        String indexName = randomAlphaOfLengthBetween(5, 20);
+        Map<String, String> 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"));
+    }
 }