1919import org .elasticsearch .core .TimeValue ;
2020import org .elasticsearch .index .IndexNotFoundException ;
2121
22+ import java .util .function .BiFunction ;
23+
2224/**
23- * Deletes the index identified by the shrink index name stored in the lifecycle state of the managed index (if any was generated)
25+ * Deletes the index identified by the index name supplier.
2426 */
25- public class CleanupShrinkIndexStep extends AsyncRetryDuringSnapshotActionStep {
26- public static final String NAME = "cleanup-shrink-index" ;
27- private static final Logger logger = LogManager .getLogger (CleanupShrinkIndexStep .class );
27+ public class CleanupGeneratedIndexStep extends AsyncRetryDuringSnapshotActionStep {
28+
29+ public static final String NAME = "cleanup-generated-index" ;
30+
31+ private static final Logger logger = LogManager .getLogger (CleanupGeneratedIndexStep .class );
2832
29- public CleanupShrinkIndexStep (StepKey key , StepKey nextStepKey , Client client ) {
33+ private final BiFunction <String , LifecycleExecutionState , String > targetIndexNameSupplier ;
34+
35+ public CleanupGeneratedIndexStep (
36+ StepKey key ,
37+ StepKey nextStepKey ,
38+ Client client ,
39+ BiFunction <String , LifecycleExecutionState , String > targetIndexNameSupplier
40+ ) {
3041 super (key , nextStepKey , client );
42+ this .targetIndexNameSupplier = targetIndexNameSupplier ;
3143 }
3244
3345 @ Override
@@ -37,40 +49,41 @@ public boolean isRetryable() {
3749
3850 @ Override
3951 void performDuringNoSnapshot (IndexMetadata indexMetadata , ProjectMetadata currentProject , ActionListener <Void > listener ) {
40- final String shrunkenIndexSource = IndexMetadata .INDEX_RESIZE_SOURCE_NAME .get (indexMetadata .getSettings ());
41- if (Strings .isNullOrEmpty (shrunkenIndexSource ) == false ) {
42- // the current managed index is a shrunk index
43- if (currentProject .index (shrunkenIndexSource ) == null ) {
44- // if the source index does not exist, we'll skip deleting the
45- // (managed) shrunk index as that will cause data loss
52+ // If the index was generated by a resize operation, and the source index does not exist anymore, we skip the deletion to avoid
53+ // data loss.
54+ final String generatedIndexSource = IndexMetadata .INDEX_RESIZE_SOURCE_NAME .get (indexMetadata .getSettings ());
55+ if (Strings .hasText (generatedIndexSource )) {
56+ if (currentProject .index (generatedIndexSource ) == null ) {
4657 String policyName = indexMetadata .getLifecyclePolicyName ();
4758 logger .warn (
4859 "managed index [{}] as part of policy [{}] is a shrunk index and the source index [{}] does not exist "
4960 + "anymore. will skip the [{}] step" ,
5061 indexMetadata .getIndex ().getName (),
5162 policyName ,
52- shrunkenIndexSource ,
63+ generatedIndexSource ,
5364 NAME
5465 );
5566 listener .onResponse (null );
5667 return ;
5768 }
5869 }
5970
60- LifecycleExecutionState lifecycleState = indexMetadata .getLifecycleExecutionState ();
61- final String shrinkIndexName = lifecycleState .shrinkIndexName ();
62- // if the shrink index was not generated there is nothing to delete so we move on
63- if (Strings .hasText (shrinkIndexName ) == false ) {
71+ final String targetIndexName = targetIndexNameSupplier .apply (
72+ indexMetadata .getIndex ().getName (),
73+ indexMetadata .getLifecycleExecutionState ()
74+ );
75+ // If no index name was generated, there is nothing for us to delete, so we can move on
76+ if (Strings .hasText (targetIndexName ) == false ) {
6477 listener .onResponse (null );
6578 return ;
6679 }
6780 getClient (currentProject .id ()).admin ()
6881 .indices ()
69- .delete (new DeleteIndexRequest (shrinkIndexName ).masterNodeTimeout (TimeValue .MAX_VALUE ), new ActionListener <>() {
82+ .delete (new DeleteIndexRequest (targetIndexName ).masterNodeTimeout (TimeValue .MAX_VALUE ), new ActionListener <>() {
7083 @ Override
7184 public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
7285 // even if not all nodes acked the delete request yet we can consider this operation as successful as
73- // we'll generate a new index name and attempt to shrink into the newly generated name
86+ // we'll generate a new index name and attempt to create a new index with the newly generated name
7487 listener .onResponse (null );
7588 }
7689
@@ -86,4 +99,7 @@ public void onFailure(Exception e) {
8699 });
87100 }
88101
102+ public BiFunction <String , LifecycleExecutionState , String > getTargetIndexNameSupplier () {
103+ return targetIndexNameSupplier ;
104+ }
89105}
0 commit comments