[TEST] Fix EvilSystemPropertyTests to be test order independent

This commit is contained in:
Simon Willnauer 2016-09-29 13:25:54 +02:00
parent 33b9e2065b
commit 7e3863d2d8
2 changed files with 20 additions and 16 deletions

View File

@ -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")); final int maxNumShards = Integer.parseInt(System.getProperty("es.index.max_number_of_shards", "1024"));
if (maxNumShards < 1) { if (maxNumShards < 1) {
throw new IllegalArgumentException("es.index.max_number_of_shards must be > 0"); 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 INDEX_SETTING_PREFIX = "index.";
public static final String SETTING_NUMBER_OF_SHARDS = "index.number_of_shards"; public static final String SETTING_NUMBER_OF_SHARDS = "index.number_of_shards";
public static final Setting<Integer> INDEX_NUMBER_OF_SHARDS_SETTING = public static final Setting<Integer> INDEX_NUMBER_OF_SHARDS_SETTING = buildNumberOfShardsSetting();
Setting.intSetting(SETTING_NUMBER_OF_SHARDS, Math.min(5, MAX_NUMBER_OF_SHARDS), 1, MAX_NUMBER_OF_SHARDS,
Property.IndexScope);
public static final String SETTING_NUMBER_OF_REPLICAS = "index.number_of_replicas"; public static final String SETTING_NUMBER_OF_REPLICAS = "index.number_of_replicas";
public static final Setting<Integer> INDEX_NUMBER_OF_REPLICAS_SETTING = public static final Setting<Integer> INDEX_NUMBER_OF_REPLICAS_SETTING =
Setting.intSetting(SETTING_NUMBER_OF_REPLICAS, 1, 0, Property.Dynamic, Property.IndexScope); Setting.intSetting(SETTING_NUMBER_OF_REPLICAS, 1, 0, Property.Dynamic, Property.IndexScope);

View File

@ -16,9 +16,8 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * 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.SuppressForbidden;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
@ -27,13 +26,20 @@ public class EvilSystemPropertyTests extends ESTestCase {
@SuppressForbidden(reason = "manipulates system properties for testing") @SuppressForbidden(reason = "manipulates system properties for testing")
public void testMaxNumShards() { 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); int limit = randomIntBetween(1, 10);
System.setProperty("es.index.max_number_of_shards", Integer.toString(limit)); System.setProperty("es.index.max_number_of_shards", Integer.toString(limit));
try { try {
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () ->
IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING IndexMetaData.buildNumberOfShardsSetting()
.get(Settings.builder().put("index.number_of_shards", 11).build())); .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 { } finally {
System.clearProperty("es.index.max_number_of_shards"); System.clearProperty("es.index.max_number_of_shards");
} }