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

Implement from_sort_value Parameter in Get Snapshots API #77618

Merged
merged 15 commits into from Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 90 additions & 4 deletions docs/reference/snapshot-restore/apis/get-snapshot-api.asciidoc
Expand Up @@ -140,14 +140,19 @@ 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.

`after_value`::
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should call the parameter after_sort_value to make the coupling to sort explicit in the name?

I am still pondering on after_. Reading your text, perhaps start_sort_value is better, though I am not really contend with that either...

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure about this or the alternatives. How about just from maybe? It's short and it indicates "start" + "inclusive"?

Copy link
Contributor

Choose a reason for hiding this comment

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

I like from, but perhaps still qualify it as from_sort_value? I think that makes it easier to see from the request what it means.

(Optional, string)
Value of the current sort column at which to start pagination. Can either be a string snapshot- or repository name when sorting by
original-brownbear marked this conversation as resolved.
Show resolved Hide resolved
snapshot or repository name, a millisecond time value or a number when sorting by index- or shard count.

`after`::
(Optional, string)
Offset identifier to start pagination from as returned by the `next` field in the response body.

`offset`::
(Optional, integer)
Numeric offset to start pagination from based on the snapshots matching this request. Using a non-zero value for this parameter is mutually
exclusive with using the `after` parameter. Defaults to `0`.
exclusive with using the `after` or `after_value` parameters. Defaults to `0`.

`slm_policy_filter`::
(Optional, string)
Expand All @@ -158,11 +163,11 @@ created by an SLM policy but not those snapshots that were not created by an SLM
policy you can use the special pattern `_none` that will match all snapshots without an SLM policy.

NOTE: The `after` parameter and `next` field allow for iterating through snapshots with some consistency guarantees regarding concurrent
creation or deletion of snapshots. It is guaranteed that any snapshot that exists at the beginning of the iteration and not concurrently
creation or deletion of snapshots. It is guaranteed that any snapshot that exists at the beginning of the iteration and is not concurrently
deleted will be seen during the iteration. Snapshots concurrently created may be seen during an iteration.

NOTE: The parameters `size`, `order`, `after`, `offset`, `slm_policy_filter` and `sort` are not supported when using `verbose=false` and
the sort order for requests with `verbose=false` is undefined.
NOTE: The parameters `size`, `order`, `after`, `after_value`, `offset`, `slm_policy_filter` and `sort` are not supported when using
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also note the incompatibility of after_value with offset and after?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well now that you pointed it out below, it would only be with "after" in the final thing which I'd code up very shortly after this one. Not sure it's worth the added text though, it obviously makes no sense setting both, the request validation will tell you so and we already point this out in the after section?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see you added the incompatibility note to offset, I missed that on my initial read. I think it still makes sense to add to either after or after_value that they are incompatible. I could see this being slightly surprising, since after_value is like a filter so it could be surprising that it does not work with pagination using after (and this could slip through some UI testing).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, added a note to after :)

`verbose=false` and the sort order for requests with `verbose=false` is undefined.

[role="child_attributes"]
[[get-snapshot-api-response-body]]
Expand Down Expand Up @@ -628,3 +633,84 @@ The API returns the following response:
// 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/]


The following request returns information for all snapshots that come after `snapshot_2` when sorted by snapshot name in the default
Copy link
Member

Choose a reason for hiding this comment

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

I suspect that sorting after a given start/end time will be the most common use case. Maybe it's worth showing as an example?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd love to but I don't see a way of doing this with a test because the timestamp on the request would have to be dynamic right?

Copy link
Member

Choose a reason for hiding this comment

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

the timestamp on the request would have to be dynamic right?

Or low enough to always return some snapshots in the response. But that's just a suggestion, let's ignore if that's too complicated or ugly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair point :) Added a docs run for this with a low timestamp now.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

ascending order.

