|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.cluster.metadata; |
| 10 | + |
| 11 | +import org.opensearch.cluster.node.DiscoveryNode; |
| 12 | +import org.opensearch.cluster.routing.allocation.RoutingAllocation; |
| 13 | +import org.opensearch.cluster.routing.allocation.decider.Decision; |
| 14 | +import org.opensearch.common.Booleans; |
| 15 | +import org.opensearch.common.settings.Setting; |
| 16 | +import org.opensearch.common.settings.Setting.Property; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.OptionalInt; |
| 23 | + |
| 24 | +import static org.opensearch.cluster.metadata.MetadataIndexStateService.isIndexVerifiedBeforeClosed; |
| 25 | + |
| 26 | +/** |
| 27 | + * This class acts as a functional wrapper around the {@code index.auto_expand_search_replicas} setting. |
| 28 | + * This setting's value expands into a minimum and maximum value, requiring special handling based on the |
| 29 | + * number of search nodes in the cluster. This class handles parsing and simplifies access to these values. |
| 30 | + * |
| 31 | + * @opensearch.internal |
| 32 | + */ |
| 33 | +public final class AutoExpandSearchReplicas { |
| 34 | + // the value we recognize in the "max" position to mean all the search nodes |
| 35 | + private static final String ALL_NODES_VALUE = "all"; |
| 36 | + |
| 37 | + private static final AutoExpandSearchReplicas FALSE_INSTANCE = new AutoExpandSearchReplicas(0, 0, false); |
| 38 | + |
| 39 | + public static final Setting<AutoExpandSearchReplicas> SETTING = new Setting<>( |
| 40 | + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS, |
| 41 | + "false", |
| 42 | + AutoExpandSearchReplicas::parse, |
| 43 | + Property.Dynamic, |
| 44 | + Property.IndexScope |
| 45 | + ); |
| 46 | + |
| 47 | + private static AutoExpandSearchReplicas parse(String value) { |
| 48 | + final int min; |
| 49 | + final int max; |
| 50 | + if (Booleans.isFalse(value)) { |
| 51 | + return FALSE_INSTANCE; |
| 52 | + } |
| 53 | + final int dash = value.indexOf('-'); |
| 54 | + if (-1 == dash) { |
| 55 | + throw new IllegalArgumentException( |
| 56 | + "failed to parse [" + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS + "] from value: [" + value + "] at index " + dash |
| 57 | + ); |
| 58 | + } |
| 59 | + final String sMin = value.substring(0, dash); |
| 60 | + try { |
| 61 | + min = Integer.parseInt(sMin); |
| 62 | + } catch (NumberFormatException e) { |
| 63 | + throw new IllegalArgumentException( |
| 64 | + "failed to parse [" + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS + "] from value: [" + value + "] at index " + dash, |
| 65 | + e |
| 66 | + ); |
| 67 | + } |
| 68 | + String sMax = value.substring(dash + 1); |
| 69 | + if (sMax.equals(ALL_NODES_VALUE)) { |
| 70 | + max = Integer.MAX_VALUE; |
| 71 | + } else { |
| 72 | + try { |
| 73 | + max = Integer.parseInt(sMax); |
| 74 | + } catch (NumberFormatException e) { |
| 75 | + throw new IllegalArgumentException( |
| 76 | + "failed to parse [" |
| 77 | + + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS |
| 78 | + + "] from value: [" |
| 79 | + + value |
| 80 | + + "] at index " |
| 81 | + + dash, |
| 82 | + e |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + return new AutoExpandSearchReplicas(min, max, true); |
| 87 | + } |
| 88 | + |
| 89 | + private final int minSearchReplicas; |
| 90 | + private final int maxSearchReplicas; |
| 91 | + private final boolean enabled; |
| 92 | + |
| 93 | + private AutoExpandSearchReplicas(int minReplicas, int maxReplicas, boolean enabled) { |
| 94 | + if (minReplicas > maxReplicas) { |
| 95 | + throw new IllegalArgumentException( |
| 96 | + "[" |
| 97 | + + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS |
| 98 | + + "] minSearchReplicas must be =< maxSearchReplicas but wasn't " |
| 99 | + + minReplicas |
| 100 | + + " > " |
| 101 | + + maxReplicas |
| 102 | + ); |
| 103 | + } |
| 104 | + this.minSearchReplicas = minReplicas; |
| 105 | + this.maxSearchReplicas = maxReplicas; |
| 106 | + this.enabled = enabled; |
| 107 | + } |
| 108 | + |
| 109 | + int getMinSearchReplicas() { |
| 110 | + return minSearchReplicas; |
| 111 | + } |
| 112 | + |
| 113 | + public int getMaxSearchReplicas() { |
| 114 | + return maxSearchReplicas; |
| 115 | + } |
| 116 | + |
| 117 | + public boolean isEnabled() { |
| 118 | + return enabled; |
| 119 | + } |
| 120 | + |
| 121 | + private OptionalInt getDesiredNumberOfSearchReplicas(IndexMetadata indexMetadata, RoutingAllocation allocation) { |
| 122 | + int numMatchingSearchNodes = (int) allocation.nodes() |
| 123 | + .getDataNodes() |
| 124 | + .values() |
| 125 | + .stream() |
| 126 | + .filter(DiscoveryNode::isSearchNode) |
| 127 | + .map(node -> allocation.deciders().shouldAutoExpandToNode(indexMetadata, node, allocation)) |
| 128 | + .filter(decision -> decision.type() != Decision.Type.NO) |
| 129 | + .count(); |
| 130 | + |
| 131 | + return calculateNumberOfSearchReplicas(numMatchingSearchNodes); |
| 132 | + } |
| 133 | + |
| 134 | + // package private for testing |
| 135 | + OptionalInt calculateNumberOfSearchReplicas(int numMatchingSearchNodes) { |
| 136 | + // Calculate the maximum possible number of search replicas |
| 137 | + int maxPossibleReplicas = Math.min(numMatchingSearchNodes, maxSearchReplicas); |
| 138 | + |
| 139 | + // Determine the number of search replicas |
| 140 | + int numberOfSearchReplicas = Math.max(minSearchReplicas, maxPossibleReplicas); |
| 141 | + |
| 142 | + // Additional check to ensure we don't exceed max possible search replicas |
| 143 | + if (numberOfSearchReplicas <= maxPossibleReplicas) { |
| 144 | + return OptionalInt.of(numberOfSearchReplicas); |
| 145 | + } |
| 146 | + |
| 147 | + return OptionalInt.empty(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String toString() { |
| 152 | + return enabled ? minSearchReplicas + "-" + maxSearchReplicas : "false"; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Checks if there are search replicas with the auto-expand feature that need to be adapted. |
| 157 | + * Returns a map of updates, which maps the indices to be updated to the desired number of search replicas. |
| 158 | + * The map has the desired number of search replicas as key and the indices to update as value, as this allows the result |
| 159 | + * of this method to be directly applied to RoutingTable.Builder#updateNumberOfSearchReplicas. |
| 160 | + */ |
| 161 | + public static Map<Integer, List<String>> getAutoExpandSearchReplicaChanges(Metadata metadata, RoutingAllocation allocation) { |
| 162 | + Map<Integer, List<String>> updatedSearchReplicas = new HashMap<>(); |
| 163 | + |
| 164 | + for (final IndexMetadata indexMetadata : metadata) { |
| 165 | + if (indexMetadata.getState() == IndexMetadata.State.OPEN || isIndexVerifiedBeforeClosed(indexMetadata)) { |
| 166 | + AutoExpandSearchReplicas autoExpandSearchReplicas = SETTING.get(indexMetadata.getSettings()); |
| 167 | + if (autoExpandSearchReplicas.isEnabled()) { |
| 168 | + autoExpandSearchReplicas.getDesiredNumberOfSearchReplicas(indexMetadata, allocation) |
| 169 | + .ifPresent(numberOfSearchReplicas -> { |
| 170 | + if (numberOfSearchReplicas != indexMetadata.getNumberOfSearchOnlyReplicas()) { |
| 171 | + updatedSearchReplicas.computeIfAbsent(numberOfSearchReplicas, ArrayList::new) |
| 172 | + .add(indexMetadata.getIndex().getName()); |
| 173 | + } |
| 174 | + }); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + return updatedSearchReplicas; |
| 179 | + } |
| 180 | +} |
0 commit comments