-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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; | ||
|
||
|
@@ -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 + "]"); | ||
} | ||
|
@@ -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 + "]"); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 + "]"); | ||
} | ||
|
@@ -570,6 +576,11 @@ private static SnapshotsInRepo sortSnapshots( | |
order | ||
); | ||
break; | ||
case REPOSITORY: | ||
isAfter = order == SortOrder.ASC | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add parenthesis around There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 + "]"); | ||
} | ||
|
@@ -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) { | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)