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

Cache DiscoveryNode#trimTier Result #80179

Merged
Changes from 1 commit
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 @@ -20,7 +20,6 @@
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

public class DiscoveryNodeFilters {

Expand Down Expand Up @@ -66,9 +65,13 @@ public static DiscoveryNodeFilters buildFromKeyValue(OpType opType, Map<String,

private final OpType opType;

@Nullable
private final DiscoveryNodeFilters trimmed;
Copy link
Contributor

Choose a reason for hiding this comment

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

Naming nit: would be good to mention that it's the tier preference that was trimmed - withoutTierPreferences or similar perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

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

++ sounds good, renamed


DiscoveryNodeFilters(OpType opType, Map<String, String[]> filters) {
this.opType = opType;
this.filters = filters;
this.filters = Map.copyOf(filters);
this.trimmed = doTrim(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: could we avoid even calling this when constructing the trimmed filters? Seems unnecessary to walk the filters twice just to check that trimming actually worked.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually ... we can just avoid that walk altogether in 8 :) Missed that fact earlier, but we don't really need to walk the map in 8 because we're only filtering out a specific key (and null which I moved elsewhere but I think that filtering is unused ... just didn't want to change behavior here).
Simplified accordingly now

}

private boolean matchByIP(String[] values, @Nullable String hostIp, @Nullable String publishIp) {
Expand All @@ -89,18 +92,24 @@ private boolean matchByIP(String[] values, @Nullable String hostIp, @Nullable St
*/
@Nullable
public static DiscoveryNodeFilters trimTier(@Nullable DiscoveryNodeFilters original) {
if (original == null) {
return null;
}
return original == null ? null : original.trimmed;
}

Map<String, String[]> newFilters = original.filters.entrySet()
.stream()
// Remove all entries that use "_tier_preference", as these will be handled elsewhere
.filter(entry -> {
String attr = entry.getKey();
return attr != null && attr.equals("_tier_preference") == false;
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
private static DiscoveryNodeFilters doTrim(DiscoveryNodeFilters original) {
boolean filtered = false;
final Map<String, String[]> newFilters = new HashMap<>(original.filters.size());
// Remove all entries that use "_tier_preference", as these will be handled elsewhere
for (Map.Entry<String, String[]> entry : original.filters.entrySet()) {
String attr = entry.getKey();
if (attr != null && attr.equals("_tier_preference") == false) {
newFilters.put(entry.getKey(), entry.getValue());
} else {
filtered = true;
}
}
if (filtered == false) {
return original;
}

if (newFilters.size() == 0) {
return null;
Expand Down