From 4abdb59221c407685ccf260deaaf9ef1b8005999 Mon Sep 17 00:00:00 2001 From: elasticsearchmachine <58790826+elasticsearchmachine@users.noreply.github.com> Date: Thu, 5 Aug 2021 10:44:17 -0400 Subject: [PATCH] Fix null value usage for index.number_of_routing_shards setting. (#76174) Backport #76069 to 7.14 branch. Ensure that the same number of routing shards is used for new indices when `index.number_of_routing_shards` is not specified and `null` as value is specified. Without this change if no index.number_of_routing_shards has been specified then logic inside the MetadataCreateIndexService#getIndexNumberOfRoutingShards(...) kicks and calculates a default based on number of primary shards use that as the true number of routing shards. If a value is specified (including null) then the value the true number of routing shards is determined from the IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING setting and in the case of when null is specified then the default value is gathered from the IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING setting. Closes #75139 Co-authored-by: Martijn van Groningen --- .../metadata/MetadataCreateIndexService.java | 4 +++- .../MetadataCreateIndexServiceTests.java | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java index 32ec46b0badc6..7375b03306da2 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java @@ -857,7 +857,9 @@ static int getIndexNumberOfRoutingShards(Settings indexSettings, @Nullable Index // in this case we either have no index to recover from or // we have a source index with 1 shard and without an explicit split factor // or one that is valid in that case we can split into whatever and auto-generate a new factor. - if (IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.exists(indexSettings)) { + // (Don't use IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(indexSettings) here, otherwise + // we get the default value when `null` has been provided as value) + if (indexSettings.get(IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey()) != null) { routingNumShards = IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(indexSettings); } else { routingNumShards = calculateNumRoutingShards(numTargetShards, indexVersionCreated); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java index eae70ed1b81cd..269ae0f6d7754 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java @@ -935,6 +935,29 @@ public void testGetIndexNumberOfRoutingShardsWhenExplicitlyConfigured() { assertThat(targetRoutingNumberOfShards, is(9)); } + public void testGetIndexNumberOfRoutingShardsNullVsNotDefined() { + int numberOfPrimaryShards = randomIntBetween(1, 16); + Settings indexSettings = settings(Version.CURRENT) + .put(INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey(), (String) null) + .put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numberOfPrimaryShards) + .build(); + int targetRoutingNumberOfShardsWithNull = getIndexNumberOfRoutingShards(indexSettings, null); + indexSettings = settings(Version.CURRENT) + .put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numberOfPrimaryShards) + .build(); + int targetRoutingNumberOfShardsWithNotDefined = getIndexNumberOfRoutingShards(indexSettings, null); + assertThat(targetRoutingNumberOfShardsWithNull, is(targetRoutingNumberOfShardsWithNotDefined)); + } + + public void testGetIndexNumberOfRoutingShardsNull() { + Settings indexSettings = settings(Version.CURRENT) + .put(INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey(), (String) null) + .put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 2) + .build(); + int targetRoutingNumberOfShardsWithNull = getIndexNumberOfRoutingShards(indexSettings, null); + assertThat(targetRoutingNumberOfShardsWithNull, is(1024)); + } + public void testGetIndexNumberOfRoutingShardsYieldsSourceNumberOfShards() { Settings indexSettings = Settings.builder() .put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 3)