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

Extract SnapshotSortKey #106015

Merged
merged 1 commit into from
Mar 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest;
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse;
import org.elasticsearch.action.admin.cluster.snapshots.get.SnapshotSortKey;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.cluster.SnapshotsInProgress;
Expand Down Expand Up @@ -101,38 +102,38 @@ private void doTestSortOrder(String repoName, Collection<String> allSnapshotName
.getSnapshots();
assertSnapshotListSorted(defaultSorting, null, order);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.NAME, order, includeIndexNames),
GetSnapshotsRequest.SortBy.NAME,
allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.NAME, order, includeIndexNames),
SnapshotSortKey.NAME,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.DURATION, order, includeIndexNames),
GetSnapshotsRequest.SortBy.DURATION,
allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.DURATION, order, includeIndexNames),
SnapshotSortKey.DURATION,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.INDICES, order, includeIndexNames),
GetSnapshotsRequest.SortBy.INDICES,
allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.INDICES, order, includeIndexNames),
SnapshotSortKey.INDICES,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.START_TIME, order, includeIndexNames),
GetSnapshotsRequest.SortBy.START_TIME,
allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.START_TIME, order, includeIndexNames),
SnapshotSortKey.START_TIME,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.SHARDS, order, includeIndexNames),
GetSnapshotsRequest.SortBy.SHARDS,
allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.SHARDS, order, includeIndexNames),
SnapshotSortKey.SHARDS,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order, includeIndexNames),
GetSnapshotsRequest.SortBy.FAILED_SHARDS,
allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.FAILED_SHARDS, order, includeIndexNames),
SnapshotSortKey.FAILED_SHARDS,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.REPOSITORY, order, includeIndexNames),
GetSnapshotsRequest.SortBy.REPOSITORY,
allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.REPOSITORY, order, includeIndexNames),
SnapshotSortKey.REPOSITORY,
order
);
}
Expand All @@ -141,16 +142,15 @@ public void testResponseSizeLimit() throws Exception {
final String repoName = "test-repo";
AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs");
final List<String> names = AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(6, 20));
for (GetSnapshotsRequest.SortBy sort : GetSnapshotsRequest.SortBy.values()) {
for (SnapshotSortKey sort : SnapshotSortKey.values()) {
for (SortOrder order : SortOrder.values()) {
logger.info("--> testing pagination for [{}] [{}]", sort, order);
doTestPagination(repoName, names, sort, order);
}
}
}

private void doTestPagination(String repoName, List<String> names, GetSnapshotsRequest.SortBy sort, SortOrder order)
throws IOException {
private void doTestPagination(String repoName, List<String> names, SnapshotSortKey sort, SortOrder order) throws IOException {
final boolean includeIndexNames = randomBoolean();
final List<SnapshotInfo> allSnapshotsSorted = allSnapshotsSorted(names, repoName, sort, order, includeIndexNames);
final GetSnapshotsResponse batch1 = sortedWithLimit(repoName, sort, null, 2, order, includeIndexNames);
Expand Down Expand Up @@ -220,18 +220,18 @@ public void testSortAndPaginateWithInProgress() throws Exception {
.equals(Map.of(SnapshotsInProgress.ShardState.INIT, 1L, SnapshotsInProgress.ShardState.QUEUED, (long) inProgressCount - 1));
return firstIndexSuccessfullySnapshot && secondIndexIsBlocked;
});
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.START_TIME);
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.NAME);
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES);
assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.START_TIME);
assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.NAME);
assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.INDICES);

AbstractSnapshotIntegTestCase.unblockAllDataNodes(repoName);
for (ActionFuture<CreateSnapshotResponse> inProgressSnapshot : inProgressSnapshots) {
AbstractSnapshotIntegTestCase.assertSuccessful(logger, inProgressSnapshot);
}

assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.START_TIME);
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.NAME);
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES);
assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.START_TIME);
assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.NAME);
assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.INDICES);
}

