diff --git a/server/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodeFilters.java b/server/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodeFilters.java index 2eab251c23c6c..e1723728d2230 100644 --- a/server/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodeFilters.java +++ b/server/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodeFilters.java @@ -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 { @@ -52,7 +51,7 @@ public static DiscoveryNodeFilters buildFromKeyValue(OpType opType, Map bFilters = new HashMap<>(); for (Map.Entry 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); } } @@ -66,9 +65,13 @@ public static DiscoveryNodeFilters buildFromKeyValue(OpType opType, Map filters) { + @Nullable + private final DiscoveryNodeFilters withoutTierPreferences; + + private DiscoveryNodeFilters(OpType opType, Map 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) { @@ -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 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 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) {