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 OLD_NAME = "cleanup-shrink-index" ;
30+ public static final String NAME = "cleanup-prefixed-index" ;
31+
32+ private static final Logger logger = LogManager .getLogger (CleanupGeneratedIndexStep .class );
2833
29- public CleanupShrinkIndexStep (StepKey key , StepKey nextStepKey , Client client ) {
34+ private final BiFunction <String , LifecycleExecutionState , String > targetIndexNameSupplier ;
35+
36+ public CleanupGeneratedIndexStep (
37+ StepKey key ,
38+ StepKey nextStepKey ,
39+ Client client ,
40+ BiFunction <String , LifecycleExecutionState , String > targetIndexNameSupplier
41+ ) {
3042 super (key , nextStepKey , client );
43+ this .targetIndexNameSupplier = targetIndexNameSupplier ;
3144 }
3245
3346 @ Override
@@ -37,40 +50,41 @@ public boolean isRetryable() {
3750
3851 @ Override
3952 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
53+ // If the index was generated by a resize operation, and the source inex does not exist anymore, we skip the deletion to avoid
54+ // data loss.
55+ final String generatedIndexSource = IndexMetadata .INDEX_RESIZE_SOURCE_NAME .get (indexMetadata .getSettings ());
56+ if (Strings .isNullOrEmpty (generatedIndexSource ) == false ) {
57+ if (currentProject .index (generatedIndexSource ) == null ) {
4658 String policyName = indexMetadata .getLifecyclePolicyName ();
4759 logger .warn (
4860 "managed index [{}] as part of policy [{}] is a shrunk index and the source index [{}] does not exist "
4961 + "anymore. will skip the [{}] step" ,
5062 indexMetadata .getIndex ().getName (),
5163 policyName ,
52- shrunkenIndexSource ,
64+ generatedIndexSource ,
5365 NAME
5466 );
5567 listener .onResponse (null );
5668 return ;
5769 }
5870 }
5971
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 ) {
72+ final String targetIndexName = targetIndexNameSupplier .apply (
73+ indexMetadata .getIndex ().getName (),
74+ indexMetadata .getLifecycleExecutionState ()
75+ );
76+ // If no index name was generated, there is nothing for us to delete, so we can move on
77+ if (Strings .hasText (targetIndexName ) == false ) {
6478 listener .onResponse (null );
6579 return ;
6680 }
6781 getClient (currentProject .id ()).admin ()
6882 .indices ()
69- .delete (new DeleteIndexRequest (shrinkIndexName ).masterNodeTimeout (TimeValue .MAX_VALUE ), new ActionListener <>() {
83+ .delete (new DeleteIndexRequest (targetIndexName ).masterNodeTimeout (TimeValue .MAX_VALUE ), new ActionListener <>() {
7084 @ Override
7185 public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
7286 // 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
87+ // we'll generate a new index name and attempt to create a new index with the newly generated name
7488 listener .onResponse (null );
7589 }
7690
@@ -86,4 +100,7 @@ public void onFailure(Exception e) {
86100 });
87101 }
88102
103+ public BiFunction <String , LifecycleExecutionState , String > getTargetIndexNameSupplier () {
104+ return targetIndexNameSupplier ;
105+ }
89106}
0 commit comments