Skip to content

Commit c1e9590

Browse files
Pagination and Sorting for Get Snapshots API (#73952)
Pagination and snapshots for get snapshots API, build on top of the current implementation to enable work that needs this API for testing. A follow-up will leverage the changes to make things more efficient via pagination. Relates #73570 which does part of the under-the-hood changes required to efficiently implement this API on the repository layer.
1 parent b1803e4 commit c1e9590

File tree

10 files changed

+1012
-25
lines changed

10 files changed

+1012
-25
lines changed

docs/reference/snapshot-restore/apis/get-snapshot-api.asciidoc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,37 @@ comprising the number of shards in the index, the total size of the index in
100100
bytes, and the maximum number of segments per shard in the index. Defaults to
101101
`false`, meaning that this information is omitted.
102102

103+
`sort`::
104+
(Optional, string)
105+
Allows setting a sort order for the result. Defaults to `start_time`, i.e. sorting by snapshot start time stamp.
106+
+
107+
.Valid values for `sort`
108+
[%collapsible%open]
109+
====
110+
`start_time`::
111+
Sort snapshots by their start time stamp and break ties by snapshot name.
112+
113+
`duration`::
114+
Sort snapshots by their duration and break ties by snapshot name.
115+
116+
`name`::
117+
Sort snapshots by their name.
118+
119+
`index_count`::
120+
Sort snapshots by the number of indices they contain and break ties by snapshot name.
121+
====
122+
123+
`size`::
124+
(Optional, integer)
125+
Maximum number of snapshots to return. Defaults to `0` which means return all that match the request without limit.
126+
127+
`order`::
128+
(Optional, string)
129+
Sort order. Valid values are `asc` for ascending and `desc` for descending order. Defaults to `asc`, meaning ascending order.
130+
131+
NOTE: The pagination parameters `size`, `order`, and `sort` are not supported when using `verbose=false` and the sort order for
132+
requests with `verbose=false` is undefined.
133+
103134
[role="child_attributes"]
104135
[[get-snapshot-api-response-body]]
105136
==== {api-response-body-title}
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.http.snapshots;
10+
11+
import org.apache.http.client.methods.HttpGet;
12+
import org.elasticsearch.action.ActionFuture;
13+
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
14+
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest;
15+
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse;
16+
import org.elasticsearch.client.Request;
17+
import org.elasticsearch.client.Response;
18+
import org.elasticsearch.common.settings.Settings;
19+
import org.elasticsearch.common.xcontent.DeprecationHandler;
20+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.common.xcontent.json.JsonXContent;
23+
import org.elasticsearch.search.sort.SortOrder;
24+
import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase;
25+
import org.elasticsearch.snapshots.SnapshotInfo;
26+
import org.elasticsearch.threadpool.ThreadPool;
27+
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.util.ArrayList;
31+
import java.util.Collection;
32+
import java.util.HashSet;
33+
import java.util.List;
34+
35+
import static org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase.assertSnapshotListSorted;
36+
import static org.hamcrest.Matchers.in;
37+
import static org.hamcrest.Matchers.is;
38+
39+
// TODO: dry up duplication across this suite and org.elasticsearch.snapshots.GetSnapshotsIT more
40+
public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
41+
42+
@Override
43+
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
44+
return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings))
45+
.put(ThreadPool.ESTIMATED_TIME_INTERVAL_SETTING.getKey(), 0) // We have tests that check by-timestamp order
46+
.build();
47+
}
48+
49+
public void testSortOrder() throws Exception {
50+
final String repoName = "test-repo";
51+
AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs");
52+
final List<String> snapshotNamesWithoutIndex =
53+
AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20));
54+
55+
createIndexWithContent("test-index");
56+
57+
final List<String> snapshotNamesWithIndex =
58+
AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20));
59+
60+
final Collection<String> allSnapshotNames = new HashSet<>(snapshotNamesWithIndex);
61+
allSnapshotNames.addAll(snapshotNamesWithoutIndex);
62+
doTestSortOrder(repoName, allSnapshotNames, SortOrder.ASC);
63+
doTestSortOrder(repoName, allSnapshotNames, SortOrder.DESC);
64+
}
65+
66+
private void doTestSortOrder(String repoName, Collection<String> allSnapshotNames, SortOrder order) throws IOException {
67+
final List<SnapshotInfo> defaultSorting =
68+
clusterAdmin().prepareGetSnapshots(repoName).setOrder(order).get().getSnapshots(repoName);
69+
assertSnapshotListSorted(defaultSorting, null, order);
70+
assertSnapshotListSorted(
71+
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.NAME, order),
72+
GetSnapshotsRequest.SortBy.NAME,
73+
order
74+
);
75+
assertSnapshotListSorted(
76+
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.DURATION, order),
77+
GetSnapshotsRequest.SortBy.DURATION,
78+
order
79+
);
80+
assertSnapshotListSorted(
81+
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.INDICES, order),
82+
GetSnapshotsRequest.SortBy.INDICES,
83+
order
84+
);
85+
assertSnapshotListSorted(
86+
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.START_TIME, order),
87+
GetSnapshotsRequest.SortBy.START_TIME,
88+
order
89+
);
90+
}
91+
92+
public void testResponseSizeLimit() throws Exception {
93+
final String repoName = "test-repo";
94+
AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs");
95+
final List<String> names = AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(6, 20));
96+
for (GetSnapshotsRequest.SortBy sort : GetSnapshotsRequest.SortBy.values()) {
97+
for (SortOrder order : SortOrder.values()) {
98+
logger.info("--> testing pagination for [{}] [{}]", sort, order);
99+
doTestPagination(repoName, names, sort, order);
100+
}
101+
}
102+
}
103+
104+
private void doTestPagination(String repoName,
105+
List<String> names,
106+
GetSnapshotsRequest.SortBy sort,
107+
SortOrder order) throws IOException {
108+
final List<SnapshotInfo> allSnapshotsSorted = allSnapshotsSorted(names, repoName, sort, order);
109+
final List<SnapshotInfo> batch1 = sortedWithLimit(repoName, sort, 2, order);
110+
assertEquals(batch1, allSnapshotsSorted.subList(0, 2));
111+
final List<SnapshotInfo> batch2 = sortedWithLimit(repoName, sort, batch1.get(1), 2, order);
112+
assertEquals(batch2, allSnapshotsSorted.subList(2, 4));
113+
final int lastBatch = names.size() - batch1.size() - batch2.size();
114+
final List<SnapshotInfo> batch3 = sortedWithLimit(repoName, sort, batch2.get(1), lastBatch, order);
115+
assertEquals(batch3, allSnapshotsSorted.subList(batch1.size() + batch2.size(), names.size()));
116+
final List<SnapshotInfo> batch3NoLimit =
117+
sortedWithLimit(repoName, sort, batch2.get(1), GetSnapshotsRequest.NO_LIMIT, order);
118+
assertEquals(batch3, batch3NoLimit);
119+
final List<SnapshotInfo> batch3LargeLimit = sortedWithLimit(
120+
repoName,
121+
sort,
122+
batch2.get(1),
123+
lastBatch + randomIntBetween(1, 100),
124+
order
125+
);
126+
assertEquals(batch3, batch3LargeLimit);
127+
}
128+
129+
public void testSortAndPaginateWithInProgress() throws Exception {
130+
final String repoName = "test-repo";
131+
AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "mock");
132+
final Collection<String> allSnapshotNames =
133+
new HashSet<>(AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20)));
134+
createIndexWithContent("test-index-1");
135+
allSnapshotNames.addAll(AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20)));
136+
createIndexWithContent("test-index-2");
137+
138+
final int inProgressCount = randomIntBetween(6, 20);
139+
final List<ActionFuture<CreateSnapshotResponse>> inProgressSnapshots = new ArrayList<>(inProgressCount);
140+
AbstractSnapshotIntegTestCase.blockAllDataNodes(repoName);
141+
for (int i = 0; i < inProgressCount; i++) {
142+
final String snapshotName = "snap-" + i;
143+
allSnapshotNames.add(snapshotName);
144+
inProgressSnapshots.add(AbstractSnapshotIntegTestCase.startFullSnapshot(logger, repoName, snapshotName, false));
145+
}
146+
AbstractSnapshotIntegTestCase.awaitNumberOfSnapshotsInProgress(logger, inProgressCount);
147+
148+
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.START_TIME);
149+
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.NAME);
150+
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES);
151+
152+
AbstractSnapshotIntegTestCase.unblockAllDataNodes(repoName);
153+
for (ActionFuture<CreateSnapshotResponse> inProgressSnapshot : inProgressSnapshots) {
154+
AbstractSnapshotIntegTestCase.assertSuccessful(logger, inProgressSnapshot);
155+
}
156+
157+
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.START_TIME);
158+
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.NAME);
159+
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES);
160+
}
161+
162+
private void createIndexWithContent(String indexName) {
163+
logger.info("--> creating index [{}]", indexName);
164+
createIndex(indexName, AbstractSnapshotIntegTestCase.SINGLE_SHARD_NO_REPLICA);
165+
ensureGreen(indexName);
166+
indexDoc(indexName, "some_id", "foo", "bar");
167+
}
168+
169+
private static void assertStablePagination(String repoName,
170+
Collection<String> allSnapshotNames,
171+
GetSnapshotsRequest.SortBy sort) throws IOException {
172+
final SortOrder order = randomFrom(SortOrder.values());
173+
final List<SnapshotInfo> allSorted = allSnapshotsSorted(allSnapshotNames, repoName, sort, order);
174+
175+
for (int i = 1; i <= allSnapshotNames.size(); i++) {
176+
final List<SnapshotInfo> subsetSorted = sortedWithLimit(repoName, sort, i, order);
177+
assertEquals(subsetSorted, allSorted.subList(0, i));
178+
}
179+
180+
for (int j = 0; j < allSnapshotNames.size(); j++) {
181+
final SnapshotInfo after = allSorted.get(j);
182+
for (int i = 1; i < allSnapshotNames.size() - j; i++) {
183+
final List<SnapshotInfo> subsetSorted = sortedWithLimit(repoName, sort, after, i, order);
184+
assertEquals(subsetSorted, allSorted.subList(j + 1, j + i + 1));
185+
}
186+
}
187+
}
188+
189+
private static List<SnapshotInfo> allSnapshotsSorted(Collection<String> allSnapshotNames,
190+
String repoName,
191+
GetSnapshotsRequest.SortBy sortBy,
192+
SortOrder order) throws IOException {
193+
final Request request = baseGetSnapshotsRequest(repoName);
194+
request.addParameter("sort", sortBy.toString());
195+
if (order == SortOrder.DESC || randomBoolean()) {
196+
request.addParameter("order", order.toString());
197+
}
198+
final Response response = getRestClient().performRequest(request);
199+
final List<SnapshotInfo> snapshotInfos = readSnapshotInfos(repoName, response);
200+
assertEquals(snapshotInfos.size(), allSnapshotNames.size());
201+
for (SnapshotInfo snapshotInfo : snapshotInfos) {
202+
assertThat(snapshotInfo.snapshotId().getName(), is(in(allSnapshotNames)));
203+
}
204+
return snapshotInfos;
205+
}
206+
207+
private static Request baseGetSnapshotsRequest(String repoName) {
208+
return new Request(HttpGet.METHOD_NAME, "/_snapshot/" + repoName + "/*");
209+
}
210+
211+
private static List<SnapshotInfo> sortedWithLimit(String repoName,
212+
GetSnapshotsRequest.SortBy sortBy,
213+
int size,
214+
SortOrder order) throws IOException {
215+
final Request request = baseGetSnapshotsRequest(repoName);
216+
request.addParameter("sort", sortBy.toString());
217+
if (order == SortOrder.DESC || randomBoolean()) {
218+
request.addParameter("order", order.toString());
219+
}
220+
request.addParameter("size", String.valueOf(size));
221+
final Response response = getRestClient().performRequest(request);
222+
return readSnapshotInfos(repoName, response);
223+
}
224+
225+
private static List<SnapshotInfo> readSnapshotInfos(String repoName, Response response) throws IOException {
226+
final List<SnapshotInfo> snapshotInfos;
227+
try (InputStream input = response.getEntity().getContent();
228+
XContentParser parser = JsonXContent.jsonXContent.createParser(
229+
NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, input)) {
230+
snapshotInfos = GetSnapshotsResponse.fromXContent(parser).getSnapshots(repoName);
231+
}
232+
return snapshotInfos;
233+
}
234+
235+
private static List<SnapshotInfo> sortedWithLimit(String repoName,
236+
GetSnapshotsRequest.SortBy sortBy,
237+
SnapshotInfo after,
238+
int size,
239+
SortOrder order) throws IOException {
240+
final Request request = baseGetSnapshotsRequest(repoName);
241+
request.addParameter("sort", sortBy.toString());
242+
if (size != GetSnapshotsRequest.NO_LIMIT || randomBoolean()) {
243+
request.addParameter("size", String.valueOf(size));
244+
}
245+
if (after != null) {
246+
request.addParameter("after", GetSnapshotsRequest.After.from(after, sortBy).value() + "," + after.snapshotId().getName());
247+
}
248+
if (order == SortOrder.DESC || randomBoolean()) {
249+
request.addParameter("order", order.toString());
250+
}
251+
final Response response = getRestClient().performRequest(request);
252+
return readSnapshotInfos(repoName, response);
253+
}
254+
}

server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ public void onRequestSent(
10561056
final ActionFuture<AcknowledgedResponse> deleteResponse = startDeleteSnapshot(repoName, snapshotName);
10571057

10581058
awaitClusterState(
1059+
logger,
10591060
otherDataNode,
10601061
state -> state.custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY)
10611062
.entries()

0 commit comments

Comments
 (0)