Skip to content

Commit

Permalink
Use default value when index.number_of_replicas is null (#14812) (#14907
Browse files Browse the repository at this point in the history
)

* Use default value when index.number_of_replicas is null



* Add integration test



* Add changelog



---------


(cherry picked from commit e485856)

Signed-off-by: Liyun Xiu <[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>
  • Loading branch information
1 parent e5adb99 commit c770738
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix create or update alias API doesn't throw exception for unsupported parameters ([#14719](https://github.com/opensearch-project/OpenSearch/pull/14719))
- Refactoring FilterPath.parse by using an iterative approach ([#14200](https://github.com/opensearch-project/OpenSearch/pull/14200))
- Refactoring Grok.validatePatternBank by using an iterative approach ([#14206](https://github.com/opensearch-project/OpenSearch/pull/14206))
- Fix NPE when creating index with index.number_of_replicas set to null ([#14812](https://github.com/opensearch-project/OpenSearch/pull/14812))
- Update help output for _cat ([#14722](https://github.com/opensearch-project/OpenSearch/pull/14722))
- Fix bulk upsert ignores the default_pipeline and final_pipeline when auto-created index matches the index template ([#12891](https://github.com/opensearch-project/OpenSearch/pull/12891))
- Fix NPE in ReplicaShardAllocator ([#14385](https://github.com/opensearch-project/OpenSearch/pull/14385))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,28 @@ public void testIndexNameInResponse() {
assertEquals("Should have index name in response", "foo", response.index());
}

public void testCreateIndexWithNullReplicaCountPickUpClusterReplica() {
int numReplicas = 3;
String indexName = "test-idx-1";
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("cluster.default_number_of_replicas", numReplicas).build())
.get()
);
Settings settings = Settings.builder()
.put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1)
.put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), (String) null)
.build();
assertAcked(client().admin().indices().prepareCreate(indexName).setSettings(settings).get());
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, internalCluster().getClusterManagerName());
for (IndexService indexService : indicesService) {
assertEquals(indexName, indexService.index().getName());
assertEquals(
numReplicas,
(int) indexService.getIndexSettings().getSettings().getAsInt(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, null)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,8 @@ static Settings aggregateIndexSettings(
}
indexSettingsBuilder.put(SETTING_NUMBER_OF_SHARDS, numberOfShards);
}
if (INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettingsBuilder) == false) {
if (INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettingsBuilder) == false
|| indexSettingsBuilder.get(SETTING_NUMBER_OF_REPLICAS) == null) {
indexSettingsBuilder.put(SETTING_NUMBER_OF_REPLICAS, DEFAULT_REPLICA_COUNT_SETTING.get(currentState.metadata().settings()));
}
if (settings.get(SETTING_AUTO_EXPAND_REPLICAS) != null && indexSettingsBuilder.get(SETTING_AUTO_EXPAND_REPLICAS) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2160,6 +2160,33 @@ public void testAsyncDurabilityThrowsExceptionWhenRestrictSettingTrue() {
);
}

public void testAggregateIndexSettingsIndexReplicaIsSetToNull() {
// This checks that aggregateIndexSettings works for the case when the index setting `index.number_of_replicas` is set to null
request = new CreateIndexClusterStateUpdateRequest("create index", "test", "test");
request.settings(Settings.builder().putNull(SETTING_NUMBER_OF_REPLICAS).build());
Integer clusterDefaultReplicaNumber = 5;
Metadata metadata = new Metadata.Builder().persistentSettings(
Settings.builder().put("cluster.default_number_of_replicas", clusterDefaultReplicaNumber).build()
).build();
ClusterState clusterState = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY))
.metadata(metadata)
.build();
Settings settings = Settings.builder().put(CLUSTER_REMOTE_INDEX_RESTRICT_ASYNC_DURABILITY_SETTING.getKey(), true).build();
clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
Settings aggregatedSettings = aggregateIndexSettings(
clusterState,
request,
Settings.EMPTY,
null,
Settings.EMPTY,
IndexScopedSettings.DEFAULT_SCOPED_SETTINGS,
randomShardLimitService(),
Collections.emptySet(),
clusterSettings
);
assertEquals(clusterDefaultReplicaNumber.toString(), aggregatedSettings.get(SETTING_NUMBER_OF_REPLICAS));
}

public void testRequestDurabilityWhenRestrictSettingTrue() {
// This checks that aggregateIndexSettings works for the case when the cluster setting
// cluster.remote_store.index.restrict.async-durability is false or not set, it allows all types of durability modes
Expand Down

0 comments on commit c770738

Please sign in to comment.