@@ -286,14 +286,7 @@ public ClusterState execute(ClusterState currentState) {
286
286
"cannot snapshot while a snapshot deletion is in-progress in [" + deletionsInProgress + "]"
287
287
);
288
288
}
289
- final RepositoryCleanupInProgress repositoryCleanupInProgress = currentState .custom (RepositoryCleanupInProgress .TYPE );
290
- if (repositoryCleanupInProgress != null && repositoryCleanupInProgress .hasCleanupInProgress ()) {
291
- throw new ConcurrentSnapshotExecutionException (
292
- repositoryName ,
293
- snapshotName ,
294
- "cannot snapshot while a repository cleanup is in-progress in [" + repositoryCleanupInProgress + "]"
295
- );
296
- }
289
+ ensureNoCleanupInProgress (currentState , repositoryName , snapshotName , "create snapshot" );
297
290
SnapshotsInProgress snapshots = currentState .custom (SnapshotsInProgress .TYPE );
298
291
// Fail if there are any concurrently running snapshots. The only exception to this being a snapshot in INIT state from
299
292
// a
@@ -496,7 +489,7 @@ public ClusterState execute(ClusterState currentState) {
496
489
if (concurrentOperationsAllowed == false && runningSnapshots .stream ().anyMatch (entry -> entry .state () != State .INIT )) {
497
490
throw new ConcurrentSnapshotExecutionException (repositoryName , snapshotName , " a snapshot is already running" );
498
491
}
499
- ensureNoCleanupInProgress (currentState , repositoryName , snapshotName );
492
+ ensureNoCleanupInProgress (currentState , repositoryName , snapshotName , "create snapshot" );
500
493
ensureBelowConcurrencyLimit (repositoryName , snapshotName , snapshots , deletionsInProgress );
501
494
// Store newSnapshot here to be processed in clusterStateProcessed
502
495
List <String > indices = Arrays .asList (indexNameExpressionResolver .concreteIndexNames (currentState , request ));
@@ -656,7 +649,7 @@ public void cloneSnapshot(CloneSnapshotRequest request, ActionListener<Void> lis
656
649
public ClusterState execute (ClusterState currentState ) {
657
650
ensureRepositoryExists (repositoryName , currentState );
658
651
ensureSnapshotNameAvailableInRepo (repositoryData , snapshotName , repository );
659
- ensureNoCleanupInProgress (currentState , repositoryName , snapshotName );
652
+ ensureNoCleanupInProgress (currentState , repositoryName , snapshotName , "clone snapshot" );
660
653
final SnapshotsInProgress snapshots = currentState .custom (SnapshotsInProgress .TYPE , SnapshotsInProgress .EMPTY );
661
654
final List <SnapshotsInProgress .Entry > runningSnapshots = snapshots .entries ();
662
655
ensureSnapshotNameNotRunning (runningSnapshots , repositoryName , snapshotName );
@@ -729,7 +722,12 @@ public void clusterStateProcessed(String source, ClusterState oldState, final Cl
729
722
}, "clone_snapshot [" + request .source () + "][" + snapshotName + ']' , listener ::onFailure );
730
723
}
731
724
732
- private static void ensureNoCleanupInProgress (ClusterState currentState , String repositoryName , String snapshotName ) {
725
+ private static void ensureNoCleanupInProgress (
726
+ final ClusterState currentState ,
727
+ final String repositoryName ,
728
+ final String snapshotName ,
729
+ final String reason
730
+ ) {
733
731
final RepositoryCleanupInProgress repositoryCleanupInProgress = currentState .custom (
734
732
RepositoryCleanupInProgress .TYPE ,
735
733
RepositoryCleanupInProgress .EMPTY
@@ -738,7 +736,13 @@ private static void ensureNoCleanupInProgress(ClusterState currentState, String
738
736
throw new ConcurrentSnapshotExecutionException (
739
737
repositoryName ,
740
738
snapshotName ,
741
- "cannot snapshot while a repository cleanup is in-progress in [" + repositoryCleanupInProgress + "]"
739
+ "cannot "
740
+ + reason
741
+ + " while a repository cleanup is in-progress in "
742
+ + repositoryCleanupInProgress .entries ()
743
+ .stream ()
744
+ .map (RepositoryCleanupInProgress .Entry ::repository )
745
+ .collect (Collectors .toSet ())
742
746
);
743
747
}
744
748
}
@@ -2507,18 +2511,17 @@ private void failSnapshotCompletionListeners(Snapshot snapshot, Exception e) {
2507
2511
* @param listener listener
2508
2512
*/
2509
2513
public void deleteSnapshots (final DeleteSnapshotRequest request , final ActionListener <Void > listener ) {
2510
-
2514
+ final String repositoryName = request . repository ();
2511
2515
final String [] snapshotNames = request .snapshots ();
2512
- final String repoName = request .repository ();
2513
2516
logger .info (
2514
2517
() -> new ParameterizedMessage (
2515
2518
"deleting snapshots [{}] from repository [{}]" ,
2516
2519
Strings .arrayToCommaDelimitedString (snapshotNames ),
2517
- repoName
2520
+ repositoryName
2518
2521
)
2519
2522
);
2520
2523
2521
- final Repository repository = repositoriesService .repository (repoName );
2524
+ final Repository repository = repositoriesService .repository (repositoryName );
2522
2525
repository .executeConsistentStateUpdate (repositoryData -> new ClusterStateUpdateTask (request .masterNodeTimeout ()) {
2523
2526
2524
2527
private Snapshot runningSnapshot ;
@@ -2541,17 +2544,46 @@ public ClusterState execute(ClusterState currentState) throws Exception {
2541
2544
+ "]"
2542
2545
);
2543
2546
}
2544
- ensureRepositoryExists (repoName , currentState );
2545
- final SnapshotsInProgress snapshots = currentState .custom (SnapshotsInProgress .TYPE , SnapshotsInProgress .EMPTY );
2546
- final List <SnapshotsInProgress .Entry > snapshotEntries = findInProgressSnapshots (snapshots , snapshotNames , repoName );
2547
- final List <SnapshotId > snapshotIds = matchingSnapshotIds (
2548
- snapshotEntries .stream ().map (e -> e .snapshot ().getSnapshotId ()).collect (Collectors .toList ()),
2549
- repositoryData ,
2550
- snapshotNames ,
2551
- repoName
2552
- );
2547
+ ensureRepositoryExists (repositoryName , currentState );
2548
+ final List <SnapshotId > snapshotIds = new ArrayList <>();
2549
+ final List <SnapshotsInProgress .Entry > snapshotEntries = new ArrayList <>();
2550
+
2551
+ // find in-progress snapshots to delete in cluster state
2552
+ final SnapshotsInProgress snapshotsInProgress = currentState .custom (SnapshotsInProgress .TYPE , SnapshotsInProgress .EMPTY );
2553
+ for (SnapshotsInProgress .Entry entry : snapshotsInProgress .entries ()) {
2554
+ final SnapshotId snapshotId = entry .snapshot ().getSnapshotId ();
2555
+ if (entry .repository ().equals (repositoryName ) && Regex .simpleMatch (snapshotNames , snapshotId .getName ())) {
2556
+ snapshotIds .add (snapshotId );
2557
+ snapshotEntries .add (entry );
2558
+ }
2559
+ }
2560
+
2561
+ // find snapshots to delete in repository data
2562
+ final Map <String , SnapshotId > snapshotsIdsInRepository = repositoryData .getSnapshotIds ()
2563
+ .stream ()
2564
+ .collect (Collectors .toMap (SnapshotId ::getName , Function .identity ()));
2565
+ for (String snapshotOrPattern : snapshotNames ) {
2566
+ if (Regex .isSimpleMatchPattern (snapshotOrPattern )) {
2567
+ for (Map .Entry <String , SnapshotId > entry : snapshotsIdsInRepository .entrySet ()) {
2568
+ if (Regex .simpleMatch (snapshotOrPattern , entry .getKey ())) {
2569
+ snapshotIds .add (entry .getValue ());
2570
+ }
2571
+ }
2572
+ } else {
2573
+ final SnapshotId foundId = snapshotsIdsInRepository .get (snapshotOrPattern );
2574
+ if (foundId == null ) {
2575
+ if (snapshotEntries .stream ()
2576
+ .noneMatch (entry -> entry .snapshot ().getSnapshotId ().getName ().equals (snapshotOrPattern ))) {
2577
+ throw new SnapshotMissingException (repositoryName , snapshotOrPattern );
2578
+ }
2579
+ } else {
2580
+ snapshotIds .add (foundId );
2581
+ }
2582
+ }
2583
+ }
2584
+
2553
2585
if (snapshotEntries .isEmpty () || minNodeVersion .onOrAfter (SnapshotsService .FULL_CONCURRENCY_VERSION )) {
2554
- deleteFromRepoTask = createDeleteStateUpdate (snapshotIds , repoName , repositoryData , Priority .NORMAL , listener );
2586
+ deleteFromRepoTask = createDeleteStateUpdate (snapshotIds , repositoryName , repositoryData , Priority .NORMAL , listener );
2555
2587
return deleteFromRepoTask .execute (currentState );
2556
2588
}
2557
2589
assert snapshotEntries .size () == 1 : "Expected just a single running snapshot but saw " + snapshotEntries ;
@@ -2606,7 +2638,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
2606
2638
.putCustom (
2607
2639
SnapshotsInProgress .TYPE ,
2608
2640
SnapshotsInProgress .of (
2609
- snapshots .entries ()
2641
+ snapshotsInProgress .entries ()
2610
2642
.stream ()
2611
2643
// remove init state snapshot we found from a previous master if there was one
2612
2644
.filter (existing -> abortedDuringInit == false || existing .equals (snapshotEntry ) == false )
@@ -2646,7 +2678,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
2646
2678
} else {
2647
2679
clusterService .submitStateUpdateTask (
2648
2680
"delete snapshot" ,
2649
- createDeleteStateUpdate (outstandingDeletes , repoName , repositoryData , Priority .IMMEDIATE , listener )
2681
+ createDeleteStateUpdate (outstandingDeletes , repositoryName , repositoryData , Priority .IMMEDIATE , listener )
2650
2682
);
2651
2683
}
2652
2684
return ;
@@ -2656,7 +2688,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
2656
2688
logger .debug ("deleted snapshot completed - deleting files" );
2657
2689
clusterService .submitStateUpdateTask (
2658
2690
"delete snapshot" ,
2659
- createDeleteStateUpdate (outstandingDeletes , repoName , result .v1 (), Priority .IMMEDIATE , listener )
2691
+ createDeleteStateUpdate (outstandingDeletes , repositoryName , result .v1 (), Priority .IMMEDIATE , listener )
2660
2692
);
2661
2693
}, e -> {
2662
2694
if (ExceptionsHelper .unwrap (e , NotMasterException .class , FailedToCommitClusterStateException .class ) != null ) {
@@ -2674,52 +2706,6 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
2674
2706
}, "delete snapshot" , listener ::onFailure );
2675
2707
}
2676
2708
2677
- private static List <SnapshotId > matchingSnapshotIds (
2678
- List <SnapshotId > inProgress ,
2679
- RepositoryData repositoryData ,
2680
- String [] snapshotsOrPatterns ,
2681
- String repositoryName
2682
- ) {
2683
- final Map <String , SnapshotId > allSnapshotIds = repositoryData .getSnapshotIds ()
2684
- .stream ()
2685
- .collect (Collectors .toMap (SnapshotId ::getName , Function .identity ()));
2686
- final Set <SnapshotId > foundSnapshots = new HashSet <>(inProgress );
2687
- for (String snapshotOrPattern : snapshotsOrPatterns ) {
2688
- if (Regex .isSimpleMatchPattern (snapshotOrPattern )) {
2689
- for (Map .Entry <String , SnapshotId > entry : allSnapshotIds .entrySet ()) {
2690
- if (Regex .simpleMatch (snapshotOrPattern , entry .getKey ())) {
2691
- foundSnapshots .add (entry .getValue ());
2692
- }
2693
- }
2694
- } else {
2695
- final SnapshotId foundId = allSnapshotIds .get (snapshotOrPattern );
2696
- if (foundId == null ) {
2697
- if (inProgress .stream ().noneMatch (snapshotId -> snapshotId .getName ().equals (snapshotOrPattern ))) {
2698
- throw new SnapshotMissingException (repositoryName , snapshotOrPattern );
2699
- }
2700
- } else {
2701
- foundSnapshots .add (allSnapshotIds .get (snapshotOrPattern ));
2702
- }
2703
- }
2704
- }
2705
- return Collections .unmodifiableList (new ArrayList <>(foundSnapshots ));
2706
- }
2707
-
2708
- // Return in-progress snapshot entries by name and repository in the given cluster state or null if none is found
2709
- private static List <SnapshotsInProgress .Entry > findInProgressSnapshots (
2710
- SnapshotsInProgress snapshots ,
2711
- String [] snapshotNames ,
2712
- String repositoryName
2713
- ) {
2714
- List <SnapshotsInProgress .Entry > entries = new ArrayList <>();
2715
- for (SnapshotsInProgress .Entry entry : snapshots .entries ()) {
2716
- if (entry .repository ().equals (repositoryName ) && Regex .simpleMatch (snapshotNames , entry .snapshot ().getSnapshotId ().getName ())) {
2717
- entries .add (entry );
2718
- }
2719
- }
2720
- return entries ;
2721
- }
2722
-
2723
2709
private ClusterStateUpdateTask createDeleteStateUpdate (
2724
2710
List <SnapshotId > snapshotIds ,
2725
2711
String repoName ,
@@ -2775,16 +2761,7 @@ public ClusterState execute(ClusterState currentState) {
2775
2761
);
2776
2762
}
2777
2763
}
2778
- final RepositoryCleanupInProgress repositoryCleanupInProgress = currentState .custom (
2779
- RepositoryCleanupInProgress .TYPE ,
2780
- RepositoryCleanupInProgress .EMPTY
2781
- );
2782
- if (repositoryCleanupInProgress .hasCleanupInProgress ()) {
2783
- throw new ConcurrentSnapshotExecutionException (
2784
- new Snapshot (repoName , snapshotIds .get (0 )),
2785
- "cannot delete snapshots while a repository cleanup is in-progress in [" + repositoryCleanupInProgress + "]"
2786
- );
2787
- }
2764
+ ensureNoCleanupInProgress (currentState , repoName , snapshotIds .get (0 ).getName (), "delete snapshot" );
2788
2765
final RestoreInProgress restoreInProgress = currentState .custom (RestoreInProgress .TYPE , RestoreInProgress .EMPTY );
2789
2766
// don't allow snapshot deletions while a restore is taking place,
2790
2767
// otherwise we could end up deleting a snapshot that is being restored
0 commit comments