From 90c8d3fc9dbb4dc3ab429471cbb6d0e333b9f778 Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Wed, 8 Jul 2020 07:35:52 -0500 Subject: [PATCH] IndexNameExpressionResolver::dataStreamNames should support exclusions --- .../metadata/IndexNameExpressionResolver.java | 47 +++++++++++++------ .../IndexNameExpressionResolverTests.java | 13 ++++- .../core/ilm/GenerateSnapshotNameStep.java | 2 +- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java index 96130cf931a..55e0e3a7815 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -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 expressionResolvers = unmodifiableList(Arrays.asList( - dateMathExpressionResolver, - new WildcardExpressionResolver())); + private final WildcardExpressionResolver wildcardExpressionResolver = new WildcardExpressionResolver(); + private final List 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 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 dataStreams = wildcardExpressionResolver.resolve(context, Arrays.asList(indexExpressions)); + return ((dataStreams == null) ? org.elasticsearch.common.collect.List.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()); + } } } } diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java index cef0f835cc3..5217e0f3097 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java @@ -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 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()); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStep.java index 4115914ab7b..1f35c8d5664 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStep.java @@ -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