Skip to content

Commit 84d7831

Browse files
Add Sort By Shard Count and Failed Shard Count to Get Snapshots API (#77011) (#77018)
It's in the title. As requested by the Kibana team, adding these two additional sort columns. relates #74350
1 parent 08ff448 commit 84d7831

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

docs/reference/snapshot-restore/apis/get-snapshot-api.asciidoc

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ Allows setting a sort order for the result. Defaults to `start_time`, i.e. sorti
120120
121121
`index_count`::
122122
Sort snapshots by the number of indices they contain and break ties by snapshot name.
123+
124+
`shard_count`::
125+
Sort snapshots by the number of shards they contain and break ties by snapshot name.
126+
127+
`failed_shard_count`::
128+
Sort snapshots by the number of shards that they failed to snapshot and break ties by snapshot name.
123129
====
124130

125131
`size`::

qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java

+10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ private void doTestSortOrder(String repoName, Collection<String> allSnapshotName
8686
GetSnapshotsRequest.SortBy.START_TIME,
8787
order
8888
);
89+
assertSnapshotListSorted(
90+
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.SHARDS, order),
91+
GetSnapshotsRequest.SortBy.SHARDS,
92+
order
93+
);
94+
assertSnapshotListSorted(
95+
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order),
96+
GetSnapshotsRequest.SortBy.FAILED_SHARDS,
97+
order
98+
);
8999
}
90100

91101
public void testResponseSizeLimit() throws Exception {

server/src/internalClusterTest/java/org/elasticsearch/snapshots/GetSnapshotsIT.java

+10
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ private void doTestSortOrder(String repoName, Collection<String> allSnapshotName
7878
GetSnapshotsRequest.SortBy.START_TIME,
7979
order
8080
);
81+
assertSnapshotListSorted(
82+
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.SHARDS, order),
83+
GetSnapshotsRequest.SortBy.SHARDS,
84+
order
85+
);
86+
assertSnapshotListSorted(
87+
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order),
88+
GetSnapshotsRequest.SortBy.FAILED_SHARDS,
89+
order
90+
);
8191
}
8292

8393
public void testResponseSizeLimit() throws Exception {

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsRequest.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
4646

4747
public static final Version NUMERIC_PAGINATION_VERSION = Version.V_7_15_0;
4848

49+
private static final Version SORT_BY_SHARD_COUNTS_VERSION = Version.V_7_16_0;
50+
4951
public static final int NO_LIMIT = -1;
5052

5153
/**
@@ -147,6 +149,9 @@ public void writeTo(StreamOutput out) throws IOException {
147149
out.writeBoolean(verbose);
148150
if (out.getVersion().onOrAfter(PAGINATED_GET_SNAPSHOTS_VERSION)) {
149151
out.writeOptionalWriteable(after);
152+
if ((sort == SortBy.SHARDS || sort == SortBy.FAILED_SHARDS) && out.getVersion().before(SORT_BY_SHARD_COUNTS_VERSION)) {
153+
throw new IllegalArgumentException("can't use sort by shard count with node version [" + out.getVersion() + "]");
154+
}
150155
out.writeEnum(sort);
151156
out.writeVInt(size);
152157
order.writeTo(out);
@@ -356,7 +361,9 @@ public enum SortBy {
356361
START_TIME("start_time"),
357362
NAME("name"),
358363
DURATION("duration"),
359-
INDICES("index_count");
364+
INDICES("index_count"),
365+
SHARDS("shard_count"),
366+
FAILED_SHARDS("failed_shard_count");
360367

361368
private final String param;
362369

@@ -379,6 +386,10 @@ public static SortBy of(String value) {
379386
return DURATION;
380387
case "index_count":
381388
return INDICES;
389+
case "shard_count":
390+
return SHARDS;
391+
case "failed_shard_count":
392+
return FAILED_SHARDS;
382393
default:
383394
throw new IllegalArgumentException("unknown sort order [" + value + "]");
384395
}
@@ -424,6 +435,12 @@ public static After from(@Nullable SnapshotInfo snapshotInfo, SortBy sortBy) {
424435
case INDICES:
425436
afterValue = String.valueOf(snapshotInfo.indices().size());
426437
break;
438+
case SHARDS:
439+
afterValue = String.valueOf(snapshotInfo.totalShards());
440+
break;
441+
case FAILED_SHARDS:
442+
afterValue = String.valueOf(snapshotInfo.failedShards());
443+
break;
427444
default:
428445
throw new AssertionError("unknown sort column [" + sortBy + "]");
429446
}

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java

+24
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,12 @@ private static SnapshotsInRepo buildSimpleSnapshotInfos(
490490
private static final Comparator<SnapshotInfo> BY_INDICES_COUNT = Comparator.<SnapshotInfo>comparingInt(sni -> sni.indices().size())
491491
.thenComparing(SnapshotInfo::snapshotId);
492492

493+
private static final Comparator<SnapshotInfo> BY_SHARDS_COUNT = Comparator.comparingInt(SnapshotInfo::totalShards)
494+
.thenComparing(SnapshotInfo::snapshotId);
495+
496+
private static final Comparator<SnapshotInfo> BY_FAILED_SHARDS_COUNT = Comparator.comparingInt(SnapshotInfo::failedShards)
497+
.thenComparing(SnapshotInfo::snapshotId);
498+
493499
private static final Comparator<SnapshotInfo> BY_NAME = Comparator.comparing(sni -> sni.snapshotId().getName());
494500

495501
private static SnapshotsInRepo sortSnapshots(
@@ -514,6 +520,12 @@ private static SnapshotsInRepo sortSnapshots(
514520
case INDICES:
515521
comparator = BY_INDICES_COUNT;
516522
break;
523+
case SHARDS:
524+
comparator = BY_SHARDS_COUNT;
525+
break;
526+
case FAILED_SHARDS:
527+
comparator = BY_FAILED_SHARDS_COUNT;
528+
break;
517529
default:
518530
throw new AssertionError("unexpected sort column [" + sortBy + "]");
519531
}
@@ -552,6 +564,18 @@ private static SnapshotsInRepo sortSnapshots(
552564
order
553565
);
554566
break;
567+
case SHARDS:
568+
isAfter = filterByLongOffset(SnapshotInfo::totalShards, Integer.parseInt(after.value()), snapshotName, repoName, order);
569+
break;
570+
case FAILED_SHARDS:
571+
isAfter = filterByLongOffset(
572+
SnapshotInfo::failedShards,
573+
Integer.parseInt(after.value()),
574+
snapshotName,
575+
repoName,
576+
order
577+
);
578+
break;
555579
default:
556580
throw new AssertionError("unexpected sort column [" + sortBy + "]");
557581
}

test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java

+6
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,12 @@ public static void assertSnapshotListSorted(List<SnapshotInfo> snapshotInfos, @N
700700
case INDICES:
701701
assertion = (s1, s2) -> assertThat(s2.indices().size(), greaterThanOrEqualTo(s1.indices().size()));
702702
break;
703+
case SHARDS:
704+
assertion = (s1, s2) -> assertThat(s2.totalShards(), greaterThanOrEqualTo(s1.totalShards()));
705+
break;
706+
case FAILED_SHARDS:
707+
assertion = (s1, s2) -> assertThat(s2.failedShards(), greaterThanOrEqualTo(s1.failedShards()));
708+
break;
703709
default:
704710
throw new AssertionError("unknown sort column [" + sort + "]");
705711
}

0 commit comments

Comments
 (0)