-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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 Numeric Offset Parameter in Get Snapshots API #76233
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,11 +197,15 @@ private static void assertStablePagination(String repoName, Collection<String> a | |
i, | ||
order | ||
); | ||
final GetSnapshotsResponse getSnapshotsResponseNumeric = sortedWithLimit(repoName, sort, j + 1, i, order); | ||
final List<SnapshotInfo> subsetSorted = getSnapshotsResponse.getSnapshots(); | ||
assertEquals(subsetSorted, getSnapshotsResponseNumeric.getSnapshots()); | ||
assertEquals(subsetSorted, allSorted.subList(j + 1, j + i + 1)); | ||
assertEquals(allSnapshotNames.size(), getSnapshotsResponse.totalCount()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should also verify the total count and remaining from the response obtained using offset. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ added assertions for that |
||
assertEquals(allSnapshotNames.size() - (j + i + 1), getSnapshotsResponse.remaining()); | ||
assertEquals(subsetSorted, allSorted.subList(j + 1, j + i + 1)); | ||
assertEquals(getSnapshotsResponseNumeric.totalCount(), getSnapshotsResponse.totalCount()); | ||
assertEquals(getSnapshotsResponseNumeric.remaining(), getSnapshotsResponse.remaining()); | ||
} | ||
} | ||
} | ||
|
@@ -230,12 +234,17 @@ private static GetSnapshotsResponse sortedWithLimit( | |
int size, | ||
SortOrder order | ||
) { | ||
final GetSnapshotsResponse response = baseGetSnapshotsRequest(repoName).setAfter(after) | ||
.setSort(sortBy) | ||
.setSize(size) | ||
.setOrder(order) | ||
.get(); | ||
return response; | ||
return baseGetSnapshotsRequest(repoName).setAfter(after).setSort(sortBy).setSize(size).setOrder(order).get(); | ||
} | ||
|
||
private static GetSnapshotsResponse sortedWithLimit( | ||
String repoName, | ||
GetSnapshotsRequest.SortBy sortBy, | ||
int offset, | ||
int size, | ||
SortOrder order | ||
) { | ||
return baseGetSnapshotsRequest(repoName).setOffset(offset).setSort(sortBy).setSize(size).setOrder(order).get(); | ||
} | ||
|
||
private static GetSnapshotsRequestBuilder baseGetSnapshotsRequest(String repoName) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,11 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest> | |
*/ | ||
private int size = NO_LIMIT; | ||
|
||
/** | ||
* Numeric offset at which to start fetching snapshots. Mutually exclusive with {@link After} if not equal to {@code 0}. | ||
*/ | ||
private int offset = 0; | ||
|
||
@Nullable | ||
private After after; | ||
|
||
|
@@ -104,6 +109,9 @@ public GetSnapshotsRequest(StreamInput in) throws IOException { | |
sort = in.readEnum(SortBy.class); | ||
size = in.readVInt(); | ||
order = SortOrder.readFromStream(in); | ||
if (in.getVersion().onOrAfter(NUMERIC_PAGINATION_VERSION)) { | ||
offset = in.readVInt(); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -130,6 +138,13 @@ public void writeTo(StreamOutput out) throws IOException { | |
out.writeEnum(sort); | ||
out.writeVInt(size); | ||
order.writeTo(out); | ||
if (out.getVersion().onOrAfter(NUMERIC_PAGINATION_VERSION)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we not want to fail if offset is non-zero when talking to a too old version? Just like 3 lines down. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ added handling for that thanks! |
||
out.writeVInt(offset); | ||
} else if (offset != 0) { | ||
throw new IllegalArgumentException( | ||
"can't use numeric offset in get snapshots request with node version [" + out.getVersion() + "]" | ||
); | ||
} | ||
} else if (sort != SortBy.START_TIME || size != NO_LIMIT || after != null || order != SortOrder.ASC) { | ||
throw new IllegalArgumentException("can't use paginated get snapshots request with node version [" + out.getVersion() + "]"); | ||
} | ||
|
@@ -151,12 +166,17 @@ public ActionRequestValidationException validate() { | |
if (size > 0) { | ||
validationException = addValidationError("can't use size limit with verbose=false", validationException); | ||
} | ||
if (offset > 0) { | ||
validationException = addValidationError("can't use offset with verbose=false", validationException); | ||
} | ||
if (after != null) { | ||
validationException = addValidationError("can't use after with verbose=false", validationException); | ||
} | ||
if (order != SortOrder.ASC) { | ||
validationException = addValidationError("can't use non-default sort order with verbose=false", validationException); | ||
} | ||
} else if (after != null && offset > 0) { | ||
validationException = addValidationError("can't use after and offset simultaneously", validationException); | ||
} | ||
return validationException; | ||
} | ||
|
@@ -265,6 +285,15 @@ public int size() { | |
return size; | ||
} | ||
|
||
public int offset() { | ||
return offset; | ||
} | ||
|
||
public GetSnapshotsRequest offset(int offset) { | ||
this.offset = offset; | ||
return this; | ||
} | ||
|
||
public SortOrder order() { | ||
return order; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,7 @@ protected void masterOperation( | |
(CancellableTask) task, | ||
request.sort(), | ||
request.after(), | ||
request.offset(), | ||
request.size(), | ||
request.order(), | ||
listener | ||
|
@@ -133,6 +134,7 @@ private void getMultipleReposSnapshotInfo( | |
CancellableTask cancellableTask, | ||
GetSnapshotsRequest.SortBy sortBy, | ||
@Nullable GetSnapshotsRequest.After after, | ||
int offset, | ||
int size, | ||
SortOrder order, | ||
ActionListener<GetSnapshotsResponse> listener | ||
|
@@ -154,7 +156,7 @@ private void getMultipleReposSnapshotInfo( | |
.map(Tuple::v1) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toMap(Tuple::v1, Tuple::v2)); | ||
final SnapshotsInRepo snInfos = sortSnapshots(allSnapshots, sortBy, after, size, order); | ||
final SnapshotsInRepo snInfos = sortSnapshots(allSnapshots, sortBy, after, offset, size, order); | ||
final List<SnapshotInfo> snapshotInfos = snInfos.snapshotInfos; | ||
final int remaining = snInfos.remaining + responses.stream() | ||
.map(Tuple::v2) | ||
|
@@ -183,7 +185,6 @@ private void getMultipleReposSnapshotInfo( | |
cancellableTask, | ||
sortBy, | ||
after, | ||
size, | ||
order, | ||
groupedActionListener.delegateResponse((groupedListener, e) -> { | ||
if (isMultiRepoRequest && e instanceof ElasticsearchException) { | ||
|
@@ -205,7 +206,6 @@ private void getSingleRepoSnapshotInfo( | |
CancellableTask task, | ||
GetSnapshotsRequest.SortBy sortBy, | ||
@Nullable final GetSnapshotsRequest.After after, | ||
int size, | ||
SortOrder order, | ||
ActionListener<SnapshotsInRepo> listener | ||
) { | ||
|
@@ -237,7 +237,6 @@ private void getSingleRepoSnapshotInfo( | |
task, | ||
sortBy, | ||
after, | ||
size, | ||
order, | ||
listener | ||
), | ||
|
@@ -277,7 +276,6 @@ private void loadSnapshotInfos( | |
CancellableTask task, | ||
GetSnapshotsRequest.SortBy sortBy, | ||
@Nullable final GetSnapshotsRequest.After after, | ||
int size, | ||
SortOrder order, | ||
ActionListener<SnapshotsInRepo> listener | ||
) { | ||
|
@@ -327,22 +325,22 @@ private void loadSnapshotInfos( | |
task, | ||
sortBy, | ||
after, | ||
size, | ||
order, | ||
listener | ||
); | ||
} else { | ||
final SnapshotsInRepo snapshotInfos; | ||
if (repositoryData != null) { | ||
// want non-current snapshots as well, which are found in the repository data | ||
snapshotInfos = buildSimpleSnapshotInfos(toResolve, repo, repositoryData, currentSnapshots, sortBy, after, size, order); | ||
snapshotInfos = buildSimpleSnapshotInfos(toResolve, repo, repositoryData, currentSnapshots, sortBy, after, order); | ||
} else { | ||
// only want current snapshots | ||
snapshotInfos = sortSnapshots( | ||
currentSnapshots.stream().map(SnapshotInfo::basic).collect(Collectors.toList()), | ||
sortBy, | ||
after, | ||
size, | ||
0, | ||
GetSnapshotsRequest.NO_LIMIT, | ||
order | ||
); | ||
} | ||
|
@@ -365,7 +363,6 @@ private void snapshots( | |
CancellableTask task, | ||
GetSnapshotsRequest.SortBy sortBy, | ||
@Nullable GetSnapshotsRequest.After after, | ||
int size, | ||
SortOrder order, | ||
ActionListener<SnapshotsInRepo> listener | ||
) { | ||
|
@@ -395,7 +392,7 @@ private void snapshots( | |
final ActionListener<Void> allDoneListener = listener.delegateFailure((l, v) -> { | ||
final ArrayList<SnapshotInfo> snapshotList = new ArrayList<>(snapshotInfos); | ||
snapshotList.addAll(snapshotSet); | ||
listener.onResponse(sortSnapshots(snapshotList, sortBy, after, size, order)); | ||
listener.onResponse(sortSnapshots(snapshotList, sortBy, after, 0, GetSnapshotsRequest.NO_LIMIT, order)); | ||
}); | ||
if (snapshotIdsToIterate.isEmpty()) { | ||
allDoneListener.onResponse(null); | ||
|
@@ -445,7 +442,6 @@ private static SnapshotsInRepo buildSimpleSnapshotInfos( | |
final List<SnapshotInfo> currentSnapshots, | ||
final GetSnapshotsRequest.SortBy sortBy, | ||
@Nullable final GetSnapshotsRequest.After after, | ||
final int size, | ||
final SortOrder order | ||
) { | ||
List<SnapshotInfo> snapshotInfos = new ArrayList<>(); | ||
|
@@ -475,7 +471,7 @@ private static SnapshotsInRepo buildSimpleSnapshotInfos( | |
) | ||
); | ||
} | ||
return sortSnapshots(snapshotInfos, sortBy, after, size, order); | ||
return sortSnapshots(snapshotInfos, sortBy, after, 0, GetSnapshotsRequest.NO_LIMIT, order); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now I just dropped the size and offset limits everywhere but before resolving the final listener. |
||
} | ||
|
||
private static final Comparator<SnapshotInfo> BY_START_TIME = Comparator.comparingLong(SnapshotInfo::startTime) | ||
|
@@ -494,6 +490,7 @@ private static SnapshotsInRepo sortSnapshots( | |
final List<SnapshotInfo> snapshotInfos, | ||
final GetSnapshotsRequest.SortBy sortBy, | ||
final @Nullable GetSnapshotsRequest.After after, | ||
final int offset, | ||
final int size, | ||
final SortOrder order | ||
) { | ||
|
@@ -518,6 +515,7 @@ private static SnapshotsInRepo sortSnapshots( | |
Stream<SnapshotInfo> infos = snapshotInfos.stream(); | ||
|
||
if (after != null) { | ||
assert offset == 0 : "can't combine after and offset but saw [" + after + "] and offset [" + offset + "]"; | ||
final Predicate<SnapshotInfo> isAfter; | ||
final String snapshotName = after.snapshotName(); | ||
final String repoName = after.repoName(); | ||
|
@@ -553,7 +551,7 @@ private static SnapshotsInRepo sortSnapshots( | |
} | ||
infos = infos.filter(isAfter); | ||
} | ||
infos = infos.sorted(order == SortOrder.DESC ? comparator.reversed() : comparator); | ||
infos = infos.sorted(order == SortOrder.DESC ? comparator.reversed() : comparator).skip(offset); | ||
final List<SnapshotInfo> allSnapshots = infos.collect(Collectors.toUnmodifiableList()); | ||
final List<SnapshotInfo> snapshots; | ||
if (size != GetSnapshotsRequest.NO_LIMIT) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also verify the total count and remaining from the response obtained using offset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++ added assertions for that