Skip to content

Commit 92e0d4e

Browse files
committed
Log deprecation warning when aliasing aliases
Support for providing aliases in the index side of update aliases, put alias and delete alias API will be removed in 6.0. This commit adds a deprecation warning for update aliases, put alias or delete alias request that specifies aliases, or wildcard expressions that match aliases, on their index side. Relates to elastic#23997
1 parent 6609f45 commit 92e0d4e

File tree

9 files changed

+306
-25
lines changed

9 files changed

+306
-25
lines changed

core/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@
5959
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> {
6060
private List<AliasActions> allAliasActions = new ArrayList<>();
6161

62-
//indices options that require every specified index to exist, expand wildcards only to open indices and
63-
//don't allow that no indices are resolved from wildcard expressions
64-
private static final IndicesOptions INDICES_OPTIONS = IndicesOptions.fromOptions(false, false, true, false);
62+
// indices options that require every specified index to exist, expand wildcards only to open
63+
// indices, don't allow that no indices are resolved from wildcard expressions and resolve the
64+
// expressions only against indices
65+
private static final IndicesOptions INDICES_OPTIONS = IndicesOptions.fromOptions(false, false, true, false, true, false, true);
6566

6667
public IndicesAliasesRequest() {
6768

core/src/main/java/org/elasticsearch/action/support/IndicesOptions.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.elasticsearch.action.support;
2020

2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.common.io.stream.StreamInput;
2324
import org.elasticsearch.common.io.stream.StreamOutput;
2425
import org.elasticsearch.rest.RestRequest;
@@ -43,6 +44,7 @@ public class IndicesOptions {
4344
private static final byte EXPAND_WILDCARDS_CLOSED = 8;
4445
private static final byte FORBID_ALIASES_TO_MULTIPLE_INDICES = 16;
4546
private static final byte FORBID_CLOSED_INDICES = 32;
47+
private static final byte IGNORE_ALIASES = 64;
4648

4749
private static final byte STRICT_EXPAND_OPEN = 6;
4850
private static final byte LENIENT_EXPAND_OPEN = 7;
@@ -51,10 +53,10 @@ public class IndicesOptions {
5153
private static final byte STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED = 48;
5254

5355
static {
54-
byte max = 1 << 6;
56+
short max = 1 << 7;
5557
VALUES = new IndicesOptions[max];
56-
for (byte id = 0; id < max; id++) {
57-
VALUES[id] = new IndicesOptions(id);
58+
for (short id = 0; id < max; id++) {
59+
VALUES[id] = new IndicesOptions((byte)id);
5860
}
5961
}
6062

@@ -106,18 +108,31 @@ public boolean forbidClosedIndices() {
106108
* @return whether aliases pointing to multiple indices are allowed
107109
*/
108110
public boolean allowAliasesToMultipleIndices() {
109-
//true is default here, for bw comp we keep the first 16 values
110-
//in the array same as before + the default value for the new flag
111+
// true is default here, for bw comp we keep the first 16 values
112+
// in the array same as before + the default value for the new flag
111113
return (id & FORBID_ALIASES_TO_MULTIPLE_INDICES) == 0;
112114
}
113115

116+
/**
117+
* @return whether aliases should be ignored (when resolving a wildcard)
118+
*/
119+
public boolean ignoreAliases() {
120+
return (id & IGNORE_ALIASES) != 0;
121+
}
122+
114123
public void writeIndicesOptions(StreamOutput out) throws IOException {
115-
out.write(id);
124+
if (out.getVersion().onOrAfter(Version.V_5_6_0_UNRELEASED)) {
125+
out.write(id);
126+
} else {
127+
// if we are talking to a node that doesn't support the newly added flag (ignoreAliases)
128+
// flip to 0 all the bits starting from the 7th
129+
out.write(id & 0x3f);
130+
}
116131
}
117132

118133
public static IndicesOptions readIndicesOptions(StreamInput in) throws IOException {
119-
//if we read from a node that doesn't support the newly added flag (allowAliasesToMultipleIndices)
120-
//we just receive the old corresponding value with the new flag set to true (default)
134+
//if we read from a node that doesn't support the newly added flag (ignoreAliases)
135+
//we just receive the old corresponding value with the new flag set to false (default)
121136
byte id = in.readByte();
122137
if (id >= VALUES.length) {
123138
throw new IllegalArgumentException("No valid missing index type id: " + id);
@@ -133,8 +148,16 @@ public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allo
133148
return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, defaultOptions.allowAliasesToMultipleIndices(), defaultOptions.forbidClosedIndices());
134149
}
135150

136-
static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices) {
137-
byte id = toByte(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices, forbidClosedIndices);
151+
public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices,
152+
boolean expandToClosedIndices, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices) {
153+
return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices,
154+
forbidClosedIndices, false);
155+
}
156+
157+
public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices,
158+
boolean expandToClosedIndices, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices, boolean ignoreAliases) {
159+
byte id = toByte(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices,
160+
forbidClosedIndices, ignoreAliases);
138161
return VALUES[id];
139162
}
140163

@@ -246,7 +269,7 @@ public static IndicesOptions lenientExpandOpen() {
246269
}
247270

248271
private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, boolean wildcardExpandToOpen,
249-
boolean wildcardExpandToClosed, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices) {
272+
boolean wildcardExpandToClosed, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices, boolean ignoreAliases) {
250273
byte id = 0;
251274
if (ignoreUnavailable) {
252275
id |= IGNORE_UNAVAILABLE;
@@ -268,6 +291,9 @@ private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, bo
268291
if (forbidClosedIndices) {
269292
id |= FORBID_CLOSED_INDICES;
270293
}
294+
if (ignoreAliases) {
295+
id |= IGNORE_ALIASES;
296+
}
271297
return id;
272298
}
273299

@@ -281,6 +307,7 @@ public String toString() {
281307
", expand_wildcards_closed=" + expandWildcardsClosed() +
282308
", allow_alisases_to_multiple_indices=" + allowAliasesToMultipleIndices() +
283309
", forbid_closed_indices=" + forbidClosedIndices() +
310+
", ignore_aliases=" + ignoreAliases() +
284311
']';
285312
}
286313
}

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
189189
} else {
190190
continue;
191191
}
192+
} else {
193+
if (aliasOrIndex.isAlias() && options.ignoreAliases()) {
194+
context.seenDeprecatedAlias();
195+
}
192196
}
193197

