Skip to content

Commit 3e37dfd

Browse files
gaobinlongdk2k
authored andcommitted
Fix wrong default value when setting index.number_of_routing_shards to null on index creation (opensearch-project#16331)
* Fix wrong value when setting index.number_of_routing_shards to null on index creation Signed-off-by: Gao Binlong <[email protected]> * Modify change log Signed-off-by: Gao Binlong <[email protected]> --------- Signed-off-by: Gao Binlong <[email protected]>
1 parent 0bbd19d commit 3e37dfd

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8282
- Fix warnings from SLF4J on startup when repository-s3 is installed ([#16194](https://github.com/opensearch-project/OpenSearch/pull/16194))
8383
- Fix protobuf-java leak through client library dependencies ([#16254](https://github.com/opensearch-project/OpenSearch/pull/16254))
8484
- Fix multi-search with template doesn't return status code ([#16265](https://github.com/opensearch-project/OpenSearch/pull/16265))
85+
- Fix wrong default value when setting `index.number_of_routing_shards` to null on index creation ([#16331](https://github.com/opensearch-project/OpenSearch/pull/16331))
8586

8687
### Security
8788

rest-api-spec/src/main/resources/rest-api-spec/test/indices.create/10_basic.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,33 @@
112112
properties:
113113
"":
114114
type: keyword
115+
116+
---
117+
"Create index with setting index.number_of_routing_shards to null":
118+
- skip:
119+
version: " - 2.99.99"
120+
reason: "fixed in 3.0.0"
121+
- do:
122+
indices.create:
123+
index: test_index
124+
body:
125+
settings:
126+
number_of_routing_shards: null
127+
- do:
128+
cluster.state:
129+
metric: [ metadata ]
130+
index: test_index
131+
- match : { metadata.indices.test_index.routing_num_shards: 1024 }
132+
133+
- do:
134+
indices.create:
135+
index: test_index1
136+
body:
137+
settings:
138+
number_of_routing_shards: null
139+
number_of_shards: 3
140+
- do:
141+
cluster.state:
142+
metric: [ metadata ]
143+
index: test_index1
144+
- match : { metadata.indices.test_index1.routing_num_shards: 768 }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ static int getIndexNumberOfRoutingShards(Settings indexSettings, @Nullable Index
12101210
// in this case we either have no index to recover from or
12111211
// we have a source index with 1 shard and without an explicit split factor
12121212
// or one that is valid in that case we can split into whatever and auto-generate a new factor.
1213-
if (IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.exists(indexSettings)) {
1213+
if (indexSettings.get(IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey()) != null) {
12141214
routingNumShards = IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(indexSettings);
12151215
} else {
12161216
routingNumShards = calculateNumRoutingShards(numTargetShards, indexVersionCreated);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,24 @@ public void testGetIndexNumberOfRoutingShardsYieldsSourceNumberOfShards() {
18571857
assertThat(targetRoutingNumberOfShards, is(6));
18581858
}
18591859

1860+
public void testGetIndexNumberOfRoutingShardsWhenExplicitlySetToNull() {
1861+
String nullValue = null;
1862+
Settings indexSettings = Settings.builder()
1863+
.put("index.version.created", Version.CURRENT)
1864+
.put(INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey(), nullValue)
1865+
.build();
1866+
int targetRoutingNumberOfShards = getIndexNumberOfRoutingShards(indexSettings, null);
1867+
assertThat(targetRoutingNumberOfShards, is(1024));
1868+
1869+
indexSettings = Settings.builder()
1870+
.put("index.version.created", Version.CURRENT)
1871+
.put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 3)
1872+
.put(INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey(), nullValue)
1873+
.build();
1874+
targetRoutingNumberOfShards = getIndexNumberOfRoutingShards(indexSettings, null);
1875+
assertThat(targetRoutingNumberOfShards, is(768));
1876+
}
1877+
18601878
public void testSoftDeletesDisabledIsRejected() {
18611879
final IllegalArgumentException error = expectThrows(IllegalArgumentException.class, () -> {
18621880
request = new CreateIndexClusterStateUpdateRequest("create index", "test", "test");

0 commit comments

Comments
 (0)