Merge pull request #16270 from javanna/enhancement/auto_create_index_tests
Simplify AutoCreateIndex and add more tests
This commit is contained in:
commit
ca2924eb66
|
@ -23,111 +23,100 @@ import org.elasticsearch.cluster.ClusterState;
|
|||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
|
||||
import org.elasticsearch.common.Booleans;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.collect.Tuple;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.regex.Regex;
|
||||
import org.elasticsearch.common.settings.Setting;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.mapper.MapperService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Encapsulates the logic of whether a new index should be automatically created when
|
||||
* a write operation is about to happen in a non existing index.
|
||||
*/
|
||||
public final class AutoCreateIndex {
|
||||
|
||||
private final boolean needToCheck;
|
||||
private final boolean globallyDisabled;
|
||||
private final boolean dynamicMappingDisabled;
|
||||
private final String[] matches;
|
||||
private final String[] matches2;
|
||||
private final IndexNameExpressionResolver resolver;
|
||||
public static final Setting<AutoCreate> AUTO_CREATE_INDEX_SETTING = new Setting<>("action.auto_create_index", "true", AutoCreate::new, false, Setting.Scope.CLUSTER);
|
||||
|
||||
private final boolean dynamicMappingDisabled;
|
||||
private final IndexNameExpressionResolver resolver;
|
||||
private final AutoCreate autoCreate;
|
||||
|
||||
@Inject
|
||||
public AutoCreateIndex(Settings settings, IndexNameExpressionResolver resolver) {
|
||||
this.resolver = resolver;
|
||||
dynamicMappingDisabled = !MapperService.INDEX_MAPPER_DYNAMIC_SETTING.get(settings);
|
||||
final AutoCreate autoCreate = AUTO_CREATE_INDEX_SETTING.get(settings);
|
||||
if (autoCreate.autoCreateIndex) {
|
||||
needToCheck = true;
|
||||
globallyDisabled = false;
|
||||
matches = autoCreate.indices;
|
||||
if (matches != null) {
|
||||
matches2 = new String[matches.length];
|
||||
for (int i = 0; i < matches.length; i++) {
|
||||
matches2[i] = matches[i].substring(1);
|
||||
}
|
||||
} else {
|
||||
matches2 = null;
|
||||
}
|
||||
} else {
|
||||
needToCheck = false;
|
||||
globallyDisabled = true;
|
||||
matches = null;
|
||||
matches2 = null;
|
||||
}
|
||||
this.autoCreate = AUTO_CREATE_INDEX_SETTING.get(settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do we really need to check if an index should be auto created?
|
||||
*/
|
||||
public boolean needToCheck() {
|
||||
return this.needToCheck;
|
||||
return this.autoCreate.autoCreateIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the index be auto created?
|
||||
*/
|
||||
public boolean shouldAutoCreate(String index, ClusterState state) {
|
||||
if (!needToCheck) {
|
||||
if (autoCreate.autoCreateIndex == false) {
|
||||
return false;
|
||||
}
|
||||
boolean exists = resolver.hasIndexOrAlias(index, state);
|
||||
if (exists) {
|
||||
if (dynamicMappingDisabled) {
|
||||
return false;
|
||||
}
|
||||
if (globallyDisabled || dynamicMappingDisabled) {
|
||||
if (resolver.hasIndexOrAlias(index, state)) {
|
||||
return false;
|
||||
}
|
||||
// matches not set, default value of "true"
|
||||
if (matches == null) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < matches.length; i++) {
|
||||
char c = matches[i].charAt(0);
|
||||
if (c == '-') {
|
||||
if (Regex.simpleMatch(matches2[i], index)) {
|
||||
return false;
|
||||
}
|
||||
} else if (c == '+') {
|
||||
if (Regex.simpleMatch(matches2[i], index)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (Regex.simpleMatch(matches[i], index)) {
|
||||
if (autoCreate.expressions.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
for (Tuple<String, Boolean> expression : autoCreate.expressions) {
|
||||
String indexExpression = expression.v1();
|
||||
boolean include = expression.v2();
|
||||
if (Regex.simpleMatch(indexExpression, index)) {
|
||||
return include;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static class AutoCreate {
|
||||
private static class AutoCreate {
|
||||
private final boolean autoCreateIndex;
|
||||
private final String[] indices;
|
||||
private final List<Tuple<String, Boolean>> expressions;
|
||||
|
||||
public AutoCreate(String value) {
|
||||
private AutoCreate(String value) {
|
||||
boolean autoCreateIndex;
|
||||
String[] indices = null;
|
||||
List<Tuple<String, Boolean>> expressions = new ArrayList<>();
|
||||
try {
|
||||
autoCreateIndex = Booleans.parseBooleanExact(value);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
try {
|
||||
indices = Strings.commaDelimitedListToStringArray(value);
|
||||
for (String string : indices) {
|
||||
if (string == null || string.length() == 0) {
|
||||
throw new IllegalArgumentException("Can't parse [" + value + "] for setting [action.auto_create_index] must be either [true, false, or a comma seperated list of index patterns]");
|
||||
String[] patterns = Strings.commaDelimitedListToStringArray(value);
|
||||
for (String pattern : patterns) {
|
||||
if (pattern == null || pattern.length() == 0) {
|
||||
throw new IllegalArgumentException("Can't parse [" + value + "] for setting [action.auto_create_index] must be either [true, false, or a comma separated list of index patterns]");
|
||||
}
|
||||
Tuple<String, Boolean> expression;
|
||||
if (pattern.startsWith("-")) {
|
||||
if (pattern.length() == 1) {
|
||||
throw new IllegalArgumentException("Can't parse [" + value + "] for setting [action.auto_create_index] must contain an index name after [-]");
|
||||
}
|
||||
expression = new Tuple<>(pattern.substring(1), false);
|
||||
} else if(pattern.startsWith("+")) {
|
||||
if (pattern.length() == 1) {
|
||||
throw new IllegalArgumentException("Can't parse [" + value + "] for setting [action.auto_create_index] must contain an index name after [+]");
|
||||
}
|
||||
expression = new Tuple<>(pattern.substring(1), true);
|
||||
} else {
|
||||
expression = new Tuple<>(pattern, true);
|
||||
}
|
||||
expressions.add(expression);
|
||||
}
|
||||
autoCreateIndex = true;
|
||||
} catch (IllegalArgumentException ex1) {
|
||||
|
@ -135,7 +124,7 @@ public final class AutoCreateIndex {
|
|||
throw ex1;
|
||||
}
|
||||
}
|
||||
this.indices = indices;
|
||||
this.expressions = expressions;
|
||||
this.autoCreateIndex = autoCreateIndex;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,57 +20,124 @@
|
|||
package org.elasticsearch.action.support;
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
|
||||
import org.elasticsearch.cluster.metadata.MetaData;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.mapper.MapperService;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
|
||||
public class AutoCreateIndexTests extends ESTestCase {
|
||||
|
||||
public void testBasic() {
|
||||
{
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(Settings.EMPTY, new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
ClusterState cs = buildClusterState("foo");
|
||||
assertFalse("already exists", autoCreateIndex.shouldAutoCreate("foo", cs));
|
||||
assertTrue(autoCreateIndex.shouldAutoCreate("foobar", cs));
|
||||
}
|
||||
{
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(Settings.builder().put("action.auto_create_index", "-foo,+b*").build(), new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
ClusterState cs = buildClusterState("foobar", "baz");
|
||||
assertFalse(autoCreateIndex.shouldAutoCreate("foo", cs));
|
||||
assertTrue(autoCreateIndex.shouldAutoCreate("bar", cs));
|
||||
assertFalse("already exists", autoCreateIndex.shouldAutoCreate("baz", cs));
|
||||
}
|
||||
|
||||
{
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(Settings.builder().put("action.auto_create_index", "-foo,+b*").put("index.mapper.dynamic", false).build(), new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
ClusterState cs = buildClusterState("foobar", "baz");
|
||||
assertFalse(autoCreateIndex.shouldAutoCreate("foo", cs));
|
||||
assertFalse(autoCreateIndex.shouldAutoCreate("bar", cs));
|
||||
assertFalse("already exists", autoCreateIndex.shouldAutoCreate("baz", cs));
|
||||
}
|
||||
|
||||
{
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(Settings.builder().put("action.auto_create_index", false).put("index.mapper.dynamic", false).build(), new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
ClusterState cs = buildClusterState("foobar", "baz");
|
||||
assertFalse(autoCreateIndex.shouldAutoCreate("foo", cs));
|
||||
assertFalse(autoCreateIndex.shouldAutoCreate("bar", cs));
|
||||
assertFalse("already exists", autoCreateIndex.shouldAutoCreate("baz", cs));
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseFailed() {
|
||||
try {
|
||||
new AutoCreateIndex(Settings.builder().put("action.auto_create_index", ",,,").build(), new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
fail("initialization should have failed");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
assertEquals("Can't parse [,,,] for setting [action.auto_create_index] must be either [true, false, or a comma seperated list of index patterns]", ex.getMessage());
|
||||
assertEquals("Can't parse [,,,] for setting [action.auto_create_index] must be either [true, false, or a comma separated list of index patterns]", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseFailedMissingIndex() {
|
||||
String prefix = randomFrom("+", "-");
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), prefix).build();
|
||||
try {
|
||||
new AutoCreateIndex(settings, new IndexNameExpressionResolver(settings));
|
||||
fail("initialization should have failed");
|
||||
} catch(IllegalArgumentException ex) {
|
||||
assertEquals("Can't parse [" + prefix + "] for setting [action.auto_create_index] must contain an index name after [" + prefix + "]", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public ClusterState buildClusterState(String... indices) {
|
||||
public void testAutoCreationDisabled() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), false).build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate(randomAsciiOfLengthBetween(1, 10), buildClusterState()), equalTo(false));
|
||||
}
|
||||
|
||||
public void testAutoCreationEnabled() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), true).build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate(randomAsciiOfLengthBetween(1, 10), buildClusterState()), equalTo(true));
|
||||
}
|
||||
|
||||
public void testDefaultAutoCreation() {
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(Settings.EMPTY, new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate(randomAsciiOfLengthBetween(1, 10), buildClusterState()), equalTo(true));
|
||||
}
|
||||
|
||||
public void testExistingIndex() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), randomFrom(true, false, randomAsciiOfLengthBetween(7, 10))).build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate(randomFrom("index1", "index2", "index3"), buildClusterState("index1", "index2", "index3")), equalTo(false));
|
||||
}
|
||||
|
||||
public void testDynamicMappingDisabled() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), randomFrom(true, randomAsciiOfLengthBetween(1, 10)))
|
||||
.put(MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey(), false).build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(Settings.EMPTY));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate(randomAsciiOfLengthBetween(1, 10), buildClusterState()), equalTo(false));
|
||||
}
|
||||
|
||||
public void testAutoCreationPatternEnabled() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), randomFrom("+index*", "index*")).build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(settings));
|
||||
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metaData(MetaData.builder()).build();
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("index" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(true));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("does_not_match" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
}
|
||||
|
||||
public void testAutoCreationPatternDisabled() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), "-index*").build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(settings));
|
||||
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metaData(MetaData.builder()).build();
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("index" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
//default is false when patterns are specified
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("does_not_match" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
}
|
||||
|
||||
public void testAutoCreationMultiplePatternsWithWildcards() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), randomFrom("+test*,-index*", "test*,-index*")).build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(settings));
|
||||
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metaData(MetaData.builder()).build();
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("index" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("test" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(true));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("does_not_match" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
}
|
||||
|
||||
public void testAutoCreationMultiplePatternsNoWildcards() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), "+test1,-index1").build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(settings));
|
||||
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metaData(MetaData.builder()).build();
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("test1", clusterState), equalTo(true));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("index" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("test" + randomAsciiOfLengthBetween(2, 5), clusterState), equalTo(false));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("does_not_match" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
}
|
||||
|
||||
public void testAutoCreationMultipleIndexNames() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), "test1,test2").build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(settings));
|
||||
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metaData(MetaData.builder()).build();
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("test1", clusterState), equalTo(true));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("test2", clusterState), equalTo(true));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("does_not_match" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
}
|
||||
|
||||
public void testAutoCreationConflictingPatternsFirstWins() {
|
||||
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), "+test1,-test1,-test2,+test2").build();
|
||||
AutoCreateIndex autoCreateIndex = new AutoCreateIndex(settings, new IndexNameExpressionResolver(settings));
|
||||
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metaData(MetaData.builder()).build();
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("test1", clusterState), equalTo(true));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("test2", clusterState), equalTo(false));
|
||||
assertThat(autoCreateIndex.shouldAutoCreate("does_not_match" + randomAsciiOfLengthBetween(1, 5), clusterState), equalTo(false));
|
||||
}
|
||||
|
||||
private static ClusterState buildClusterState(String... indices) {
|
||||
MetaData.Builder metaData = MetaData.builder();
|
||||
for (String index : indices) {
|
||||
metaData.put(IndexMetaData.builder(index).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1));
|
||||
|
|
Loading…
Reference in New Issue