diff --git a/docs/changelog/91461.yaml b/docs/changelog/91461.yaml new file mode 100644 index 0000000000000..507e23e190b6f --- /dev/null +++ b/docs/changelog/91461.yaml @@ -0,0 +1,5 @@ +pr: 91461 +summary: Datastream unavailable exception metadata +area: Infra/Core +type: bug +issues: [] 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 b319aebe18caf..440e2c2c7aad5 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -346,27 +346,18 @@ Index[] concreteIndices(Context context, String... indexExpressions) { } } - boolean excludedDataStreams = false; final Set concreteIndices = Sets.newLinkedHashSetWithExpectedSize(expressions.size()); final SortedMap indicesLookup = context.state.metadata().getIndicesLookup(); for (String expression : expressions) { + if (options.ignoreUnavailable() == false) { + ensureAliasOrIndexExists(context, expression); + } IndexAbstraction indexAbstraction = indicesLookup.get(expression); if (indexAbstraction == null) { - if (options.ignoreUnavailable() == false) { - assert options.expandWildcardExpressions() == false; - throw notFoundException(expression); - } else { - continue; - } + continue; } else if (indexAbstraction.getType() == Type.ALIAS && context.getOptions().ignoreAliases()) { - if (options.ignoreUnavailable() == false) { - assert options.expandWildcardExpressions() == false; - throw aliasesNotSupportedException(expression); - } else { - continue; - } + continue; } else if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) { - excludedDataStreams = true; continue; } @@ -415,33 +406,12 @@ Index[] concreteIndices(Context context, String... indexExpressions) { } if (options.allowNoIndices() == false && concreteIndices.isEmpty()) { - IndexNotFoundException infe = notFoundException(indexExpressions); - if (excludedDataStreams) { - // Allows callers to handle IndexNotFoundException differently based on whether data streams were excluded. - infe.addMetadata(EXCLUDED_DATA_STREAMS_KEY, "true"); - } - throw infe; + throw notFoundException(indexExpressions); } checkSystemIndexAccess(context, concreteIndices); return concreteIndices.toArray(Index.EMPTY_ARRAY); } - private IndexNotFoundException notFoundException(String... indexExpressions) { - IndexNotFoundException infe; - if (indexExpressions.length == 1) { - if (indexExpressions[0].equals(Metadata.ALL)) { - infe = new IndexNotFoundException("no indices exist", indexExpressions[0]); - } else { - infe = new IndexNotFoundException(indexExpressions[0]); - } - infe.setResources("index_expression", indexExpressions[0]); - } else { - infe = new IndexNotFoundException((String) null); - infe.setResources("index_expression", indexExpressions); - } - return infe; - } - private void checkSystemIndexAccess(Context context, Set concreteIndices) { final Metadata metadata = context.getState().metadata(); final Predicate systemIndexAccessPredicate = context.getSystemIndexAccessPredicate().negate(); @@ -488,6 +458,40 @@ private void checkSystemIndexAccess(Context context, Set concreteIndices) } } + private static IndexNotFoundException notFoundException(String... indexExpressions) { + IndexNotFoundException infe; + if (indexExpressions != null && indexExpressions.length == 1) { + if (Metadata.ALL.equals(indexExpressions[0])) { + infe = new IndexNotFoundException("no indices exist", indexExpressions[0]); + } else { + infe = new IndexNotFoundException(indexExpressions[0]); + } + infe.setResources("index_or_alias", indexExpressions[0]); + } else { + infe = new IndexNotFoundException((String) null); + infe.setResources("index_expression", indexExpressions); + } + return infe; + } + + @Nullable + private static void ensureAliasOrIndexExists(Context context, String expression) { + IndexAbstraction indexAbstraction = context.getState().getMetadata().getIndicesLookup().get(expression); + if (indexAbstraction == null) { + throw notFoundException(expression); + } + // treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api) + if (indexAbstraction.getType() == Type.ALIAS && context.getOptions().ignoreAliases()) { + throw aliasesNotSupportedException(expression); + } + if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) { + IndexNotFoundException infe = notFoundException(expression); + // Allows callers to handle IndexNotFoundException differently based on whether data streams were excluded. + infe.addMetadata(EXCLUDED_DATA_STREAMS_KEY, "true"); + throw infe; + } + } + private static boolean shouldTrackConcreteIndex(Context context, IndicesOptions options, Index index) { if (context.systemIndexAccessLevel == SystemIndexAccessLevel.BACKWARDS_COMPATIBLE_ONLY && context.netNewSystemIndexPredicate.test(index.getName())) { @@ -1215,7 +1219,7 @@ private static Collection innerResolve(Context context, List exp matchingOpenClosedNames.forEachOrdered(result::add); } if (emptyWildcardExpansion.get()) { - throw indexNotFoundException(expression); + throw notFoundException(expression); } } else { if (isExclusion) { @@ -1247,7 +1251,7 @@ private static Collection innerResolve(Context context, List exp private static String validateAliasOrIndex(String expression) { if (Strings.isEmpty(expression)) { - throw indexNotFoundException(expression); + throw notFoundException(expression); } // Expressions can not start with an underscore. This is reserved for APIs. If the check gets here, the API // does not exist and the path is interpreted as an expression. If the expression begins with an underscore, @@ -1259,28 +1263,6 @@ private static String validateAliasOrIndex(String expression) { return expression; } - @Nullable - private static void ensureAliasOrIndexExists(Context context, String expression) { - final IndicesOptions options = context.getOptions(); - IndexAbstraction indexAbstraction = context.getState().getMetadata().getIndicesLookup().get(expression); - if (indexAbstraction == null) { - throw indexNotFoundException(expression); - } - // treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api) - if (indexAbstraction.getType() == Type.ALIAS && options.ignoreAliases()) { - throw aliasesNotSupportedException(expression); - } - if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) { - throw indexNotFoundException(expression); - } - } - - private static IndexNotFoundException indexNotFoundException(String expression) { - IndexNotFoundException infe = new IndexNotFoundException(expression); - infe.setResources("index_or_alias", expression); - return infe; - } - private static IndexMetadata.State excludeState(IndicesOptions options) { final IndexMetadata.State excludeState; if (options.expandWildcardsOpen() && options.expandWildcardsClosed()) {