Skip to content

Commit b59ee4d

Browse files
authored
[7.14] Remove and inline methods in SnapshotsService.deleteSnapshots() (#76151) (#76164)
Backport of #76079
1 parent 65a79b6 commit b59ee4d

File tree

1 file changed

+62
-85
lines changed

1 file changed

+62
-85
lines changed

server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java

Lines changed: 62 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,7 @@ public ClusterState execute(ClusterState currentState) {
286286
"cannot snapshot while a snapshot deletion is in-progress in [" + deletionsInProgress + "]"
287287
);
288288
}
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");
297290
SnapshotsInProgress snapshots = currentState.custom(SnapshotsInProgress.TYPE);
298291
// Fail if there are any concurrently running snapshots. The only exception to this being a snapshot in INIT state from
299292
// a
@@ -496,7 +489,7 @@ public ClusterState execute(ClusterState currentState) {
496489
if (concurrentOperationsAllowed == false && runningSnapshots.stream().anyMatch(entry -> entry.state() != State.INIT)) {
497490
throw new ConcurrentSnapshotExecutionException(repositoryName, snapshotName, " a snapshot is already running");
498491
}
499-
ensureNoCleanupInProgress(currentState, repositoryName, snapshotName);
492+
ensureNoCleanupInProgress(currentState, repositoryName, snapshotName, "create snapshot");
500493
ensureBelowConcurrencyLimit(repositoryName, snapshotName, snapshots, deletionsInProgress);
501494
// Store newSnapshot here to be processed in clusterStateProcessed
502495
List<String> indices = Arrays.asList(indexNameExpressionResolver.concreteIndexNames(currentState, request));
@@ -656,7 +649,7 @@ public void cloneSnapshot(CloneSnapshotRequest request, ActionListener<Void> lis
656649
public ClusterState execute(ClusterState currentState) {
657650
ensureRepositoryExists(repositoryName, currentState);
658651
ensureSnapshotNameAvailableInRepo(repositoryData, snapshotName, repository);
659-
ensureNoCleanupInProgress(currentState, repositoryName, snapshotName);
652+
ensureNoCleanupInProgress(currentState, repositoryName, snapshotName, "clone snapshot");
660653
final SnapshotsInProgress snapshots = currentState.custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY);
661654
final List<SnapshotsInProgress.Entry> runningSnapshots = snapshots.entries();
662655
ensureSnapshotNameNotRunning(runningSnapshots, repositoryName, snapshotName);
@@ -729,7 +722,12 @@ public void clusterStateProcessed(String source, ClusterState oldState, final Cl
729722
}, "clone_snapshot [" + request.source() + "][" + snapshotName + ']', listener::onFailure);
730723
}
731724

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+
) {
733731
final RepositoryCleanupInProgress repositoryCleanupInProgress = currentState.custom(
734732
RepositoryCleanupInProgress.TYPE,
735733
RepositoryCleanupInProgress.EMPTY
@@ -738,7 +736,13 @@ private static void ensureNoCleanupInProgress(ClusterState currentState, String
738736
throw new ConcurrentSnapshotExecutionException(
739737
repositoryName,
740738
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())
742746
);
743747
}
744748
}
@@ -2507,18 +2511,17 @@ private void failSnapshotCompletionListeners(Snapshot snapshot, Exception e) {
25072511
* @param listener listener
25082512
*/
25092513
public void deleteSnapshots(final DeleteSnapshotRequest request, final ActionListener<Void> listener) {
2510-
2514+
final String repositoryName = request.repository();
25112515
final String[] snapshotNames = request.snapshots();
2512-
final String repoName = request.repository();
25132516
logger.info(
25142517
() -> new ParameterizedMessage(
25152518
"deleting snapshots [{}] from repository [{}]",
25162519
Strings.arrayToCommaDelimitedString(snapshotNames),
2517-
repoName
2520+
repositoryName
25182521
)
25192522
);
25202523

2521-
final Repository repository = repositoriesService.repository(repoName);
2524+
final Repository repository = repositoriesService.repository(repositoryName);
25222525
repository.executeConsistentStateUpdate(repositoryData -> new ClusterStateUpdateTask(request.masterNodeTimeout()) {
25232526

25242527
private Snapshot runningSnapshot;
@@ -2541,17 +2544,46 @@ public ClusterState execute(ClusterState currentState) throws Exception {
25412544
+ "]"
25422545
);
25432546
}
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+
25532585
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);
25552587
return deleteFromRepoTask.execute(currentState);
25562588
}
25572589
assert snapshotEntries.size() == 1 : "Expected just a single running snapshot but saw " + snapshotEntries;
@@ -2606,7 +2638,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
26062638
.putCustom(
26072639
SnapshotsInProgress.TYPE,
26082640
SnapshotsInProgress.of(
2609-
snapshots.entries()
2641+
snapshotsInProgress.entries()
26102642
.stream()
26112643
// remove init state snapshot we found from a previous master if there was one
26122644
.filter(existing -> abortedDuringInit == false || existing.equals(snapshotEntry) == false)
@@ -2646,7 +2678,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
26462678
} else {
26472679
clusterService.submitStateUpdateTask(
26482680
"delete snapshot",
2649-
createDeleteStateUpdate(outstandingDeletes, repoName, repositoryData, Priority.IMMEDIATE, listener)
2681+
createDeleteStateUpdate(outstandingDeletes, repositoryName, repositoryData, Priority.IMMEDIATE, listener)
26502682
);
26512683
}
26522684
return;
@@ -2656,7 +2688,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
26562688
logger.debug("deleted snapshot completed - deleting files");
26572689
clusterService.submitStateUpdateTask(
26582690
"delete snapshot",
2659-
createDeleteStateUpdate(outstandingDeletes, repoName, result.v1(), Priority.IMMEDIATE, listener)
2691+
createDeleteStateUpdate(outstandingDeletes, repositoryName, result.v1(), Priority.IMMEDIATE, listener)
26602692
);
26612693
}, e -> {
26622694
if (ExceptionsHelper.unwrap(e, NotMasterException.class, FailedToCommitClusterStateException.class) != null) {
@@ -2674,52 +2706,6 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
26742706
}, "delete snapshot", listener::onFailure);
26752707
}
26762708

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-
27232709
private ClusterStateUpdateTask createDeleteStateUpdate(
27242710
List<SnapshotId> snapshotIds,
27252711
String repoName,
@@ -2775,16 +2761,7 @@ public ClusterState execute(ClusterState currentState) {
27752761
);
27762762
}
27772763
}
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");
27882765
final RestoreInProgress restoreInProgress = currentState.custom(RestoreInProgress.TYPE, RestoreInProgress.EMPTY);
27892766
// don't allow snapshot deletions while a restore is taking place,
27902767
// otherwise we could end up deleting a snapshot that is being restored

0 commit comments

Comments
 (0)