add tests for AutoExpandReplicas

This commit is contained in:
Simon Willnauer 2016-01-15 22:14:14 +01:00
parent 8a4fca2947
commit 2eecb9c02d
5 changed files with 94 additions and 10 deletions

View File

@ -21,6 +21,11 @@ package org.elasticsearch.cluster.metadata;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.settings.Setting;
/**
* This class acts as a functional wrapper around the <tt>index.auto_expand_replicas</tt> setting.
* This setting or rather it's value is expanded into a min and max value which requires special handling
* based on the number of datanodes in the cluster. This class handels all the parsing and streamlines the access to these values.
*/
final class AutoExpandReplicas {
// the value we recognize in the "max" position to mean all the nodes
private static final String ALL_NODES_VALUE = "all";
@ -32,13 +37,13 @@ final class AutoExpandReplicas {
}
final int dash = value.indexOf('-');
if (-1 == dash) {
throw new IllegalArgumentException("Can't parse auto expand clause from " + dash);
throw new IllegalArgumentException("failed to parse [" + IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS + "] form [" + value + "] at index " + dash);
}
final String sMin = value.substring(0, dash);
try {
min = Integer.parseInt(sMin);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Can't parse auto expand clause from " + dash, e);
throw new IllegalArgumentException("failed to parse [" + IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS + "] form [" + value + "] at index " + dash, e);
}
String sMax = value.substring(dash + 1);
if (sMax.equals(ALL_NODES_VALUE)) {
@ -47,29 +52,30 @@ final class AutoExpandReplicas {
try {
max = Integer.parseInt(sMax);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Can't parse auto expand clause from " + dash, e);
throw new IllegalArgumentException("failed to parse [" + IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS + "] form [" + value + "] at index " + dash, e);
}
}
return new AutoExpandReplicas(min, max, true);
}, true, Setting.Scope.INDEX);
private final int minReplicas;
private final int maxReplicas;
private final boolean enabled;
public AutoExpandReplicas(int minReplicas, int maxReplicas, boolean enabled) {
AutoExpandReplicas(int minReplicas, int maxReplicas, boolean enabled) {
if (minReplicas > maxReplicas) {
throw new IllegalArgumentException("min must be >= max");
throw new IllegalArgumentException("[" + IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS + "] minReplicas must be =< maxReplicas but wasn't " + minReplicas + " > " + maxReplicas);
}
this.minReplicas = minReplicas;
this.maxReplicas = maxReplicas;
this.enabled = enabled;
}
public int getMinReplicas() {
int getMinReplicas() {
return minReplicas;
}
public int getMaxReplicas(int numDataNodes) {
int getMaxReplicas(int numDataNodes) {
return Math.min(maxReplicas, numDataNodes-1);
}
@ -78,7 +84,7 @@ final class AutoExpandReplicas {
return enabled == false ? Boolean.toString(enabled) : minReplicas + "-" + maxReplicas;
}
public boolean isEnabled() {
boolean isEnabled() {
return enabled;
}
}

View File

@ -0,0 +1,75 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.cluster.metadata;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESTestCase;
public class AutoExpandReplicasTests extends ESTestCase {
public void testParseSettings() {
AutoExpandReplicas autoExpandReplicas = AutoExpandReplicas.SETTING.get(Settings.builder().put("index.auto_expand_replicas", "0-5").build());
assertEquals(0, autoExpandReplicas.getMinReplicas());
assertEquals(5, autoExpandReplicas.getMaxReplicas(8));
assertEquals(2, autoExpandReplicas.getMaxReplicas(3));
autoExpandReplicas = AutoExpandReplicas.SETTING.get(Settings.builder().put("index.auto_expand_replicas", "0-all").build());
assertEquals(0, autoExpandReplicas.getMinReplicas());
assertEquals(5, autoExpandReplicas.getMaxReplicas(6));
assertEquals(2, autoExpandReplicas.getMaxReplicas(3));
autoExpandReplicas = AutoExpandReplicas.SETTING.get(Settings.builder().put("index.auto_expand_replicas", "1-all").build());
assertEquals(1, autoExpandReplicas.getMinReplicas());
assertEquals(5, autoExpandReplicas.getMaxReplicas(6));
assertEquals(2, autoExpandReplicas.getMaxReplicas(3));
}
public void testInvalidValues() {
try {
AutoExpandReplicas.SETTING.get(Settings.builder().put("index.auto_expand_replicas", "boom").build());
fail();
} catch (IllegalArgumentException ex) {
assertEquals("failed to parse [index.auto_expand_replicas] form [boom] at index -1", ex.getMessage());
}
try {
AutoExpandReplicas.SETTING.get(Settings.builder().put("index.auto_expand_replicas", "1-boom").build());
fail();
} catch (IllegalArgumentException ex) {
assertEquals("failed to parse [index.auto_expand_replicas] form [1-boom] at index 1", ex.getMessage());
assertEquals("For input string: \"boom\"", ex.getCause().getMessage());
}
try {
AutoExpandReplicas.SETTING.get(Settings.builder().put("index.auto_expand_replicas", "boom-1").build());
fail();
} catch (IllegalArgumentException ex) {
assertEquals("failed to parse [index.auto_expand_replicas] form [boom-1] at index 4", ex.getMessage());
assertEquals("For input string: \"boom\"", ex.getCause().getMessage());
}
try {
AutoExpandReplicas.SETTING.get(Settings.builder().put("index.auto_expand_replicas", "2-1").build());
} catch (IllegalArgumentException ex) {
assertEquals("[index.auto_expand_replicas] minReplicas must be =< maxReplicas but wasn't 2 > 1", ex.getMessage());
}
}
}

View File

@ -34,6 +34,7 @@ import static org.hamcrest.Matchers.lessThan;
* Basic Tests for {@link GeoDistance}
*/
public class GeoDistanceTests extends ESTestCase {
public void testGeoDistanceSerialization() throws IOException {
// make sure that ordinals don't change, because we rely on then in serialization
assertThat(GeoDistance.PLANE.ordinal(), equalTo(0));

View File

@ -40,6 +40,7 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.InternalSettingsPluging;
import org.elasticsearch.test.VersionUtils;
import java.io.IOException;
@ -72,7 +73,7 @@ import static org.hamcrest.Matchers.equalTo;
public class GeoDistanceTests extends ESIntegTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(GroovyPlugin.class);
return pluginList(GroovyPlugin.class, InternalSettingsPluging.class);
}
public void testSimpleDistance() throws Exception {

View File

@ -53,6 +53,7 @@ import org.elasticsearch.search.sort.ScriptSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.InternalSettingsPluging;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.hamcrest.Matchers;
@ -105,7 +106,7 @@ import static org.hamcrest.Matchers.nullValue;
public class SimpleSortTests extends ESIntegTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(GroovyPlugin.class);
return pluginList(GroovyPlugin.class, InternalSettingsPluging.class);
}
@TestLogging("action.search.type:TRACE")