Skip to content

Commit 90b38d9

Browse files
authored
[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
1 parent 19001e7 commit 90b38d9

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* new index has been created. Useful for actions such as shrink.
2323
*/
2424
public class CopyExecutionStateStep extends ClusterStateActionStep {
25-
public static final String NAME = "copy_execution_state";
25+
public static final String NAME = "copy-execution-state";
2626

2727
private static final Logger logger = LogManager.getLogger(CopyExecutionStateStep.class);
2828

@@ -52,6 +52,13 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
5252
String targetIndexName = shrunkIndexPrefix + indexName;
5353
IndexMetaData targetIndexMetaData = clusterState.metaData().index(targetIndexName);
5454

55+
if (targetIndexMetaData == null) {
56+
logger.warn("[{}] index [{}] unable to copy execution state to target index [{}] as target index does not exist",
57+
getKey().getAction(), index.getName(), targetIndexName);
58+
throw new IllegalStateException("unable to copy execution state from [" + index.getName() +
59+
"] to [" + targetIndexName + "] as target index does not exist");
60+
}
61+
5562
LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData);
5663
String phase = lifecycleState.getPhase();
5764
String action = lifecycleState.getAction();

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
1919
import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionStateTests.createCustomMetadata;
20+
import static org.hamcrest.Matchers.equalTo;
2021

2122
public class CopyExecutionStateStepTests extends AbstractStepTestCase<CopyExecutionStateStep> {
2223
@Override
@@ -86,4 +87,25 @@ public void testPerformAction() {
8687
assertEquals(oldIndexData.getAction(), newIndexData.getAction());
8788
assertEquals(ShrunkenIndexCheckStep.NAME, newIndexData.getStep());
8889
}
90+
public void testPerformActionWithNoTarget() {
91+
CopyExecutionStateStep step = createRandomInstance();
92+
String indexName = randomAlphaOfLengthBetween(5, 20);
93+
Map<String, String> customMetadata = createCustomMetadata();
94+
95+
IndexMetaData originalIndexMetaData = IndexMetaData.builder(indexName)
96+
.settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5))
97+
.numberOfReplicas(randomIntBetween(1,5))
98+
.putCustom(ILM_CUSTOM_METADATA_KEY, customMetadata)
99+
.build();
100+
ClusterState originalClusterState = ClusterState.builder(ClusterName.DEFAULT)
101+
.metaData(MetaData.builder()
102+
.put(originalIndexMetaData, false))
103+
.build();
104+
105+
IllegalStateException e = expectThrows(IllegalStateException.class,
106+
() -> step.performAction(originalIndexMetaData.getIndex(), originalClusterState));
107+
108+
assertThat(e.getMessage(), equalTo("unable to copy execution state from [" +
109+
indexName + "] to [" + step.getShrunkIndexPrefix() + indexName + "] as target index does not exist"));
110+
}
89111
}

0 commit comments

Comments
 (0)