Skip to content

Commit be700f8

Browse files
Wipe Snapshots Before Indices in RestTests (#39662) (#39765)
* Wipe Snapshots Before Indices in RestTests * If we have a snapshot ongoing from the previous test and enter this method, then deleting the indices fails, which in turn fails the whole wipe * Fixed by first deleting/aborting snapshots
1 parent 4b5d658 commit be700f8

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.elasticsearch.client.RestClient;
3737
import org.elasticsearch.client.RestClientBuilder;
3838
import org.elasticsearch.client.WarningsHandler;
39+
import org.elasticsearch.cluster.SnapshotsInProgress;
3940
import org.elasticsearch.common.CheckedRunnable;
4041
import org.elasticsearch.common.Strings;
4142
import org.elasticsearch.common.io.PathUtils;
@@ -72,6 +73,7 @@
7273
import java.security.cert.CertificateException;
7374
import java.util.ArrayList;
7475
import java.util.Arrays;
76+
import java.util.HashMap;
7577
import java.util.HashSet;
7678
import java.util.List;
7779
import java.util.Map;
@@ -449,6 +451,8 @@ private void wipeCluster() throws Exception {
449451
waitForPendingRollupTasks();
450452
}
451453

454+
final Map<String, List<Map<?,?>>> inProgressSnapshots = wipeSnapshots();
455+
452456
if (preserveIndicesUponCompletion() == false) {
453457
// wipe indices
454458
try {
@@ -489,8 +493,6 @@ private void wipeCluster() throws Exception {
489493
}
490494
}
491495

492-
wipeSnapshots();
493-
494496
// wipe cluster settings
495497
if (preserveClusterSettings() == false) {
496498
wipeClusterSettings();
@@ -499,14 +501,18 @@ private void wipeCluster() throws Exception {
499501
if (hasXPack && false == preserveILMPoliciesUponCompletion()) {
500502
deleteAllPolicies();
501503
}
504+
505+
assertTrue("Found in progress snapshots [" + inProgressSnapshots + "].", inProgressSnapshots.isEmpty());
502506
}
503507

504508
/**
505509
* Wipe fs snapshots we created one by one and all repositories so that the next test can create the repositories fresh and they'll
506510
* start empty. There isn't an API to delete all snapshots. There is an API to delete all snapshot repositories but that leaves all of
507511
* the snapshots intact in the repository.
512+
* @return Map of repository name to list of snapshots found in unfinished state
508513
*/
509-
private void wipeSnapshots() throws IOException {
514+
private Map<String, List<Map<?, ?>>> wipeSnapshots() throws IOException {
515+
final Map<String, List<Map<?, ?>>> inProgressSnapshots = new HashMap<>();
510516
for (Map.Entry<String, ?> repo : entityAsMap(adminClient.performRequest(new Request("GET", "/_snapshot/_all"))).entrySet()) {
511517
String repoName = repo.getKey();
512518
Map<?, ?> repoSpec = (Map<?, ?>) repo.getValue();
@@ -519,6 +525,9 @@ private void wipeSnapshots() throws IOException {
519525
for (Object snapshot : snapshots) {
520526
Map<?, ?> snapshotInfo = (Map<?, ?>) snapshot;
521527
String name = (String) snapshotInfo.get("snapshot");
528+
if (SnapshotsInProgress.State.valueOf((String) snapshotInfo.get("state")).completed() == false) {
529+
inProgressSnapshots.computeIfAbsent(repoName, key -> new ArrayList<>()).add(snapshotInfo);
530+
}
522531
logger.debug("wiping snapshot [{}/{}]", repoName, name);
523532
adminClient().performRequest(new Request("DELETE", "/_snapshot/" + repoName + "/" + name));
524533
}
@@ -528,6 +537,7 @@ private void wipeSnapshots() throws IOException {
528537
adminClient().performRequest(new Request("DELETE", "_snapshot/" + repoName));
529538
}
530539
}
540+
return inProgressSnapshots;
531541
}
532542

533543
/**

0 commit comments

Comments
 (0)