Skip to content

Commit e208672

Browse files
opensearch-trigger-bot[bot]github-actions[bot]dbwiddis
authored
Update setting API honors cluster's replica setting as default #14810 (#14948) (#16287)
* Update setting API honors cluster's replica setting as default #14810 * Update changelog & add one more test case --------- (cherry picked from commit b3459fd) Signed-off-by: Liyun Xiu <[email protected]> Signed-off-by: Daniel Widdis <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Daniel Widdis <[email protected]>
1 parent ad048b6 commit e208672

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6565
- Fix search_as_you_type not supporting multi-fields ([#15988](https://github.com/opensearch-project/OpenSearch/pull/15988))
6666
- Avoid infinite loop when `flat_object` field contains invalid token ([#15985](https://github.com/opensearch-project/OpenSearch/pull/15985))
6767
- Fix infinite loop in nested agg ([#15931](https://github.com/opensearch-project/OpenSearch/pull/15931))
68+
- Fix update settings with null replica not honoring cluster setting bug ([#14948](https://github.com/opensearch-project/OpenSearch/pull/14948))
6869
- Fix race condition in node-join and node-left ([#15521](https://github.com/opensearch-project/OpenSearch/pull/15521))
6970
- Streaming bulk request hangs ([#16158](https://github.com/opensearch-project/OpenSearch/pull/16158))
7071
- Fix warnings from SLF4J on startup when repository-s3 is installed ([#16194](https://github.com/opensearch-project/OpenSearch/pull/16194))

server/src/internalClusterTest/java/org/opensearch/indices/settings/UpdateSettingsIT.java

+63
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,69 @@ private void runTestDefaultNumberOfReplicasTest(final boolean closeIndex) {
894894
assertThat(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(response.getIndexToSettings().get("test")), equalTo(1));
895895
}
896896

897+
public void testNullReplicaUpdate() {
898+
internalCluster().ensureAtLeastNumDataNodes(2);
899+
900+
// cluster setting
901+
String defaultNumberOfReplica = "3";
902+
assertAcked(
903+
client().admin()
904+
.cluster()
905+
.prepareUpdateSettings()
906+
.setPersistentSettings(Settings.builder().put("cluster.default_number_of_replicas", defaultNumberOfReplica))
907+
.get()
908+
);
909+
// create index with number of replicas will ignore default value
910+
assertAcked(
911+
client().admin()
912+
.indices()
913+
.prepareCreate("test")
914+
.setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, "2"))
915+
);
916+
917+
String numberOfReplicas = client().admin()
918+
.indices()
919+
.prepareGetSettings("test")
920+
.get()
921+
.getSetting("test", IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
922+
assertEquals("2", numberOfReplicas);
923+
// update setting with null replica will use cluster setting of replica number
924+
assertAcked(
925+
client().admin()
926+
.indices()
927+
.prepareUpdateSettings("test")
928+
.setSettings(Settings.builder().putNull(IndexMetadata.SETTING_NUMBER_OF_REPLICAS))
929+
);
930+
931+
numberOfReplicas = client().admin()
932+
.indices()
933+
.prepareGetSettings("test")
934+
.get()
935+
.getSetting("test", IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
936+
assertEquals(defaultNumberOfReplica, numberOfReplicas);
937+
938+
// Clean up cluster setting, then update setting with null replica should pick up default value of 1
939+
assertAcked(
940+
client().admin()
941+
.cluster()
942+
.prepareUpdateSettings()
943+
.setPersistentSettings(Settings.builder().putNull("cluster.default_number_of_replicas"))
944+
);
945+
assertAcked(
946+
client().admin()
947+
.indices()
948+
.prepareUpdateSettings("test")
949+
.setSettings(Settings.builder().putNull(IndexMetadata.SETTING_NUMBER_OF_REPLICAS))
950+
);
951+
952+
numberOfReplicas = client().admin()
953+
.indices()
954+
.prepareGetSettings("test")
955+
.get()
956+
.getSetting("test", IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
957+
assertEquals("1", numberOfReplicas);
958+
}
959+
897960
public void testNoopUpdate() {
898961
internalCluster().ensureAtLeastNumDataNodes(2);
899962
final ClusterService clusterService = internalCluster().getClusterManagerNodeInstance(ClusterService.class);

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public void updateSettings(
140140

141141
validateRefreshIntervalSettings(normalizedSettings, clusterService.getClusterSettings());
142142
validateTranslogDurabilitySettings(normalizedSettings, clusterService.getClusterSettings(), clusterService.getSettings());
143+
final int defaultReplicaCount = clusterService.getClusterSettings().get(Metadata.DEFAULT_REPLICA_COUNT_SETTING);
143144

144145
Settings.Builder settingsForClosedIndices = Settings.builder();
145146
Settings.Builder settingsForOpenIndices = Settings.builder();
@@ -248,7 +249,10 @@ public ClusterState execute(ClusterState currentState) {
248249
}
249250

250251
if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(openSettings)) {
251-
final int updatedNumberOfReplicas = IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(openSettings);
252+
final int updatedNumberOfReplicas = openSettings.getAsInt(
253+
IndexMetadata.SETTING_NUMBER_OF_REPLICAS,
254+
defaultReplicaCount
255+
);
252256
if (preserveExisting == false) {
253257
for (Index index : request.indices()) {
254258
if (index.getName().charAt(0) != '.') {
@@ -329,15 +333,13 @@ public ClusterState execute(ClusterState currentState) {
329333
/*
330334
* The setting index.number_of_replicas is special; we require that this setting has a value in the index. When
331335
* creating the index, we ensure this by explicitly providing a value for the setting to the default (one) if
332-
* there is a not value provided on the source of the index creation. A user can update this setting though,
333-
* including updating it to null, indicating that they want to use the default value. In this case, we again
334-
* have to provide an explicit value for the setting to the default (one).
336+
* there is no value provided on the source of the index creation or "cluster.default_number_of_replicas" is
337+
* not set. A user can update this setting though, including updating it to null, indicating that they want to
338+
* use the value configured by cluster settings or a default value 1. In this case, we again have to provide
339+
* an explicit value for the setting.
335340
*/
336341
if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettings) == false) {
337-
indexSettings.put(
338-
IndexMetadata.SETTING_NUMBER_OF_REPLICAS,
339-
IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(Settings.EMPTY)
340-
);
342+
indexSettings.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, defaultReplicaCount);
341343
}
342344
Settings finalSettings = indexSettings.build();
343345
indexScopedSettings.validate(

0 commit comments

Comments
 (0)