Skip to content

Adds checks to ensure index metadata exists when we try to use it #33455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexNotFoundException;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -56,8 +55,9 @@ public boolean getWaitOnAllShardCopies() {
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetaData idxMeta = clusterState.metaData().index(index);
if (idxMeta == null) {
throw new IndexNotFoundException("Index not found when executing " + getKey().getAction() + " lifecycle action.",
index.getName());
// Index must have been since deleted, ignore it
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName());
return new Result(false, null);
}
if (ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()) == false) {
logger.debug("[{}] lifecycle action for index [{}] cannot make progress because not all shards are active",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package org.elasticsearch.xpack.core.indexlifecycle;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
Expand All @@ -14,14 +16,21 @@
public final class InitializePolicyContextStep extends ClusterStateActionStep {
public static final String INITIALIZATION_PHASE = "new";
public static final StepKey KEY = new StepKey(INITIALIZATION_PHASE, "init", "init");
private static final Logger logger = LogManager.getLogger(InitializePolicyContextStep.class);

public InitializePolicyContextStep(Step.StepKey key, StepKey nextStepKey) {
super(key, nextStepKey);
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
Settings settings = clusterState.metaData().index(index).getSettings();
IndexMetaData indexMetaData = clusterState.getMetaData().index(index);
if (indexMetaData == null) {
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName());
// Index must have been since deleted, ignore it
return clusterState;
}
Settings settings = indexMetaData.getSettings();
if (settings.hasValue(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE)) {
return clusterState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package org.elasticsearch.xpack.core.indexlifecycle;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.ParseField;
Expand All @@ -19,6 +21,7 @@

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

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

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetaData idxMeta = clusterState.getMetaData().index(index);
if (idxMeta == null) {
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName());
// Index must have been since deleted, ignore it
return new Result(false, null);
}
String shrunkenIndexSource = IndexMetaData.INDEX_RESIZE_SOURCE_NAME.get(
clusterState.metaData().index(index).getSettings());
if (Strings.isNullOrEmpty(shrunkenIndexSource)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.node.Node;
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep.Result;
Expand Down Expand Up @@ -288,9 +287,9 @@ public void testExecuteIndexMissing() throws Exception {

AllocationRoutedStep step = createRandomInstance();

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

private void assertAllocateStatus(Index index, int shards, int replicas, AllocationRoutedStep step, Settings.Builder existingSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public ClusterState execute(ClusterState currentState) throws IOException {
Step currentStep = startStep;
IndexMetaData indexMetaData = currentState.metaData().index(index);
if (indexMetaData == null) {
logger.debug("lifecycle for index [{}] executed but index no longer exists", index.getName());
// This index doesn't exist any more, there's nothing to execute currently
return currentState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public void onFailure(Exception e) {
}

private void runPolicy(IndexMetaData indexMetaData, ClusterState currentState) {
if (indexMetaData == null) {
Copy link
Member

@dakrone dakrone Sep 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my preference would be to handle this in the caller of runPolicy, what do you think? (I'm okay either way)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moveToStep is the only caller of this method, so I am also more-or-less indifferent to where this goes

// This index doesn't exist any more, there's nothing to execute
return;
}
Settings indexSettings = indexMetaData.getSettings();
String policy = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings);
runPolicy(policy, indexMetaData, currentState, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings;
Expand Down Expand Up @@ -49,7 +50,12 @@ Exception getCause() {

@Override
public ClusterState execute(ClusterState currentState) throws IOException {
Settings indexSettings = currentState.getMetaData().index(index).getSettings();
IndexMetaData idxMeta = currentState.getMetaData().index(index);
if (idxMeta == null) {
// Index must have been since deleted, ignore it
return currentState;
}
Settings indexSettings = idxMeta.getSettings();
if (policy.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings))
&& currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexSettings))) {
return IndexLifecycleRunner.moveClusterStateToErrorStep(index, currentState, currentStepKey, cause, nowSupplier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.index.Index;
Expand Down Expand Up @@ -48,7 +49,12 @@ ToXContentObject getStepInfo() {

@Override
public ClusterState execute(ClusterState currentState) throws IOException {
Settings indexSettings = currentState.getMetaData().index(index).getSettings();
IndexMetaData idxMeta = currentState.getMetaData().index(index);
if (idxMeta == null) {
// Index must have been since deleted, ignore it
return currentState;
}
Settings indexSettings = idxMeta.getSettings();
if (policy.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings))
&& currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexSettings))) {
return IndexLifecycleRunner.addStepInfoToClusterState(index, currentState, stepInfo);
Expand Down