-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add cross-setting validator to prevent balance factor zero crash loop #22347
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
Open
Dhanwani
wants to merge
1
commit into
opensearch-project:main
Choose a base branch
from
Dhanwani:fix/balance-factor-zero-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
123 changes: 123 additions & 0 deletions
123
...opensearch/cluster/routing/allocation/allocator/BalancedShardsAllocatorSettingsTests.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,123 @@ | ||
| /* | ||
| * 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.routing.allocation.allocator; | ||
|
|
||
| import org.opensearch.common.settings.ClusterSettings; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| import static org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING; | ||
| import static org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING; | ||
|
|
||
| public class BalancedShardsAllocatorSettingsTests extends OpenSearchTestCase { | ||
|
|
||
| public void testBothBalanceFactorsZeroIsRejectedOnConstruction() { | ||
| final Settings settings = Settings.builder() | ||
| .put(INDEX_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .put(SHARD_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .build(); | ||
| final IllegalArgumentException e = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> new BalancedShardsAllocator(settings, new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)) | ||
| ); | ||
| assertThat(e.getMessage(), org.hamcrest.Matchers.containsString("must sum to a value greater than zero")); | ||
| } | ||
|
|
||
| public void testBothBalanceFactorsZeroIsRejectedOnDynamicUpdate() { | ||
| final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); | ||
| new BalancedShardsAllocator(Settings.EMPTY, clusterSettings); | ||
|
|
||
| final Settings newSettings = Settings.builder() | ||
| .put(INDEX_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .put(SHARD_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .build(); | ||
|
|
||
| final IllegalArgumentException e = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> clusterSettings.applySettings(newSettings) | ||
| ); | ||
| assertThat(e.getMessage(), org.hamcrest.Matchers.containsString("illegal value can't update")); | ||
| assertNotNull(e.getCause()); | ||
| assertThat(e.getCause().getMessage(), org.hamcrest.Matchers.containsString("must sum to a value greater than zero")); | ||
| } | ||
|
|
||
| public void testIndexBalanceZeroWithNonZeroShardBalanceIsAccepted() { | ||
| final Settings settings = Settings.builder() | ||
| .put(INDEX_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .put(SHARD_BALANCE_FACTOR_SETTING.getKey(), "1.0") | ||
| .build(); | ||
| final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); | ||
| BalancedShardsAllocator allocator = new BalancedShardsAllocator(settings, clusterSettings); | ||
| assertEquals(0.0f, allocator.getIndexBalance(), 0.0f); | ||
| assertEquals(1.0f, allocator.getShardBalance(), 0.0f); | ||
| } | ||
|
|
||
| public void testShardBalanceZeroWithNonZeroIndexBalanceIsAccepted() { | ||
| final Settings settings = Settings.builder() | ||
| .put(INDEX_BALANCE_FACTOR_SETTING.getKey(), "1.0") | ||
| .put(SHARD_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .build(); | ||
| final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); | ||
| BalancedShardsAllocator allocator = new BalancedShardsAllocator(settings, clusterSettings); | ||
| assertEquals(1.0f, allocator.getIndexBalance(), 0.0f); | ||
| assertEquals(0.0f, allocator.getShardBalance(), 0.0f); | ||
| } | ||
|
|
||
| public void testDynamicUpdateIndexBalanceToZeroWhileShardBalanceAlreadyZeroIsRejected() { | ||
| final Settings initialSettings = Settings.builder() | ||
| .put(SHARD_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .put(INDEX_BALANCE_FACTOR_SETTING.getKey(), "1.0") | ||
| .build(); | ||
| final ClusterSettings clusterSettings = new ClusterSettings(initialSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); | ||
| new BalancedShardsAllocator(initialSettings, clusterSettings); | ||
|
|
||
| final Settings newSettings = Settings.builder() | ||
| .put(INDEX_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .build(); | ||
|
|
||
| final IllegalArgumentException e = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> clusterSettings.applySettings(newSettings) | ||
| ); | ||
| assertThat(e.getCause().getMessage(), org.hamcrest.Matchers.containsString("must sum to a value greater than zero")); | ||
| } | ||
|
|
||
| public void testDynamicUpdateShardBalanceToZeroWhileIndexBalanceAlreadyZeroIsRejected() { | ||
| final Settings initialSettings = Settings.builder() | ||
| .put(INDEX_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .put(SHARD_BALANCE_FACTOR_SETTING.getKey(), "1.0") | ||
| .build(); | ||
| final ClusterSettings clusterSettings = new ClusterSettings(initialSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); | ||
| new BalancedShardsAllocator(initialSettings, clusterSettings); | ||
|
|
||
| final Settings newSettings = Settings.builder() | ||
| .put(SHARD_BALANCE_FACTOR_SETTING.getKey(), "0.0") | ||
| .build(); | ||
|
|
||
| final IllegalArgumentException e = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> clusterSettings.applySettings(newSettings) | ||
| ); | ||
| assertThat(e.getCause().getMessage(), org.hamcrest.Matchers.containsString("must sum to a value greater than zero")); | ||
| } | ||
|
|
||
| public void testValidDynamicUpdateIsAccepted() { | ||
| final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); | ||
| BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY, clusterSettings); | ||
|
|
||
| final Settings newSettings = Settings.builder() | ||
| .put(INDEX_BALANCE_FACTOR_SETTING.getKey(), "0.3") | ||
| .put(SHARD_BALANCE_FACTOR_SETTING.getKey(), "0.7") | ||
| .build(); | ||
|
|
||
| clusterSettings.applySettings(newSettings); | ||
| assertEquals(0.3f, allocator.getIndexBalance(), 0.001f); | ||
| assertEquals(0.7f, allocator.getShardBalance(), 0.001f); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need individual classes for them, In the constructor we can pass the setting name to be fetched