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) #79318

Merged
merged 1 commit into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
205 changes: 201 additions & 4 deletions docs/reference/snapshot-restore/apis/get-snapshot-api.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,15 @@ 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.

`from_sort_value`::
(Optional, string)
Value of the current sort column at which to start retrieval. Can either be a string snapshot- or repository name when sorting by
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 identifier to start pagination from as returned by the `next` field in the response body. Using this parameter is mutually exclusive
with using the `from_sort_value` parameter.

`offset`::
(Optional, integer)
Expand All @@ -158,11 +164,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`, `from_sort_value`, `offset`, `slm_policy_filter` and `sort` are not supported when using
`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 +634,194 @@ 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
ascending order.

[source,console]
----
GET /_snapshot/my_repository/*?sort=name&from_sort_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/]


The following request returns information for all snapshots with names starting with `snapshot_` and that started on or after timestamp
`1577833200000` (Jan 1st 2020) when sorted by snapshot start time in the default ascending order.

[source,console]
----
GET /_snapshot/my_repository/snapshot_*?sort=start_time&from_sort_value=1577833200000
----

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.128Z",
"start_time_in_millis": 1593093628849,
"end_time": "2020-07-06T21:55:18.129Z",
"end_time_in_millis": 1593093628850,
"duration_in_millis": 1,
"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": 1593093628851,
"duration_in_millis": 0,
"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.131Z",
"start_time_in_millis": 1593093628852,
"end_time": "2020-07-06T21:55:18.135Z",
"end_time_in_millis": 1593093628856,
"duration_in_millis": 4,
"failures": [],
"shards": {
"total": 0,
"failed": 0,
"successful": 0
}
}
],
"total": 3,
"remaining": 0
}
----
// TESTRESPONSE[s/"uuid": "dKb54xw67gvdRctLCxSket"/"uuid": $body.snapshots.0.uuid/]
// TESTRESPONSE[s/"uuid": "vdRctLCxSketdKb54xw67g"/"uuid": $body.snapshots.1.uuid/]
// TESTRESPONSE[s/"uuid": "dRctdKb54xw67gvLCxSket"/"uuid": $body.snapshots.2.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.128Z"/"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": "2020-07-06T21:55:18.131Z"/"start_time": $body.snapshots.2.start_time/]
// TESTRESPONSE[s/"start_time_in_millis": 1593093628849/"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/"start_time_in_millis": 1593093628852/"start_time_in_millis": $body.snapshots.2.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": "2020-07-06T21:55:18.135Z"/"end_time": $body.snapshots.2.end_time/]
// TESTRESPONSE[s/"end_time_in_millis": 1593093628850/"end_time_in_millis": $body.snapshots.0.end_time_in_millis/]
// TESTRESPONSE[s/"end_time_in_millis": 1593093628851/"end_time_in_millis": $body.snapshots.1.end_time_in_millis/]
// TESTRESPONSE[s/"end_time_in_millis": 1593093628856/"end_time_in_millis": $body.snapshots.2.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/]
// TESTRESPONSE[s/"duration_in_millis": 4/"duration_in_millis": $body.snapshots.2.duration_in_millis/]
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

public void testSortAfterStartTime() throws Exception {
final String repoName = "test-repo";
AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs");
final HashSet<Long> startTimes = new HashSet<>();
final SnapshotInfo snapshot1 = createFullSnapshotWithUniqueStartTime(repoName, "snapshot-1", startTimes);
final SnapshotInfo snapshot2 = createFullSnapshotWithUniqueStartTime(repoName, "snapshot-2", startTimes);
final SnapshotInfo snapshot3 = createFullSnapshotWithUniqueStartTime(repoName, "snapshot-3", startTimes);

final List<SnapshotInfo> allSnapshotInfo = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME)
.get()
.getSnapshots();
assertThat(allSnapshotInfo, is(org.elasticsearch.core.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(org.elasticsearch.core.List.of(snapshot2, snapshot3)));
assertThat(allAfterStartTimeAscending(startTime3), is(org.elasticsearch.core.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(org.elasticsearch.core.List.of(snapshot3, snapshot2, snapshot1)));

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

// create a snapshot that is guaranteed to have a unique start time
private SnapshotInfo createFullSnapshotWithUniqueStartTime(String repoName, String snapshotName, Set<Long> forbiddenStartTimes) {
while (true) {
final SnapshotInfo snapshotInfo = AbstractSnapshotIntegTestCase.createFullSnapshot(logger, repoName, snapshotName);
if (forbiddenStartTimes.contains(snapshotInfo.startTime())) {
logger.info("--> snapshot start time collided");
assertAcked(clusterAdmin().prepareDeleteSnapshot(repoName, snapshotName).get());
} else {
assertTrue(forbiddenStartTimes.add(snapshotInfo.startTime()));
return snapshotInfo;
}
}
}

private List<SnapshotInfo> allAfterStartTimeAscending(long timestamp) throws IOException {
final Request request = baseGetSnapshotsRequest("*");
request.addParameter("sort", GetSnapshotsRequest.SortBy.START_TIME.toString());
request.addParameter("from_sort_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("from_sort_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
Loading