Skip to content

Commit

Permalink
Add Sort By Shard Count and Failed Shard Count to Get Snapshots API (e…
Browse files Browse the repository at this point in the history
…lastic#77011)

It's in the title. As requested by the Kibana team, adding these two additional sort columns.

relates elastic#74350
  • Loading branch information
original-brownbear committed Aug 30, 2021
1 parent 08ff448 commit 209fa7c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ Allows setting a sort order for the result. Defaults to `start_time`, i.e. sorti
`index_count`::
Sort snapshots by the number of indices they contain and break ties by snapshot name.
`shard_count`::
Sort snapshots by the number of shards they contain and break ties by snapshot name.
`failed_shard_count`::
Sort snapshots by the number of shards that they failed to snapshot and break ties by snapshot name.
====

`size`::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ private void doTestSortOrder(String repoName, Collection<String> allSnapshotName
GetSnapshotsRequest.SortBy.START_TIME,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.SHARDS, order),
GetSnapshotsRequest.SortBy.SHARDS,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order),
GetSnapshotsRequest.SortBy.FAILED_SHARDS,
order
);
}

public void testResponseSizeLimit() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ private void doTestSortOrder(String repoName, Collection<String> allSnapshotName
GetSnapshotsRequest.SortBy.START_TIME,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.SHARDS, order),
GetSnapshotsRequest.SortBy.SHARDS,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order),
GetSnapshotsRequest.SortBy.FAILED_SHARDS,
order
);
}

public void testResponseSizeLimit() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>

public static final Version NUMERIC_PAGINATION_VERSION = Version.V_7_15_0;

private static final Version SORT_BY_SHARD_COUNTS_VERSION = Version.V_7_16_0;

public static final int NO_LIMIT = -1;

/**
Expand Down Expand Up @@ -147,6 +149,9 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(verbose);
if (out.getVersion().onOrAfter(PAGINATED_GET_SNAPSHOTS_VERSION)) {
out.writeOptionalWriteable(after);
if ((sort == SortBy.SHARDS || sort == SortBy.FAILED_SHARDS) && out.getVersion().before(SORT_BY_SHARD_COUNTS_VERSION)) {
throw new IllegalArgumentException("can't use sort by shard count with node version [" + out.getVersion() + "]");
}
out.writeEnum(sort);
out.writeVInt(size);
order.writeTo(out);
Expand Down Expand Up @@ -356,7 +361,9 @@ public enum SortBy {
START_TIME("start_time"),
NAME("name"),
DURATION("duration"),
INDICES("index_count");
INDICES("index_count"),
SHARDS("shard_count"),
FAILED_SHARDS("failed_shard_count");

private final String param;

Expand All @@ -379,6 +386,10 @@ public static SortBy of(String value) {
return DURATION;
case "index_count":
return INDICES;
case "shard_count":
return SHARDS;
case "failed_shard_count":
return FAILED_SHARDS;
default:
throw new IllegalArgumentException("unknown sort order [" + value + "]");
}
Expand Down Expand Up @@ -424,6 +435,12 @@ public static After from(@Nullable SnapshotInfo snapshotInfo, SortBy sortBy) {
case INDICES:
afterValue = String.valueOf(snapshotInfo.indices().size());
break;
case SHARDS:
afterValue = String.valueOf(snapshotInfo.totalShards());
break;
case FAILED_SHARDS:
afterValue = String.valueOf(snapshotInfo.failedShards());
break;
default:
throw new AssertionError("unknown sort column [" + sortBy + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,12 @@ private static SnapshotsInRepo buildSimpleSnapshotInfos(
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 SnapshotsInRepo sortSnapshots(
Expand All @@ -514,6 +520,12 @@ private static SnapshotsInRepo sortSnapshots(
case INDICES:
comparator = BY_INDICES_COUNT;
break;
case SHARDS:
comparator = BY_SHARDS_COUNT;
break;
case FAILED_SHARDS:
comparator = BY_FAILED_SHARDS_COUNT;
break;
default:
throw new AssertionError("unexpected sort column [" + sortBy + "]");
}
Expand Down Expand Up @@ -552,6 +564,18 @@ private static SnapshotsInRepo sortSnapshots(
order
);
break;
case SHARDS:
isAfter = filterByLongOffset(SnapshotInfo::totalShards, Integer.parseInt(after.value()), snapshotName, repoName, order);
break;
case FAILED_SHARDS:
isAfter = filterByLongOffset(
SnapshotInfo::failedShards,
Integer.parseInt(after.value()),
snapshotName,
repoName,
order
);
break;
default:
throw new AssertionError("unexpected sort column [" + sortBy + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,12 @@ public static void assertSnapshotListSorted(List<SnapshotInfo> snapshotInfos, @N
case INDICES:
assertion = (s1, s2) -> assertThat(s2.indices().size(), greaterThanOrEqualTo(s1.indices().size()));
break;
case SHARDS:
assertion = (s1, s2) -> assertThat(s2.totalShards(), greaterThanOrEqualTo(s1.totalShards()));
break;
case FAILED_SHARDS:
assertion = (s1, s2) -> assertThat(s2.failedShards(), greaterThanOrEqualTo(s1.failedShards()));
break;
default:
throw new AssertionError("unknown sort column [" + sort + "]");
}
Expand Down

0 comments on commit 209fa7c

Please sign in to comment.