IndexNameExpressionResolver::dataStreamNames should support exclusions

This commit is contained in:
Dan Hermann 2020-07-08 07:35:52 -05:00 committed by GitHub
parent 9268b25789
commit 90c8d3fc9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 18 deletions

View File

@ -58,14 +58,12 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static java.util.Collections.unmodifiableList;
public class IndexNameExpressionResolver {
private final DateMathExpressionResolver dateMathExpressionResolver = new DateMathExpressionResolver();
private final List<ExpressionResolver> expressionResolvers = unmodifiableList(Arrays.asList(
dateMathExpressionResolver,
new WildcardExpressionResolver()));
private final WildcardExpressionResolver wildcardExpressionResolver = new WildcardExpressionResolver();
private final List<ExpressionResolver> expressionResolvers =
org.elasticsearch.common.collect.List.of(dateMathExpressionResolver, wildcardExpressionResolver);
/**
* Same as {@link #concreteIndexNames(ClusterState, IndicesOptions, String...)}, but the index expressions and options
@ -114,10 +112,16 @@ public class IndexNameExpressionResolver {
}
public List<String> dataStreamNames(ClusterState state, IndicesOptions options, String... indexExpressions) {
Context context = new Context(state, options, false, false, true);
return Arrays.stream(indexExpressions)
.flatMap(expression -> WildcardExpressionResolver.matches(context, state.metadata(), expression).values().stream())
.filter(i -> i.getType() == IndexAbstraction.Type.DATA_STREAM)
Context context = new Context(state, options, false, false, true, true);
if (indexExpressions == null || indexExpressions.length == 0) {
indexExpressions = new String[]{"*"};
}
List<String> dataStreams = wildcardExpressionResolver.resolve(context, Arrays.asList(indexExpressions));
return ((dataStreams == null) ? org.elasticsearch.common.collect.List.<String>of() : dataStreams).stream()
.map(x -> state.metadata().getIndicesLookup().get(x))
.filter(Objects::nonNull)
.filter(ia -> ia.getType() == IndexAbstraction.Type.DATA_STREAM)
.map(IndexAbstraction::getName)
.collect(Collectors.toList());
}
@ -157,7 +161,7 @@ public class IndexNameExpressionResolver {
* indices options in the context don't allow such a case.
*/
public Index[] concreteIndices(ClusterState state, IndicesRequest request, long startTime) {
Context context = new Context(state, request.indicesOptions(), startTime, false, false, request.includeDataStreams());
Context context = new Context(state, request.indicesOptions(), startTime, false, false, request.includeDataStreams(), false);
return concreteIndices(context, request.indices());
}
@ -647,6 +651,7 @@ public class IndexNameExpressionResolver {
private final boolean preserveAliases;
private final boolean resolveToWriteIndex;
private final boolean includeDataStreams;
private final boolean preserveDataStreams;
Context(ClusterState state, IndicesOptions options) {
this(state, options, System.currentTimeMillis());
@ -654,21 +659,27 @@ public class IndexNameExpressionResolver {
Context(ClusterState state, IndicesOptions options, boolean preserveAliases, boolean resolveToWriteIndex,
boolean includeDataStreams) {
this(state, options, System.currentTimeMillis(), preserveAliases, resolveToWriteIndex, includeDataStreams);
this(state, options, System.currentTimeMillis(), preserveAliases, resolveToWriteIndex, includeDataStreams, false);
}
Context(ClusterState state, IndicesOptions options, boolean preserveAliases, boolean resolveToWriteIndex,
boolean includeDataStreams, boolean preserveDataStreams) {
this(state, options, System.currentTimeMillis(), preserveAliases, resolveToWriteIndex, includeDataStreams, preserveDataStreams);
}
Context(ClusterState state, IndicesOptions options, long startTime) {
this(state, options, startTime, false, false, false);
this(state, options, startTime, false, false, false, false);
}
protected Context(ClusterState state, IndicesOptions options, long startTime,
boolean preserveAliases, boolean resolveToWriteIndex, boolean includeDataStreams) {
protected Context(ClusterState state, IndicesOptions options, long startTime, boolean preserveAliases, boolean resolveToWriteIndex,
boolean includeDataStreams, boolean preserveDataStreams) {
this.state = state;
this.options = options;
this.startTime = startTime;
this.preserveAliases = preserveAliases;
this.resolveToWriteIndex = resolveToWriteIndex;
this.includeDataStreams = includeDataStreams;
this.preserveDataStreams = preserveDataStreams;
}
public ClusterState getState() {
@ -703,6 +714,10 @@ public class IndexNameExpressionResolver {
public boolean includeDataStreams() {
return includeDataStreams;
}
public boolean isPreserveDataStreams() {
return preserveDataStreams;
}
}
private interface ExpressionResolver {
@ -942,7 +957,9 @@ public class IndexNameExpressionResolver {
expand.add(meta.getIndex().getName());
}
}
if (context.isPreserveDataStreams() && indexAbstraction.getType() == IndexAbstraction.Type.DATA_STREAM) {
expand.add(indexAbstraction.getName());
}
}
}
}

View File

@ -1960,14 +1960,14 @@ public class IndexNameExpressionResolverTests extends ESTestCase {
Arrays.sort(result, Comparator.comparing(Index::getName));
assertThat(result.length, equalTo(2));
assertThat(result[0].getName(), equalTo(DataStream.getDefaultBackingIndexName(dataStream1, 2)));
assertThat(result[1].getName(), equalTo(DataStream.getDefaultBackingIndexName(dataStream2, 2)));;
assertThat(result[1].getName(), equalTo(DataStream.getDefaultBackingIndexName(dataStream2, 2)));
}
{
Index[] result = indexNameExpressionResolver.concreteIndices(state, indicesOptions, true, "*");
Arrays.sort(result, Comparator.comparing(Index::getName));
assertThat(result.length, equalTo(2));
assertThat(result[0].getName(), equalTo(DataStream.getDefaultBackingIndexName(dataStream1, 2)));
assertThat(result[1].getName(), equalTo(DataStream.getDefaultBackingIndexName(dataStream2, 2)));;
assertThat(result[1].getName(), equalTo(DataStream.getDefaultBackingIndexName(dataStream2, 2)));
}
}
@ -2027,6 +2027,9 @@ public class IndexNameExpressionResolverTests extends ESTestCase {
List<String> names = indexNameExpressionResolver.dataStreamNames(state, IndicesOptions.lenientExpand(), "log*");
assertEquals(Collections.singletonList(dataStream1), names);
names = indexNameExpressionResolver.dataStreamNames(state, IndicesOptions.lenientExpand(), dataStream1);
assertEquals(Collections.singletonList(dataStream1), names);
names = indexNameExpressionResolver.dataStreamNames(state, IndicesOptions.lenientExpand(), "other*");
assertEquals(Collections.singletonList(dataStream2), names);
@ -2038,5 +2041,11 @@ public class IndexNameExpressionResolverTests extends ESTestCase {
names = indexNameExpressionResolver.dataStreamNames(state, IndicesOptions.lenientExpand(), index3.getIndex().getName());
assertThat(names, empty());
names = indexNameExpressionResolver.dataStreamNames(state, IndicesOptions.lenientExpand(), "*", "-logs-foobar");
assertThat(names, containsInAnyOrder(dataStream2));
names = indexNameExpressionResolver.dataStreamNames(state, IndicesOptions.lenientExpand(), "*", "-*");
assertThat(names, empty());
}
}

View File

@ -132,7 +132,7 @@ public class GenerateSnapshotNameStep extends ClusterStateActionStep {
}
public ResolverContext(long startTime) {
super(null, null, startTime, false, false, false);
super(null, null, startTime, false, false, false, false);
}
@Override