Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Next Field in Paginated GetSnapshots Response #74236

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 138 additions & 1 deletion docs/reference/snapshot-restore/apis/get-snapshot-api.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ PUT /_snapshot/my_repository

PUT /_snapshot/my_repository/my_snapshot?wait_for_completion=true

PUT /_snapshot/my_repository/snapshot_1?wait_for_completion=true
PUT /_snapshot/my_repository/snapshot_2?wait_for_completion=true
PUT /_snapshot/my_repository/snapshot_3?wait_for_completion=true
----
// TESTSETUP
////
Expand Down Expand Up @@ -128,7 +130,11 @@ Allows setting a sort order for the result. Defaults to `start_time`, i.e. sorti
(Optional, string)
Sort order. Valid values are `asc` for ascending and `desc` for descending order. Defaults to `asc`, meaning ascending order.

NOTE: The pagination parameters `size`, `order`, and `sort` are not supported when using `verbose=false` and the sort order for
`after`::
(Optional, string)
Offset identifier to start pagination from as returned by the `next` field in the response body.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is worth spending a few lines explaining that the underlying list can change and how pagination works in case of deletes and additions during pagination (i.e., you are sure to not miss any that were in the list originally and have not been deleted during pagination and you may see snapshots added during pagination).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ added a few lines on this


NOTE: The pagination parameters `size`, `order`, `after` and `sort` are not supported when using `verbose=false` and the sort order for
requests with `verbose=false` is undefined.

[role="child_attributes"]
Expand Down Expand Up @@ -268,6 +274,10 @@ The snapshot `state` can be one of the following values:
that were not processed correctly.
====
--
`next`::
(string)
If the request contained a size limit and there might be more results, a `next` field will be added to the response and can be used as the
`after` query parameter to fetch additional results.

[[get-snapshot-api-example]]
==== {api-examples-title}
Expand Down Expand Up @@ -319,3 +329,130 @@ The API returns the following response:
// TESTRESPONSE[s/"end_time": "2020-07-06T21:55:18.129Z"/"end_time": $body.snapshots.0.end_time/]
// TESTRESPONSE[s/"end_time_in_millis": 1593094752018/"end_time_in_millis": $body.snapshots.0.end_time_in_millis/]
// TESTRESPONSE[s/"duration_in_millis": 0/"duration_in_millis": $body.snapshots.0.duration_in_millis/]

The following request returns information for all snapshots with prefix `snapshot` in the `my_repository` repository,
limiting the response size to 2 and sorting by snapshot name.

[source,console]
----
GET /_snapshot/my_repository/snapshot*?size=2&sort=name
----

The API returns the following response:

