From 19a41282ded243935fd8d532611f9e7ab91e0f60 Mon Sep 17 00:00:00 2001 From: Albert Zaharovits Date: Wed, 9 Nov 2022 15:04:24 +0200 Subject: [PATCH 1/5] handle ignore unavailable for datastream related --- .../cluster/metadata/IndexNameExpressionResolver.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 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 b319aebe18caf..5d3e69be752a0 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -366,8 +366,13 @@ Index[] concreteIndices(Context context, String... indexExpressions) { continue; } } else if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) { - excludedDataStreams = true; - continue; + if (options.ignoreUnavailable() == false) { + assert options.expandWildcardExpressions() == false; + throw notFoundException(expression); + } else { + excludedDataStreams = true; + continue; + } } if (indexAbstraction.getType() == Type.ALIAS && context.isResolveToWriteIndex()) { From 938d2c03429fb721ddb9fa6df3d9995e563b951b Mon Sep 17 00:00:00 2001 From: Albert Zaharovits Date: Wed, 9 Nov 2022 15:23:00 +0200 Subject: [PATCH 2/5] Move EXCLUDED_DATA_STREAMS_KEY --- .../metadata/IndexNameExpressionResolver.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 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 5d3e69be752a0..2174b243cec6d 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -346,7 +346,6 @@ 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) { @@ -368,9 +367,11 @@ Index[] concreteIndices(Context context, String... indexExpressions) { } else if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) { if (options.ignoreUnavailable() == false) { assert options.expandWildcardExpressions() == false; - throw notFoundException(expression); + IndexNotFoundException infe = notFoundException(indexExpressions); + // Allows callers to handle IndexNotFoundException differently based on whether data streams were excluded. + infe.addMetadata(EXCLUDED_DATA_STREAMS_KEY, "true"); + throw infe; } else { - excludedDataStreams = true; continue; } } @@ -420,12 +421,7 @@ 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); From d1f11d43bf21797d27b7da7cd2883a08d792f719 Mon Sep 17 00:00:00 2001 From: Albert Zaharovits Date: Wed, 9 Nov 2022 15:57:55 +0200 Subject: [PATCH 3/5] Reuse method notFoundException --- .../metadata/IndexNameExpressionResolver.java | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 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 2174b243cec6d..d9164e604b944 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -427,22 +427,6 @@ Index[] concreteIndices(Context context, String... indexExpressions) { 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(); @@ -489,6 +473,22 @@ 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; + } + private static boolean shouldTrackConcreteIndex(Context context, IndicesOptions options, Index index) { if (context.systemIndexAccessLevel == SystemIndexAccessLevel.BACKWARDS_COMPATIBLE_ONLY && context.netNewSystemIndexPredicate.test(index.getName())) { @@ -1216,7 +1216,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) { @@ -1248,7 +1248,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, @@ -1265,23 +1265,17 @@ 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); + 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 && options.ignoreAliases()) { throw aliasesNotSupportedException(expression); } if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) { - throw indexNotFoundException(expression); + throw notFoundException(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()) { From 6b2dc8e83700f0ffcfaaca87323ef932d7a220e3 Mon Sep 17 00:00:00 2001 From: Albert Zaharovits Date: Wed, 9 Nov 2022 16:35:20 +0200 Subject: [PATCH 4/5] Reuse ensureAliasOrIndexExists --- .../metadata/IndexNameExpressionResolver.java | 61 ++++++++----------- 1 file changed, 24 insertions(+), 37 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 d9164e604b944..440e2c2c7aad5 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -349,31 +349,16 @@ Index[] concreteIndices(Context context, String... indexExpressions) { 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) { - if (options.ignoreUnavailable() == false) { - assert options.expandWildcardExpressions() == false; - IndexNotFoundException infe = notFoundException(indexExpressions); - // Allows callers to handle IndexNotFoundException differently based on whether data streams were excluded. - infe.addMetadata(EXCLUDED_DATA_STREAMS_KEY, "true"); - throw infe; - } else { - continue; - } + continue; } if (indexAbstraction.getType() == Type.ALIAS && context.isResolveToWriteIndex()) { @@ -489,6 +474,24 @@ private static IndexNotFoundException notFoundException(String... indexExpressio 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())) { @@ -1260,22 +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 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 && options.ignoreAliases()) { - throw aliasesNotSupportedException(expression); - } - if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) { - throw notFoundException(expression); - } - } - private static IndexMetadata.State excludeState(IndicesOptions options) { final IndexMetadata.State excludeState; if (options.expandWildcardsOpen() && options.expandWildcardsClosed()) { From be5d5cbc25a5c962f8cb80ea14057d9e155259b9 Mon Sep 17 00:00:00 2001 From: Albert Zaharovits Date: Mon, 14 Nov 2022 13:46:16 +0200 Subject: [PATCH 5/5] Update docs/changelog/91461.yaml --- docs/changelog/91461.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/changelog/91461.yaml 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: []