@@ -111,7 +111,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
111111 public void prepare () {
112112 threadPool = new TestThreadPool ("test" );
113113 noopClient = new NoOpClient (threadPool );
114- historyStore = new NoOpHistoryStore ();
114+ historyStore = new NoOpHistoryStore (noopClient );
115115 }
116116
117117 @ After
@@ -552,6 +552,77 @@ public void testRunStateChangePolicyWithAsyncActionNextStep() throws Exception {
552552 "\" state\" :{\" phase\" :\" phase\" ,\" action\" :\" action\" ,\" step\" :\" async_action_step\" ,\" step_time\" :\" 0\" }}" ));
553553 }
554554
555+ public void testRunAsyncActionReturningFalseEntersError () throws Exception {
556+ String policyName = "foo" ;
557+ StepKey stepKey = new StepKey ("phase" , "action" , "async_action_step" );
558+ StepKey nextStepKey = new StepKey ("phase" , "action" , "cluster_state_action_step" );
559+ MockAsyncActionStep step = new MockAsyncActionStep (stepKey , nextStepKey );
560+ MockClusterStateActionStep nextStep = new MockClusterStateActionStep (nextStepKey , null );
561+ MockPolicyStepsRegistry stepRegistry = createMultiStepPolicyStepRegistry (policyName , Arrays .asList (step , nextStep ));
562+ stepRegistry .setResolver ((i , k ) -> {
563+ if (stepKey .equals (k )) {
564+ return step ;
565+ } else if (nextStepKey .equals (k )) {
566+ return nextStep ;
567+ } else {
568+ fail ("should not try to retrieve different step" );
569+ return null ;
570+ }
571+ });
572+ LifecycleExecutionState les = LifecycleExecutionState .builder ()
573+ .setPhase ("phase" )
574+ .setAction ("action" )
575+ .setStep ("async_action_step" )
576+ .build ();
577+ IndexMetadata indexMetadata = IndexMetadata .builder ("test" )
578+ .settings (Settings .builder ()
579+ .put (IndexMetadata .SETTING_VERSION_CREATED , Version .CURRENT )
580+ .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
581+ .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 1 )
582+ .put (LifecycleSettings .LIFECYCLE_NAME , policyName ))
583+ .putCustom (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY , les .asMap ())
584+ .build ();
585+ ClusterService clusterService = ClusterServiceUtils .createClusterService (threadPool );
586+ DiscoveryNode node = clusterService .localNode ();
587+ IndexLifecycleMetadata ilm = new IndexLifecycleMetadata (Collections .emptyMap (), OperationMode .RUNNING );
588+ ClusterState state = ClusterState .builder (new ClusterName ("cluster" ))
589+ .metadata (Metadata .builder ()
590+ .put (indexMetadata , true )
591+ .putCustom (IndexLifecycleMetadata .TYPE , ilm ))
592+ .nodes (DiscoveryNodes .builder ()
593+ .add (node )
594+ .masterNodeId (node .getId ())
595+ .localNodeId (node .getId ()))
596+ .build ();
597+ ClusterServiceUtils .setState (clusterService , state );
598+ IndexLifecycleRunner runner = new IndexLifecycleRunner (stepRegistry , historyStore , clusterService , threadPool , () -> 0L );
599+
600+ CountDownLatch latch = new CountDownLatch (1 );
601+ step .setLatch (latch );
602+ step .setWillComplete (false );
603+ ClusterState before = clusterService .state ();
604+ runner .maybeRunAsyncAction (before , indexMetadata , policyName , stepKey );
605+
606+ // Wait for the action step to finish
607+ awaitLatch (latch , 5 , TimeUnit .SECONDS );
608+
609+ assertThat (step .getExecuteCount (), equalTo (1L ));
610+
611+ try {
612+ assertBusy (() -> {
613+ ILMHistoryItem historyItem = historyStore .getItems ().stream ()
614+ .findFirst ()
615+ .orElseThrow (() -> new AssertionError ("failed to register ILM history" ));
616+ assertThat (historyItem .toString (),
617+ containsString ("\" {\\ \" type\\ \" :\\ \" illegal_state_exception\\ \" ,\\ \" reason\\ \" :" +
618+ "\\ \" unknown exception for step {\\ \\ \\ \" phase\\ \\ \\ \" :\\ \\ \\ \" phase\\ \\ \\ \" ,\\ \\ \\ \" action" +
619+ "\\ \\ \\ \" :\\ \\ \\ \" action\\ \\ \\ \" ,\\ \\ \\ \" name\\ \\ \\ \" :\\ \\ \\ \" async_action_step\\ \\ \\ \" } in policy foo\\ \" " ));
620+ });
621+ } finally {
622+ clusterService .close ();
623+ }
624+ }
625+
555626 public void testRunPeriodicStep () throws Exception {
556627 String policyName = "foo" ;
557628 StepKey stepKey = new StepKey ("phase" , "action" , "cluster_state_action_step" );
@@ -880,7 +951,7 @@ public static void assertClusterStateOnNextStep(ClusterState oldClusterState, In
880951 static class MockAsyncActionStep extends AsyncActionStep {
881952
882953 private Exception exception ;
883- private boolean willComplete ;
954+ private boolean willComplete = true ;
884955 private boolean indexSurvives = true ;
885956 private long executeCount = 0 ;
886957 private CountDownLatch latch ;
@@ -1178,11 +1249,12 @@ public static MockPolicyStepsRegistry createMultiStepPolicyStepRegistry(String p
11781249 return new MockPolicyStepsRegistry (lifecyclePolicyMap , firstStepMap , stepMap , REGISTRY , client );
11791250 }
11801251
1181- private class NoOpHistoryStore extends ILMHistoryStore {
1252+ private static class NoOpHistoryStore extends ILMHistoryStore {
1253+ private static final Logger logger = LogManager .getLogger (NoOpHistoryStore .class );
11821254
11831255 private final List <ILMHistoryItem > items = new ArrayList <>();
11841256
1185- NoOpHistoryStore () {
1257+ NoOpHistoryStore (Client noopClient ) {
11861258 super (Settings .EMPTY , noopClient , null , null );
11871259 }
11881260
0 commit comments