[source,console-result]
----
{
"snapshots": [
{
"snapshot": "snapshot_1",
"uuid": "dKb54xw67gvdRctLCxSket",
"repository": "my_repository",
"version_id": <version_id>,
"version": <version>,
"indices": [],
"data_streams": [],
"feature_states": [],
"include_global_state": true,
"state": "SUCCESS",
"start_time": "2020-07-06T21:55:18.129Z",
"start_time_in_millis": 1593093628850,
"end_time": "2020-07-06T21:55:18.129Z",
"end_time_in_millis": 1593094752018,
"duration_in_millis": 0,
"failures": [],
"shards": {
"total": 0,
"failed": 0,
"successful": 0
}
},
{
"snapshot": "snapshot_2",
"uuid": "vdRctLCxSketdKb54xw67g",
"repository": "my_repository",
"version_id": <version_id>,
"version": <version>,
"indices": [],
"data_streams": [],
"feature_states": [],
"include_global_state": true,
"state": "SUCCESS",
"start_time": "2020-07-06T21:55:18.130Z",
"start_time_in_millis": 1593093628851,
"end_time": "2020-07-06T21:55:18.130Z",
"end_time_in_millis": 1593094752019,
"duration_in_millis": 1,
"failures": [],
"shards": {
"total": 0,
"failed": 0,
"successful": 0
}
}
],
"next": "snapshot_2,snapshot_2"
}
----
// TESTRESPONSE[s/"uuid": "dKb54xw67gvdRctLCxSket"/"uuid": $body.snapshots.0.uuid/]
// TESTRESPONSE[s/"uuid": "vdRctLCxSketdKb54xw67g"/"uuid": $body.snapshots.1.uuid/]
// TESTRESPONSE[s/"version_id": <version_id>/"version_id": $body.snapshots.0.version_id/]
// TESTRESPONSE[s/"version": <version>/"version": $body.snapshots.0.version/]
// TESTRESPONSE[s/"start_time": "2020-07-06T21:55:18.129Z"/"start_time": $body.snapshots.0.start_time/]
// TESTRESPONSE[s/"start_time": "2020-07-06T21:55:18.130Z"/"start_time": $body.snapshots.1.start_time/]
// TESTRESPONSE[s/"start_time_in_millis": 1593093628850/"start_time_in_millis": $body.snapshots.0.start_time_in_millis/]
// TESTRESPONSE[s/"start_time_in_millis": 1593093628851/"start_time_in_millis": $body.snapshots.1.start_time_in_millis/]
// TESTRESPONSE[s/"end_time": "2020-07-06T21:55:18.129Z"/"end_time": $body.snapshots.0.end_time/]
// TESTRESPONSE[s/"end_time": "2020-07-06T21:55:18.130Z"/"end_time": $body.snapshots.1.end_time/]
// TESTRESPONSE[s/"end_time_in_millis": 1593094752018/"end_time_in_millis": $body.snapshots.0.end_time_in_millis/]
// TESTRESPONSE[s/"end_time_in_millis": 1593094752019/"end_time_in_millis": $body.snapshots.1.end_time_in_millis/]
// TESTRESPONSE[s/"duration_in_millis": 0/"duration_in_millis": $body.snapshots.0.duration_in_millis/]
// TESTRESPONSE[s/"duration_in_millis": 1/"duration_in_millis": $body.snapshots.1.duration_in_millis/]

A subsequent request for the remaining snapshots can then be made using the `next` value from the previous response as `after` parameter.

[source,console]
----
GET /_snapshot/my_repository/snapshot*?size=2&sort=name&after=snapshot_2,snapshot_2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry we will have users running into encoding problems with this API, since we permit characters like & and % in snapshot names and the effects of failing to encode this parameter properly might be pretty subtle. Could we base64-encode this parameter to avoid all that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ will do :)

----

The API returns the following response:

[source,console-result]
----
{
"snapshots": [
{
"snapshot": "snapshot_3",
"uuid": "dRctdKb54xw67gvLCxSket",
"repository": "my_repository",
"version_id": <version_id>,
"version": <version>,
"indices": [],
"data_streams": [],
"feature_states": [],
"include_global_state": true,
"state": "SUCCESS",
"start_time": "2020-07-06T21:55:18.129Z",
"start_time_in_millis": 1593093628850,
"end_time": "2020-07-06T21:55:18.129Z",
"end_time_in_millis": 1593094752018,
"duration_in_millis": 0,
"failures": [],
"shards": {
"total": 0,
"failed": 0,
"successful": 0
}
}
]
}
----
// TESTRESPONSE[s/"uuid": "dRctdKb54xw67gvLCxSket"/"uuid": $body.snapshots.0.uuid/]
// TESTRESPONSE[s/"version_id": <version_id>/"version_id": $body.snapshots.0.version_id/]
// TESTRESPONSE[s/"version": <version>/"version": $body.snapshots.0.version/]
// TESTRESPONSE[s/"start_time": "2020-07-06T21:55:18.129Z"/"start_time": $body.snapshots.0.start_time/]
// TESTRESPONSE[s/"start_time_in_millis": 1593093628850/"start_time_in_millis": $body.snapshots.0.start_time_in_millis/]
// TESTRESPONSE[s/"end_time": "2020-07-06T21:55:18.129Z"/"end_time": $body.snapshots.0.end_time/]
// TESTRESPONSE[s/"end_time_in_millis": 1593094752018/"end_time_in_millis": $body.snapshots.0.end_time_in_millis/]
// TESTRESPONSE[s/"duration_in_millis": 0/"duration_in_millis": $body.snapshots.0.duration_in_millis/]
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase;
import org.elasticsearch.snapshots.SnapshotInfo;
Expand Down Expand Up @@ -64,8 +65,7 @@ public void testSortOrder() throws Exception {
}

