Skip to content

Commit 57f1cbc

Browse files
chishuikaushalmahi12
authored andcommitted
Use default value when index.number_of_replicas is null (opensearch-project#14812)
* Use default value when index.number_of_replicas is null Signed-off-by: Liyun Xiu <xiliyun@amazon.com> * Add integration test Signed-off-by: Liyun Xiu <xiliyun@amazon.com> * Add changelog Signed-off-by: Liyun Xiu <xiliyun@amazon.com> --------- Signed-off-by: Liyun Xiu <xiliyun@amazon.com> Signed-off-by: Kaushal Kumar <ravi.kaushal97@gmail.com>
1 parent f1235e0 commit 57f1cbc

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
@@ -86,6 +86,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8686
- Fix create or update alias API doesn't throw exception for unsupported parameters ([#14719](https://github.com/opensearch-project/OpenSearch/pull/14719))
8787
- Refactoring FilterPath.parse by using an iterative approach ([#14200](https://github.com/opensearch-project/OpenSearch/pull/14200))
8888
- Refactoring Grok.validatePatternBank by using an iterative approach ([#14206](https://github.com/opensearch-project/OpenSearch/pull/14206))
89+
- Fix NPE when creating index with index.number_of_replicas set to null ([#14812](https://github.com/opensearch-project/OpenSearch/pull/14812))
8990
- Update help output for _cat ([#14722](https://github.com/opensearch-project/OpenSearch/pull/14722))
9091
- 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))
9192
- 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
@@ -406,4 +406,28 @@ public void testIndexNameInResponse() {
406406
assertEquals("Should have index name in response", "foo", response.index());
407407
}
408408

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,8 @@ static Settings aggregateIndexSettings(
946946
if (INDEX_NUMBER_OF_SHARDS_SETTING.exists(indexSettingsBuilder) == false) {
947947
indexSettingsBuilder.put(SETTING_NUMBER_OF_SHARDS, INDEX_NUMBER_OF_SHARDS_SETTING.get(settings));
948948
}
949-
if (INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettingsBuilder) == false) {
949+
if (INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettingsBuilder) == false
950+
|| indexSettingsBuilder.get(SETTING_NUMBER_OF_REPLICAS) == null) {
950951
indexSettingsBuilder.put(SETTING_NUMBER_OF_REPLICAS, DEFAULT_REPLICA_COUNT_SETTING.get(currentState.metadata().settings()));
951952
}
952953
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
@@ -2151,6 +2151,33 @@ public void testAsyncDurabilityThrowsExceptionWhenRestrictSettingTrue() {
21512151
);
21522152
}
21532153

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

0 commit comments

Comments
 (0)