Skip to content
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

Update setting API honors cluster's replica setting as default #14810 #14948

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add validation for the search backpressure cancellation settings ([#15501](https://github.com/opensearch-project/OpenSearch/pull/15501))
- Avoid infinite loop when `flat_object` field contains invalid token ([#15985](https://github.com/opensearch-project/OpenSearch/pull/15985))
- Fix infinite loop in nested agg ([#15931](https://github.com/opensearch-project/OpenSearch/pull/15931))
- Fix update settings with null replica not honoring cluster setting bug ([#14948](https://github.com/opensearch-project/OpenSearch/pull/14948))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,69 @@ private void runTestDefaultNumberOfReplicasTest(final boolean closeIndex) {
assertThat(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(response.getIndexToSettings().get("test")), equalTo(1));
}

public void testNullReplicaUpdate() {
internalCluster().ensureAtLeastNumDataNodes(2);

// cluster setting
String defaultNumberOfReplica = "3";
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("cluster.default_number_of_replicas", defaultNumberOfReplica))
.get()
);
// create index with number of replicas will ignore default value
assertAcked(
client().admin()
.indices()
.prepareCreate("test")
.setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, "2"))
);

String numberOfReplicas = client().admin()
.indices()
.prepareGetSettings("test")
.get()
.getSetting("test", IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
assertEquals("2", numberOfReplicas);
// update setting with null replica will use cluster setting of replica number
assertAcked(
client().admin()
.indices()
.prepareUpdateSettings("test")
.setSettings(Settings.builder().putNull(IndexMetadata.SETTING_NUMBER_OF_REPLICAS))
);

numberOfReplicas = client().admin()
.indices()
.prepareGetSettings("test")
.get()
.getSetting("test", IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
assertEquals(defaultNumberOfReplica, numberOfReplicas);

// Clean up cluster setting, then update setting with null replica should pick up default value of 1
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().putNull("cluster.default_number_of_replicas"))
);
assertAcked(
client().admin()
.indices()
.prepareUpdateSettings("test")
.setSettings(Settings.builder().putNull(IndexMetadata.SETTING_NUMBER_OF_REPLICAS))
);

numberOfReplicas = client().admin()
.indices()
.prepareGetSettings("test")
.get()
.getSetting("test", IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
assertEquals("1", numberOfReplicas);
}

public void testNoopUpdate() {
internalCluster().ensureAtLeastNumDataNodes(2);
final ClusterService clusterService = internalCluster().getClusterManagerNodeInstance(ClusterService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@

validateRefreshIntervalSettings(normalizedSettings, clusterService.getClusterSettings());
validateTranslogDurabilitySettings(normalizedSettings, clusterService.getClusterSettings(), clusterService.getSettings());
final int defaultReplicaCount = clusterService.getClusterSettings().get(Metadata.DEFAULT_REPLICA_COUNT_SETTING);

Settings.Builder settingsForClosedIndices = Settings.builder();
Settings.Builder settingsForOpenIndices = Settings.builder();
Expand Down Expand Up @@ -248,7 +249,10 @@
}

if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(openSettings)) {
final int updatedNumberOfReplicas = IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(openSettings);
final int updatedNumberOfReplicas = openSettings.getAsInt(
IndexMetadata.SETTING_NUMBER_OF_REPLICAS,
defaultReplicaCount
);
if (preserveExisting == false) {
for (Index index : request.indices()) {
if (index.getName().charAt(0) != '.') {
Expand Down Expand Up @@ -329,15 +333,13 @@
/*
* The setting index.number_of_replicas is special; we require that this setting has a value in the index. When
* creating the index, we ensure this by explicitly providing a value for the setting to the default (one) if
* there is a not value provided on the source of the index creation. A user can update this setting though,
* including updating it to null, indicating that they want to use the default value. In this case, we again
* have to provide an explicit value for the setting to the default (one).
* there is no value provided on the source of the index creation or "cluster.default_number_of_replicas" is
* not set. A user can update this setting though, including updating it to null, indicating that they want to
* use the value configured by cluster settings or a default value 1. In this case, we again have to provide
* an explicit value for the setting.
*/
if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettings) == false) {
indexSettings.put(
IndexMetadata.SETTING_NUMBER_OF_REPLICAS,
IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(Settings.EMPTY)
);
indexSettings.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, defaultReplicaCount);

Check warning on line 342 in server/src/main/java/org/opensearch/cluster/metadata/MetadataUpdateSettingsService.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/MetadataUpdateSettingsService.java#L342

Added line #L342 was not covered by tests
}
Settings finalSettings = indexSettings.build();
indexScopedSettings.validate(
Expand Down
Loading