Ver código fonte

[TEST] Fix EvilSystemPropertyTests to be test order independent

Simon Willnauer 9 anos atrás
pai
commit
7e3863d2d8

+ 9 - 11
core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java

@@ -157,25 +157,23 @@ public class IndexMetaData implements Diffable<IndexMetaData>, FromXContentBuild
         }
     }
 
-    static {
+    static Setting<Integer> 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<Integer> 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<Integer> INDEX_NUMBER_OF_SHARDS_SETTING = buildNumberOfShardsSetting();
     public static final String SETTING_NUMBER_OF_REPLICAS = "index.number_of_replicas";
     public static final Setting<Integer> INDEX_NUMBER_OF_REPLICAS_SETTING =
         Setting.intSetting(SETTING_NUMBER_OF_REPLICAS, 1, 0, Property.Dynamic, Property.IndexScope);

+ 11 - 5
qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSystemPropertyTests.java → 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");
         }