Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datastream unavailable exception metadata #91461

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,27 +346,18 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
}
}

boolean excludedDataStreams = false;
final Set<Index> concreteIndices = Sets.newLinkedHashSetWithExpectedSize(expressions.size());
final SortedMap<String, IndexAbstraction> 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;
}

Comment on lines +352 to 363
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this is going to be moved, in a follow-up PR, inside the resolveExpression method on line 339.

Expand Down Expand Up @@ -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<Index> concreteIndices) {
final Metadata metadata = context.getState().metadata();
final Predicate<String> systemIndexAccessPredicate = context.getSystemIndexAccessPredicate().negate();
Expand Down Expand Up @@ -488,6 +458,40 @@ private void checkSystemIndexAccess(Context context, Set<Index> 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())) {
Expand Down Expand Up @@ -1215,7 +1219,7 @@ private static Collection<String> innerResolve(Context context, List<String> exp
matchingOpenClosedNames.forEachOrdered(result::add);
}
if (emptyWildcardExpansion.get()) {
throw indexNotFoundException(expression);
throw notFoundException(expression);
}
} else {
if (isExclusion) {
Expand Down Expand Up @@ -1247,7 +1251,7 @@ private static Collection<String> innerResolve(Context context, List<String> 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,
Expand All @@ -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()) {
Expand Down