Skip to content

Commit

Permalink
Cache DiscoveryNode#trimTier Result (#80179)
Browse files Browse the repository at this point in the history
No need to recompute this over and over. This is the most expensive part of
`FilterAllocationDecider` in benchmarking. Since we already cache the filter itself
on the `IndexMetadata`, we can just cache the trimmed version also and
`FilterAllocationDecider` effectively disappears from profiling.
Also, made the filter immutable (which should as a side-effect also make it faster
when iterating over it in the match method).
  • Loading branch information
original-brownbear authored Nov 2, 2021
1 parent d13baad commit 268b189
Showing 1 changed file with 16 additions and 20 deletions.
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 @@ -52,7 +51,7 @@ public static DiscoveryNodeFilters buildFromKeyValue(OpType opType, Map<String,
Map<String, String[]> bFilters = new HashMap<>();
for (Map.Entry<String, String> entry : filters.entrySet()) {
String[] values = Strings.tokenizeToStringArray(entry.getValue(), ",");
if (values.length > 0) {
if (values.length > 0 && entry.getKey() != null) {
bFilters.put(entry.getKey(), values);
}
}
Expand All @@ -66,9 +65,13 @@ public static DiscoveryNodeFilters buildFromKeyValue(OpType opType, Map<String,

private final OpType opType;

DiscoveryNodeFilters(OpType opType, Map<String, String[]> filters) {
@Nullable
private final DiscoveryNodeFilters withoutTierPreferences;

private DiscoveryNodeFilters(OpType opType, Map<String, String[]> filters) {
this.opType = opType;
this.filters = filters;
this.filters = Map.copyOf(filters);
this.withoutTierPreferences = doTrimTier(this);
}

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

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));
return original == null ? null : original.withoutTierPreferences;
}

if (newFilters.size() == 0) {
return null;
} else {
return new DiscoveryNodeFilters(original.opType, newFilters);
private static DiscoveryNodeFilters doTrimTier(DiscoveryNodeFilters original) {
if (original.filters.containsKey("_tier_preference") == false) {
return original;
}
final Map<String, String[]> newFilters = new HashMap<>(original.filters);
final String[] removed = newFilters.remove("_tier_preference");
assert removed != null;
return newFilters.isEmpty() ? null : new DiscoveryNodeFilters(original.opType, newFilters);
}

public boolean match(DiscoveryNode node) {
Expand Down

0 comments on commit 268b189

Please sign in to comment.