diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java index 353616c5a94..c5ccd3bc6ff 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java @@ -157,25 +157,23 @@ public class IndexMetaData implements Diffable, FromXContentBuild } } - static { + static Setting buildNumberOfShardsSetting() { + /* This is a safety limit that should only be exceeded in very rare and special cases. The assumption is that + * 99% of the users have less than 1024 shards per index. We also make it a hard check that requires restart of nodes + * if a cluster should allow to create more than 1024 shards per index. NOTE: this does not limit the number of shards per cluster. + * this also prevents creating stuff like a new index with millions of shards by accident which essentially kills the entire cluster + * with OOM on the spot.*/ final int maxNumShards = Integer.parseInt(System.getProperty("es.index.max_number_of_shards", "1024")); if (maxNumShards < 1) { throw new IllegalArgumentException("es.index.max_number_of_shards must be > 0"); } - MAX_NUMBER_OF_SHARDS = maxNumShards; + return Setting.intSetting(SETTING_NUMBER_OF_SHARDS, Math.min(5, maxNumShards), 1, maxNumShards, + Property.IndexScope); } - /* This is a safety limit that should only be exceeded in very rare and special cases. The assumption is that - * 99% of the users have less than 1024 shards per index. We also make it a hard check that requires restart of nodes - * if a cluster should allow to create more than 1024 shards per index. NOTE: this does not limit the number of shards per cluster. - * this also prevents creating stuff like a new index with millions of shards by accident which essentially kills the entire cluster - * with OOM on the spot.*/ - private static final int MAX_NUMBER_OF_SHARDS; public static final String INDEX_SETTING_PREFIX = "index."; public static final String SETTING_NUMBER_OF_SHARDS = "index.number_of_shards"; - public static final Setting INDEX_NUMBER_OF_SHARDS_SETTING = - Setting.intSetting(SETTING_NUMBER_OF_SHARDS, Math.min(5, MAX_NUMBER_OF_SHARDS), 1, MAX_NUMBER_OF_SHARDS, - Property.IndexScope); + public static final Setting INDEX_NUMBER_OF_SHARDS_SETTING = buildNumberOfShardsSetting(); public static final String SETTING_NUMBER_OF_REPLICAS = "index.number_of_replicas"; public static final Setting INDEX_NUMBER_OF_REPLICAS_SETTING = Setting.intSetting(SETTING_NUMBER_OF_REPLICAS, 1, 0, Property.Dynamic, Property.IndexScope); diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSystemPropertyTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/cluster/metadata/EvilSystemPropertyTests.java similarity index 64% rename from qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSystemPropertyTests.java rename to qa/evil-tests/src/test/java/org/elasticsearch/cluster/metadata/EvilSystemPropertyTests.java index 878803c007c..5e44fdbefad 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSystemPropertyTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/cluster/metadata/EvilSystemPropertyTests.java @@ -16,9 +16,8 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.bootstrap; +package org.elasticsearch.cluster.metadata; -import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESTestCase; @@ -27,13 +26,20 @@ public class EvilSystemPropertyTests extends ESTestCase { @SuppressForbidden(reason = "manipulates system properties for testing") public void testMaxNumShards() { + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> + IndexMetaData.buildNumberOfShardsSetting() + .get(Settings.builder().put("index.number_of_shards", 1025).build())); + assertEquals("Failed to parse value [1025] for setting [index.number_of_shards] must be <= 1024", exception.getMessage()); + + Integer numShards = IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(Settings.builder().put("index.number_of_shards", 100).build()); + assertEquals(100, numShards.intValue()); int limit = randomIntBetween(1, 10); System.setProperty("es.index.max_number_of_shards", Integer.toString(limit)); try { - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> - IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> + IndexMetaData.buildNumberOfShardsSetting() .get(Settings.builder().put("index.number_of_shards", 11).build())); - assertEquals("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit, exception.getMessage()); + assertEquals("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit, e.getMessage()); } finally { System.clearProperty("es.index.max_number_of_shards"); }