Skip to content

Commit d7b93c7

Browse files
committed
unit tests
1 parent daaf405 commit d7b93c7

File tree

2 files changed

+94
-56
lines changed

2 files changed

+94
-56
lines changed

x-pack/plugin/slm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTask.java

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,17 @@
1717
import org.elasticsearch.action.admin.cluster.snapshots.get.TransportGetSnapshotsAction;
1818
import org.elasticsearch.client.internal.Client;
1919
import org.elasticsearch.cluster.ClusterState;
20-
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
2120
import org.elasticsearch.cluster.ClusterStateUpdateTask;
2221
import org.elasticsearch.cluster.ProjectState;
23-
import org.elasticsearch.cluster.SimpleBatchedExecutor;
2422
import org.elasticsearch.cluster.SnapshotsInProgress;
2523
import org.elasticsearch.cluster.metadata.ProjectId;
2624
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2725
import org.elasticsearch.cluster.service.ClusterService;
28-
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
29-
import org.elasticsearch.common.Priority;
3026
import org.elasticsearch.common.Strings;
3127
import org.elasticsearch.common.scheduler.SchedulerEngine;
3228
import org.elasticsearch.core.FixForMultiProject;
3329
import org.elasticsearch.core.SuppressForbidden;
3430
import org.elasticsearch.core.TimeValue;
35-
import org.elasticsearch.core.Tuple;
3631
import org.elasticsearch.snapshots.RegisteredPolicySnapshots;
3732
import org.elasticsearch.snapshots.RegisteredPolicySnapshots.PolicySnapshot;
3833
import org.elasticsearch.snapshots.SnapshotException;
@@ -75,7 +70,6 @@ public class SnapshotLifecycleTask implements SchedulerEngine.Listener {
7570
private final Client client;
7671
private final ClusterService clusterService;
7772
private final SnapshotHistoryStore historyStore;
78-
private final MasterServiceTaskQueue<UpdatePolicyStatsTask> updatePolicyStatsQueue;
7973

8074
public SnapshotLifecycleTask(
8175
final ProjectId projectId,
@@ -87,34 +81,6 @@ public SnapshotLifecycleTask(
8781
this.client = client;
8882
this.clusterService = clusterService;
8983
this.historyStore = historyStore;
90-
91-
ClusterStateTaskExecutor<UpdatePolicyStatsTask> executor = new SimpleBatchedExecutor<>() {
92-
@Override
93-
public Tuple<ClusterState, Object> executeTask(UpdatePolicyStatsTask updatePolicyStatsTask, ClusterState clusterState)
94-
throws Exception {
95-
// TODO
96-
return null;
97-
}
98-
99-
@Override
100-
public void taskSucceeded(UpdatePolicyStatsTask updatePolicyStatsTask, Object o) {
101-
// TODO
102-
}
103-
};
104-
this.updatePolicyStatsQueue = clusterService.createTaskQueue("slm-update-policy-stats", Priority.HIGH, executor);
105-
}
106-
107-
static class UpdatePolicyStatsTask extends ClusterStateUpdateTask {
108-
109-
@Override
110-
public ClusterState execute(ClusterState currentState) throws Exception {
111-
return null;
112-
}
113-
114-
@Override
115-
public void onFailure(Exception e) {
116-
// TODO
117-
}
11884
}
11985

12086
static List<String> findStaleRegisteredSnapshotIds(ProjectState projectState, String policyId) {
@@ -529,11 +495,15 @@ public ClusterState execute(ClusterState currentState) throws Exception {
529495
for (PolicySnapshot registeredSnapshot : registeredSnapshots.getSnapshots()) {
530496
SnapshotId registeredSnapshotId = registeredSnapshot.getSnapshotId();
531497
if (registeredSnapshotId.equals(snapshotId)) {
532-
// skip the snapshot just completed, it will be handled below
498+
// skip the snapshot just completed, it will be handled later
499+
continue;
500+
}
501+
if (snapLifecycles.containsKey(registeredSnapshot.getPolicy()) == false) {
502+
// the SLM policy no longer exists, just remove the snapshot from registered set, no need to record stats
533503
continue;
534504
}
535505
if (registeredSnapshot.getPolicy().equals(policyName) == false || runningSnapshots.contains(registeredSnapshotId)) {
536-
// the snapshot is for another policy, or is still running, so keep it in the registered set
506+
// the snapshot is for another policy, leave it to that policy to clean up, or the snapshot is still running
537507
newRegistered.add(registeredSnapshot);
538508
} else {
539509
// the snapshot was completed and should be removed from registered snapshots, update state accordingly

x-pack/plugin/slm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)