3636import org .elasticsearch .client .RestClient ;
3737import org .elasticsearch .client .RestClientBuilder ;
3838import org .elasticsearch .client .WarningsHandler ;
39+ import org .elasticsearch .cluster .SnapshotsInProgress ;
3940import org .elasticsearch .common .CheckedRunnable ;
4041import org .elasticsearch .common .Strings ;
4142import org .elasticsearch .common .io .PathUtils ;
7273import java .security .cert .CertificateException ;
7374import java .util .ArrayList ;
7475import java .util .Arrays ;
76+ import java .util .HashMap ;
7577import java .util .HashSet ;
7678import java .util .List ;
7779import 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