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

Implement Sort By Repository Name in Get Snapshots API #77049

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 @@ -118,6 +118,9 @@ Allows setting a sort order for the result. Defaults to `start_time`, i.e. sorti
`name`::
Sort snapshots by their name.

`repository`::
Sort snapshots by their repository name and break ties by snapshot name.

`index_count`::
Sort snapshots by the number of indices they contain and break ties by snapshot name.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ private void doTestSortOrder(String repoName, Collection<String> allSnapshotName
GetSnapshotsRequest.SortBy.FAILED_SHARDS,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.REPOSITORY, order),
GetSnapshotsRequest.SortBy.REPOSITORY,
order
);
}

public void testResponseSizeLimit() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,36 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
}

public void testSortBy() throws Exception {
final String repoName = "test-repo";
final String repoNameA = "test-repo-a";
final Path repoPath = randomRepoPath();
createRepository(repoName, "fs", repoPath);
maybeInitWithOldSnapshotVersion(repoName, repoPath);
final List<String> snapshotNamesWithoutIndex = createNSnapshots(repoName, randomIntBetween(3, 20));
createRepository(repoNameA, "fs", repoPath);
maybeInitWithOldSnapshotVersion(repoNameA, repoPath);
final String repoNameB = "test-repo-b";
createRepository(repoNameB, "fs");

final List<String> snapshotNamesWithoutIndexA = createNSnapshots(repoNameA, randomIntBetween(3, 20));
final List<String> snapshotNamesWithoutIndexB = createNSnapshots(repoNameB, randomIntBetween(3, 20));

createIndexWithContent("test-index");

final List<String> snapshotNamesWithIndex = createNSnapshots(repoName, randomIntBetween(3, 20));
final List<String> snapshotNamesWithIndexA = createNSnapshots(repoNameA, randomIntBetween(3, 20));
final List<String> snapshotNamesWithIndexB = createNSnapshots(repoNameB, randomIntBetween(3, 20));

final Collection<String> allSnapshotNamesA = new HashSet<>(snapshotNamesWithIndexA);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: What do you think about creating the set in one-line with the Streams API?

Stream.concat(snapshotNamesWithIndexA.stream(), snapshotNamesWithoutIndexA.stream()).collect(toSet())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the biggest fan to be honest but that's just personal preference :)

final Collection<String> allSnapshotNamesB = new HashSet<>(snapshotNamesWithIndexB);
allSnapshotNamesA.addAll(snapshotNamesWithoutIndexA);
allSnapshotNamesB.addAll(snapshotNamesWithoutIndexB);

final Collection<String> allSnapshotNames = new HashSet<>(snapshotNamesWithIndex);
allSnapshotNames.addAll(snapshotNamesWithoutIndex);
doTestSortOrder(repoNameA, allSnapshotNamesA, SortOrder.ASC);
doTestSortOrder(repoNameA, allSnapshotNamesA, SortOrder.DESC);

doTestSortOrder(repoName, allSnapshotNames, SortOrder.ASC);
doTestSortOrder(repoName, allSnapshotNames, SortOrder.DESC);
doTestSortOrder(repoNameB, allSnapshotNamesB, SortOrder.ASC);
doTestSortOrder(repoNameB, allSnapshotNamesB, SortOrder.DESC);

final Collection<String> allSnapshots = new HashSet<>(allSnapshotNamesA);
allSnapshots.addAll(allSnapshotNamesB);
doTestSortOrder("*", allSnapshots, SortOrder.ASC);
doTestSortOrder("*", allSnapshots, SortOrder.DESC);
}

private void doTestSortOrder(String repoName, Collection<String> allSnapshotNames, SortOrder order) {
Expand Down Expand Up @@ -88,6 +103,11 @@ private void doTestSortOrder(String repoName, Collection<String> allSnapshotName
GetSnapshotsRequest.SortBy.FAILED_SHARDS,
order
);
assertSnapshotListSorted(
allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.REPOSITORY, order),
GetSnapshotsRequest.SortBy.REPOSITORY,
order
);
}

public void testResponseSizeLimit() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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;
private static final Version SORT_BY_SHARDS_OR_REPO_VERSION = Version.V_7_16_0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being a little lazy here :) <= we don't run BwC tests that would fail because of this and this is going to backport problem free.


