Skip to content

Commit 0da211f

Browse files
Allow Deleting Multiple Snapshots at Once (#55474)
Adds deleting multiple snapshots in one go without significantly changing the mechanics of snapshot deletes otherwise. This change does not yet allow mixing snapshot delete and abort. Abort is still only allowed for a single snapshot delete by exact name.
1 parent 0fb74db commit 0da211f

File tree

32 files changed

+383
-249
lines changed

32 files changed

+383
-249
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotRequestConverters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static Request restoreSnapshot(RestoreSnapshotRequest restoreSnapshotRequest) th
176176
static Request deleteSnapshot(DeleteSnapshotRequest deleteSnapshotRequest) {
177177
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_snapshot")
178178
.addPathPart(deleteSnapshotRequest.repository())
179-
.addPathPart(deleteSnapshotRequest.snapshot())
179+
.addCommaSeparatedPathParts(deleteSnapshotRequest.snapshots())
180180
.build();
181181
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
182182

client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotRequestConvertersTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public void testDeleteSnapshot() {
269269

270270
DeleteSnapshotRequest deleteSnapshotRequest = new DeleteSnapshotRequest();
271271
deleteSnapshotRequest.repository(repository);
272-
deleteSnapshotRequest.snapshot(snapshot);
272+
deleteSnapshotRequest.snapshots(snapshot);
273273
RequestConvertersTests.setRandomMasterTimeout(deleteSnapshotRequest, expectedParams);
274274

275275
Request request = SnapshotRequestConverters.deleteSnapshot(deleteSnapshotRequest);

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ public void testSnapshotDeleteSnapshot() throws IOException {
753753

754754
// tag::delete-snapshot-request
755755
DeleteSnapshotRequest request = new DeleteSnapshotRequest(repositoryName);
756-
request.snapshot(snapshotName);
756+
request.snapshots(snapshotName);
757757
// end::delete-snapshot-request
758758

759759
// tag::delete-snapshot-request-masterTimeout

docs/reference/snapshot-restore/take-snapshot.asciidoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ created the snapshotting process will be aborted and all files created as part o
193193
cleaned. Therefore, the delete snapshot operation can be used to cancel long running snapshot operations that were
194194
started by mistake.
195195

196+
It is also possible to delete multiple snapshots from a repository in one go, for example:
197+
198+
[source,console]
199+
-----------------------------------
200+
DELETE /_snapshot/my_backup/my_backup,my_fs_backup
201+
DELETE /_snapshot/my_backup/snap*
202+
-----------------------------------
203+
// TEST[skip:no my_fs_backup]
204+
196205
A repository can be unregistered using the following command:
197206

198207
[source,console]

plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.elasticsearch.threadpool.Scheduler;
5050
import org.elasticsearch.threadpool.ThreadPool;
5151

52+
import java.util.Collection;
5253
import java.util.List;
5354
import java.util.Map;
5455
import java.util.concurrent.TimeUnit;
@@ -250,12 +251,12 @@ public void finalizeSnapshot(SnapshotId snapshotId, ShardGenerations shardGenera
250251
}
251252

252253
@Override
253-
public void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId, Version repositoryMetaVersion,
254-
ActionListener<Void> listener) {
254+
public void deleteSnapshots(Collection<SnapshotId> snapshotIds, long repositoryStateId, Version repositoryMetaVersion,
255+
ActionListener<Void> listener) {
255256
if (SnapshotsService.useShardGenerations(repositoryMetaVersion) == false) {
256257
listener = delayedListener(listener);
257258
}
258-
super.deleteSnapshot(snapshotId, repositoryStateId, repositoryMetaVersion, listener);
259+
super.deleteSnapshots(snapshotIds, repositoryStateId, repositoryMetaVersion, listener);
259260
}
260261

261262
/**

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/delete/DeleteSnapshotRequest.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.action.support.master.MasterNodeRequest;
2424
import org.elasticsearch.common.io.stream.StreamInput;
2525
import org.elasticsearch.common.io.stream.StreamOutput;
26+
import org.elasticsearch.snapshots.SnapshotsService;
2627

2728
import java.io.IOException;
2829

@@ -31,15 +32,14 @@
3132
/**
3233
* Delete snapshot request
3334
* <p>
34-
* Delete snapshot request removes the snapshot record from the repository and cleans up all
35-
* files that are associated with this particular snapshot. All files that are shared with
36-
* at least one other existing snapshot are left intact.
35+
* Delete snapshot request removes snapshots from the repository and cleans up all files that are associated with the snapshots.
36+
* All files that are shared with at least one other existing snapshot are left intact.
3737
*/
3838
public class DeleteSnapshotRequest extends MasterNodeRequest<DeleteSnapshotRequest> {
3939

4040
private String repository;
4141

42-
private String snapshot;
42+
private String[] snapshots;
4343

4444
/**
4545
* Constructs a new delete snapshots request
@@ -48,14 +48,14 @@ public DeleteSnapshotRequest() {
4848
}
4949

5050
/**
51-
* Constructs a new delete snapshots request with repository and snapshot name
51+
* Constructs a new delete snapshots request with repository and snapshot names
5252
*
5353
* @param repository repository name
54-
* @param snapshot snapshot name
54+
* @param snapshots snapshot names
5555
*/
56-
public DeleteSnapshotRequest(String repository, String snapshot) {
56+
public DeleteSnapshotRequest(String repository, String... snapshots) {
5757
this.repository = repository;
58-
this.snapshot = snapshot;
58+
this.snapshots = snapshots;
5959
}
6060

6161
/**
@@ -70,14 +70,26 @@ public DeleteSnapshotRequest(String repository) {
7070
public DeleteSnapshotRequest(StreamInput in) throws IOException {
7171
super(in);
7272
repository = in.readString();
73-
snapshot = in.readString();
73+
if (in.getVersion().onOrAfter(SnapshotsService.MULTI_DELETE_VERSION)) {
74+
snapshots = in.readStringArray();
75+
} else {
76+
snapshots = new String[] {in.readString()};
77+
}
7478
}
7579

7680
@Override
7781
public void writeTo(StreamOutput out) throws IOException {
7882
super.writeTo(out);
7983
out.writeString(repository);
80-
out.writeString(snapshot);
84+
if (out.getVersion().onOrAfter(SnapshotsService.MULTI_DELETE_VERSION)) {
85+
out.writeStringArray(snapshots);
86+
} else {
87+
if (snapshots.length != 1) {
88+
throw new IllegalArgumentException(
89+
"Can't write snapshot delete with more than one snapshot to version [" + out.getVersion() + "]");
90+
}
91+
out.writeString(snapshots[0]);
92+
}
8193
}
8294

8395
@Override
@@ -86,8 +98,8 @@ public ActionRequestValidationException validate() {
8698
if (repository == null) {
8799
validationException = addValidationError("repository is missing", validationException);
88100
}
89-
if (snapshot == null) {
90-
validationException = addValidationError("snapshot is missing", validationException);
101+
if (snapshots == null || snapshots.length == 0) {
102+
validationException = addValidationError("snapshots are missing", validationException);
91103
}
92104
return validationException;
93105
}
@@ -108,21 +120,21 @@ public String repository() {
108120
}
109121

110122
/**
111-
* Returns repository name
123+
* Returns snapshot names
112124
*
113-
* @return repository name
125+
* @return snapshot names
114126
*/
115-
public String snapshot() {
116-
return this.snapshot;
127+
public String[] snapshots() {
128+
return this.snapshots;
117129
}
118130

119131
/**
120-
* Sets snapshot name
132+
* Sets snapshot names
121133
*
122134
* @return this request
123135
*/
124-
public DeleteSnapshotRequest snapshot(String snapshot) {
125-
this.snapshot = snapshot;
136+
public DeleteSnapshotRequest snapshots(String... snapshots) {
137+
this.snapshots = snapshots;
126138
return this;
127139
}
128140
}

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/delete/DeleteSnapshotRequestBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public DeleteSnapshotRequestBuilder(ElasticsearchClient client, DeleteSnapshotAc
3939
/**
4040
* Constructs delete snapshot request builder with specified repository and snapshot names
4141
*/
42-
public DeleteSnapshotRequestBuilder(ElasticsearchClient client, DeleteSnapshotAction action, String repository, String snapshot) {
43-
super(client, action, new DeleteSnapshotRequest(repository, snapshot));
42+
public DeleteSnapshotRequestBuilder(ElasticsearchClient client, DeleteSnapshotAction action, String repository, String... snapshots) {
43+
super(client, action, new DeleteSnapshotRequest(repository, snapshots));
4444
}
4545

4646
/**
@@ -57,11 +57,11 @@ public DeleteSnapshotRequestBuilder setRepository(String repository) {
5757
/**
5858
* Sets the snapshot name
5959
*
60-
* @param snapshot snapshot name
60+
* @param snapshots snapshot names
6161
* @return this builder
6262
*/
63-
public DeleteSnapshotRequestBuilder setSnapshot(String snapshot) {
64-
request.snapshot(snapshot);
63+
public DeleteSnapshotRequestBuilder setSnapshots(String... snapshots) {
64+
request.snapshots(snapshots);
6565
return this;
6666
}
6767
}

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/delete/TransportDeleteSnapshotAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.elasticsearch.transport.TransportService;
3737

3838
import java.io.IOException;
39+
import java.util.Arrays;
3940

4041
/**
4142
* Transport action for delete snapshot operation
@@ -71,7 +72,7 @@ protected ClusterBlockException checkBlock(DeleteSnapshotRequest request, Cluste
7172
@Override
7273
protected void masterOperation(Task task, final DeleteSnapshotRequest request, ClusterState state,
7374
final ActionListener<AcknowledgedResponse> listener) {
74-
snapshotsService.deleteSnapshot(request.repository(), request.snapshot(),
75+
snapshotsService.deleteSnapshots(request.repository(), Arrays.asList(request.snapshots()),
7576
ActionListener.map(listener, v -> new AcknowledgedResponse(true)));
7677
}
7778
}

server/src/main/java/org/elasticsearch/client/ClusterAdminClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ public interface ClusterAdminClient extends ElasticsearchClient {
529529
/**
530530
* Delete snapshot.
531531
*/
532-
DeleteSnapshotRequestBuilder prepareDeleteSnapshot(String repository, String snapshot);
532+
DeleteSnapshotRequestBuilder prepareDeleteSnapshot(String repository, String... snapshot);
533533

534534
/**
535535
* Restores a snapshot.

server/src/main/java/org/elasticsearch/client/Requests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,14 @@ public static RestoreSnapshotRequest restoreSnapshotRequest(String repository, S
502502
}
503503

504504
/**
505-
* Deletes a snapshot
505+
* Deletes snapshots
506506
*
507-
* @param snapshot snapshot name
507+
* @param snapshots snapshot names
508508
* @param repository repository name
509509
* @return delete snapshot request
510510
*/
511-
public static DeleteSnapshotRequest deleteSnapshotRequest(String repository, String snapshot) {
512-
return new DeleteSnapshotRequest(repository, snapshot);
511+
public static DeleteSnapshotRequest deleteSnapshotRequest(String repository, String... snapshots) {
512+
return new DeleteSnapshotRequest(repository, snapshots);
513513
}
514514

515515
/**

0 commit comments

Comments
 (0)