Skip to content

Commit f836413

Browse files
authored
Adds checks to ensure index metadata exists when we try to use it (#33455)
* Adds checks to ensure index metadata exists when we try to use it * Fixes failing test
1 parent 017ffe5 commit f836413

File tree

8 files changed

+44
-10
lines changed

8 files changed

+44
-10
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.common.xcontent.ToXContentObject;
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
2929
import org.elasticsearch.index.Index;
30-
import org.elasticsearch.index.IndexNotFoundException;
3130

3231
import java.io.IOException;
3332
import java.util.Collections;
@@ -56,8 +55,9 @@ public boolean getWaitOnAllShardCopies() {
5655
public Result isConditionMet(Index index, ClusterState clusterState) {
5756
IndexMetaData idxMeta = clusterState.metaData().index(index);
5857
if (idxMeta == null) {
59-
throw new IndexNotFoundException("Index not found when executing " + getKey().getAction() + " lifecycle action.",
60-
index.getName());
58+
// Index must have been since deleted, ignore it
59+
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName());
60+
return new Result(false, null);
6161
}
6262
if (ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()) == false) {
6363
logger.debug("[{}] lifecycle action for index [{}] cannot make progress because not all shards are active",

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package org.elasticsearch.xpack.core.indexlifecycle;
77

8+
import org.apache.logging.log4j.LogManager;
9+
import org.apache.logging.log4j.Logger;
810
import org.elasticsearch.cluster.ClusterState;
911
import org.elasticsearch.cluster.metadata.IndexMetaData;
1012
import org.elasticsearch.cluster.metadata.MetaData;
@@ -14,14 +16,21 @@
1416
public final class InitializePolicyContextStep extends ClusterStateActionStep {
1517
public static final String INITIALIZATION_PHASE = "new";
1618
public static final StepKey KEY = new StepKey(INITIALIZATION_PHASE, "init", "init");
19+
private static final Logger logger = LogManager.getLogger(InitializePolicyContextStep.class);
1720

1821
public InitializePolicyContextStep(Step.StepKey key, StepKey nextStepKey) {
1922
super(key, nextStepKey);
2023
}
2124

2225
@Override
2326
public ClusterState performAction(Index index, ClusterState clusterState) {
24-
Settings settings = clusterState.metaData().index(index).getSettings();
27+
IndexMetaData indexMetaData = clusterState.getMetaData().index(index);
28+
if (indexMetaData == null) {
29+
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName());
30+
// Index must have been since deleted, ignore it
31+
return clusterState;
32+
}
33+
Settings settings = indexMetaData.getSettings();
2534
if (settings.hasValue(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE)) {
2635
return clusterState;
2736
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package org.elasticsearch.xpack.core.indexlifecycle;
77

8+
import org.apache.logging.log4j.LogManager;
9+
import org.apache.logging.log4j.Logger;
810
import org.elasticsearch.cluster.ClusterState;
911
import org.elasticsearch.cluster.metadata.IndexMetaData;
1012
import org.elasticsearch.common.ParseField;
@@ -19,6 +21,7 @@
1921

2022
public class ShrunkenIndexCheckStep extends ClusterStateWaitStep {
2123
public static final String NAME = "is-shrunken-index";
24+
private static final Logger logger = LogManager.getLogger(InitializePolicyContextStep.class);
2225
private String shrunkIndexPrefix;
2326

2427
public ShrunkenIndexCheckStep(StepKey key, StepKey nextStepKey, String shrunkIndexPrefix) {
@@ -32,6 +35,12 @@ String getShrunkIndexPrefix() {
3235

3336
@Override
3437
public Result isConditionMet(Index index, ClusterState clusterState) {
38+
IndexMetaData idxMeta = clusterState.getMetaData().index(index);
39+
if (idxMeta == null) {
40+
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName());
41+
// Index must have been since deleted, ignore it
42+
return new Result(false, null);
43+
}
3544
String shrunkenIndexSource = IndexMetaData.INDEX_RESIZE_SOURCE_NAME.get(
3645
clusterState.metaData().index(index).getSettings());
3746
if (Strings.isNullOrEmpty(shrunkenIndexSource)) {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.common.settings.Settings;
2323
import org.elasticsearch.common.transport.TransportAddress;
2424
import org.elasticsearch.index.Index;
25-
import org.elasticsearch.index.IndexNotFoundException;
2625
import org.elasticsearch.index.shard.ShardId;
2726
import org.elasticsearch.node.Node;
2827
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep.Result;
@@ -288,9 +287,9 @@ public void testExecuteIndexMissing() throws Exception {
288287

289288
AllocationRoutedStep step = createRandomInstance();
290289

291-
IndexNotFoundException thrownException = expectThrows(IndexNotFoundException.class, () -> step.isConditionMet(index, clusterState));
292-
assertEquals("Index not found when executing " + step.getKey().getAction() + " lifecycle action.", thrownException.getMessage());
293-
assertEquals(index.getName(), thrownException.getIndex().getName());
290+
Result actualResult = step.isConditionMet(index, clusterState);
291+
assertFalse(actualResult.isComplete());
292+
assertNull(actualResult.getInfomationContext());
294293
}
295294

296295
private void assertAllocateStatus(Index index, int shards, int replicas, AllocationRoutedStep step, Settings.Builder existingSettings,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public ClusterState execute(ClusterState currentState) throws IOException {
6666
Step currentStep = startStep;
6767
IndexMetaData indexMetaData = currentState.metaData().index(index);
6868
if (indexMetaData == null) {
69+
logger.debug("lifecycle for index [{}] executed but index no longer exists", index.getName());
6970
// This index doesn't exist any more, there's nothing to execute currently
7071
return currentState;
7172
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public void onFailure(Exception e) {
158158
}
159159

160160
private void runPolicy(IndexMetaData indexMetaData, ClusterState currentState) {
161+
if (indexMetaData == null) {
162+
// This index doesn't exist any more, there's nothing to execute
163+
return;
164+
}
161165
Settings indexSettings = indexMetaData.getSettings();
162166
String policy = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings);
163167
runPolicy(policy, indexMetaData, currentState, false);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.elasticsearch.ElasticsearchException;
99
import org.elasticsearch.cluster.ClusterState;
1010
import org.elasticsearch.cluster.ClusterStateUpdateTask;
11+
import org.elasticsearch.cluster.metadata.IndexMetaData;
1112
import org.elasticsearch.common.settings.Settings;
1213
import org.elasticsearch.index.Index;
1314
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings;
@@ -49,7 +50,12 @@ Exception getCause() {
4950

5051
@Override
5152
public ClusterState execute(ClusterState currentState) throws IOException {
52-
Settings indexSettings = currentState.getMetaData().index(index).getSettings();
53+
IndexMetaData idxMeta = currentState.getMetaData().index(index);
54+
if (idxMeta == null) {
55+
// Index must have been since deleted, ignore it
56+
return currentState;
57+
}
58+
Settings indexSettings = idxMeta.getSettings();
5359
if (policy.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings))
5460
&& currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexSettings))) {
5561
return IndexLifecycleRunner.moveClusterStateToErrorStep(index, currentState, currentStepKey, cause, nowSupplier);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.elasticsearch.ElasticsearchException;
1010
import org.elasticsearch.cluster.ClusterState;
1111
import org.elasticsearch.cluster.ClusterStateUpdateTask;
12+
import org.elasticsearch.cluster.metadata.IndexMetaData;
1213
import org.elasticsearch.common.settings.Settings;
1314
import org.elasticsearch.common.xcontent.ToXContentObject;
1415
import org.elasticsearch.index.Index;
@@ -48,7 +49,12 @@ ToXContentObject getStepInfo() {
4849

4950
@Override
5051
public ClusterState execute(ClusterState currentState) throws IOException {
51-
Settings indexSettings = currentState.getMetaData().index(index).getSettings();
52+
IndexMetaData idxMeta = currentState.getMetaData().index(index);
53+
if (idxMeta == null) {
54+
// Index must have been since deleted, ignore it
55+
return currentState;
56+
}
57+
Settings indexSettings = idxMeta.getSettings();
5258
if (policy.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings))
5359
&& currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexSettings))) {
5460
return IndexLifecycleRunner.addStepInfoToClusterState(index, currentState, stepInfo);

0 commit comments

Comments
 (0)