Skip to content

Avoid extra allocations in RestGetAliasesAction #126177

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

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,28 @@ static RestResponse buildRestResponse(
) throws Exception {
final Set<String> indicesToDisplay = new HashSet<>();
final Set<String> returnedAliasNames = new HashSet<>();
for (final Map.Entry<String, List<AliasMetadata>> cursor : responseAliasMap.entrySet()) {
for (final AliasMetadata aliasMetadata : cursor.getValue()) {
if (aliasesExplicitlyRequested) {
// only display indices that have aliases
indicesToDisplay.add(cursor.getKey());
if (aliasesExplicitlyRequested || requestedAliases.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I've peered into the original code...
Does aliasesExplicitlyRequested have any influence? I think we can remove it (consider it either true or false in the original code).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it can be true even when requestedAliases.length == 0 in the case _all is used for the name param. In prepareRequest() it uses request.paramAsStringArrayOrEmptyIfAll("name"). I originally wanted to eliminate aliasesExplicitlyRequested, but some tests broke even when the RestGetAliasesActionTests passed. An example was:

ClientYamlTestSuiteIT > test {yaml=indices.get_alias/10_basic/Get aliases via /_alias/_all} FAILED
    java.lang.AssertionError: Failure at [indices.get_alias/10_basic:70]: field [test_index_3] has a true value but it shouldn't

I added a new unit test case in RestGetAliasesActionTests that uses _all to cover this scenario.

I also refactored to simplify the code and just use aliasesExplicitlyRequested. Please let me know how this looks to you.

for (final Map.Entry<String, List<AliasMetadata>> cursor : responseAliasMap.entrySet()) {
final var aliases = cursor.getValue();
if (aliases.isEmpty() == false) {
if (aliasesExplicitlyRequested) {
// only display indices that have aliases
indicesToDisplay.add(cursor.getKey());
}
if (requestedAliases.length > 0) {
for (final AliasMetadata aliasMetadata : aliases) {
returnedAliasNames.add(aliasMetadata.alias());
}
}
}
returnedAliasNames.add(aliasMetadata.alias());
}
}
dataStreamAliases.entrySet()
.stream()
.flatMap(entry -> entry.getValue().stream())
.forEach(dataStreamAlias -> returnedAliasNames.add(dataStreamAlias.getName()));
if (requestedAliases.length > 0) {
dataStreamAliases.entrySet()
.stream()
Copy link
Contributor

Choose a reason for hiding this comment

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

May as well destreamify this one here too.

.flatMap(entry -> entry.getValue().stream())
.forEach(dataStreamAlias -> returnedAliasNames.add(dataStreamAlias.getName()));
}

// compute explicitly requested aliases that have are not returned in the result
final SortedSet<String> missingAliases = new TreeSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

can we move the code that computes the missingAliases into a separate function.

Expand Down