@@ -456,15 +456,21 @@ public void testInitiatingSnapRemovedButStillRunningRemains() throws Exception {
456456 assertEquals (List .of (stillRunning ), newRegisteredPolicySnapshots .getSnapshotsByPolicy (policyId ));
457457 }
458458
459- public void testInferFailureInitiatedBySuccess () throws Exception {
459+ public void testCleanUpRegisteredInitiatedBySuccess () throws Exception {
460460 final String policyId = randomAlphaOfLength (10 );
461461 final SnapshotId initiatingSnapshot = randSnapshotId ();
462- final SnapshotId previousFailedSnapshot = randSnapshotId ();
462+ final SnapshotId inferredFailureSnapshot = randSnapshotId ();
463463 // currently running snapshots
464464 final SnapshotId stillRunning = randSnapshotId ();
465465
466+ final SnapshotInfo snapshotInfoSuccess = randomSnapshotInfoSuccess (projectId );
467+ final SnapshotInfo snapshotInfoFailure = randomSnapshotInfoFailure (projectId );
468+
466469 var definedSlmPolicies = List .of (policyId );
467- var registeredSnapshots = Map .of (policyId , List .of (stillRunning , previousFailedSnapshot ));
470+ var registeredSnapshots = Map .of (
471+ policyId ,
472+ List .of (stillRunning , inferredFailureSnapshot , snapshotInfoSuccess .snapshotId (), snapshotInfoFailure .snapshotId ())
473+ );
468474 var inProgress = Map .of (policyId , List .of (stillRunning ));
469475 ClusterState clusterState = buildClusterState (projectId , definedSlmPolicies , registeredSnapshots , inProgress );
470476
@@ -474,39 +480,57 @@ public void testInferFailureInitiatedBySuccess() throws Exception {
474480 initiatingSnapshot ,
475481 randomLong (),
476482 randomLong (),
477- Collections . emptyList ( )
483+ List . of ( snapshotInfoSuccess , snapshotInfoFailure )
478484 );
479485
480486 ClusterState newClusterState = writeJobTask .execute (clusterState );
481487
482- // previous failure is now recorded in stats and metadata
488+ // snapshotInfoSuccess, initiatingSnapshot
489+ int expectedSuccessCount = 2 ;
490+ // inferredFailureSnapshot, snapshotInfoFailure
491+ int expectedFailureCount = 2 ;
492+ // the last snapshot (initiatingSnapshot) was successful
493+ int expectedInvocationsSinceLastSuccess = 0 ;
494+ // registered snapshots state is now recorded in stats and metadata
483495 SnapshotLifecycleMetadata newSlmMetadata = newClusterState .metadata ().getProject (projectId ).custom (SnapshotLifecycleMetadata .TYPE );
484496 SnapshotLifecycleStats newStats = newSlmMetadata .getStats ();
485497 SnapshotLifecycleStats .SnapshotPolicyStats snapshotPolicyStats = newStats .getMetrics ().get (policyId );
486- assertEquals (1 , snapshotPolicyStats .getSnapshotFailedCount ());
487- assertEquals (1 , snapshotPolicyStats .getSnapshotTakenCount ());
498+ assertEquals (expectedFailureCount , snapshotPolicyStats .getSnapshotFailedCount ());
499+ assertEquals (expectedSuccessCount , snapshotPolicyStats .getSnapshotTakenCount ());
488500
489501 SnapshotLifecyclePolicyMetadata newSlmPolicyMetadata = newSlmMetadata .getSnapshotConfigurations ().get (policyId );
490- assertEquals (previousFailedSnapshot .getName (), newSlmPolicyMetadata .getLastFailure ().getSnapshotName ());
502+ assertEquals (snapshotInfoFailure . snapshotId () .getName (), newSlmPolicyMetadata .getLastFailure ().getSnapshotName ());
491503 assertEquals (initiatingSnapshot .getName (), newSlmPolicyMetadata .getLastSuccess ().getSnapshotName ());
492- assertEquals (0 , newSlmPolicyMetadata .getInvocationsSinceLastSuccess ());
504+ assertEquals (expectedInvocationsSinceLastSuccess , newSlmPolicyMetadata .getInvocationsSinceLastSuccess ());
493505
494- // failed snapshot no longer in registeredSnapshot set
506+ // completed snapshot no longer in registeredSnapshot set
495507 RegisteredPolicySnapshots newRegisteredPolicySnapshots = newClusterState .metadata ()
496508 .getProject (projectId )
497509 .custom (RegisteredPolicySnapshots .TYPE );
498510 List <SnapshotId > newRegisteredSnapIds = newRegisteredPolicySnapshots .getSnapshotsByPolicy (policyId );
499511 assertEquals (List .of (stillRunning ), newRegisteredSnapIds );
500512 }
501513
502- public void testInferFailureInitiatedByFailure () throws Exception {
514+ public void testCleanUpRegisteredInitiatedByFailure () throws Exception {
503515 final String policyId = randomAlphaOfLength (10 );
504516 final SnapshotId initiatingSnapshot = randSnapshotId ();
505- final SnapshotId previousFailedSnapshot = randSnapshotId ();
517+ final SnapshotId inferredFailureSnapshot = randSnapshotId ();
506518 final SnapshotId stillRunning = randSnapshotId ();
519+ final SnapshotInfo snapshotInfoSuccess = randomSnapshotInfoSuccess (projectId );
520+ final SnapshotInfo snapshotInfoFailure1 = randomSnapshotInfoFailure (projectId );
521+ final SnapshotInfo snapshotInfoFailure2 = randomSnapshotInfoFailure (projectId );
507522
508523 var definedSlmPolicies = List .of (policyId );
509- var registeredSnapshots = Map .of (policyId , List .of (stillRunning , previousFailedSnapshot ));
524+ var registeredSnapshots = Map .of (
525+ policyId ,
526+ List .of (
527+ stillRunning ,
528+ inferredFailureSnapshot ,
529+ snapshotInfoSuccess .snapshotId (),
530+ snapshotInfoFailure1 .snapshotId (),
531+ snapshotInfoFailure2 .snapshotId ()
532+ )
533+ );
510534 var inProgress = Map .of (policyId , List .of (stillRunning ));
511535 ClusterState clusterState = buildClusterState (projectId , definedSlmPolicies , registeredSnapshots , inProgress );
512536
@@ -515,25 +539,31 @@ public void testInferFailureInitiatedByFailure() throws Exception {
515539 policyId ,
516540 initiatingSnapshot ,
517541 randomLong (),
518- Collections . emptyList ( ),
542+ List . of ( snapshotInfoSuccess , snapshotInfoFailure1 , snapshotInfoFailure2 ),
519543 new RuntimeException ()
520544 );
521545
522546 ClusterState newClusterState = writeJobTask .execute (clusterState );
523547
524- // previous failure is now recorded in stats and metadata
548+ // snapshotInfoSuccess
549+ int expectedSuccessCount = 1 ;
550+ // inferredFailureSnapshot, snapshotInfoFailure1, snapshotInfoFailure2, initiatingSnapshot
551+ int expectedFailureCount = 4 ;
552+ // snapshotInfoFailure1, snapshotInfoFailure2, initiatingSnapshot
553+ int expectedInvocationsSinceLastSuccess = 3 ;
554+ // registered snapshots state is now recorded in stats and metadata
525555 SnapshotLifecycleMetadata newSlmMetadata = newClusterState .metadata ().getProject (projectId ).custom (SnapshotLifecycleMetadata .TYPE );
526556 SnapshotLifecycleStats newStats = newSlmMetadata .getStats ();
527557 SnapshotLifecycleStats .SnapshotPolicyStats snapshotPolicyStats = newStats .getMetrics ().get (policyId );
528- assertEquals (2 , snapshotPolicyStats .getSnapshotFailedCount ());
529- assertEquals (0 , snapshotPolicyStats .getSnapshotTakenCount ());
558+ assertEquals (expectedFailureCount , snapshotPolicyStats .getSnapshotFailedCount ());
559+ assertEquals (expectedSuccessCount , snapshotPolicyStats .getSnapshotTakenCount ());
530560
531561 SnapshotLifecyclePolicyMetadata newSlmPolicyMetadata = newSlmMetadata .getSnapshotConfigurations ().get (policyId );
532562 assertEquals (initiatingSnapshot .getName (), newSlmPolicyMetadata .getLastFailure ().getSnapshotName ());
533- assertNull ( newSlmPolicyMetadata .getLastSuccess ());
534- assertEquals (2 , newSlmPolicyMetadata .getInvocationsSinceLastSuccess ());
563+ assertEquals ( snapshotInfoSuccess . snapshotId (). getName (), newSlmPolicyMetadata .getLastSuccess (). getSnapshotName ());
564+ assertEquals (expectedInvocationsSinceLastSuccess , newSlmPolicyMetadata .getInvocationsSinceLastSuccess ());
535565
536- // failed snapshot no longer in registeredSnapshot set
566+ // completed snapshot no longer in registeredSnapshot set
537567 RegisteredPolicySnapshots newRegisteredPolicySnapshots = newClusterState .metadata ()
538568 .getProject (projectId )
539569 .custom (RegisteredPolicySnapshots .TYPE );
@@ -703,4 +733,42 @@ public void putAsync(SnapshotHistoryItem item) {
703733 verifier .accept (item );
704734 }
705735 }
736+
737+ private static SnapshotInfo randomSnapshotInfoSuccess (ProjectId projectId ) {
738+ long startTime = randomNonNegativeLong ();
739+ long endTime = randomLongBetween (startTime , Long .MAX_VALUE );
740+ return new SnapshotInfo (
741+ new Snapshot (projectId , "repo" , randSnapshotId ()),
742+ List .of ("index1" , "index2" ),
743+ List .of (),
744+ List .of (),
745+ null ,
746+ endTime ,
747+ 2 ,
748+ List .of (),
749+ randomBoolean (),
750+ Map .of (),
751+ startTime ,
752+ Map .of ()
753+ );
754+ }
755+
756+ private static SnapshotInfo randomSnapshotInfoFailure (ProjectId projectId ) {
757+ long startTime = randomNonNegativeLong ();
758+ long endTime = randomLongBetween (startTime , Long .MAX_VALUE );
759+ return new SnapshotInfo (
760+ new Snapshot (projectId , "repo" , randSnapshotId ()),
761+ List .of ("index1" , "index2" ),
762+ List .of (),
763+ List .of (),
764+ "failed snapshot" ,
765+ endTime ,
766+ 2 ,
767+ List .of (new SnapshotShardFailure ("nodeId" , new ShardId ("index" , "uuid" , 0 ), "forced failure" )),
768+ randomBoolean (),
769+ Map .of (),
770+ startTime ,
771+ Map .of ()
772+ );
773+ }
706774}
0 commit comments