Extract SnapshotSortKey (#106015)

The behaviour of the get-snapshots API varies quite considerably
depending on the sort key chosen. Today this logic is implemented using
scattered `switch` statements and other conditionals but it'd be clearer
if we delegated this stuff to the sort key instances themselves. This
commit moves the sort key enum to the top level and replaces one of the
`switch` statements with a method on the enum instances.
This commit is contained in:
David Turner 2024-03-06 15:27:57 +00:00 committed by GitHub
parent 46beceb180
commit 1fae3e7501
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 191 additions and 202 deletions

View file

@ -14,6 +14,7 @@ import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; 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.GetSnapshotsRequest;
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse; 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.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.cluster.SnapshotsInProgress; import org.elasticsearch.cluster.SnapshotsInProgress;
@ -101,38 +102,38 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
.getSnapshots(); .getSnapshots();
assertSnapshotListSorted(defaultSorting, null, order); assertSnapshotListSorted(defaultSorting, null, order);
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.NAME, order, includeIndexNames), allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.NAME, order, includeIndexNames),
GetSnapshotsRequest.SortBy.NAME, SnapshotSortKey.NAME,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.DURATION, order, includeIndexNames), allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.DURATION, order, includeIndexNames),
GetSnapshotsRequest.SortBy.DURATION, SnapshotSortKey.DURATION,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.INDICES, order, includeIndexNames), allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.INDICES, order, includeIndexNames),
GetSnapshotsRequest.SortBy.INDICES, SnapshotSortKey.INDICES,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.START_TIME, order, includeIndexNames), allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.START_TIME, order, includeIndexNames),
GetSnapshotsRequest.SortBy.START_TIME, SnapshotSortKey.START_TIME,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.SHARDS, order, includeIndexNames), allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.SHARDS, order, includeIndexNames),
GetSnapshotsRequest.SortBy.SHARDS, SnapshotSortKey.SHARDS,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order, includeIndexNames), allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.FAILED_SHARDS, order, includeIndexNames),
GetSnapshotsRequest.SortBy.FAILED_SHARDS, SnapshotSortKey.FAILED_SHARDS,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.REPOSITORY, order, includeIndexNames), allSnapshotsSorted(allSnapshotNames, repoName, SnapshotSortKey.REPOSITORY, order, includeIndexNames),
GetSnapshotsRequest.SortBy.REPOSITORY, SnapshotSortKey.REPOSITORY,
order order
); );
} }
@ -141,7 +142,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
final String repoName = "test-repo"; final String repoName = "test-repo";
AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs"); AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs");
final List<String> names = AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(6, 20)); 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()) { for (SortOrder order : SortOrder.values()) {
logger.info("--> testing pagination for [{}] [{}]", sort, order); logger.info("--> testing pagination for [{}] [{}]", sort, order);
doTestPagination(repoName, names, sort, order); doTestPagination(repoName, names, sort, order);
@ -149,8 +150,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
} }
} }
private void doTestPagination(String repoName, List<String> names, GetSnapshotsRequest.SortBy sort, SortOrder order) private void doTestPagination(String repoName, List<String> names, SnapshotSortKey sort, SortOrder order) throws IOException {
throws IOException {
final boolean includeIndexNames = randomBoolean(); final boolean includeIndexNames = randomBoolean();
final List<SnapshotInfo> allSnapshotsSorted = allSnapshotsSorted(names, repoName, sort, order, includeIndexNames); final List<SnapshotInfo> allSnapshotsSorted = allSnapshotsSorted(names, repoName, sort, order, includeIndexNames);
final GetSnapshotsResponse batch1 = sortedWithLimit(repoName, sort, null, 2, order, includeIndexNames); final GetSnapshotsResponse batch1 = sortedWithLimit(repoName, sort, null, 2, order, includeIndexNames);
@ -220,18 +220,18 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
.equals(Map.of(SnapshotsInProgress.ShardState.INIT, 1L, SnapshotsInProgress.ShardState.QUEUED, (long) inProgressCount - 1)); .equals(Map.of(SnapshotsInProgress.ShardState.INIT, 1L, SnapshotsInProgress.ShardState.QUEUED, (long) inProgressCount - 1));
return firstIndexSuccessfullySnapshot && secondIndexIsBlocked; return firstIndexSuccessfullySnapshot && secondIndexIsBlocked;
}); });
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.START_TIME); assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.START_TIME);
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.NAME); assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.NAME);
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES); assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.INDICES);
AbstractSnapshotIntegTestCase.unblockAllDataNodes(repoName); AbstractSnapshotIntegTestCase.unblockAllDataNodes(repoName);
for (ActionFuture<CreateSnapshotResponse> inProgressSnapshot : inProgressSnapshots) { for (ActionFuture<CreateSnapshotResponse> inProgressSnapshot : inProgressSnapshots) {
AbstractSnapshotIntegTestCase.assertSuccessful(logger, inProgressSnapshot); AbstractSnapshotIntegTestCase.assertSuccessful(logger, inProgressSnapshot);
} }
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.START_TIME); assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.START_TIME);
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.NAME); assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.NAME);
assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES); assertStablePagination(repoName, allSnapshotNames, SnapshotSortKey.INDICES);
} }
public void testFilterBySLMPolicy() throws Exception { public void testFilterBySLMPolicy() throws Exception {
@ -240,7 +240,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(1, 5)); AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(1, 5));
final List<SnapshotInfo> snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots("*") final List<SnapshotInfo> snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots("*")
.setSnapshots("*") .setSnapshots("*")
.setSort(GetSnapshotsRequest.SortBy.NAME) .setSort(SnapshotSortKey.NAME)
.get() .get()
.getSnapshots(); .getSnapshots();
final String snapshotWithPolicy = "snapshot-with-policy"; final String snapshotWithPolicy = "snapshot-with-policy";
@ -277,7 +277,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName, "no-such-policy*"), is(List.of(withOtherPolicy, withPolicy))); assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName, "no-such-policy*"), is(List.of(withOtherPolicy, withPolicy)));
final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots("*") final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots("*")
.setSnapshots("*") .setSnapshots("*")
.setSort(GetSnapshotsRequest.SortBy.NAME) .setSort(SnapshotSortKey.NAME)
.get() .get()
.getSnapshots(); .getSnapshots();
assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), is(allSnapshots)); assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), is(allSnapshots));
@ -294,7 +294,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
final List<SnapshotInfo> allSnapshotInfo = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> allSnapshotInfo = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME) .setSort(SnapshotSortKey.START_TIME)
.get() .get()
.getSnapshots(); .getSnapshots();
assertThat(allSnapshotInfo, is(List.of(snapshot1, snapshot2, snapshot3))); assertThat(allSnapshotInfo, is(List.of(snapshot1, snapshot2, snapshot3)));
@ -311,7 +311,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
final List<SnapshotInfo> allSnapshotInfoDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> allSnapshotInfoDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME) .setSort(SnapshotSortKey.START_TIME)
.setOrder(SortOrder.DESC) .setOrder(SortOrder.DESC)
.get() .get()
.getSnapshots(); .getSnapshots();
@ -340,7 +340,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
private List<SnapshotInfo> allAfterStartTimeAscending(long timestamp) throws IOException { private List<SnapshotInfo> allAfterStartTimeAscending(long timestamp) throws IOException {
final Request request = baseGetSnapshotsRequest("*"); 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("from_sort_value", String.valueOf(timestamp));
final Response response = getRestClient().performRequest(request); final Response response = getRestClient().performRequest(request);
return readSnapshotInfos(response).getSnapshots(); return readSnapshotInfos(response).getSnapshots();
@ -348,7 +348,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
private List<SnapshotInfo> allBeforeStartTimeDescending(long timestamp) throws IOException { private List<SnapshotInfo> allBeforeStartTimeDescending(long timestamp) throws IOException {
final Request request = baseGetSnapshotsRequest("*"); 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("from_sort_value", String.valueOf(timestamp));
request.addParameter("order", SortOrder.DESC.toString()); request.addParameter("order", SortOrder.DESC.toString());
final Response response = getRestClient().performRequest(request); final Response response = getRestClient().performRequest(request);
@ -358,7 +358,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
private static List<SnapshotInfo> getAllSnapshotsForPolicies(String... policies) throws IOException { private static List<SnapshotInfo> getAllSnapshotsForPolicies(String... policies) throws IOException {
final Request requestWithPolicy = new Request(HttpGet.METHOD_NAME, "/_snapshot/*/*"); final Request requestWithPolicy = new Request(HttpGet.METHOD_NAME, "/_snapshot/*/*");
requestWithPolicy.addParameter("slm_policy_filter", Strings.arrayToCommaDelimitedString(policies)); 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(); return readSnapshotInfos(getRestClient().performRequest(requestWithPolicy)).getSnapshots();
} }
@ -369,10 +369,10 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
indexDoc(indexName, "some_id", "foo", "bar"); 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 { throws IOException {
final SortOrder order = randomFrom(SortOrder.values()); 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); final List<SnapshotInfo> allSorted = allSnapshotsSorted(allSnapshotNames, repoName, sort, order, includeIndexNames);
for (int i = 1; i <= allSnapshotNames.size(); i++) { for (int i = 1; i <= allSnapshotNames.size(); i++) {
@ -413,7 +413,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
private static List<SnapshotInfo> allSnapshotsSorted( private static List<SnapshotInfo> allSnapshotsSorted(
Collection<String> allSnapshotNames, Collection<String> allSnapshotNames,
String repoName, String repoName,
GetSnapshotsRequest.SortBy sortBy, SnapshotSortKey sortBy,
SortOrder order, SortOrder order,
boolean includeIndices boolean includeIndices
) throws IOException { ) throws IOException {
@ -454,7 +454,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
private static GetSnapshotsResponse sortedWithLimit( private static GetSnapshotsResponse sortedWithLimit(
String repoName, String repoName,
GetSnapshotsRequest.SortBy sortBy, SnapshotSortKey sortBy,
String after, String after,
int size, int size,
SortOrder order, SortOrder order,
@ -486,7 +486,7 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase {
private static GetSnapshotsResponse sortedWithLimit( private static GetSnapshotsResponse sortedWithLimit(
String repoName, String repoName,
GetSnapshotsRequest.SortBy sortBy, SnapshotSortKey sortBy,
int offset, int offset,
int size, int size,
SortOrder order, SortOrder order,

View file

@ -14,6 +14,7 @@ import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRes
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest; import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest;
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequestBuilder; import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequestBuilder;
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse; import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse;
import org.elasticsearch.action.admin.cluster.snapshots.get.SnapshotSortKey;
import org.elasticsearch.cluster.SnapshotsInProgress; import org.elasticsearch.cluster.SnapshotsInProgress;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.repositories.RepositoryMissingException; import org.elasticsearch.repositories.RepositoryMissingException;
@ -84,39 +85,31 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
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(defaultSorting, null, order);
final String[] repos = { repoName }; final String[] repos = { repoName };
assertSnapshotListSorted(allSnapshotsSorted(allSnapshotNames, repos, SnapshotSortKey.NAME, order), SnapshotSortKey.NAME, order);
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repos, GetSnapshotsRequest.SortBy.NAME, order), allSnapshotsSorted(allSnapshotNames, repos, SnapshotSortKey.DURATION, order),
GetSnapshotsRequest.SortBy.NAME, SnapshotSortKey.DURATION,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repos, GetSnapshotsRequest.SortBy.DURATION, order), allSnapshotsSorted(allSnapshotNames, repos, SnapshotSortKey.INDICES, order),
GetSnapshotsRequest.SortBy.DURATION, SnapshotSortKey.INDICES,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repos, GetSnapshotsRequest.SortBy.INDICES, order), allSnapshotsSorted(allSnapshotNames, repos, SnapshotSortKey.START_TIME, order),
GetSnapshotsRequest.SortBy.INDICES, SnapshotSortKey.START_TIME,
order
);
assertSnapshotListSorted(allSnapshotsSorted(allSnapshotNames, repos, SnapshotSortKey.SHARDS, order), SnapshotSortKey.SHARDS, order);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repos, SnapshotSortKey.FAILED_SHARDS, order),
SnapshotSortKey.FAILED_SHARDS,
order order
); );
assertSnapshotListSorted( assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repos, GetSnapshotsRequest.SortBy.START_TIME, order), allSnapshotsSorted(allSnapshotNames, repos, SnapshotSortKey.REPOSITORY, order),
GetSnapshotsRequest.SortBy.START_TIME, SnapshotSortKey.REPOSITORY,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repos, GetSnapshotsRequest.SortBy.SHARDS, order),
GetSnapshotsRequest.SortBy.SHARDS,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repos, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order),
GetSnapshotsRequest.SortBy.FAILED_SHARDS,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repos, GetSnapshotsRequest.SortBy.REPOSITORY, order),
GetSnapshotsRequest.SortBy.REPOSITORY,
order order
); );
} }
@ -127,7 +120,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
createRepository(repoName, "fs", repoPath); createRepository(repoName, "fs", repoPath);
maybeInitWithOldSnapshotVersion(repoName, repoPath); maybeInitWithOldSnapshotVersion(repoName, repoPath);
final List<String> names = createNSnapshots(repoName, randomIntBetween(6, 20)); final List<String> names = createNSnapshots(repoName, randomIntBetween(6, 20));
for (GetSnapshotsRequest.SortBy sort : GetSnapshotsRequest.SortBy.values()) { for (SnapshotSortKey sort : SnapshotSortKey.values()) {
for (SortOrder order : SortOrder.values()) { for (SortOrder order : SortOrder.values()) {
logger.info("--> testing pagination for [{}] [{}]", sort, order); logger.info("--> testing pagination for [{}] [{}]", sort, order);
doTestPagination(repoName, names, sort, order); doTestPagination(repoName, names, sort, order);
@ -135,7 +128,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
} }
} }
private void doTestPagination(String repoName, List<String> names, GetSnapshotsRequest.SortBy sort, SortOrder order) { private void doTestPagination(String repoName, List<String> names, SnapshotSortKey sort, SortOrder order) {
final String[] repos = { repoName }; final String[] repos = { repoName };
final List<SnapshotInfo> allSnapshotsSorted = allSnapshotsSorted(names, repos, sort, order); final List<SnapshotInfo> allSnapshotsSorted = allSnapshotsSorted(names, repos, sort, order);
final GetSnapshotsResponse batch1 = sortedWithLimit(repos, sort, null, 2, order); final GetSnapshotsResponse batch1 = sortedWithLimit(repos, sort, null, 2, order);
@ -191,9 +184,9 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
) )
); );
final String[] repos = { repoName }; final String[] repos = { repoName };
assertStablePagination(repos, allSnapshotNames, GetSnapshotsRequest.SortBy.START_TIME); assertStablePagination(repos, allSnapshotNames, SnapshotSortKey.START_TIME);
assertStablePagination(repos, allSnapshotNames, GetSnapshotsRequest.SortBy.NAME); assertStablePagination(repos, allSnapshotNames, SnapshotSortKey.NAME);
assertStablePagination(repos, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES); assertStablePagination(repos, allSnapshotNames, SnapshotSortKey.INDICES);
final List<SnapshotInfo> currentSnapshots = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> currentSnapshots = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(GetSnapshotsRequest.CURRENT_SNAPSHOT) .setSnapshots(GetSnapshotsRequest.CURRENT_SNAPSHOT)
.get() .get()
@ -215,9 +208,9 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
assertSuccessful(inProgressSnapshot); assertSuccessful(inProgressSnapshot);
} }
assertStablePagination(repos, allSnapshotNames, GetSnapshotsRequest.SortBy.START_TIME); assertStablePagination(repos, allSnapshotNames, SnapshotSortKey.START_TIME);
assertStablePagination(repos, allSnapshotNames, GetSnapshotsRequest.SortBy.NAME); assertStablePagination(repos, allSnapshotNames, SnapshotSortKey.NAME);
assertStablePagination(repos, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES); assertStablePagination(repos, allSnapshotNames, SnapshotSortKey.INDICES);
} }
public void testPaginationRequiresVerboseListing() throws Exception { public void testPaginationRequiresVerboseListing() throws Exception {
@ -228,14 +221,14 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
ActionRequestValidationException.class, ActionRequestValidationException.class,
clusterAdmin().prepareGetSnapshots(repoName) clusterAdmin().prepareGetSnapshots(repoName)
.setVerbose(false) .setVerbose(false)
.setSort(GetSnapshotsRequest.SortBy.DURATION) .setSort(SnapshotSortKey.DURATION)
.setSize(GetSnapshotsRequest.NO_LIMIT) .setSize(GetSnapshotsRequest.NO_LIMIT)
); );
expectThrows( expectThrows(
ActionRequestValidationException.class, ActionRequestValidationException.class,
clusterAdmin().prepareGetSnapshots(repoName) clusterAdmin().prepareGetSnapshots(repoName)
.setVerbose(false) .setVerbose(false)
.setSort(GetSnapshotsRequest.SortBy.START_TIME) .setSort(SnapshotSortKey.START_TIME)
.setSize(randomIntBetween(1, 100)) .setSize(randomIntBetween(1, 100))
); );
} }
@ -258,16 +251,11 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
allSnapshotNames.addAll(namesOtherRepo); allSnapshotNames.addAll(namesOtherRepo);
final SortOrder order = SortOrder.DESC; final SortOrder order = SortOrder.DESC;
final List<SnapshotInfo> allSorted = allSnapshotsSorted( final List<SnapshotInfo> allSorted = allSnapshotsSorted(allSnapshotNames, new String[] { "*" }, SnapshotSortKey.REPOSITORY, order);
allSnapshotNames,
new String[] { "*" },
GetSnapshotsRequest.SortBy.REPOSITORY,
order
);
final List<SnapshotInfo> allSortedWithoutOther = allSnapshotsSorted( final List<SnapshotInfo> allSortedWithoutOther = allSnapshotsSorted(
allSnapshotNamesWithoutOther, allSnapshotNamesWithoutOther,
new String[] { "*", "-" + otherRepo }, new String[] { "*", "-" + otherRepo },
GetSnapshotsRequest.SortBy.REPOSITORY, SnapshotSortKey.REPOSITORY,
order order
); );
assertThat(allSortedWithoutOther, is(allSorted.subList(0, allSnapshotNamesWithoutOther.size()))); assertThat(allSortedWithoutOther, is(allSorted.subList(0, allSnapshotNamesWithoutOther.size())));
@ -275,7 +263,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final List<SnapshotInfo> allInOther = allSnapshotsSorted( final List<SnapshotInfo> allInOther = allSnapshotsSorted(
namesOtherRepo, namesOtherRepo,
new String[] { "*", "-test-repo-*" }, new String[] { "*", "-test-repo-*" },
GetSnapshotsRequest.SortBy.REPOSITORY, SnapshotSortKey.REPOSITORY,
order order
); );
assertThat(allInOther, is(allSorted.subList(allSnapshotNamesWithoutOther.size(), allSorted.size()))); assertThat(allInOther, is(allSorted.subList(allSnapshotNamesWithoutOther.size(), allSorted.size())));
@ -289,7 +277,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final List<SnapshotInfo> allInOtherWithoutOtherPrefix = allSnapshotsSorted( final List<SnapshotInfo> allInOtherWithoutOtherPrefix = allSnapshotsSorted(
namesOtherRepo, namesOtherRepo,
patternOtherRepo, patternOtherRepo,
GetSnapshotsRequest.SortBy.REPOSITORY, SnapshotSortKey.REPOSITORY,
order, order,
"-other*" "-other*"
); );
@ -298,7 +286,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final List<SnapshotInfo> allInOtherWithoutOtherExplicit = allSnapshotsSorted( final List<SnapshotInfo> allInOtherWithoutOtherExplicit = allSnapshotsSorted(
namesOtherRepo, namesOtherRepo,
patternOtherRepo, patternOtherRepo,
GetSnapshotsRequest.SortBy.REPOSITORY, SnapshotSortKey.REPOSITORY,
order, order,
"-" + otherPrefixSnapshot1, "-" + otherPrefixSnapshot1,
"-" + otherPrefixSnapshot2 "-" + otherPrefixSnapshot2
@ -345,7 +333,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final SnapshotInfo weirdSnapshot2InWeird2 = createFullSnapshot(weirdRepo2, weirdSnapshot2); final SnapshotInfo weirdSnapshot2InWeird2 = createFullSnapshot(weirdRepo2, weirdSnapshot2);
final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.REPOSITORY) .setSort(SnapshotSortKey.REPOSITORY)
.get() .get()
.getSnapshots(); .getSnapshots();
assertThat(allSnapshots, hasSize(9)); assertThat(allSnapshots, hasSize(9));
@ -407,11 +395,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
} }
private List<SnapshotInfo> getAllByPatterns(String[] repos, String[] snapshots) { private List<SnapshotInfo> getAllByPatterns(String[] repos, String[] snapshots) {
return clusterAdmin().prepareGetSnapshots(repos) return clusterAdmin().prepareGetSnapshots(repos).setSnapshots(snapshots).setSort(SnapshotSortKey.REPOSITORY).get().getSnapshots();
.setSnapshots(snapshots)
.setSort(GetSnapshotsRequest.SortBy.REPOSITORY)
.get()
.getSnapshots();
} }
public void testFilterBySLMPolicy() throws Exception { public void testFilterBySLMPolicy() throws Exception {
@ -420,7 +404,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
createNSnapshots(repoName, randomIntBetween(1, 5)); createNSnapshots(repoName, randomIntBetween(1, 5));
final List<SnapshotInfo> snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.NAME) .setSort(SnapshotSortKey.NAME)
.get() .get()
.getSnapshots(); .getSnapshots();
final String snapshotWithPolicy = "snapshot-with-policy"; final String snapshotWithPolicy = "snapshot-with-policy";
@ -456,7 +440,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.NAME) .setSort(SnapshotSortKey.NAME)
.get() .get()
.getSnapshots(); .getSnapshots();
assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), is(allSnapshots)); assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), is(allSnapshots));
@ -477,7 +461,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final List<SnapshotInfo> allSnapshotInfo = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> allSnapshotInfo = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME) .setSort(SnapshotSortKey.START_TIME)
.get() .get()
.getSnapshots(); .getSnapshots();
assertThat(allSnapshotInfo, is(List.of(snapshot1, snapshot2, snapshot3))); assertThat(allSnapshotInfo, is(List.of(snapshot1, snapshot2, snapshot3)));
@ -504,7 +488,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final List<SnapshotInfo> allSnapshotInfoDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> allSnapshotInfoDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.START_TIME) .setSort(SnapshotSortKey.START_TIME)
.setOrder(SortOrder.DESC) .setOrder(SortOrder.DESC)
.get() .get()
.getSnapshots(); .getSnapshots();
@ -525,7 +509,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final List<SnapshotInfo> allSnapshotInfoByDuration = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> allSnapshotInfoByDuration = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.DURATION) .setSort(SnapshotSortKey.DURATION)
.get() .get()
.getSnapshots(); .getSnapshots();
@ -541,7 +525,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final List<SnapshotInfo> allSnapshotInfoByDurationDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final List<SnapshotInfo> allSnapshotInfoByDurationDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setSort(GetSnapshotsRequest.SortBy.DURATION) .setSort(SnapshotSortKey.DURATION)
.setOrder(SortOrder.DESC) .setOrder(SortOrder.DESC)
.get() .get()
.getSnapshots(); .getSnapshots();
@ -554,12 +538,12 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
final SnapshotInfo otherSnapshot = createFullSnapshot(repoName, "other-snapshot"); final SnapshotInfo otherSnapshot = createFullSnapshot(repoName, "other-snapshot");
assertThat(allSnapshots(new String[] { "snap*" }, GetSnapshotsRequest.SortBy.NAME, SortOrder.ASC, "a"), is(allSnapshotInfo)); assertThat(allSnapshots(new String[] { "snap*" }, SnapshotSortKey.NAME, SortOrder.ASC, "a"), is(allSnapshotInfo));
assertThat(allSnapshots(new String[] { "o*" }, GetSnapshotsRequest.SortBy.NAME, SortOrder.ASC, "a"), is(List.of(otherSnapshot))); assertThat(allSnapshots(new String[] { "o*" }, SnapshotSortKey.NAME, SortOrder.ASC, "a"), is(List.of(otherSnapshot)));
final GetSnapshotsResponse paginatedResponse = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final GetSnapshotsResponse paginatedResponse = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots("snap*") .setSnapshots("snap*")
.setSort(GetSnapshotsRequest.SortBy.NAME) .setSort(SnapshotSortKey.NAME)
.setFromSortValue("a") .setFromSortValue("a")
.setOffset(1) .setOffset(1)
.setSize(1) .setSize(1)
@ -568,7 +552,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
assertThat(paginatedResponse.totalCount(), is(3)); assertThat(paginatedResponse.totalCount(), is(3));
final GetSnapshotsResponse paginatedResponse2 = clusterAdmin().prepareGetSnapshots(matchAllPattern()) final GetSnapshotsResponse paginatedResponse2 = clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots("snap*") .setSnapshots("snap*")
.setSort(GetSnapshotsRequest.SortBy.NAME) .setSort(SnapshotSortKey.NAME)
.setFromSortValue("a") .setFromSortValue("a")
.setOffset(0) .setOffset(0)
.setSize(2) .setSize(2)
@ -587,7 +571,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
snapshotNames.sort(String::compareTo); snapshotNames.sort(String::compareTo);
final GetSnapshotsResponse response = clusterAdmin().prepareGetSnapshots(repoName, missingRepoName) final GetSnapshotsResponse response = clusterAdmin().prepareGetSnapshots(repoName, missingRepoName)
.setSort(GetSnapshotsRequest.SortBy.NAME) .setSort(SnapshotSortKey.NAME)
.get(); .get();
assertThat(response.getSnapshots().stream().map(info -> info.snapshotId().getName()).toList(), equalTo(snapshotNames)); assertThat(response.getSnapshots().stream().map(info -> info.snapshotId().getName()).toList(), equalTo(snapshotNames));
assertTrue(response.getFailures().containsKey(missingRepoName)); assertTrue(response.getFailures().containsKey(missingRepoName));
@ -618,35 +602,30 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
} }
private List<SnapshotInfo> allAfterStartTimeAscending(long timestamp) { private List<SnapshotInfo> allAfterStartTimeAscending(long timestamp) {
return allSnapshots(matchAllPattern(), GetSnapshotsRequest.SortBy.START_TIME, SortOrder.ASC, timestamp); return allSnapshots(matchAllPattern(), SnapshotSortKey.START_TIME, SortOrder.ASC, timestamp);
} }
private List<SnapshotInfo> allBeforeStartTimeDescending(long timestamp) { private List<SnapshotInfo> allBeforeStartTimeDescending(long timestamp) {
return allSnapshots(matchAllPattern(), GetSnapshotsRequest.SortBy.START_TIME, SortOrder.DESC, timestamp); return allSnapshots(matchAllPattern(), SnapshotSortKey.START_TIME, SortOrder.DESC, timestamp);
} }
private List<SnapshotInfo> allAfterNameAscending(String name) { private List<SnapshotInfo> allAfterNameAscending(String name) {
return allSnapshots(matchAllPattern(), GetSnapshotsRequest.SortBy.NAME, SortOrder.ASC, name); return allSnapshots(matchAllPattern(), SnapshotSortKey.NAME, SortOrder.ASC, name);
} }
private List<SnapshotInfo> allBeforeNameDescending(String name) { private List<SnapshotInfo> allBeforeNameDescending(String name) {
return allSnapshots(matchAllPattern(), GetSnapshotsRequest.SortBy.NAME, SortOrder.DESC, name); return allSnapshots(matchAllPattern(), SnapshotSortKey.NAME, SortOrder.DESC, name);
} }
private List<SnapshotInfo> allAfterDurationAscending(long duration) { private List<SnapshotInfo> allAfterDurationAscending(long duration) {
return allSnapshots(matchAllPattern(), GetSnapshotsRequest.SortBy.DURATION, SortOrder.ASC, duration); return allSnapshots(matchAllPattern(), SnapshotSortKey.DURATION, SortOrder.ASC, duration);
} }
private List<SnapshotInfo> allBeforeDurationDescending(long duration) { private List<SnapshotInfo> allBeforeDurationDescending(long duration) {
return allSnapshots(matchAllPattern(), GetSnapshotsRequest.SortBy.DURATION, SortOrder.DESC, duration); return allSnapshots(matchAllPattern(), SnapshotSortKey.DURATION, SortOrder.DESC, duration);
} }
private static List<SnapshotInfo> allSnapshots( private static List<SnapshotInfo> allSnapshots(String[] snapshotNames, SnapshotSortKey sortBy, SortOrder order, Object fromSortValue) {
String[] snapshotNames,
GetSnapshotsRequest.SortBy sortBy,
SortOrder order,
Object fromSortValue
) {
return clusterAdmin().prepareGetSnapshots(matchAllPattern()) return clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(snapshotNames) .setSnapshots(snapshotNames)
.setSort(sortBy) .setSort(sortBy)
@ -660,12 +639,12 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
return clusterAdmin().prepareGetSnapshots(matchAllPattern()) return clusterAdmin().prepareGetSnapshots(matchAllPattern())
.setSnapshots(matchAllPattern()) .setSnapshots(matchAllPattern())
.setPolicies(policies) .setPolicies(policies)
.setSort(GetSnapshotsRequest.SortBy.NAME) .setSort(SnapshotSortKey.NAME)
.get() .get()
.getSnapshots(); .getSnapshots();
} }
private static void assertStablePagination(String[] repoNames, Collection<String> allSnapshotNames, GetSnapshotsRequest.SortBy sort) { private static void assertStablePagination(String[] repoNames, Collection<String> allSnapshotNames, SnapshotSortKey sort) {
final SortOrder order = randomFrom(SortOrder.values()); final SortOrder order = randomFrom(SortOrder.values());
final List<SnapshotInfo> allSorted = allSnapshotsSorted(allSnapshotNames, repoNames, sort, order); final List<SnapshotInfo> allSorted = allSnapshotsSorted(allSnapshotNames, repoNames, sort, order);
@ -700,7 +679,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
private static List<SnapshotInfo> allSnapshotsSorted( private static List<SnapshotInfo> allSnapshotsSorted(
Collection<String> allSnapshotNames, Collection<String> allSnapshotNames,
String[] repoNames, String[] repoNames,
GetSnapshotsRequest.SortBy sortBy, SnapshotSortKey sortBy,
SortOrder order, SortOrder order,
String... namePatterns String... namePatterns
) { ) {
@ -724,7 +703,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
private static GetSnapshotsResponse sortedWithLimit( private static GetSnapshotsResponse sortedWithLimit(
String[] repoNames, String[] repoNames,
GetSnapshotsRequest.SortBy sortBy, SnapshotSortKey sortBy,
String after, String after,
int size, int size,
SortOrder order, SortOrder order,
@ -738,13 +717,7 @@ public class GetSnapshotsIT extends AbstractSnapshotIntegTestCase {
.get(); .get();
} }
private static GetSnapshotsResponse sortedWithLimit( private static GetSnapshotsResponse sortedWithLimit(String[] repoNames, SnapshotSortKey sortBy, int offset, int size, SortOrder order) {
String[] repoNames,
GetSnapshotsRequest.SortBy sortBy,
int offset,
int size,
SortOrder order
) {
return baseGetSnapshotsRequest(repoNames).setOffset(offset).setSort(sortBy).setSize(size).setOrder(order).get(); return baseGetSnapshotsRequest(repoNames).setOffset(offset).setSort(sortBy).setSize(size).setOrder(order).get();
} }

View file

@ -61,7 +61,7 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
@Nullable @Nullable
private String fromSortValue; private String fromSortValue;
private SortBy sort = SortBy.START_TIME; private SnapshotSortKey sort = SnapshotSortKey.START_TIME;
private SortOrder order = SortOrder.ASC; private SortOrder order = SortOrder.ASC;
@ -106,7 +106,7 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
ignoreUnavailable = in.readBoolean(); ignoreUnavailable = in.readBoolean();
verbose = in.readBoolean(); verbose = in.readBoolean();
after = in.readOptionalWriteable(After::new); after = in.readOptionalWriteable(After::new);
sort = in.readEnum(SortBy.class); sort = in.readEnum(SnapshotSortKey.class);
size = in.readVInt(); size = in.readVInt();
order = SortOrder.readFromStream(in); order = SortOrder.readFromStream(in);
offset = in.readVInt(); offset = in.readVInt();
@ -146,7 +146,7 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
validationException = addValidationError("size must be -1 or greater than 0", validationException); validationException = addValidationError("size must be -1 or greater than 0", validationException);
} }
if (verbose == false) { if (verbose == false) {
if (sort != SortBy.START_TIME) { if (sort != SnapshotSortKey.START_TIME) {
validationException = addValidationError("can't use non-default sort with verbose=false", validationException); validationException = addValidationError("can't use non-default sort with verbose=false", validationException);
} }
if (size > 0) { if (size > 0) {
@ -287,7 +287,7 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
return after; return after;
} }
public SortBy sort() { public SnapshotSortKey sort() {
return sort; return sort;
} }
@ -306,7 +306,7 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
return fromSortValue; return fromSortValue;
} }
public GetSnapshotsRequest sort(SortBy sort) { public GetSnapshotsRequest sort(SnapshotSortKey sort) {
this.sort = sort; this.sort = sort;
return this; return this;
} }
@ -350,40 +350,6 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
return new CancellableTask(id, type, action, getDescription(), parentTaskId, headers); return new CancellableTask(id, type, action, getDescription(), parentTaskId, headers);
} }
public enum SortBy {
START_TIME("start_time"),
NAME("name"),
DURATION("duration"),
INDICES("index_count"),
SHARDS("shard_count"),
FAILED_SHARDS("failed_shard_count"),
REPOSITORY("repository");
private final String param;
SortBy(String param) {
this.param = param;
}
@Override
public String toString() {
return param;
}
public static SortBy of(String value) {
return switch (value) {
case "start_time" -> START_TIME;
case "name" -> NAME;
case "duration" -> DURATION;
case "index_count" -> INDICES;
case "shard_count" -> SHARDS;
case "failed_shard_count" -> FAILED_SHARDS;
case "repository" -> REPOSITORY;
default -> throw new IllegalArgumentException("unknown sort order [" + value + "]");
};
}
}
public static final class After implements Writeable { public static final class After implements Writeable {
private final String value; private final String value;
@ -405,7 +371,7 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
} }
@Nullable @Nullable
public static After from(@Nullable SnapshotInfo snapshotInfo, SortBy sortBy) { public static After from(@Nullable SnapshotInfo snapshotInfo, SnapshotSortKey sortBy) {
if (snapshotInfo == null) { if (snapshotInfo == null) {
return null; return null;
} }

View file

@ -122,7 +122,7 @@ public class GetSnapshotsRequestBuilder extends MasterNodeOperationRequestBuilde
return this; return this;
} }
public GetSnapshotsRequestBuilder setSort(GetSnapshotsRequest.SortBy sort) { public GetSnapshotsRequestBuilder setSort(SnapshotSortKey sort) {
request.sort(sort); request.sort(sort);
return this; return this;
} }

View file

@ -0,0 +1,83 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.action.admin.cluster.snapshots.get;
import org.elasticsearch.snapshots.SnapshotInfo;
import java.util.Comparator;
/**
* Sort key for snapshots e.g. returned from the get-snapshots API. All values break ties using {@link SnapshotInfo#snapshotId} (i.e. by
* name).
*/
public enum SnapshotSortKey {
/**
* Sort by snapshot start time.
*/
START_TIME("start_time", Comparator.comparingLong(SnapshotInfo::startTime)),
/**
* Sort by snapshot name.
*/
NAME("name", Comparator.comparing(sni -> sni.snapshotId().getName())),
/**
* Sort by snapshot duration (end time minus start time).
*/
DURATION("duration", Comparator.comparingLong(sni -> sni.endTime() - sni.startTime())),
/**
* Sort by number of indices in the snapshot.
*/
INDICES("index_count", Comparator.comparingInt(sni -> sni.indices().size())),
/**
* Sort by number of shards in the snapshot.
*/
SHARDS("shard_count", Comparator.comparingInt(SnapshotInfo::totalShards)),
/**
* Sort by number of failed shards in the snapshot.
*/
FAILED_SHARDS("failed_shard_count", Comparator.comparingInt(SnapshotInfo::failedShards)),
/**
* Sort by repository name.
*/
REPOSITORY("repository", Comparator.comparing(SnapshotInfo::repository));
private final String name;
private final Comparator<SnapshotInfo> snapshotInfoComparator;
SnapshotSortKey(String name, Comparator<SnapshotInfo> snapshotInfoComparator) {
this.name = name;
this.snapshotInfoComparator = snapshotInfoComparator.thenComparing(SnapshotInfo::snapshotId);
}
@Override
public String toString() {
return name;
}
public final Comparator<SnapshotInfo> getSnapshotInfoComparator() {
return snapshotInfoComparator;
}
public static SnapshotSortKey of(String name) {
return switch (name) {
case "start_time" -> START_TIME;
case "name" -> NAME;
case "duration" -> DURATION;
case "index_count" -> INDICES;
case "shard_count" -> SHARDS;
case "failed_shard_count" -> FAILED_SHARDS;
case "repository" -> REPOSITORY;
default -> throw new IllegalArgumentException("unknown sort key [" + name + "]");
};
}
}

View file

@ -148,7 +148,7 @@ public class TransportGetSnapshotsAction extends TransportMasterNodeAction<GetSn
private final SnapshotPredicates predicates; private final SnapshotPredicates predicates;
// snapshot ordering/pagination // snapshot ordering/pagination
private final GetSnapshotsRequest.SortBy sortBy; private final SnapshotSortKey sortBy;
private final SortOrder order; private final SortOrder order;
@Nullable @Nullable
private final String fromSortValue; private final String fromSortValue;
@ -177,7 +177,7 @@ public class TransportGetSnapshotsAction extends TransportMasterNodeAction<GetSn
String[] snapshots, String[] snapshots,
boolean ignoreUnavailable, boolean ignoreUnavailable,
SnapshotPredicates predicates, SnapshotPredicates predicates,
GetSnapshotsRequest.SortBy sortBy, SnapshotSortKey sortBy,
SortOrder order, SortOrder order,
String fromSortValue, String fromSortValue,
int offset, int offset,
@ -214,7 +214,7 @@ public class TransportGetSnapshotsAction extends TransportMasterNodeAction<GetSn
* the sort value range if possible. * the sort value range if possible.
*/ */
private List<RepositoryMetadata> maybeFilterRepositories() { private List<RepositoryMetadata> maybeFilterRepositories() {
if (sortBy != GetSnapshotsRequest.SortBy.REPOSITORY || fromSortValue == null) { if (sortBy != SnapshotSortKey.REPOSITORY || fromSortValue == null) {
return repositories; return repositories;
} }
final Predicate<RepositoryMetadata> predicate = order == SortOrder.ASC final Predicate<RepositoryMetadata> predicate = order == SortOrder.ASC
@ -486,27 +486,6 @@ public class TransportGetSnapshotsAction extends TransportMasterNodeAction<GetSn
return sortSnapshotsWithNoOffsetOrLimit(snapshotInfos); return sortSnapshotsWithNoOffsetOrLimit(snapshotInfos);
} }
private static final Comparator<SnapshotInfo> BY_START_TIME = Comparator.comparingLong(SnapshotInfo::startTime)
.thenComparing(SnapshotInfo::snapshotId);
private static final Comparator<SnapshotInfo> BY_DURATION = Comparator.<SnapshotInfo>comparingLong(
sni -> sni.endTime() - sni.startTime()
).thenComparing(SnapshotInfo::snapshotId);
private static final Comparator<SnapshotInfo> BY_INDICES_COUNT = Comparator.<SnapshotInfo>comparingInt(sni -> sni.indices().size())
.thenComparing(SnapshotInfo::snapshotId);
private static final Comparator<SnapshotInfo> BY_SHARDS_COUNT = Comparator.comparingInt(SnapshotInfo::totalShards)
.thenComparing(SnapshotInfo::snapshotId);
private static final Comparator<SnapshotInfo> BY_FAILED_SHARDS_COUNT = Comparator.comparingInt(SnapshotInfo::failedShards)
.thenComparing(SnapshotInfo::snapshotId);
private static final Comparator<SnapshotInfo> BY_NAME = Comparator.comparing(sni -> sni.snapshotId().getName());
private static final Comparator<SnapshotInfo> BY_REPOSITORY = Comparator.comparing(SnapshotInfo::repository)
.thenComparing(SnapshotInfo::snapshotId);
private SnapshotsInRepo sortSnapshotsWithNoOffsetOrLimit(List<SnapshotInfo> snapshotInfos) { private SnapshotsInRepo sortSnapshotsWithNoOffsetOrLimit(List<SnapshotInfo> snapshotInfos) {
return sortSnapshots(snapshotInfos.stream(), snapshotInfos.size(), 0, GetSnapshotsRequest.NO_LIMIT); return sortSnapshots(snapshotInfos.stream(), snapshotInfos.size(), 0, GetSnapshotsRequest.NO_LIMIT);
} }
@ -532,15 +511,7 @@ public class TransportGetSnapshotsAction extends TransportMasterNodeAction<GetSn
} }
private Comparator<SnapshotInfo> buildComparator() { private Comparator<SnapshotInfo> buildComparator() {
final Comparator<SnapshotInfo> comparator = switch (sortBy) { final var comparator = sortBy.getSnapshotInfoComparator();
case START_TIME -> BY_START_TIME;
case NAME -> BY_NAME;
case DURATION -> BY_DURATION;
case INDICES -> BY_INDICES_COUNT;
case SHARDS -> BY_SHARDS_COUNT;
case FAILED_SHARDS -> BY_FAILED_SHARDS_COUNT;
case REPOSITORY -> BY_REPOSITORY;
};
return order == SortOrder.DESC ? comparator.reversed() : comparator; return order == SortOrder.DESC ? comparator.reversed() : comparator;
} }
@ -732,7 +703,7 @@ public class TransportGetSnapshotsAction extends TransportMasterNodeAction<GetSn
return excludes.length == 0 || Regex.simpleMatch(excludes, policy) == false; return excludes.length == 0 || Regex.simpleMatch(excludes, policy) == false;
} }
private static SnapshotPredicates getSortValuePredicate(String fromSortValue, GetSnapshotsRequest.SortBy sortBy, SortOrder order) { private static SnapshotPredicates getSortValuePredicate(String fromSortValue, SnapshotSortKey sortBy, SortOrder order) {
if (fromSortValue == null) { if (fromSortValue == null) {
return MATCH_ALL; return MATCH_ALL;
} }

View file

@ -9,6 +9,7 @@
package org.elasticsearch.rest.action.admin.cluster; package org.elasticsearch.rest.action.admin.cluster;
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest; import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest;
import org.elasticsearch.action.admin.cluster.snapshots.get.SnapshotSortKey;
import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
@ -59,7 +60,7 @@ public class RestGetSnapshotsAction extends BaseRestHandler {
GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest(repositories).snapshots(snapshots); GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest(repositories).snapshots(snapshots);
getSnapshotsRequest.ignoreUnavailable(request.paramAsBoolean("ignore_unavailable", getSnapshotsRequest.ignoreUnavailable())); getSnapshotsRequest.ignoreUnavailable(request.paramAsBoolean("ignore_unavailable", getSnapshotsRequest.ignoreUnavailable()));
getSnapshotsRequest.verbose(request.paramAsBoolean("verbose", getSnapshotsRequest.verbose())); getSnapshotsRequest.verbose(request.paramAsBoolean("verbose", getSnapshotsRequest.verbose()));
final GetSnapshotsRequest.SortBy sort = GetSnapshotsRequest.SortBy.of(request.param("sort", getSnapshotsRequest.sort().toString())); final SnapshotSortKey sort = SnapshotSortKey.of(request.param("sort", getSnapshotsRequest.sort().toString()));
getSnapshotsRequest.sort(sort); getSnapshotsRequest.sort(sort);
final int size = request.paramAsInt("size", getSnapshotsRequest.size()); final int size = request.paramAsInt("size", getSnapshotsRequest.size());
getSnapshotsRequest.size(size); getSnapshotsRequest.size(size);

View file

@ -39,8 +39,7 @@ public class GetSnapshotsRequestTests extends ESTestCase {
assertThat(e.getMessage(), containsString("can't use offset with verbose=false")); assertThat(e.getMessage(), containsString("can't use offset with verbose=false"));
} }
{ {
final GetSnapshotsRequest request = new GetSnapshotsRequest("repo", "snapshot").verbose(false) final GetSnapshotsRequest request = new GetSnapshotsRequest("repo", "snapshot").verbose(false).sort(SnapshotSortKey.INDICES);
.sort(GetSnapshotsRequest.SortBy.INDICES);
final ActionRequestValidationException e = request.validate(); final ActionRequestValidationException e = request.validate();
assertThat(e.getMessage(), containsString("can't use non-default sort with verbose=false")); assertThat(e.getMessage(), containsString("can't use non-default sort with verbose=false"));
} }

View file

@ -13,7 +13,7 @@ import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRunnable; import org.elasticsearch.action.ActionRunnable;
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; 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.SnapshotSortKey;
import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.support.GroupedActionListener; import org.elasticsearch.action.support.GroupedActionListener;
import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.action.support.PlainActionFuture;
@ -734,11 +734,7 @@ public abstract class AbstractSnapshotIntegTestCase extends ESIntegTestCase {
}); });
} }
public static void assertSnapshotListSorted( public static void assertSnapshotListSorted(List<SnapshotInfo> snapshotInfos, @Nullable SnapshotSortKey sort, SortOrder sortOrder) {
List<SnapshotInfo> snapshotInfos,
@Nullable GetSnapshotsRequest.SortBy sort,
SortOrder sortOrder
) {
final BiConsumer<SnapshotInfo, SnapshotInfo> assertion; final BiConsumer<SnapshotInfo, SnapshotInfo> assertion;
if (sort == null) { if (sort == null) {
assertion = (s1, s2) -> assertThat(s2, greaterThanOrEqualTo(s1)); assertion = (s1, s2) -> assertThat(s2, greaterThanOrEqualTo(s1));