public void testFilterBySLMPolicy() throws Exception {
Expand All @@ -240,7 +240,7 @@ public void testFilterBySLMPolicy() throws Exception {
AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(1, 5));
final List<SnapshotInfo> snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots("*")
.setSnapshots("*")
.setSort(GetSnapshotsRequest.SortBy.NAME)
.setSort(SnapshotSortKey.NAME)
.get()
.getSnapshots();
final String snapshotWithPolicy = "snapshot-with-policy";
Expand Down Expand Up @@ -277,7 +277,7 @@ public void testFilterBySLMPolicy() throws Exception {
assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName, "no-such-policy*"), is(List.of(withOtherPolicy, withPolicy)));
final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots("*")
.setSnapshots("*")
.setSort(GetSnapshotsRequest.SortBy.NAME)
.setSort(SnapshotSortKey.NAME)
.get()
.getSnapshots();
assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), is(allSnapshots));
Expand All @@ -294,7 +294,7 @@ public void testSortAfterStartTime() throws Exception {

final List<SnapshotInfo> allSnapshotInfo = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME)
.setSort(SnapshotSortKey.START_TIME)
.get()
.getSnapshots();
assertThat(allSnapshotInfo, is(List.of(snapshot1, snapshot2, snapshot3)));
Expand All @@ -311,7 +311,7 @@ public void testSortAfterStartTime() throws Exception {

final List<SnapshotInfo> allSnapshotInfoDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME)
.setSort(SnapshotSortKey.START_TIME)
.setOrder(SortOrder.DESC)
.get()
.getSnapshots();
Expand Down Expand Up @@ -340,15 +340,15 @@ private SnapshotInfo createFullSnapshotWithUniqueStartTime(String repoName, Stri

private List<SnapshotInfo> allAfterStartTimeAscending(long timestamp) throws IOException {
final Request request = baseGetSnapshotsRequest("*");
request.addParameter("sort", GetSnapshotsRequest.SortBy.START_TIME.toString());
request.addParameter("sort", SnapshotSortKey.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("sort", SnapshotSortKey.START_TIME.toString());
request.addParameter("from_sort_value", String.valueOf(timestamp));
request.addParameter("order", SortOrder.DESC.toString());
final Response response = getRestClient().performRequest(request);
Expand All @@ -358,7 +358,7 @@ private List<SnapshotInfo> allBeforeStartTimeDescending(long timestamp) throws I
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));
requestWithPolicy.addParameter("sort", GetSnapshotsRequest.SortBy.NAME.toString());
requestWithPolicy.addParameter("sort", SnapshotSortKey.NAME.toString());
return readSnapshotInfos(getRestClient().performRequest(requestWithPolicy)).getSnapshots();
}

Expand All @@ -369,10 +369,10 @@ private void createIndexWithContent(String indexName) {
indexDoc(indexName, "some_id", "foo", "bar");
}

private static void assertStablePagination(String repoName, Collection<String> allSnapshotNames, GetSnapshotsRequest.SortBy sort)
private static void assertStablePagination(String repoName, Collection<String> allSnapshotNames, SnapshotSortKey sort)
throws IOException {
final SortOrder order = randomFrom(SortOrder.values());
final boolean includeIndexNames = sort == GetSnapshotsRequest.SortBy.INDICES || randomBoolean();
final boolean includeIndexNames = sort == SnapshotSortKey.INDICES || randomBoolean();
final List<SnapshotInfo> allSorted = allSnapshotsSorted(allSnapshotNames, repoName, sort, order, includeIndexNames);

for (int i = 1; i <= allSnapshotNames.size(); i++) {
Expand Down Expand Up @@ -413,7 +413,7 @@ private static void assertStablePagination(String repoName, Collection<String> a
private static List<SnapshotInfo> allSnapshotsSorted(
Collection<String> allSnapshotNames,
String repoName,
GetSnapshotsRequest.SortBy sortBy,
SnapshotSortKey sortBy,
SortOrder order,
boolean includeIndices
) throws IOException {
Expand Down Expand Up @@ -454,7 +454,7 @@ private static GetSnapshotsResponse readSnapshotInfos(Response response) throws

private static GetSnapshotsResponse sortedWithLimit(
String repoName,
GetSnapshotsRequest.SortBy sortBy,
SnapshotSortKey sortBy,
String after,
int size,
SortOrder order,
Expand Down Expand Up @@ -486,7 +486,7 @@ private static void addIndexNamesParameter(boolean includeIndices, Request reque

private static GetSnapshotsResponse sortedWithLimit(
String repoName,
GetSnapshotsRequest.SortBy sortBy,
SnapshotSortKey sortBy,
int offset,
int size,
SortOrder order,
Expand Down
Loading