-
Couldn't load subscription status.
- Fork 2.3k
Support AutoExpand for SearchReplica #17741
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
mch2
merged 7 commits into
opensearch-project:main
from
vinaykpud:rw/search-replica-auto-expand4
Apr 3, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
10481ce
Added AutoExpand for SearchReplica
vinaykpud f10918a
Included SearchReplica AutoExpand settings for SearchReplica balance
vinaykpud f50e0fe
Updated the createNode method in AutoExpandReplicasTest
vinaykpud c67e13f
Updated based on PR comments
vinaykpud 8fd21ed
Merge branch 'main' into rw/search-replica-auto-expand4
vinaykpud 33ffa2c
Merge branch 'main' into rw/search-replica-auto-expand4
vinaykpud 18d3d4a
Add changelog
vinaykpud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
.../internalClusterTest/java/org/opensearch/cluster/metadata/AutoExpandSearchReplicasIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.metadata; | ||
|
|
||
| import org.opensearch.cluster.routing.UnassignedInfo; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.common.unit.TimeValue; | ||
| import org.opensearch.common.util.FeatureFlags; | ||
| import org.opensearch.indices.replication.common.ReplicationType; | ||
| import org.opensearch.remotestore.RemoteStoreBaseIntegTestCase; | ||
| import org.opensearch.test.InternalTestCluster; | ||
| import org.opensearch.test.OpenSearchIntegTestCase; | ||
|
|
||
| import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE; | ||
|
|
||
| @OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0) | ||
| public class AutoExpandSearchReplicasIT extends RemoteStoreBaseIntegTestCase { | ||
|
|
||
| @Override | ||
| protected Settings featureFlagSettings() { | ||
| return Settings.builder().put(super.featureFlagSettings()).put(FeatureFlags.READER_WRITER_SPLIT_EXPERIMENTAL, Boolean.TRUE).build(); | ||
| } | ||
|
|
||
| public void testAutoExpandSearchReplica() throws Exception { | ||
| String indexName = "test"; | ||
| internalCluster().startClusterManagerOnlyNode(); | ||
|
|
||
| // Create a cluster with 2 data nodes and 1 search node | ||
| internalCluster().startDataOnlyNode(); | ||
| internalCluster().startDataOnlyNode(); | ||
| String searchNode = internalCluster().startSearchOnlyNode(); | ||
|
|
||
| // Create index with 1 primary, 1 replica and 1 search replica shards | ||
| createIndex( | ||
| indexName, | ||
| Settings.builder() | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1) | ||
| .put(SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) | ||
| .put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.timeValueMillis(0)) | ||
| .build() | ||
| ); | ||
| ensureGreen(); | ||
|
|
||
| assertBusy(() -> assertEquals(1, getNumShards(indexName).numSearchReplicas)); | ||
|
|
||
| // Enable auto expand for search replica | ||
| client().admin() | ||
| .indices() | ||
| .prepareUpdateSettings(indexName) | ||
| .setSettings(Settings.builder().put("index.auto_expand_search_replicas", "0-all")) | ||
| .get(); | ||
|
|
||
| // Add 1 more search nodes | ||
| internalCluster().startSearchOnlyNode(); | ||
|
|
||
| assertBusy(() -> assertEquals(2, getNumShards(indexName).numSearchReplicas)); | ||
|
|
||
| // Stop a node which hosts search replica | ||
| internalCluster().stopRandomNode(InternalTestCluster.nameFilter(searchNode)); | ||
| assertBusy(() -> assertEquals(1, getNumShards(indexName).numSearchReplicas)); | ||
|
|
||
| // Add 1 more search nodes | ||
| internalCluster().startSearchOnlyNode(); | ||
| assertBusy(() -> assertEquals(2, getNumShards(indexName).numSearchReplicas)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
server/src/main/java/org/opensearch/cluster/metadata/AutoExpandSearchReplicas.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.metadata; | ||
|
|
||
| import org.opensearch.cluster.node.DiscoveryNode; | ||
| import org.opensearch.cluster.routing.allocation.RoutingAllocation; | ||
| import org.opensearch.cluster.routing.allocation.decider.Decision; | ||
| import org.opensearch.common.Booleans; | ||
| import org.opensearch.common.settings.Setting; | ||
| import org.opensearch.common.settings.Setting.Property; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.OptionalInt; | ||
|
|
||
| import static org.opensearch.cluster.metadata.MetadataIndexStateService.isIndexVerifiedBeforeClosed; | ||
|
|
||
| /** | ||
| * This class acts as a functional wrapper around the {@code index.auto_expand_search_replicas} setting. | ||
| * This setting's value expands into a minimum and maximum value, requiring special handling based on the | ||
| * number of search nodes in the cluster. This class handles parsing and simplifies access to these values. | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public final class AutoExpandSearchReplicas { | ||
| // the value we recognize in the "max" position to mean all the search nodes | ||
| private static final String ALL_NODES_VALUE = "all"; | ||
|
|
||
| private static final AutoExpandSearchReplicas FALSE_INSTANCE = new AutoExpandSearchReplicas(0, 0, false); | ||
|
|
||
| public static final Setting<AutoExpandSearchReplicas> SETTING = new Setting<>( | ||
| IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS, | ||
| "false", | ||
| AutoExpandSearchReplicas::parse, | ||
| Property.Dynamic, | ||
| Property.IndexScope | ||
| ); | ||
|
|
||
| private static AutoExpandSearchReplicas parse(String value) { | ||
| final int min; | ||
| final int max; | ||
| if (Booleans.isFalse(value)) { | ||
| return FALSE_INSTANCE; | ||
| } | ||
| final int dash = value.indexOf('-'); | ||
| if (-1 == dash) { | ||
| throw new IllegalArgumentException( | ||
| "failed to parse [" + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS + "] from value: [" + value + "] at index " + dash | ||
| ); | ||
| } | ||
| final String sMin = value.substring(0, dash); | ||
| try { | ||
| min = Integer.parseInt(sMin); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException( | ||
| "failed to parse [" + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS + "] from value: [" + value + "] at index " + dash, | ||
| e | ||
| ); | ||
| } | ||
| String sMax = value.substring(dash + 1); | ||
| if (sMax.equals(ALL_NODES_VALUE)) { | ||
| max = Integer.MAX_VALUE; | ||
| } else { | ||
| try { | ||
| max = Integer.parseInt(sMax); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException( | ||
| "failed to parse [" | ||
| + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS | ||
| + "] from value: [" | ||
| + value | ||
| + "] at index " | ||
| + dash, | ||
| e | ||
| ); | ||
| } | ||
| } | ||
| return new AutoExpandSearchReplicas(min, max, true); | ||
| } | ||
|
|
||
| private final int minSearchReplicas; | ||
| private final int maxSearchReplicas; | ||
| private final boolean enabled; | ||
|
|
||
| private AutoExpandSearchReplicas(int minReplicas, int maxReplicas, boolean enabled) { | ||
| if (minReplicas > maxReplicas) { | ||
| throw new IllegalArgumentException( | ||
| "[" | ||
| + IndexMetadata.SETTING_AUTO_EXPAND_SEARCH_REPLICAS | ||
| + "] minSearchReplicas must be =< maxSearchReplicas but wasn't " | ||
| + minReplicas | ||
| + " > " | ||
| + maxReplicas | ||
| ); | ||
| } | ||
| this.minSearchReplicas = minReplicas; | ||
| this.maxSearchReplicas = maxReplicas; | ||
| this.enabled = enabled; | ||
| } | ||
|
|
||
| int getMinSearchReplicas() { | ||
| return minSearchReplicas; | ||
| } | ||
|
|
||
| public int getMaxSearchReplicas() { | ||
| return maxSearchReplicas; | ||
| } | ||
|
|
||
| public boolean isEnabled() { | ||
| return enabled; | ||
| } | ||
|
|
||
| private OptionalInt getDesiredNumberOfSearchReplicas(IndexMetadata indexMetadata, RoutingAllocation allocation) { | ||
| int numMatchingSearchNodes = (int) allocation.nodes() | ||
| .getDataNodes() | ||
| .values() | ||
| .stream() | ||
| .filter(DiscoveryNode::isSearchNode) | ||
| .map(node -> allocation.deciders().shouldAutoExpandToNode(indexMetadata, node, allocation)) | ||
| .filter(decision -> decision.type() != Decision.Type.NO) | ||
| .count(); | ||
|
|
||
| return calculateNumberOfSearchReplicas(numMatchingSearchNodes); | ||
| } | ||
|
|
||
| // package private for testing | ||
| OptionalInt calculateNumberOfSearchReplicas(int numMatchingSearchNodes) { | ||
| // Calculate the maximum possible number of search replicas | ||
| int maxPossibleReplicas = Math.min(numMatchingSearchNodes, maxSearchReplicas); | ||
|
|
||
| // Determine the number of search replicas | ||
| int numberOfSearchReplicas = Math.max(minSearchReplicas, maxPossibleReplicas); | ||
|
|
||
| // Additional check to ensure we don't exceed max possible search replicas | ||
| if (numberOfSearchReplicas <= maxPossibleReplicas) { | ||
| return OptionalInt.of(numberOfSearchReplicas); | ||
| } | ||
|
|
||
| return OptionalInt.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return enabled ? minSearchReplicas + "-" + maxSearchReplicas : "false"; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if there are search replicas with the auto-expand feature that need to be adapted. | ||
| * Returns a map of updates, which maps the indices to be updated to the desired number of search replicas. | ||
| * The map has the desired number of search replicas as key and the indices to update as value, as this allows the result | ||
| * of this method to be directly applied to RoutingTable.Builder#updateNumberOfSearchReplicas. | ||
| */ | ||
| public static Map<Integer, List<String>> getAutoExpandSearchReplicaChanges(Metadata metadata, RoutingAllocation allocation) { | ||
| Map<Integer, List<String>> updatedSearchReplicas = new HashMap<>(); | ||
|
|
||
| for (final IndexMetadata indexMetadata : metadata) { | ||
| if (indexMetadata.getState() == IndexMetadata.State.OPEN || isIndexVerifiedBeforeClosed(indexMetadata)) { | ||
| AutoExpandSearchReplicas autoExpandSearchReplicas = SETTING.get(indexMetadata.getSettings()); | ||
| if (autoExpandSearchReplicas.isEnabled()) { | ||
| autoExpandSearchReplicas.getDesiredNumberOfSearchReplicas(indexMetadata, allocation) | ||
| .ifPresent(numberOfSearchReplicas -> { | ||
| if (numberOfSearchReplicas != indexMetadata.getNumberOfSearchOnlyReplicas()) { | ||
| updatedSearchReplicas.computeIfAbsent(numberOfSearchReplicas, ArrayList::new) | ||
| .add(indexMetadata.getIndex().getName()); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| return updatedSearchReplicas; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.