Skip to content

Commit b5a64bb

Browse files
committed
Modify DeleteStep to allow deletion of other indices
Currently, the `DeleteStep` is only able to delete the index that ILM runs on. By accepting a target index name supplier, we can use it to delete other indices as well.
1 parent 3f5bb53 commit b5a64bb

File tree

1 file changed

+32
-2
lines changed
  • x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm

1 file changed

+32
-2
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,56 @@
1616
import org.elasticsearch.cluster.metadata.DataStream;
1717
import org.elasticsearch.cluster.metadata.IndexAbstraction;
1818
import org.elasticsearch.cluster.metadata.IndexMetadata;
19+
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
1920
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2021
import org.elasticsearch.common.Strings;
2122
import org.elasticsearch.core.TimeValue;
2223
import org.elasticsearch.index.Index;
2324

25+
import java.util.function.BiFunction;
26+
2427
/**
2528
* Deletes a single index.
2629
*/
2730
public class DeleteStep extends AsyncRetryDuringSnapshotActionStep {
31+
2832
public static final String NAME = "delete";
2933
private static final Logger logger = LogManager.getLogger(DeleteStep.class);
34+
private static final BiFunction<String, LifecycleExecutionState, String> DEFAULT_TARGET_INDEX_NAME_SUPPLIER = (
35+
indexName,
36+
lifecycleState) -> indexName;
37+
38+
private final BiFunction<String, LifecycleExecutionState, String> targetIndexNameSupplier;
39+
private final boolean indexSurvives;
3040

41+
/**
42+
* Use this constructor to delete the index that ILM is currently operating on.
43+
*/
3144
public DeleteStep(StepKey key, StepKey nextStepKey, Client client) {
45+
this(key, nextStepKey, client, DEFAULT_TARGET_INDEX_NAME_SUPPLIER, false);
46+
}
47+
48+
/**
49+
* Use this constructor to delete a specific index, potentially different from the one that ILM is currently operating on. The parameter
50+
* {@code indexSurvives} indicates whether the index that ILM runs on will survive (i.e. not get deleted) this step.
51+
* Look at the callers of {@link AsyncActionStep#indexSurvives()} for more details.
52+
*/
53+
public DeleteStep(
54+
StepKey key,
55+
StepKey nextStepKey,
56+
Client client,
57+
BiFunction<String, LifecycleExecutionState, String> targetIndexNameSupplier,
58+
boolean indexSurvives
59+
) {
3260
super(key, nextStepKey, client);
61+
this.targetIndexNameSupplier = targetIndexNameSupplier;
62+
this.indexSurvives = indexSurvives;
3363
}
3464

3565
@Override
3666
public void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener<Void> listener) {
3767
String policyName = indexMetadata.getLifecyclePolicyName();
38-
String indexName = indexMetadata.getIndex().getName();
68+
String indexName = targetIndexNameSupplier.apply(indexMetadata.getIndex().getName(), indexMetadata.getLifecycleExecutionState());
3969
IndexAbstraction indexAbstraction = currentProject.getIndicesLookup().get(indexName);
4070
assert indexAbstraction != null : "invalid cluster metadata. index [" + indexName + "] was not found";
4171
DataStream dataStream = indexAbstraction.getParentDataStream();
@@ -88,7 +118,7 @@ public void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata
88118

89119
@Override
90120
public boolean indexSurvives() {
91-
return false;
121+
return indexSurvives;
92122
}
93123

94124
@Override

0 commit comments

Comments
 (0)