Skip to content

Commit 09606ab

Browse files
Wipe Snapshots Before Indices in RestTests (#39662) (#39764)
* 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 63d16d1 commit 09606ab

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

Lines changed: 13 additions & 3 deletions
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;
@@ -468,6 +470,8 @@ private void wipeCluster() throws Exception {
468470
waitForPendingRollupTasks();
469471
}
470472

473+
final Map<String, List<Map<?,?>>> inProgressSnapshots = wipeSnapshots();
474+
471475
if (preserveIndicesUponCompletion() == false) {
472476
// wipe indices
473477
try {
@@ -508,8 +512,6 @@ private void wipeCluster() throws Exception {
508512
}
509513
}
510514

511-
wipeSnapshots();
512-
513515
// wipe cluster settings
514516
if (preserveClusterSettings() == false) {
515517
wipeClusterSettings();
@@ -518,14 +520,18 @@ private void wipeCluster() throws Exception {
518520
if (hasXPack && false == preserveILMPoliciesUponCompletion()) {
519521
deleteAllPolicies();
520522
}
523+
524+
assertTrue("Found in progress snapshots [" + inProgressSnapshots + "].", inProgressSnapshots.isEmpty());
521525
}
522526

523527
/**
524528
* Wipe fs snapshots we created one by one and all repositories so that the next test can create the repositories fresh and they'll
525529
* 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
526530
* the snapshots intact in the repository.
531+
* @return Map of repository name to list of snapshots found in unfinished state
527532
*/
528-
private void wipeSnapshots() throws IOException {
533+
private Map<String, List<Map<?, ?>>> wipeSnapshots() throws IOException {
534+
final Map<String, List<Map<?, ?>>> inProgressSnapshots = new HashMap<>();
529535
for (Map.Entry<String, ?> repo : entityAsMap(adminClient.performRequest(new Request("GET", "/_snapshot/_all"))).entrySet()) {
530536
String repoName = repo.getKey();
531537
Map<?, ?> repoSpec = (Map<?, ?>) repo.getValue();
@@ -538,6 +544,9 @@ private void wipeSnapshots() throws IOException {
538544
for (Object snapshot : snapshots) {
539545
Map<?, ?> snapshotInfo = (Map<?, ?>) snapshot;
540546
String name = (String) snapshotInfo.get("snapshot");
547+
if (SnapshotsInProgress.State.valueOf((String) snapshotInfo.get("state")).completed() == false) {
548+
inProgressSnapshots.computeIfAbsent(repoName, key -> new ArrayList<>()).add(snapshotInfo);
549+
}
541550
logger.debug("wiping snapshot [{}/{}]", repoName, name);
542551
adminClient().performRequest(new Request("DELETE", "/_snapshot/" + repoName + "/" + name));
543552
}
@@ -547,6 +556,7 @@ private void wipeSnapshots() throws IOException {
547556
adminClient().performRequest(new Request("DELETE", "_snapshot/" + repoName));
548557
}
549558
}
559+
return inProgressSnapshots;
550560
}
551561

552562
/**

0 commit comments

Comments
 (0)