Skip to content

Commit 6fc31d3

Browse files
Datastream unavailable exception metadata (#91461)
Fixes minor "not found" error behavior for unavailable datastreams.
1 parent 23bd5c5 commit 6fc31d3

File tree

2 files changed

+47
-60
lines changed

2 files changed

+47
-60
lines changed

docs/changelog/91461.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 91461
2+
summary: Datastream unavailable exception metadata
3+
area: Infra/Core
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -346,27 +346,18 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
346346
}
347347
}
348348

349-
boolean excludedDataStreams = false;
350349
final Set<Index> concreteIndices = Sets.newLinkedHashSetWithExpectedSize(expressions.size());
351350
final SortedMap<String, IndexAbstraction> indicesLookup = context.state.metadata().getIndicesLookup();
352351
for (String expression : expressions) {
352+
if (options.ignoreUnavailable() == false) {
353+
ensureAliasOrIndexExists(context, expression);
354+
}
353355
IndexAbstraction indexAbstraction = indicesLookup.get(expression);
354356
if (indexAbstraction == null) {
355-
if (options.ignoreUnavailable() == false) {
356-
assert options.expandWildcardExpressions() == false;
357-
throw notFoundException(expression);
358-
} else {
359-
continue;
360-
}
357+
continue;
361358
} else if (indexAbstraction.getType() == Type.ALIAS && context.getOptions().ignoreAliases()) {
362-
if (options.ignoreUnavailable() == false) {
363-
assert options.expandWildcardExpressions() == false;
364-
throw aliasesNotSupportedException(expression);
365-
} else {
366-
continue;
367-
}
359+
continue;
368360
} else if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) {
369-
excludedDataStreams = true;
370361
continue;
371362
}
372363

@@ -415,33 +406,12 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
415406
}
416407

417408
if (options.allowNoIndices() == false && concreteIndices.isEmpty()) {
418-
IndexNotFoundException infe = notFoundException(indexExpressions);
419-
if (excludedDataStreams) {
420-
// Allows callers to handle IndexNotFoundException differently based on whether data streams were excluded.
421-
infe.addMetadata(EXCLUDED_DATA_STREAMS_KEY, "true");
422-
}
423-
throw infe;
409+
throw notFoundException(indexExpressions);
424410
}
425411
checkSystemIndexAccess(context, concreteIndices);
426412
return concreteIndices.toArray(Index.EMPTY_ARRAY);
427413
}
428414

429-
private IndexNotFoundException notFoundException(String... indexExpressions) {
430-
IndexNotFoundException infe;
431-
if (indexExpressions.length == 1) {
432-
if (indexExpressions[0].equals(Metadata.ALL)) {
433-
infe = new IndexNotFoundException("no indices exist", indexExpressions[0]);
434-
} else {
435-
infe = new IndexNotFoundException(indexExpressions[0]);
436-
}
437-
infe.setResources("index_expression", indexExpressions[0]);
438-
} else {
439-
infe = new IndexNotFoundException((String) null);
440-
infe.setResources("index_expression", indexExpressions);
441-
}
442-
return infe;
443-
}
444-
445415
private void checkSystemIndexAccess(Context context, Set<Index> concreteIndices) {
446416
final Metadata metadata = context.getState().metadata();
447417
final Predicate<String> systemIndexAccessPredicate = context.getSystemIndexAccessPredicate().negate();
@@ -488,6 +458,40 @@ private void checkSystemIndexAccess(Context context, Set<Index> concreteIndices)
488458
}
489459
}
490460

461+
private static IndexNotFoundException notFoundException(String... indexExpressions) {
462+
IndexNotFoundException infe;
463+
if (indexExpressions != null && indexExpressions.length == 1) {
464+
if (Metadata.ALL.equals(indexExpressions[0])) {
465+
infe = new IndexNotFoundException("no indices exist", indexExpressions[0]);
466+
} else {
467+
infe = new IndexNotFoundException(indexExpressions[0]);
468+
}
469+
infe.setResources("index_or_alias", indexExpressions[0]);
470+
} else {
471+
infe = new IndexNotFoundException((String) null);
472+
infe.setResources("index_expression", indexExpressions);
473+
}
474+
return infe;
475+
}
476+
477+
@Nullable
478+
private static void ensureAliasOrIndexExists(Context context, String expression) {
479+
IndexAbstraction indexAbstraction = context.getState().getMetadata().getIndicesLookup().get(expression);
480+
if (indexAbstraction == null) {
481+
throw notFoundException(expression);
482+
}
483+
// treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api)
484+
if (indexAbstraction.getType() == Type.ALIAS && context.getOptions().ignoreAliases()) {
485+
throw aliasesNotSupportedException(expression);
486+
}
487+
if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) {
488+
IndexNotFoundException infe = notFoundException(expression);
489+
// Allows callers to handle IndexNotFoundException differently based on whether data streams were excluded.
490+
infe.addMetadata(EXCLUDED_DATA_STREAMS_KEY, "true");
491+
throw infe;
492+
}
493+
}
494+
491495
private static boolean shouldTrackConcreteIndex(Context context, IndicesOptions options, Index index) {
492496
if (context.systemIndexAccessLevel == SystemIndexAccessLevel.BACKWARDS_COMPATIBLE_ONLY
493497
&& context.netNewSystemIndexPredicate.test(index.getName())) {
@@ -1215,7 +1219,7 @@ private static Collection<String> innerResolve(Context context, List<String> exp
12151219
matchingOpenClosedNames.forEachOrdered(result::add);
12161220
}
12171221
if (emptyWildcardExpansion.get()) {
1218-
throw indexNotFoundException(expression);
1222+
throw notFoundException(expression);
12191223
}
12201224
} else {
12211225
if (isExclusion) {
@@ -1247,7 +1251,7 @@ private static Collection<String> innerResolve(Context context, List<String> exp
12471251

12481252
private static String validateAliasOrIndex(String expression) {
12491253
if (Strings.isEmpty(expression)) {
1250-
throw indexNotFoundException(expression);
1254+
throw notFoundException(expression);
12511255
}
12521256
// Expressions can not start with an underscore. This is reserved for APIs. If the check gets here, the API
12531257
// 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) {
12591263
return expression;
12601264
}
12611265

1262-
@Nullable
1263-
private static void ensureAliasOrIndexExists(Context context, String expression) {
1264-
final IndicesOptions options = context.getOptions();
1265-
IndexAbstraction indexAbstraction = context.getState().getMetadata().getIndicesLookup().get(expression);
1266-
if (indexAbstraction == null) {
1267-
throw indexNotFoundException(expression);
1268-
}
1269-
// treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api)
1270-
if (indexAbstraction.getType() == Type.ALIAS && options.ignoreAliases()) {
1271-
throw aliasesNotSupportedException(expression);
1272-
}
1273-
if (indexAbstraction.isDataStreamRelated() && context.includeDataStreams() == false) {
1274-
throw indexNotFoundException(expression);
1275-
}
1276-
}
1277-
1278-
private static IndexNotFoundException indexNotFoundException(String expression) {
1279-
IndexNotFoundException infe = new IndexNotFoundException(expression);
1280-
infe.setResources("index_or_alias", expression);
1281-
return infe;
1282-
}
1283-
12841266
private static IndexMetadata.State excludeState(IndicesOptions options) {
12851267
final IndexMetadata.State excludeState;
12861268
if (options.expandWildcardsOpen() && options.expandWildcardsClosed()) {

0 commit comments

Comments
 (0)