Skip to content

Commit a932591

Browse files
authored
Treat aliases as unavailable indices in delete index and update aliases api (#25524)
When resolving wildcards, aliases should be treated as unavailable indices when the `ignoreAliases` option is set to `true` (currently enabled with delete index api and update aliases api). This way the `allow_no_indices` and `ignore_unavailable` options can be honoured, otherwise WildcardExpressionResolver ends up treating aliases differently and there is no way to control when an error is thrown. The default behaviour for the delete index api, which has `ignore_unavailable` set to `false` and `allow_no_indices` set to `true` by default, is to throw an error when executed against an alias, same as when it's executed against an index that does not exist.
1 parent 09378f4 commit a932591

File tree

2 files changed

+214
-240
lines changed

2 files changed

+214
-240
lines changed

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

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,11 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
152152
}
153153
MetaData metaData = context.getState().metaData();
154154
IndicesOptions options = context.getOptions();
155-
boolean failClosed = options.forbidClosedIndices() && options.ignoreUnavailable() == false;
156-
boolean failNoIndices = options.ignoreUnavailable() == false;
155+
final boolean failClosed = options.forbidClosedIndices() && options.ignoreUnavailable() == false;
157156
// If only one index is specified then whether we fail a request if an index is missing depends on the allow_no_indices
158157
// option. At some point we should change this, because there shouldn't be a reason why whether a single index
159158
// or multiple indices are specified yield different behaviour.
160-
if (indexExpressions.length == 1) {
161-
failNoIndices = options.allowNoIndices() == false;
162-
}
159+
final boolean failNoIndices = indexExpressions.length == 1 ? !options.allowNoIndices() : !options.ignoreUnavailable();
163160
List<String> expressions = Arrays.asList(indexExpressions);
164161
for (ExpressionResolver expressionResolver : expressionResolvers) {
165162
expressions = expressionResolver.resolve(context, expressions);
@@ -569,7 +566,7 @@ public List<String> resolve(Context context, List<String> expressions) {
569566
}
570567

571568
if (isEmptyOrTrivialWildcard(expressions)) {
572-
return resolveEmptyOrTrivialWildcard(options, metaData, true);
569+
return resolveEmptyOrTrivialWildcard(options, metaData);
573570
}
574571

575572
Set<String> result = innerResolve(context, expressions, options, metaData);
@@ -590,25 +587,21 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
590587
boolean wildcardSeen = false;
591588
for (int i = 0; i < expressions.size(); i++) {
592589
String expression = expressions.get(i);
593-
if (aliasOrIndexExists(metaData, expression)) {
590+
if (Strings.isEmpty(expression)) {
591+
throw infe(expression);
592+
}
593+
if (aliasOrIndexExists(options, metaData, expression)) {
594594
if (result != null) {
595595
result.add(expression);
596596
}
597597
continue;
598598
}
599-
if (Strings.isEmpty(expression)) {
600-
throw infe(expression);
601-
}
602-
boolean add = true;
603-
if (expression.charAt(0) == '-') {
604-
// if there is a negation without a wildcard being previously seen, add it verbatim,
605-
// otherwise return the expression
606-
if (wildcardSeen) {
607-
add = false;
608-
expression = expression.substring(1);
609-
} else {
610-
add = true;
611-
}
599+
final boolean add;
600+
if (expression.charAt(0) == '-' && wildcardSeen) {
601+
add = false;
602+
expression = expression.substring(1);
603+
} else {
604+
add = true;
612605
}
613606
if (result == null) {
614607
// add all the previous ones...
@@ -634,28 +627,24 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
634627
} else {
635628
result.removeAll(expand);
636629
}
637-
638-
if (!noIndicesAllowedOrMatches(options, matches)) {
630+
if (options.allowNoIndices() == false && matches.isEmpty()) {
639631
throw infe(expression);
640632
}
641-
642633
if (Regex.isSimpleMatchPattern(expression)) {
643634
wildcardSeen = true;
644635
}
645636
}
646637
return result;
647638
}
648639

649-
private boolean noIndicesAllowedOrMatches(IndicesOptions options, Map<String, AliasOrIndex> matches) {
650-
return options.allowNoIndices() || !matches.isEmpty();
640+
private static boolean unavailableIgnoredOrExists(IndicesOptions options, MetaData metaData, String expression) {
641+
return options.ignoreUnavailable() || aliasOrIndexExists(options, metaData, expression);
651642
}
652643

653-
private boolean unavailableIgnoredOrExists(IndicesOptions options, MetaData metaData, String expression) {
654-
return options.ignoreUnavailable() || aliasOrIndexExists(metaData, expression);
655-
}
656-
657-
private boolean aliasOrIndexExists(MetaData metaData, String expression) {
658-
return metaData.getAliasAndIndexLookup().containsKey(expression);
644+
private static boolean aliasOrIndexExists(IndicesOptions options, MetaData metaData, String expression) {
645+
AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(expression);
646+
//treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api)
647+
return aliasOrIndex != null && (options.ignoreAliases() == false || aliasOrIndex.isAlias() == false);
659648
}
660649

661650
private static IndexNotFoundException infe(String expression) {
@@ -742,15 +731,14 @@ private boolean isEmptyOrTrivialWildcard(List<String> expressions) {
742731
return expressions.isEmpty() || (expressions.size() == 1 && (MetaData.ALL.equals(expressions.get(0)) || Regex.isMatchAllPattern(expressions.get(0))));
743732
}
744733

745-
private List<String> resolveEmptyOrTrivialWildcard(IndicesOptions options, MetaData metaData, boolean assertEmpty) {
734+
private static List<String> resolveEmptyOrTrivialWildcard(IndicesOptions options, MetaData metaData) {
746735
if (options.expandWildcardsOpen() && options.expandWildcardsClosed()) {
747736
return Arrays.asList(metaData.getConcreteAllIndices());
748737
} else if (options.expandWildcardsOpen()) {
749738
return Arrays.asList(metaData.getConcreteAllOpenIndices());
750739
} else if (options.expandWildcardsClosed()) {
751740
return Arrays.asList(metaData.getConcreteAllClosedIndices());
752741
} else {
753-
assert assertEmpty : "Shouldn't end up here";
754742
return Collections.emptyList();
755743
}
756744
}

0 commit comments

Comments
 (0)