194198
Collection<IndexMetaData> resolvedIndices = aliasOrIndex.getIndices();
@@ -223,6 +227,11 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
223227
infe.setResources("index_expression", indexExpressions);
224228
throw infe;
225229
}
230+
if (context.hasSeenDeprecatedAlias()) {
231+
DEPRECATION_LOGGER.deprecated("Support for providing / matching aliases as part of the index parameter in the " +
232+
"delete index, update aliases, put alias,and delete alias APIs is deprecated.");
233+
}
234+
226235
return concreteIndices.toArray(new Index[concreteIndices.size()]);
227236
}
228237

@@ -504,6 +513,7 @@ static final class Context {
504513
private final IndicesOptions options;
505514
private final long startTime;
506515
private final boolean preserveAliases;
516+
private boolean seenDeprecatedAlias = false;
507517

508518
Context(ClusterState state, IndicesOptions options) {
509519
this(state, options, System.currentTimeMillis());
@@ -544,6 +554,14 @@ public long getStartTime() {
544554
boolean isPreserveAliases() {
545555
return preserveAliases;
546556
}
557+
558+
void seenDeprecatedAlias() {
559+
seenDeprecatedAlias = true;
560+
}
561+
562+
boolean hasSeenDeprecatedAlias() {
563+
return seenDeprecatedAlias;
564+
}
547565
}
548566

549567
private interface ExpressionResolver {
@@ -572,7 +590,7 @@ public List<String> resolve(Context context, List<String> expressions) {
572590
}
573591

574592
if (isEmptyOrTrivialWildcard(expressions)) {
575-
return resolveEmptyOrTrivialWildcard(options, metaData, true);
593+
return resolveEmptyOrTrivialWildcard(options, metaData);
576594
}
577595

578596
Set<String> result = innerResolve(context, expressions, options, metaData);
@@ -594,15 +612,15 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
594612
boolean plusSeen = false;
595613
for (int i = 0; i < expressions.size(); i++) {
596614
String expression = expressions.get(i);
615+
if (Strings.isEmpty(expression)) {
616+
throw infe(expression);
617+
}
597618
if (aliasOrIndexExists(metaData, expression)) {
598619
if (result != null) {
599620
result.add(expression);
600621
}
601622
continue;
602623
}
603-
if (Strings.isEmpty(expression)) {
604-
throw infe(expression);
605-
}
606624
boolean add = true;
607625
if (expression.charAt(0) == '+') {
608626
// if its the first, add empty result set
@@ -639,6 +657,14 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
639657

640658
final IndexMetaData.State excludeState = excludeState(options);
641659
final Map<String, AliasOrIndex> matches = matches(metaData, expression);
660+
if (options.ignoreAliases()) {
661+
for (Map.Entry<String, AliasOrIndex> entry : matches.entrySet()) {
662+
if (entry.getValue().isAlias()) {
663+
context.seenDeprecatedAlias();
664+
break;
665+
}
666+
}
667+
}
642668
Set<String> expand = expand(context, excludeState, matches);
643669
if (add) {
644670
result.addAll(expand);
@@ -655,7 +681,7 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
655681
}
656682
}
657683
if (plusSeen) {
658-
DEPRECATION_LOGGER.deprecated("support for '+' as part of index expressions is deprecated");
684+
DEPRECATION_LOGGER.deprecated("support for '+' as part of index expressions is deprecated");
659685
}
660686
return result;
661687
}
@@ -668,7 +694,7 @@ private boolean unavailableIgnoredOrExists(IndicesOptions options, MetaData meta
668694
return options.ignoreUnavailable() || aliasOrIndexExists(metaData, expression);
669695
}
670696

671-
private boolean aliasOrIndexExists(MetaData metaData, String expression) {
697+
private static boolean aliasOrIndexExists(MetaData metaData, String expression) {
672698
return metaData.getAliasAndIndexLookup().containsKey(expression);
673699
}
674700

@@ -693,7 +719,7 @@ private static IndexMetaData.State excludeState(IndicesOptions options) {
693719
return excludeState;
694720
}
695721

696-
private static Map<String, AliasOrIndex> matches(MetaData metaData, String expression) {
722+
static Map<String, AliasOrIndex> matches(MetaData metaData, String expression) {
697723
if (Regex.isMatchAllPattern(expression)) {
698724
// Can only happen if the expressions was initially: '-*'
699725
return metaData.getAliasAndIndexLookup();
@@ -743,15 +769,14 @@ private boolean isEmptyOrTrivialWildcard(List<String> expressions) {
743769
return expressions.isEmpty() || (expressions.size() == 1 && (MetaData.ALL.equals(expressions.get(0)) || Regex.isMatchAllPattern(expressions.get(0))));
744770
}
745771

746-
private List<String> resolveEmptyOrTrivialWildcard(IndicesOptions options, MetaData metaData, boolean assertEmpty) {
772+
private List<String> resolveEmptyOrTrivialWildcard(IndicesOptions options, MetaData metaData) {
747773
if (options.expandWildcardsOpen() && options.expandWildcardsClosed()) {
748774
return Arrays.asList(metaData.getConcreteAllIndices());
749775
} else if (options.expandWildcardsOpen()) {
750776
return Arrays.asList(metaData.getConcreteAllOpenIndices());
751777
} else if (options.expandWildcardsClosed()) {
752778
return Arrays.asList(metaData.getConcreteAllClosedIndices());
753779
} else {
754-
assert assertEmpty : "Shouldn't end up here";
755780
return Collections.emptyList();
756781
}
757782
}

core/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public class IndicesOptionsTests extends ESTestCase {
3131
public void testSerialization() throws Exception {
3232
int iterations = randomIntBetween(5, 20);
3333
for (int i = 0; i < iterations; i++) {
34-
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
34+
IndicesOptions indicesOptions = IndicesOptions.fromOptions(
35+
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
3536

3637
BytesStreamOutput output = new BytesStreamOutput();
3738
Version outputVersion = randomVersion(random());
@@ -49,6 +50,12 @@ public void testSerialization() throws Exception {
4950

5051
assertThat(indicesOptions2.forbidClosedIndices(), equalTo(indicesOptions.forbidClosedIndices()));
5152
assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(indicesOptions.allowAliasesToMultipleIndices()));
53+
54+
if (output.getVersion().onOrAfter(Version.V_5_6_0_UNRELEASED)) {
55+
assertEquals(indicesOptions2.ignoreAliases(), indicesOptions.ignoreAliases());
56+
} else {
57+
assertFalse(indicesOptions2.ignoreAliases());
58+
}
5259
}
5360
}
5461

@@ -61,9 +68,11 @@ public void testFromOptions() {
6168
boolean expandToClosedIndices = randomBoolean();
6269
boolean allowAliasesToMultipleIndices = randomBoolean();
6370
boolean forbidClosedIndices = randomBoolean();
71+
boolean ignoreAliases = randomBoolean();
72+
6473
IndicesOptions indicesOptions = IndicesOptions.fromOptions(
6574
ignoreUnavailable, allowNoIndices,expandToOpenIndices, expandToClosedIndices,
66-
allowAliasesToMultipleIndices, forbidClosedIndices
75+
allowAliasesToMultipleIndices, forbidClosedIndices, ignoreAliases
6776
);
6877

6978
assertThat(indicesOptions.ignoreUnavailable(), equalTo(ignoreUnavailable));
@@ -73,6 +82,7 @@ public void testFromOptions() {
7382
assertThat(indicesOptions.allowAliasesToMultipleIndices(), equalTo(allowAliasesToMultipleIndices));
7483
assertThat(indicesOptions.allowAliasesToMultipleIndices(), equalTo(allowAliasesToMultipleIndices));
7584
assertThat(indicesOptions.forbidClosedIndices(), equalTo(forbidClosedIndices));
85+
assertEquals(ignoreAliases, indicesOptions.ignoreAliases());
7686
}
7787
}
7888
}

core/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Arrays;
3434
import java.util.Collections;
3535
import java.util.HashSet;
36+
import java.util.List;
3637

3738
import static org.elasticsearch.common.util.set.Sets.newHashSet;
3839
import static org.hamcrest.Matchers.arrayContaining;
@@ -645,6 +646,68 @@ public void testConcreteIndicesWildcardWithNegation() {
645646
assertEquals(0, indexNames.length);
646647
}
647648

649+
public void testConcreteIndicesWildcardAndAliases() {
650+
MetaData.Builder mdBuilder = MetaData.builder()
651+
.put(indexBuilder("foo_foo").state(State.OPEN).putAlias(AliasMetaData.builder("foo")))
652+
.put(indexBuilder("bar_bar").state(State.OPEN).putAlias(AliasMetaData.builder("foo")));
653+
ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
654+
655+
// when ignoreAliases option is set, concreteIndexNames resolves the provided
656+
// expressions against the defined indices and aliases and issues a deprecation warning
657+
IndicesOptions ignoreAliasesOptions = IndicesOptions.fromOptions(false, false, true, false, true, false, true);
658+
659+
String[] indexNamesIndexWildcard = indexNameExpressionResolver.concreteIndexNames(state, ignoreAliasesOptions, "foo*");
660+
assertWarnings("Support for providing / matching aliases as part of the index parameter in the " +
661+
"delete index, update aliases, put alias,and delete alias APIs is deprecated.");
662+
assertEquals(2, indexNamesIndexWildcard.length);
663+
assertEquals("foo_foo", indexNamesIndexWildcard[0]);
664+
assertEquals("bar_bar", indexNamesIndexWildcard[1]);
665+
666+
indexNamesIndexWildcard = indexNameExpressionResolver.concreteIndexNames(state, ignoreAliasesOptions, "*o");
667+
assertWarnings("Support for providing / matching aliases as part of the index parameter in the " +
668+
"delete index, update aliases, put alias,and delete alias APIs is deprecated.");
669+
assertEquals(2, indexNamesIndexWildcard.length);
670+
assertEquals("foo_foo", indexNamesIndexWildcard[0]);
671+
assertEquals("bar_bar", indexNamesIndexWildcard[1]);
672+
673+
indexNamesIndexWildcard = indexNameExpressionResolver.concreteIndexNames(state, ignoreAliasesOptions, "f*o");
674+
assertWarnings("Support for providing / matching aliases as part of the index parameter in the " +
675+
"delete index, update aliases, put alias,and delete alias APIs is deprecated.");
676+
assertEquals(2, indexNamesIndexWildcard.length);
677+
assertEquals("foo_foo", indexNamesIndexWildcard[0]);
678+
assertEquals("bar_bar", indexNamesIndexWildcard[1]);
679+
680+
List<String> indexNames = Arrays.asList(indexNameExpressionResolver.concreteIndexNames(state, ignoreAliasesOptions, "foo"));
681+
assertWarnings("Support for providing / matching aliases as part of the index parameter in the " +
682+
"delete index, update aliases, put alias,and delete alias APIs is deprecated.");
683+
assertEquals(2, indexNames.size());
684+
assertTrue(indexNames.contains("foo_foo"));
685+
assertTrue(indexNames.contains("bar_bar"));
686+
687+
// when ignoreAliases option is not set, no deprecation warnings are issued
688+
IndicesOptions indicesAndAliasesOptions = IndicesOptions.fromOptions(false, false, true, false, true, false, false);
689+
690+
indexNames = Arrays.asList(indexNameExpressionResolver.concreteIndexNames(state, indicesAndAliasesOptions, "foo*"));
691+
assertEquals(2, indexNames.size());
692+
assertTrue(indexNames.contains("foo_foo"));
693+
assertTrue(indexNames.contains("bar_bar"));
694+
695+
indexNames = Arrays.asList(indexNameExpressionResolver.concreteIndexNames(state, indicesAndAliasesOptions, "*o"));
696+
assertEquals(2, indexNames.size());
697+
assertTrue(indexNames.contains("foo_foo"));
698+
assertTrue(indexNames.contains("bar_bar"));
699+
700+
indexNames = Arrays.asList(indexNameExpressionResolver.concreteIndexNames(state, indicesAndAliasesOptions, "f*o"));
701+
assertEquals(2, indexNames.size());
702+
assertTrue(indexNames.contains("foo_foo"));
703+
assertTrue(indexNames.contains("bar_bar"));
704+
705+
indexNames = Arrays.asList(indexNameExpressionResolver.concreteIndexNames(state, indicesAndAliasesOptions, "foo"));
706+
assertEquals(2, indexNames.size());
707+
assertTrue(indexNames.contains("foo_foo"));
708+
assertTrue(indexNames.contains("bar_bar"));
709+
}
710+
648711
/**
649712
* test resolving _all pattern (null, empty array or "_all") for random IndicesOptions
650713
*/

0 commit comments

Comments
 (0)