public static final int NO_LIMIT = -1;

Expand Down Expand Up @@ -138,8 +138,11 @@ 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() + "]");
if ((sort == SortBy.SHARDS || sort == SortBy.FAILED_SHARDS || sort == SortBy.REPOSITORY)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if it would make sense to extract all the 7.x incombatible parameters to a final set and then do a set.contains check instead of a combination of or disjunctions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we could but I think it's a little easier to follow this exceptional path by spelling it out directly. The performance here doesn't matter much with the request rates expected here so I think keeping it shorter is fine.

&& out.getVersion().before(SORT_BY_SHARDS_OR_REPO_VERSION)) {
throw new IllegalArgumentException(
"can't use sort by shard count or repository name with node version [" + out.getVersion() + "]"
);
}
out.writeEnum(sort);
out.writeVInt(size);
Expand Down Expand Up @@ -327,7 +330,8 @@ public enum SortBy {
DURATION("duration"),
INDICES("index_count"),
SHARDS("shard_count"),
FAILED_SHARDS("failed_shard_count");
FAILED_SHARDS("failed_shard_count"),
REPOSITORY("repository");

private final String param;

Expand All @@ -354,6 +358,8 @@ public static SortBy of(String value) {
return SHARDS;
case "failed_shard_count":
return FAILED_SHARDS;
case "repository":
return REPOSITORY;
default:
throw new IllegalArgumentException("unknown sort order [" + value + "]");
}
Expand Down Expand Up @@ -405,6 +411,9 @@ public static After from(@Nullable SnapshotInfo snapshotInfo, SortBy sortBy) {
case FAILED_SHARDS:
afterValue = String.valueOf(snapshotInfo.failedShards());
break;
case REPOSITORY:
afterValue = snapshotInfo.repository();
break;
default:
throw new AssertionError("unknown sort column [" + sortBy + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ private static SnapshotsInRepo buildSimpleSnapshotInfos(

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 static SnapshotsInRepo sortSnapshots(
final List<SnapshotInfo> snapshotInfos,
final GetSnapshotsRequest.SortBy sortBy,
Expand Down Expand Up @@ -520,6 +523,9 @@ private static SnapshotsInRepo sortSnapshots(
case FAILED_SHARDS:
comparator = BY_FAILED_SHARDS_COUNT;
break;
case REPOSITORY:
comparator = BY_REPOSITORY;
break;
default:
throw new AssertionError("unexpected sort column [" + sortBy + "]");
}
Expand Down Expand Up @@ -570,6 +576,11 @@ private static SnapshotsInRepo sortSnapshots(
order
);
break;
case REPOSITORY:
isAfter = order == SortOrder.ASC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add parenthesis around order == SortOrder.ASC to make a clear distinction between the assignment and the ternary operator. Otherwise the reader needs to know the order priority of Java operators to know that isAfter = order doesn't get executed :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be OK to expect that of the reader, but if a parenthesis is added, it should cover the entire ternary expression.

? (info -> compareRepositoryName(snapshotName, repoName, info) < 0)
: (info -> compareRepositoryName(snapshotName, repoName, info) > 0);
break;
default:
throw new AssertionError("unexpected sort column [" + sortBy + "]");
}
Expand Down Expand Up @@ -606,6 +617,14 @@ private static Predicate<SnapshotInfo> filterByLongOffset(
};
}

private static int compareRepositoryName(String name, String repoName, SnapshotInfo info) {
final int res = repoName.compareTo(info.repository());
if (res != 0) {
return res;
}
return name.compareTo(info.snapshotId().getName());
}

private static int compareName(String name, String repoName, SnapshotInfo info) {
final int res = name.compareTo(info.snapshotId().getName());
if (res != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,9 @@ public static void assertSnapshotListSorted(List<SnapshotInfo> snapshotInfos, @N
case FAILED_SHARDS:
assertion = (s1, s2) -> assertThat(s2.failedShards(), greaterThanOrEqualTo(s1.failedShards()));
break;
case REPOSITORY:
assertion = (s1, s2) -> assertThat(s2.repository(), greaterThanOrEqualTo(s1.repository()));
break;
default:
throw new AssertionError("unknown sort column [" + sort + "]");
}
Expand Down