diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java index a6cedfceb1fbe..176cb013010b6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java @@ -16,26 +16,56 @@ import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.IndexAbstraction; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.LifecycleExecutionState; import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; +import java.util.function.BiFunction; + /** * Deletes a single index. */ public class DeleteStep extends AsyncRetryDuringSnapshotActionStep { + public static final String NAME = "delete"; private static final Logger logger = LogManager.getLogger(DeleteStep.class); + private static final BiFunction DEFAULT_TARGET_INDEX_NAME_SUPPLIER = ( + indexName, + lifecycleState) -> indexName; + + private final BiFunction targetIndexNameSupplier; + private final boolean indexSurvives; + /** + * Use this constructor to delete the index that ILM is currently operating on. + */ public DeleteStep(StepKey key, StepKey nextStepKey, Client client) { + this(key, nextStepKey, client, DEFAULT_TARGET_INDEX_NAME_SUPPLIER, false); + } + + /** + * Use this constructor to delete a specific index, potentially different from the one that ILM is currently operating on. The parameter + * {@code indexSurvives} indicates whether the index that ILM runs on will survive (i.e. not get deleted) this step. + * Look at the callers of {@link AsyncActionStep#indexSurvives()} for more details. + */ + public DeleteStep( + StepKey key, + StepKey nextStepKey, + Client client, + BiFunction targetIndexNameSupplier, + boolean indexSurvives + ) { super(key, nextStepKey, client); + this.targetIndexNameSupplier = targetIndexNameSupplier; + this.indexSurvives = indexSurvives; } @Override public void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener) { String policyName = indexMetadata.getLifecyclePolicyName(); - String indexName = indexMetadata.getIndex().getName(); + String indexName = targetIndexNameSupplier.apply(indexMetadata.getIndex().getName(), indexMetadata.getLifecycleExecutionState()); IndexAbstraction indexAbstraction = currentProject.getIndicesLookup().get(indexName); assert indexAbstraction != null : "invalid cluster metadata. index [" + indexName + "] was not found"; DataStream dataStream = indexAbstraction.getParentDataStream(); @@ -88,7 +118,7 @@ public void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata @Override public boolean indexSurvives() { - return false; + return indexSurvives; } @Override