[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
This commit is contained in:
Lee Hinman 2018-11-15 09:29:26 -07:00 committed by GitHub
parent 19001e71cc
commit 90b38d9b3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -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();

View File

@ -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"));
}
}