| 
16 | 16 | import org.elasticsearch.cluster.metadata.DataStream;  | 
17 | 17 | import org.elasticsearch.cluster.metadata.IndexAbstraction;  | 
18 | 18 | import org.elasticsearch.cluster.metadata.IndexMetadata;  | 
 | 19 | +import org.elasticsearch.cluster.metadata.LifecycleExecutionState;  | 
19 | 20 | import org.elasticsearch.cluster.metadata.ProjectMetadata;  | 
20 | 21 | import org.elasticsearch.common.Strings;  | 
21 | 22 | import org.elasticsearch.core.TimeValue;  | 
22 | 23 | import org.elasticsearch.index.Index;  | 
23 | 24 | 
 
  | 
 | 25 | +import java.util.function.BiFunction;  | 
 | 26 | + | 
24 | 27 | /**  | 
25 | 28 |  * Deletes a single index.  | 
26 | 29 |  */  | 
27 | 30 | public class DeleteStep extends AsyncRetryDuringSnapshotActionStep {  | 
 | 31 | + | 
28 | 32 |     public static final String NAME = "delete";  | 
29 | 33 |     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;  | 
30 | 40 | 
 
  | 
 | 41 | +    /**  | 
 | 42 | +     * Use this constructor to delete the index that ILM is currently operating on.  | 
 | 43 | +     */  | 
31 | 44 |     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 | +    ) {  | 
32 | 60 |         super(key, nextStepKey, client);  | 
 | 61 | +        this.targetIndexNameSupplier = targetIndexNameSupplier;  | 
 | 62 | +        this.indexSurvives = indexSurvives;  | 
33 | 63 |     }  | 
34 | 64 | 
 
  | 
35 | 65 |     @Override  | 
36 | 66 |     public void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener<Void> listener) {  | 
37 | 67 |         String policyName = indexMetadata.getLifecyclePolicyName();  | 
38 |  | -        String indexName = indexMetadata.getIndex().getName();  | 
 | 68 | +        String indexName = targetIndexNameSupplier.apply(indexMetadata.getIndex().getName(), indexMetadata.getLifecycleExecutionState());  | 
39 | 69 |         IndexAbstraction indexAbstraction = currentProject.getIndicesLookup().get(indexName);  | 
40 | 70 |         assert indexAbstraction != null : "invalid cluster metadata. index [" + indexName + "] was not found";  | 
41 | 71 |         DataStream dataStream = indexAbstraction.getParentDataStream();  | 
@@ -88,7 +118,7 @@ public void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata  | 
88 | 118 | 
 
  | 
89 | 119 |     @Override  | 
90 | 120 |     public boolean indexSurvives() {  | 
91 |  | -        return false;  | 
 | 121 | +        return indexSurvives;  | 
92 | 122 |     }  | 
93 | 123 | 
 
  | 
94 | 124 |     @Override  | 
 | 
0 commit comments