private void doTestSortOrder(String repoName, Collection<String> allSnapshotNames, SortOrder order) throws IOException {
final List<SnapshotInfo> defaultSorting =
clusterAdmin().prepareGetSnapshots(repoName).setOrder(order).get().getSnapshots();
final List<SnapshotInfo> defaultSorting = clusterAdmin().prepareGetSnapshots(repoName).setOrder(order).get().getSnapshots();
assertSnapshotListSorted(defaultSorting, null, order);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.NAME, order),
Expand Down Expand Up @@ -106,24 +106,31 @@ private void doTestPagination(String repoName,
GetSnapshotsRequest.SortBy sort,
SortOrder order) throws IOException {
final List<SnapshotInfo> allSnapshotsSorted = allSnapshotsSorted(names, repoName, sort, order);
final List<SnapshotInfo> batch1 = sortedWithLimit(repoName, sort, 2, order);
assertEquals(batch1, allSnapshotsSorted.subList(0, 2));
final List<SnapshotInfo> batch2 = sortedWithLimit(repoName, sort, batch1.get(1), 2, order);
assertEquals(batch2, allSnapshotsSorted.subList(2, 4));
final int lastBatch = names.size() - batch1.size() - batch2.size();
final List<SnapshotInfo> batch3 = sortedWithLimit(repoName, sort, batch2.get(1), lastBatch, order);
assertEquals(batch3, allSnapshotsSorted.subList(batch1.size() + batch2.size(), names.size()));
final List<SnapshotInfo> batch3NoLimit =
sortedWithLimit(repoName, sort, batch2.get(1), GetSnapshotsRequest.NO_LIMIT, order);
assertEquals(batch3, batch3NoLimit);
final List<SnapshotInfo> batch3LargeLimit = sortedWithLimit(
final Tuple<String, List<SnapshotInfo>> batch1 = sortedWithLimit(repoName, sort, null, 2, order);
assertEquals(allSnapshotsSorted.subList(0, 2), batch1.v2());
final Tuple<String, List<SnapshotInfo>> batch2 = sortedWithLimit(repoName, sort, batch1.v1(), 2, order);
assertEquals(allSnapshotsSorted.subList(2, 4), batch2.v2());
final int lastBatch = names.size() - batch1.v2().size() - batch2.v2().size();
final Tuple<String, List<SnapshotInfo>> batch3 = sortedWithLimit(repoName, sort, batch2.v1(), lastBatch, order);
assertEquals(batch3.v2(), allSnapshotsSorted.subList(batch1.v2().size() + batch2.v2().size(), names.size()));
final Tuple<String, List<SnapshotInfo>> batch3NoLimit = sortedWithLimit(
repoName,
sort,
batch2.get(1),
batch2.v1(),
GetSnapshotsRequest.NO_LIMIT,
order
);
assertNull(batch3NoLimit.v1());
assertEquals(batch3.v2(), batch3NoLimit.v2());
final Tuple<String, List<SnapshotInfo>> batch3LargeLimit = sortedWithLimit(
repoName,
sort,
batch2.v1(),
lastBatch + randomIntBetween(1, 100),
order
);
assertEquals(batch3, batch3LargeLimit);
assertEquals(batch3.v2(), batch3LargeLimit.v2());
assertNull(batch3LargeLimit.v1());
}

public void testSortAndPaginateWithInProgress() throws Exception {
Expand Down Expand Up @@ -173,14 +180,15 @@ private static void assertStablePagination(String repoName,
final List<SnapshotInfo> allSorted = allSnapshotsSorted(allSnapshotNames, repoName, sort, order);

for (int i = 1; i <= allSnapshotNames.size(); i++) {
final List<SnapshotInfo> subsetSorted = sortedWithLimit(repoName, sort, i, order);
final List<SnapshotInfo> subsetSorted = sortedWithLimit(repoName, sort, i, order).v2();
assertEquals(subsetSorted, allSorted.subList(0, i));
}

for (int j = 0; j < allSnapshotNames.size(); j++) {
final SnapshotInfo after = allSorted.get(j);
for (int i = 1; i < allSnapshotNames.size() - j; i++) {
final List<SnapshotInfo> subsetSorted = sortedWithLimit(repoName, sort, after, i, order);
final List<SnapshotInfo> subsetSorted = sortedWithLimit(
repoName, sort, GetSnapshotsRequest.After.from(after, sort).asQueryParam(), i, order).v2();
assertEquals(subsetSorted, allSorted.subList(j + 1, j + i + 1));
}
}
Expand All @@ -196,7 +204,7 @@ private static List<SnapshotInfo> allSnapshotsSorted(Collection<String> allSnaps
request.addParameter("order", order.toString());
}
final Response response = getRestClient().performRequest(request);
final List<SnapshotInfo> snapshotInfos = readSnapshotInfos(response);
final List<SnapshotInfo> snapshotInfos = readSnapshotInfos(response).v2();
assertEquals(snapshotInfos.size(), allSnapshotNames.size());
for (SnapshotInfo snapshotInfo : snapshotInfos) {
assertThat(snapshotInfo.snapshotId().getName(), is(in(allSnapshotNames)));
Expand All @@ -208,10 +216,10 @@ private static Request baseGetSnapshotsRequest(String repoName) {
return new Request(HttpGet.METHOD_NAME, "/_snapshot/" + repoName + "/*");
}

private static List<SnapshotInfo> sortedWithLimit(String repoName,
GetSnapshotsRequest.SortBy sortBy,
int size,
SortOrder order) throws IOException {
private static Tuple<String, List<SnapshotInfo>> sortedWithLimit(String repoName,
GetSnapshotsRequest.SortBy sortBy,
int size,
SortOrder order) throws IOException {
final Request request = baseGetSnapshotsRequest(repoName);
original-brownbear marked this conversation as resolved.
Show resolved Hide resolved
request.addParameter("sort", sortBy.toString());
if (order == SortOrder.DESC || randomBoolean()) {
Expand All @@ -222,19 +230,18 @@ private static List<SnapshotInfo> sortedWithLimit(String repoName,
return readSnapshotInfos(response);
}

private static List<SnapshotInfo> readSnapshotInfos(Response response) throws IOException {
final List<SnapshotInfo> snapshotInfos;
private static Tuple<String, List<SnapshotInfo>> readSnapshotInfos(Response response) throws IOException {
try (InputStream input = response.getEntity().getContent();
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, input)) {
snapshotInfos = GetSnapshotsResponse.fromXContent(parser).getSnapshots();
final GetSnapshotsResponse getSnapshotsResponse = GetSnapshotsResponse.fromXContent(parser);
return Tuple.tuple(getSnapshotsResponse.next(), getSnapshotsResponse.getSnapshots());
}
return snapshotInfos;
}

private static List<SnapshotInfo> sortedWithLimit(String repoName,
private static Tuple<String, List<SnapshotInfo>> sortedWithLimit(String repoName,
GetSnapshotsRequest.SortBy sortBy,
SnapshotInfo after,
String after,
int size,
SortOrder order) throws IOException {
final Request request = baseGetSnapshotsRequest(repoName);
Expand All @@ -243,7 +250,7 @@ private static List<SnapshotInfo> sortedWithLimit(String repoName,
request.addParameter("size", String.valueOf(size));
}
if (after != null) {
request.addParameter("after", GetSnapshotsRequest.After.from(after, sortBy).value() + "," + after.snapshotId().getName());
request.addParameter("after", after);
}
if (order == SortOrder.DESC || randomBoolean()) {
request.addParameter("order", order.toString());
Expand Down
Loading