2828import org .elasticsearch .common .settings .ClusterSettings ;
2929import org .elasticsearch .common .settings .Settings ;
3030import org .elasticsearch .common .transport .TransportAddress ;
31+ import org .elasticsearch .common .util .set .Sets ;
3132import org .elasticsearch .core .TimeValue ;
3233import org .elasticsearch .index .Index ;
3334import org .elasticsearch .index .IndexSettings ;
3435import org .elasticsearch .index .IndexVersion ;
36+ import org .elasticsearch .test .ClusterServiceUtils ;
3537import org .elasticsearch .test .ESTestCase ;
3638import org .elasticsearch .test .NodeRoles ;
39+ import org .elasticsearch .test .hamcrest .ElasticsearchAssertions ;
3740import org .elasticsearch .threadpool .TestThreadPool ;
3841import org .elasticsearch .threadpool .ThreadPool ;
3942import org .elasticsearch .xpack .core .ilm .CheckShrinkReadyStep ;
@@ -348,7 +351,6 @@ public void testExceptionStillProcessesOtherIndicesOnMaster() {
348351 doTestExceptionStillProcessesOtherIndices (true );
349352 }
350353
351- @ SuppressWarnings ("unchecked" )
352354 public void doTestExceptionStillProcessesOtherIndices (boolean useOnMaster ) {
353355 String policy1 = randomAlphaOfLengthBetween (1 , 20 );
354356 Step .StepKey i1currentStepKey = randomStepKey ();
@@ -377,7 +379,7 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
377379 }
378380 MockAction mockAction = new MockAction (List .of (i2mockStep ));
379381 Phase i2phase = new Phase ("phase" , TimeValue .ZERO , Map .of ("action" , mockAction ));
380- LifecyclePolicy i2policy = newTestLifecyclePolicy (policy1 , Map .of (i2phase .getName (), i1phase ));
382+ LifecyclePolicy i2policy = newTestLifecyclePolicy (policy2 , Map .of (i2phase .getName (), i2phase ));
381383 Index index2 = new Index (
382384 randomValueOtherThan (index1 .getName (), () -> randomAlphaOfLengthBetween (1 , 20 )),
383385 randomAlphaOfLengthBetween (1 , 20 )
@@ -403,8 +405,8 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
403405 ((IndexLifecycleRunnerTests .MockClusterStateActionStep ) i1mockStep ).setException (
404406 failStep1 ? new IllegalArgumentException ("forcing a failure for index 1" ) : null
405407 );
406- ((IndexLifecycleRunnerTests .MockClusterStateActionStep ) i1mockStep ).setLatch (stepLatch );
407- ((IndexLifecycleRunnerTests .MockClusterStateActionStep ) i1mockStep ).setException (
408+ ((IndexLifecycleRunnerTests .MockClusterStateActionStep ) i2mockStep ).setLatch (stepLatch );
409+ ((IndexLifecycleRunnerTests .MockClusterStateActionStep ) i2mockStep ).setException (
408410 failStep1 ? null : new IllegalArgumentException ("forcing a failure for index 2" )
409411 );
410412 }
@@ -420,7 +422,7 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
420422 .numberOfReplicas (randomIntBetween (0 , 5 ))
421423 .build ();
422424 IndexMetadata i2indexMetadata = IndexMetadata .builder (index2 .getName ())
423- .settings (settings (IndexVersion .current ()).put (LifecycleSettings .LIFECYCLE_NAME , policy1 ))
425+ .settings (settings (IndexVersion .current ()).put (LifecycleSettings .LIFECYCLE_NAME , policy2 ))
424426 .putCustom (ILM_CUSTOM_METADATA_KEY , i2lifecycleState .build ().asMap ())
425427 .numberOfShards (randomIntBetween (1 , 5 ))
426428 .numberOfReplicas (randomIntBetween (0 , 5 ))
@@ -433,23 +435,46 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
433435 .persistentSettings (settings (IndexVersion .current ()).build ())
434436 .build ();
435437
438+ Settings settings = Settings .builder ().put (LifecycleSettings .LIFECYCLE_POLL_INTERVAL , "1s" ).build ();
439+ var clusterSettings = new ClusterSettings (
440+ settings ,
441+ Sets .union (ClusterSettings .BUILT_IN_CLUSTER_SETTINGS , Set .of (LifecycleSettings .LIFECYCLE_POLL_INTERVAL_SETTING ))
442+ );
443+ ClusterService clusterService = ClusterServiceUtils .createClusterService (threadPool , clusterSettings );
444+ DiscoveryNode node = clusterService .localNode ();
436445 ClusterState currentState = ClusterState .builder (ClusterName .DEFAULT )
437446 .metadata (metadata )
438- .nodes (DiscoveryNodes .builder ().localNodeId ( nodeId ).masterNodeId (nodeId ). add ( masterNode ). build ( ))
447+ .nodes (DiscoveryNodes .builder ().add ( node ).masterNodeId (node . getId ()). localNodeId ( node . getId () ))
439448 .build ();
449+ ClusterServiceUtils .setState (clusterService , currentState );
450+
451+ indexLifecycleService = new IndexLifecycleService (
452+ Settings .EMPTY ,
453+ mock (Client .class ),
454+ clusterService ,
455+ threadPool ,
456+ systemUTC (),
457+ () -> now ,
458+ null ,
459+ null ,
460+ null
461+ );
440462
463+ ClusterChangedEvent event = new ClusterChangedEvent ("_source" , currentState , ClusterState .EMPTY_STATE );
464+ indexLifecycleService .applyClusterState (event );
441465 if (useOnMaster ) {
442- when (clusterService .state ()).thenReturn (currentState );
443466 indexLifecycleService .onMaster (currentState );
444467 } else {
445- indexLifecycleService .triggerPolicies (currentState , randomBoolean ());
468+ // TODO: this relies on the master task queue
469+ indexLifecycleService .triggerPolicies (currentState , true );
446470 }
447471 try {
448- stepLatch . await ( 5 , TimeUnit .SECONDS );
472+ ElasticsearchAssertions . awaitLatch ( stepLatch , 5 , TimeUnit .SECONDS );
449473 } catch (InterruptedException e ) {
450474 logger .error ("failure while waiting for step execution" , e );
451475 fail ("both steps should have been executed, even with an exception" );
452476 }
477+ clusterService .close ();
453478 }
454479
455480 public void testClusterChangedWaitsForTheStateToBeRecovered () {
0 commit comments