[source,console]
----
GET /_snapshot/my_repository/*?sort=name&after_value=snapshot_2
----

The API returns the following response:

[source,console-result]
----
{
"snapshots": [
{
"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
}
},
{
"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
}
}
],
"total": 2,
"remaining": 0
}
----
// TESTRESPONSE[s/"uuid": "vdRctLCxSketdKb54xw67g"/"uuid": $body.snapshots.0.uuid/]
// TESTRESPONSE[s/"uuid": "dRctdKb54xw67gvLCxSket"/"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.130Z"/"start_time": $body.snapshots.0.start_time/]
// TESTRESPONSE[s/"start_time": "2020-07-06T21:55:18.129Z"/"start_time": $body.snapshots.1.start_time/]
// TESTRESPONSE[s/"start_time_in_millis": 1593093628851/"start_time_in_millis": $body.snapshots.0.start_time_in_millis/]
// TESTRESPONSE[s/"start_time_in_millis": 1593093628850/"start_time_in_millis": $body.snapshots.1.start_time_in_millis/]
// TESTRESPONSE[s/"end_time": "2020-07-06T21:55:18.130Z"/"end_time": $body.snapshots.0.end_time/]
// TESTRESPONSE[s/"end_time": "2020-07-06T21:55:18.129Z"/"end_time": $body.snapshots.1.end_time/]
// TESTRESPONSE[s/"end_time_in_millis": 1593094752019/"end_time_in_millis": $body.snapshots.0.end_time_in_millis/]
// TESTRESPONSE[s/"end_time_in_millis": 1593094752018/"end_time_in_millis": $body.snapshots.1.end_time_in_millis/]
// TESTRESPONSE[s/"duration_in_millis": 1/"duration_in_millis": $body.snapshots.0.duration_in_millis/]
// TESTRESPONSE[s/"duration_in_millis": 0/"duration_in_millis": $body.snapshots.1.duration_in_millis/]
Expand Up @@ -36,6 +36,7 @@
import java.util.Map;

import static org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase.assertSnapshotListSorted;
import static org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase.matchAllPattern;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -241,6 +242,62 @@ public void testFilterBySLMPolicy() throws Exception {
);
}

public void testSortAfterStartTime() throws IOException {
final String repoName = "test-repo";
AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs");
final SnapshotInfo snapshot1 = AbstractSnapshotIntegTestCase.createFullSnapshot(logger, repoName, "snapshot-1");
final SnapshotInfo snapshot2 = AbstractSnapshotIntegTestCase.createFullSnapshot(logger, repoName, "snapshot-2");
final SnapshotInfo snapshot3 = AbstractSnapshotIntegTestCase.createFullSnapshot(logger, repoName, "snapshot-3");

final List<SnapshotInfo> allSnapshotInfo = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME)
.get()
.getSnapshots();
assertThat(allSnapshotInfo, is(List.of(snapshot1, snapshot2, snapshot3)));

final long startTime1 = snapshot1.startTime();
final long startTime2 = snapshot2.startTime();
final long startTime3 = snapshot3.startTime();

assertThat(allAfterStartTimeAscending(startTime1 - 1), is(allSnapshotInfo));
assertThat(allAfterStartTimeAscending(startTime1), is(allSnapshotInfo));
assertThat(allAfterStartTimeAscending(startTime2), is(List.of(snapshot2, snapshot3)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure that snapshot1 and snapshot2 get different timestamps?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe not on Windows where the clock is less accurate actually ... let me make sure they will.

Copy link
Member Author

Choose a reason for hiding this comment

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

Adjusted the code to ensure the timestamps never collide now.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure I see that here in the rest test case, only in the internal cluster test? Maybe I missed it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh right missed this one, adding a fix here as well :)

assertThat(allAfterStartTimeAscending(startTime3), is(List.of(snapshot3)));
assertThat(allAfterStartTimeAscending(startTime3 + 1), empty());

final List<SnapshotInfo> allSnapshotInfoDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME)
.setOrder(SortOrder.DESC)
.get()
.getSnapshots();
assertThat(allSnapshotInfoDesc, is(List.of(snapshot3, snapshot2, snapshot1)));

assertThat(allBeforeStartTimeDescending(startTime3 + 1), is(allSnapshotInfoDesc));
assertThat(allBeforeStartTimeDescending(startTime3), is(allSnapshotInfoDesc));
assertThat(allBeforeStartTimeDescending(startTime2), is(List.of(snapshot2, snapshot1)));
assertThat(allBeforeStartTimeDescending(startTime1), is(List.of(snapshot1)));
assertThat(allBeforeStartTimeDescending(startTime1 - 1), empty());
}

private List<SnapshotInfo> allAfterStartTimeAscending(long timestamp) throws IOException {
final Request request = baseGetSnapshotsRequest("*");
request.addParameter("sort", GetSnapshotsRequest.SortBy.START_TIME.toString());
request.addParameter("after_value", String.valueOf(timestamp));
final Response response = getRestClient().performRequest(request);
return readSnapshotInfos(response).getSnapshots();
}

private List<SnapshotInfo> allBeforeStartTimeDescending(long timestamp) throws IOException {
final Request request = baseGetSnapshotsRequest("*");
request.addParameter("sort", GetSnapshotsRequest.SortBy.START_TIME.toString());
request.addParameter("after_value", String.valueOf(timestamp));
request.addParameter("order", SortOrder.DESC.toString());
final Response response = getRestClient().performRequest(request);
return readSnapshotInfos(response).getSnapshots();
}

private static List<SnapshotInfo> getAllSnapshotsForPolicies(String... policies) throws IOException {
final Request requestWithPolicy = new Request(HttpGet.METHOD_NAME, "/_snapshot/*/*");
requestWithPolicy.addParameter("slm_policy_filter", Strings.arrayToCommaDelimitedString(policies));
Expand Down