Skip to content

Commit b5d6e0d

Browse files
Fix wrong default value when setting index.number_of_routing_shards to null on index creation (#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]> (cherry picked from commit a853b75) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 0053014 commit b5d6e0d

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
@@ -78,6 +78,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
7878
- Fix warnings from SLF4J on startup when repository-s3 is installed ([#16194](https://github.com/opensearch-project/OpenSearch/pull/16194))
7979
- Fix protobuf-java leak through client library dependencies ([#16254](https://github.com/opensearch-project/OpenSearch/pull/16254))
8080
- Fix multi-search with template doesn't return status code ([#16265](https://github.com/opensearch-project/OpenSearch/pull/16265))
81+
- 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))
8182

8283
### Security
8384

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
@@ -1232,7 +1232,7 @@ static int getIndexNumberOfRoutingShards(Settings indexSettings, @Nullable Index
12321232
// in this case we either have no index to recover from or
12331233
// we have a source index with 1 shard and without an explicit split factor
12341234
// or one that is valid in that case we can split into whatever and auto-generate a new factor.
1235-
if (IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.exists(indexSettings)) {
1235+
if (indexSettings.get(IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey()) != null) {
12361236
routingNumShards = IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(indexSettings);
12371237
} else {
12381238
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
@@ -1866,6 +1866,24 @@ public void testGetIndexNumberOfRoutingShardsYieldsSourceNumberOfShards() {
18661866
assertThat(targetRoutingNumberOfShards, is(6));
18671867
}
18681868

1869+
public void testGetIndexNumberOfRoutingShardsWhenExplicitlySetToNull() {
1870+
String nullValue = null;
1871+
Settings indexSettings = Settings.builder()
1872+
.put("index.version.created", Version.CURRENT)
1873+
.put(INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey(), nullValue)
1874+
.build();
1875+
int targetRoutingNumberOfShards = getIndexNumberOfRoutingShards(indexSettings, null);
1876+
assertThat(targetRoutingNumberOfShards, is(1024));
1877+
1878+
indexSettings = Settings.builder()
1879+
.put("index.version.created", Version.CURRENT)
1880+
.put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 3)
1881+
.put(INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey(), nullValue)
1882+
.build();
1883+
targetRoutingNumberOfShards = getIndexNumberOfRoutingShards(indexSettings, null);
1884+
assertThat(targetRoutingNumberOfShards, is(768));
1885+
}
1886+
18691887
public void testSoftDeletesDisabledIsRejected() {
18701888
final IllegalArgumentException error = expectThrows(IllegalArgumentException.class, () -> {
18711889
request = new CreateIndexClusterStateUpdateRequest("create index", "test", "test");

0 commit comments

Comments
 (0)