Skip to content

Commit 80edff1

Browse files
author
Andrey Ershov
committed
Get snapshots support for multiple repositories
1 parent 3647d7c commit 80edff1

File tree

31 files changed

+706
-316
lines changed

31 files changed

+706
-316
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
@@ -104,7 +104,7 @@ static Request createSnapshot(CreateSnapshotRequest createSnapshotRequest) throw
104104

105105
static Request getSnapshots(GetSnapshotsRequest getSnapshotsRequest) {
106106
RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder().addPathPartAsIs("_snapshot")
107-
.addPathPart(getSnapshotsRequest.repository());
107+
.addCommaSeparatedPathParts(getSnapshotsRequest.repositories());
108108
String endpoint;
109109
if (getSnapshotsRequest.snapshots().length == 0) {
110110
endpoint = endpointBuilder.addPathPart("_all").build();

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
import org.elasticsearch.repositories.fs.FsRepository;
4242
import org.elasticsearch.rest.RestStatus;
4343
import org.elasticsearch.snapshots.RestoreInfo;
44+
import org.mockito.internal.util.collections.Sets;
4445

4546
import java.io.IOException;
4647
import java.util.Collections;
47-
import java.util.stream.Collectors;
4848

49-
import static org.hamcrest.Matchers.contains;
5049
import static org.hamcrest.Matchers.equalTo;
5150
import static org.hamcrest.Matchers.greaterThan;
51+
import static org.hamcrest.Matchers.hasSize;
5252
import static org.hamcrest.Matchers.is;
5353

5454
public class SnapshotIT extends ESRestHighLevelClientTestCase {
@@ -155,39 +155,48 @@ public void testCreateSnapshot() throws IOException {
155155
}
156156

157157
public void testGetSnapshots() throws IOException {
158-
String repository = "test_repository";
158+
String repository1 = "test_repository1";
159+
String repository2 = "test_repository2";
159160
String snapshot1 = "test_snapshot1";
160161
String snapshot2 = "test_snapshot2";
161162

162-
AcknowledgedResponse putRepositoryResponse = createTestRepository(repository, FsRepository.TYPE, "{\"location\": \".\"}");
163+
AcknowledgedResponse putRepositoryResponse =
164+
createTestRepository(repository1, FsRepository.TYPE, "{\"location\": \"loc1\"}");
163165
assertTrue(putRepositoryResponse.isAcknowledged());
164166

165-
CreateSnapshotRequest createSnapshotRequest1 = new CreateSnapshotRequest(repository, snapshot1);
167+
AcknowledgedResponse putRepositoryResponse2 =
168+
createTestRepository(repository2, FsRepository.TYPE, "{\"location\": \"loc2\"}");
169+
assertTrue(putRepositoryResponse2.isAcknowledged());
170+
171+
CreateSnapshotRequest createSnapshotRequest1 = new CreateSnapshotRequest(repository1, snapshot1);
166172
createSnapshotRequest1.waitForCompletion(true);
167173
CreateSnapshotResponse putSnapshotResponse1 = createTestSnapshot(createSnapshotRequest1);
168-
CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest(repository, snapshot2);
174+
CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest(repository2, snapshot2);
169175
createSnapshotRequest2.waitForCompletion(true);
170176
CreateSnapshotResponse putSnapshotResponse2 = createTestSnapshot(createSnapshotRequest2);
171177
// check that the request went ok without parsing JSON here. When using the high level client, check acknowledgement instead.
172178
assertEquals(RestStatus.OK, putSnapshotResponse1.status());
173179
assertEquals(RestStatus.OK, putSnapshotResponse2.status());
174180

175-
GetSnapshotsRequest request;
176-
if (randomBoolean()) {
177-
request = new GetSnapshotsRequest(repository);
178-
} else if (randomBoolean()) {
179-
request = new GetSnapshotsRequest(repository, new String[] {"_all"});
181+
GetSnapshotsRequest request = new GetSnapshotsRequest(
182+
randomFrom(new String[]{"_all"}, new String[]{"*"}, new String[]{repository1, repository2}),
183+
randomFrom(new String[]{"_all"}, new String[]{"*"}, new String[]{snapshot1, snapshot2})
184+
);
185+
request.ignoreUnavailable(true);
180186

181-
} else {
182-
request = new GetSnapshotsRequest(repository, new String[] {snapshot1, snapshot2});
183-
}
184187
GetSnapshotsResponse response = execute(request, highLevelClient().snapshot()::get, highLevelClient().snapshot()::getAsync);
185188

186-
assertEquals(2, response.getSnapshots().size());
187-
assertThat(response.getSnapshots().stream().map((s) -> s.snapshotId().getName()).collect(Collectors.toList()),
188-
contains("test_snapshot1", "test_snapshot2"));
189+
assertThat(response.isFailed(), is(false));
190+
assertThat(response.getRepositories(), equalTo(Sets.newSet(repository1, repository2)));
191+
192+
assertThat(response.getSnapshots(repository1), hasSize(1));
193+
assertThat(response.getSnapshots(repository1).get(0).snapshotId().getName(), equalTo(snapshot1));
194+
195+
assertThat(response.getSnapshots(repository2), hasSize(1));
196+
assertThat(response.getSnapshots(repository2).get(0).snapshotId().getName(), equalTo(snapshot2));
189197
}
190198

199+
191200
public void testSnapshotsStatus() throws IOException {
192201
String testRepository = "test";
193202
String testSnapshot = "snapshot";

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
import java.io.IOException;
4343
import java.nio.file.Path;
44-
import java.util.Arrays;
4544
import java.util.HashMap;
4645
import java.util.Locale;
4746
import java.util.Map;
@@ -148,15 +147,16 @@ public void testCreateSnapshot() throws IOException {
148147

149148
public void testGetSnapshots() {
150149
Map<String, String> expectedParams = new HashMap<>();
151-
String repository = RequestConvertersTests.randomIndicesNames(1, 1)[0];
150+
String repository1 = randomAlphaOfLength(10);
151+
String repository2 = randomAlphaOfLength(10);
152152
String snapshot1 = "snapshot1-" + randomAlphaOfLengthBetween(2, 5).toLowerCase(Locale.ROOT);
153153
String snapshot2 = "snapshot2-" + randomAlphaOfLengthBetween(2, 5).toLowerCase(Locale.ROOT);
154154

155-
String endpoint = String.format(Locale.ROOT, "/_snapshot/%s/%s,%s", repository, snapshot1, snapshot2);
155+
String endpoint = String.format(Locale.ROOT, "/_snapshot/%s,%s/%s,%s", repository1, repository2, snapshot1, snapshot2);
156156

157157
GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest();
158-
getSnapshotsRequest.repository(repository);
159-
getSnapshotsRequest.snapshots(Arrays.asList(snapshot1, snapshot2).toArray(new String[0]));
158+
getSnapshotsRequest.repositories(repository1, repository2);
159+
getSnapshotsRequest.snapshots(new String[]{snapshot1, snapshot2});
160160
RequestConvertersTests.setRandomMasterTimeout(getSnapshotsRequest, expectedParams);
161161

162162
if (randomBoolean()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public void testSnapshotGetSnapshots() throws IOException {
590590
// end::get-snapshots-request
591591

592592
// tag::get-snapshots-request-repositoryName
593-
request.repository(repositoryName); // <1>
593+
request.repositories(repositoryName); // <1>
594594
// end::get-snapshots-request-repositoryName
595595

596596
// tag::get-snapshots-request-snapshots
@@ -616,7 +616,7 @@ public void testSnapshotGetSnapshots() throws IOException {
616616
// end::get-snapshots-execute
617617

618618
// tag::get-snapshots-response
619-
List<SnapshotInfo> snapshotsInfos = response.getSnapshots();
619+
List<SnapshotInfo> snapshotsInfos = response.getSnapshots(repositoryName);
620620
SnapshotInfo snapshotInfo = snapshotsInfos.get(0);
621621
RestStatus restStatus = snapshotInfo.status(); // <1>
622622
SnapshotId snapshotId = snapshotInfo.snapshotId(); // <2>

docs/reference/cat/snapshots.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Which looks like:
1818

1919
[source,txt]
2020
--------------------------------------------------
21-
id status start_epoch start_time end_epoch end_time duration indices successful_shards failed_shards total_shards
22-
snap1 FAILED 1445616705 18:11:45 1445616978 18:16:18 4.6m 1 4 1 5
23-
snap2 SUCCESS 1445634298 23:04:58 1445634672 23:11:12 6.2m 2 10 0 10
21+
id repository status start_epoch start_time end_epoch end_time duration indices successful_shards failed_shards total_shards
22+
snap1 repo1 FAILED 1445616705 18:11:45 1445616978 18:16:18 4.6m 1 4 1 5
23+
snap2 repo1 SUCCESS 1445634298 23:04:58 1445634672 23:11:12 6.2m 2 10 0 10
2424
--------------------------------------------------
2525
// TESTRESPONSE[s/FAILED/SUCCESS/ s/14456\d+/\\d+/ s/\d+(\.\d+)?(m|s|ms)/\\d+(\\.\\d+)?(m|s|ms)/]
2626
// TESTRESPONSE[s/\d+:\d+:\d+/\\d+:\\d+:\\d+/]

modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLSnapshotRestoreTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void testUrlRepository() throws Exception {
8888
.prepareGetSnapshots("test-repo")
8989
.setSnapshots("test-snap")
9090
.get()
91-
.getSnapshots()
91+
.getSnapshots("test-repo")
9292
.get(0)
9393
.state();
9494
assertThat(state, equalTo(SnapshotState.SUCCESS));
@@ -116,16 +116,16 @@ public void testUrlRepository() throws Exception {
116116

117117
logger.info("--> list available shapshots");
118118
GetSnapshotsResponse getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
119-
assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
120-
assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1));
119+
assertThat(getSnapshotsResponse.getSnapshots("url-repo"), notNullValue());
120+
assertThat(getSnapshotsResponse.getSnapshots("url-repo").size(), equalTo(1));
121121

122122
logger.info("--> delete snapshot");
123123
AcknowledgedResponse deleteSnapshotResponse = client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get();
124124
assertAcked(deleteSnapshotResponse);
125125

126126
logger.info("--> list available shapshot again, no snapshots should be returned");
127127
getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
128-
assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
129-
assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(0));
128+
assertThat(getSnapshotsResponse.getSnapshots("url-repo"), notNullValue());
129+
assertThat(getSnapshotsResponse.getSnapshots("url-repo").size(), equalTo(0));
130130
}
131131
}

plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void testSimpleWorkflow() {
9191
.prepareGetSnapshots("test-repo")
9292
.setSnapshots("test-snap")
9393
.get()
94-
.getSnapshots()
94+
.getSnapshots("test-repo")
9595
.get(0)
9696
.state(),
9797
equalTo(SnapshotState.SUCCESS));

rest-api-spec/src/main/resources/rest-api-spec/test/cat.snapshots/10_basic.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
---
22
"Help":
3+
- skip:
4+
version: " - 7.9.99"
5+
reason: Repository field added in 8.0
6+
37
- do:
48
cat.snapshots:
59
help: true
610

711
- match:
812
$body: |
913
/^ id .+ \n
14+
repository .+ \n
1015
status .+ \n
1116
start_epoch .+ \n
1217
start_time .+ \n
@@ -21,6 +26,9 @@
2126
$/
2227
---
2328
"Test cat snapshots output":
29+
- skip:
30+
version: " - 7.9.99"
31+
reason: Repository field added in 8.0
2432

2533
- do:
2634
snapshot.create_repository:
@@ -74,6 +82,6 @@
7482

7583
- match:
7684
$body: |
77-
/^ snap1\s+ SUCCESS\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \S+\s+ 2\s+ 2\s+ 0\s+ 2\s*\n
78-
snap2\s+ SUCCESS\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \S+\s+ 2\s+ 2\s+ 0\s+ 2\s*\n
85+
/^ snap1\s+ test_cat_snapshots_1\+ SUCCESS\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \S+\s+ 2\s+ 2\s+ 0\s+ 2\s*\n
86+
snap2\s+ test_cat_snapshots_1\+ SUCCESS\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \S+\s+ 2\s+ 2\s+ 0\s+ 2\s*\n
7987
$/

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/GetRepositoriesResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public class GetRepositoriesResponse extends ActionResponse implements ToXConten
4949
this.repositories = repositories;
5050
}
5151

52+
public GetRepositoriesResponse(StreamInput in) throws IOException {
53+
readFrom(in);
54+
}
55+
5256
/**
5357
* List of repositories to return
5458
*

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsRequest.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.cluster.snapshots.get;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.ActionRequestValidationException;
2324
import org.elasticsearch.action.support.master.MasterNodeRequest;
2425
import org.elasticsearch.common.Strings;
@@ -37,8 +38,9 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
3738
public static final String ALL_SNAPSHOTS = "_all";
3839
public static final String CURRENT_SNAPSHOT = "_current";
3940
public static final boolean DEFAULT_VERBOSE_MODE = true;
41+
public static final Version MULTIPLE_REPOSITORIES_SUPPORT_ADDED = Version.V_8_0_0;
4042

41-
private String repository;
43+
private String[] repositories;
4244

4345
private String[] snapshots = Strings.EMPTY_ARRAY;
4446

@@ -50,28 +52,32 @@ public GetSnapshotsRequest() {
5052
}
5153

5254
/**
53-
* Constructs a new get snapshots request with given repository name and list of snapshots
55+
* Constructs a new get snapshots request with given repository names and list of snapshots
5456
*
55-
* @param repository repository name
57+
* @param repositories repository names
5658
* @param snapshots list of snapshots
5759
*/
58-
public GetSnapshotsRequest(String repository, String[] snapshots) {
59-
this.repository = repository;
60+
public GetSnapshotsRequest(String[] repositories, String[] snapshots) {
61+
this.repositories = repositories;
6062
this.snapshots = snapshots;
6163
}
6264

6365
/**
64-
* Constructs a new get snapshots request with given repository name
66+
* Constructs a new get snapshots request with given repository names
6567
*
66-
* @param repository repository name
68+
* @param repositories repository names
6769
*/
68-
public GetSnapshotsRequest(String repository) {
69-
this.repository = repository;
70+
public GetSnapshotsRequest(String... repositories) {
71+
this.repositories = repositories;
7072
}
7173

7274
public GetSnapshotsRequest(StreamInput in) throws IOException {
7375
super(in);
74-
repository = in.readString();
76+
if (in.getVersion().onOrAfter(MULTIPLE_REPOSITORIES_SUPPORT_ADDED)) {
77+
repositories = in.readStringArray();
78+
} else {
79+
repositories = new String[]{in.readString()};
80+
}
7581
snapshots = in.readStringArray();
7682
ignoreUnavailable = in.readBoolean();
7783
verbose = in.readBoolean();
@@ -80,7 +86,11 @@ public GetSnapshotsRequest(StreamInput in) throws IOException {
8086
@Override
8187
public void writeTo(StreamOutput out) throws IOException {
8288
super.writeTo(out);
83-
out.writeString(repository);
89+
if (out.getVersion().onOrAfter(MULTIPLE_REPOSITORIES_SUPPORT_ADDED)) {
90+
out.writeStringArray(repositories);
91+
} else {
92+
out.writeString(repositories[0]);
93+
}
8494
out.writeStringArray(snapshots);
8595
out.writeBoolean(ignoreUnavailable);
8696
out.writeBoolean(verbose);
@@ -89,30 +99,30 @@ public void writeTo(StreamOutput out) throws IOException {
8999
@Override
90100
public ActionRequestValidationException validate() {
91101
ActionRequestValidationException validationException = null;
92-
if (repository == null) {
93-
validationException = addValidationError("repository is missing", validationException);
102+
if (repositories == null || repositories.length == 0) {
103+
validationException = addValidationError("repositories are missing", validationException);
94104
}
95105
return validationException;
96106
}
97107

98108
/**
99-
* Sets repository name
109+
* Sets repository names
100110
*
101-
* @param repository repository name
111+
* @param repositories repository names
102112
* @return this request
103113
*/
104-
public GetSnapshotsRequest repository(String repository) {
105-
this.repository = repository;
114+
public GetSnapshotsRequest repositories(String... repositories) {
115+
this.repositories = repositories;
106116
return this;
107117
}
108118

109119
/**
110-
* Returns repository name
120+
* Returns repository names
111121
*
112-
* @return repository name
122+
* @return repository names
113123
*/
114-
public String repository() {
115-
return this.repository;
124+
public String[] repositories() {
125+
return this.repositories;
116126
}
117127

118128
/**
@@ -176,4 +186,4 @@ public boolean verbose() {
176186
public void readFrom(StreamInput in) throws IOException {
177187
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
178188
}
179-
}
189+
}

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsRequestBuilder.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,20 @@ public class GetSnapshotsRequestBuilder extends MasterNodeOperationRequestBuilde
3030
GetSnapshotsResponse, GetSnapshotsRequestBuilder> {
3131

3232
/**
33-
* Constructs the new get snapshot request
33+
* Constructs the new get snapshot request with specified repositories
3434
*/
35-
public GetSnapshotsRequestBuilder(ElasticsearchClient client, GetSnapshotsAction action) {
36-
super(client, action, new GetSnapshotsRequest());
35+
public GetSnapshotsRequestBuilder(ElasticsearchClient client, GetSnapshotsAction action, String... repositories) {
36+
super(client, action, new GetSnapshotsRequest(repositories));
3737
}
3838

3939
/**
40-
* Constructs the new get snapshot request with specified repository
41-
*/
42-
public GetSnapshotsRequestBuilder(ElasticsearchClient client, GetSnapshotsAction action, String repository) {
43-
super(client, action, new GetSnapshotsRequest(repository));
44-
}
45-
46-
/**
47-
* Sets the repository name
40+
* Sets the repository names
4841
*
49-
* @param repository repository name
42+
* @param repositories repository names
5043
* @return this builder
5144
*/
52-
public GetSnapshotsRequestBuilder setRepository(String repository) {
53-
request.repository(repository);
45+
public GetSnapshotsRequestBuilder setRepositories(String... repositories) {
46+
request.repositories(repositories);
5447
return this;
5548
}
5649

0 commit comments

Comments
 (0)