Skip to content

Commit c770738

Browse files
Use default value when index.number_of_replicas is null (#14812) (#14907)
* 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>
1 parent e5adb99 commit c770738

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8181
- Fix create or update alias API doesn't throw exception for unsupported parameters ([#14719](https://github.com/opensearch-project/OpenSearch/pull/14719))
8282
- Refactoring FilterPath.parse by using an iterative approach ([#14200](https://github.com/opensearch-project/OpenSearch/pull/14200))
8383
- Refactoring Grok.validatePatternBank by using an iterative approach ([#14206](https://github.com/opensearch-project/OpenSearch/pull/14206))
84+
- Fix NPE when creating index with index.number_of_replicas set to null ([#14812](https://github.com/opensearch-project/OpenSearch/pull/14812))
8485
- Update help output for _cat ([#14722](https://github.com/opensearch-project/OpenSearch/pull/14722))
8586
- 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))
8687
- Fix NPE in ReplicaShardAllocator ([#14385](https://github.com/opensearch-project/OpenSearch/pull/14385))

server/src/internalClusterTest/java/org/opensearch/action/admin/indices/create/CreateIndexIT.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,28 @@ public void testIndexNameInResponse() {
405405
assertEquals("Should have index name in response", "foo", response.index());
406406
}
407407

408+
public void testCreateIndexWithNullReplicaCountPickUpClusterReplica() {
409+
int numReplicas = 3;
410+
String indexName = "test-idx-1";
411+
assertAcked(
412+
client().admin()
413+
.cluster()
414+
.prepareUpdateSettings()
415+
.setPersistentSettings(Settings.builder().put("cluster.default_number_of_replicas", numReplicas).build())
416+
.get()
417+
);
418+
Settings settings = Settings.builder()
419+
.put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1)
420+
.put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), (String) null)
421+
.build();
422+
assertAcked(client().admin().indices().prepareCreate(indexName).setSettings(settings).get());
423+
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, internalCluster().getClusterManagerName());
424+
for (IndexService indexService : indicesService) {
425+
assertEquals(indexName, indexService.index().getName());
426+
assertEquals(
427+
numReplicas,
428+
(int) indexService.getIndexSettings().getSettings().getAsInt(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, null)
429+
);
430+
}
431+
}
408432
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ static Settings aggregateIndexSettings(
952952
}
953953
indexSettingsBuilder.put(SETTING_NUMBER_OF_SHARDS, numberOfShards);
954954
}
955-
if (INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettingsBuilder) == false) {
955+
if (INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettingsBuilder) == false
956+
|| indexSettingsBuilder.get(SETTING_NUMBER_OF_REPLICAS) == null) {
956957
indexSettingsBuilder.put(SETTING_NUMBER_OF_REPLICAS, DEFAULT_REPLICA_COUNT_SETTING.get(currentState.metadata().settings()));
957958
}
958959
if (settings.get(SETTING_AUTO_EXPAND_REPLICAS) != null && indexSettingsBuilder.get(SETTING_AUTO_EXPAND_REPLICAS) == null) {

server/src/test/java/org/opensearch/cluster/metadata/MetadataCreateIndexServiceTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,33 @@ public void testAsyncDurabilityThrowsExceptionWhenRestrictSettingTrue() {
21602160
);
21612161
}
21622162

2163+
public void testAggregateIndexSettingsIndexReplicaIsSetToNull() {
2164+
// This checks that aggregateIndexSettings works for the case when the index setting `index.number_of_replicas` is set to null
2165+
request = new CreateIndexClusterStateUpdateRequest("create index", "test", "test");
2166+
request.settings(Settings.builder().putNull(SETTING_NUMBER_OF_REPLICAS).build());
2167+
Integer clusterDefaultReplicaNumber = 5;
2168+
Metadata metadata = new Metadata.Builder().persistentSettings(
2169+
Settings.builder().put("cluster.default_number_of_replicas", clusterDefaultReplicaNumber).build()
2170+
).build();
2171+
ClusterState clusterState = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY))
2172+
.metadata(metadata)
2173+
.build();
2174+
Settings settings = Settings.builder().put(CLUSTER_REMOTE_INDEX_RESTRICT_ASYNC_DURABILITY_SETTING.getKey(), true).build();
2175+
clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
2176+
Settings aggregatedSettings = aggregateIndexSettings(
2177+
clusterState,
2178+
request,
2179+
Settings.EMPTY,
2180+
null,
2181+
Settings.EMPTY,
2182+
IndexScopedSettings.DEFAULT_SCOPED_SETTINGS,
2183+
randomShardLimitService(),
2184+
Collections.emptySet(),
2185+
clusterSettings
2186+
);
2187+
assertEquals(clusterDefaultReplicaNumber.toString(), aggregatedSettings.get(SETTING_NUMBER_OF_REPLICAS));
2188+
}
2189+
21632190
public void testRequestDurabilityWhenRestrictSettingTrue() {
21642191
// This checks that aggregateIndexSettings works for the case when the cluster setting
21652192
// cluster.remote_store.index.restrict.async-durability is false or not set, it allows all types of durability modes

0 commit comments

Comments
 (0)