Skip to content

Commit 9f7919a

Browse files
committed
MetaData Builder doesn't properly prevent an alias with the same name as an index (#26804)
Elasticsearch doesn't allow having an index alias named with the same name as an existing index. We currently have logic that tries to prevents that in the `MetaData.Builder#build()` method. Sadly that logic is flawed. Depending on iteration order, we may allow the above to happen (if we encounter the alias before the index). This commit fixes the above and improves the error message while at it. Note that we have a lot of protections in place before we end up relying on the metadata builder (validating this when we process APIs). I takes quite an abuse of the cluster to get that far.
1 parent ba075d2 commit 9f7919a

File tree

2 files changed

+90
-34
lines changed

2 files changed

+90
-34
lines changed

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

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings;
3636
import org.elasticsearch.cluster.service.ClusterService;
3737
import org.elasticsearch.common.Nullable;
38+
import org.elasticsearch.common.Strings;
3839
import org.elasticsearch.common.UUIDs;
3940
import org.elasticsearch.common.collect.HppcMaps;
4041
import org.elasticsearch.common.collect.ImmutableOpenMap;
@@ -68,6 +69,7 @@
6869
import java.util.Comparator;
6970
import java.util.EnumSet;
7071
import java.util.HashMap;
72+
import java.util.HashSet;
7173
import java.util.Iterator;
7274
import java.util.List;
7375
import java.util.Map;
@@ -998,55 +1000,70 @@ public MetaData build() {
9981000
// while these datastructures aren't even used.
9991001
// 2) The aliasAndIndexLookup can be updated instead of rebuilding it all the time.
10001002

1001-
// build all concrete indices arrays:
1002-
// TODO: I think we can remove these arrays. it isn't worth the effort, for operations on all indices.
1003-
// When doing an operation across all indices, most of the time is spent on actually going to all shards and
1004-
// do the required operations, the bottleneck isn't resolving expressions into concrete indices.
1005-
List<String> allIndicesLst = new ArrayList<>();
1003+
final Set<String> allIndices = new HashSet<>(indices.size());
1004+
final List<String> allOpenIndices = new ArrayList<>();
1005+
final List<String> allClosedIndices = new ArrayList<>();
1006+
final Set<String> duplicateAliasesIndices = new HashSet<>();
10061007
for (ObjectCursor<IndexMetaData> cursor : indices.values()) {
1007-
allIndicesLst.add(cursor.value.getIndex().getName());
1008-
}
1009-
String[] allIndices = allIndicesLst.toArray(new String[allIndicesLst.size()]);
1010-
1011-
List<String> allOpenIndicesLst = new ArrayList<>();
1012-
List<String> allClosedIndicesLst = new ArrayList<>();
1013-
for (ObjectCursor<IndexMetaData> cursor : indices.values()) {
1014-
IndexMetaData indexMetaData = cursor.value;
1008+
final IndexMetaData indexMetaData = cursor.value;
1009+
final String name = indexMetaData.getIndex().getName();
1010+
boolean added = allIndices.add(name);
1011+
assert added : "double index named [" + name + "]";
10151012
if (indexMetaData.getState() == IndexMetaData.State.OPEN) {
1016-
allOpenIndicesLst.add(indexMetaData.getIndex().getName());
1013+
allOpenIndices.add(indexMetaData.getIndex().getName());
10171014
} else if (indexMetaData.getState() == IndexMetaData.State.CLOSE) {
1018-
allClosedIndicesLst.add(indexMetaData.getIndex().getName());
1015+
allClosedIndices.add(indexMetaData.getIndex().getName());
10191016
}
1017+
indexMetaData.getAliases().keysIt().forEachRemaining(duplicateAliasesIndices::add);
1018+
}
1019+
duplicateAliasesIndices.retainAll(allIndices);
1020+
if (duplicateAliasesIndices.isEmpty() == false) {
1021+
// iterate again and constructs a helpful message
1022+
ArrayList<String> duplicates = new ArrayList<>();
1023+
for (ObjectCursor<IndexMetaData> cursor : indices.values()) {
1024+
for (String alias: duplicateAliasesIndices) {
1025+
if (cursor.value.getAliases().containsKey(alias)) {
1026+
duplicates.add(alias + " (alias of " + cursor.value.getIndex() + ")");
1027+
}
1028+
}
1029+
}
1030+
assert duplicates.size() > 0;
1031+
throw new IllegalStateException("index and alias names need to be unique, but the following duplicates were found ["
1032+
+ Strings.collectionToCommaDelimitedString(duplicates)+ "]");
1033+
10201034
}
1021-
String[] allOpenIndices = allOpenIndicesLst.toArray(new String[allOpenIndicesLst.size()]);
1022-
String[] allClosedIndices = allClosedIndicesLst.toArray(new String[allClosedIndicesLst.size()]);
10231035

10241036
// build all indices map
10251037
SortedMap<String, AliasOrIndex> aliasAndIndexLookup = new TreeMap<>();
10261038
for (ObjectCursor<IndexMetaData> cursor : indices.values()) {
10271039
IndexMetaData indexMetaData = cursor.value;
1028-
aliasAndIndexLookup.put(indexMetaData.getIndex().getName(), new AliasOrIndex.Index(indexMetaData));
1040+
AliasOrIndex existing = aliasAndIndexLookup.put(indexMetaData.getIndex().getName(), new AliasOrIndex.Index(indexMetaData));
1041+
assert existing == null : "duplicate for " + indexMetaData.getIndex();
10291042

10301043
for (ObjectObjectCursor<String, AliasMetaData> aliasCursor : indexMetaData.getAliases()) {
10311044
AliasMetaData aliasMetaData = aliasCursor.value;
1032-
AliasOrIndex aliasOrIndex = aliasAndIndexLookup.get(aliasMetaData.getAlias());
1033-
if (aliasOrIndex == null) {
1034-
aliasOrIndex = new AliasOrIndex.Alias(aliasMetaData, indexMetaData);
1035-
aliasAndIndexLookup.put(aliasMetaData.getAlias(), aliasOrIndex);
1036-
} else if (aliasOrIndex instanceof AliasOrIndex.Alias) {
1037-
AliasOrIndex.Alias alias = (AliasOrIndex.Alias) aliasOrIndex;
1038-
alias.addIndex(indexMetaData);
1039-
} else if (aliasOrIndex instanceof AliasOrIndex.Index) {
1040-
AliasOrIndex.Index index = (AliasOrIndex.Index) aliasOrIndex;
1041-
throw new IllegalStateException("index and alias names need to be unique, but alias [" + aliasMetaData.getAlias() + "] and index " + index.getIndex().getIndex() + " have the same name");
1042-
} else {
1043-
throw new IllegalStateException("unexpected alias [" + aliasMetaData.getAlias() + "][" + aliasOrIndex + "]");
1044-
}
1045+
aliasAndIndexLookup.compute(aliasMetaData.getAlias(), (aliasName, alias) -> {
1046+
if (alias == null) {
1047+
return new AliasOrIndex.Alias(aliasMetaData, indexMetaData);
1048+
} else {
1049+
assert alias instanceof AliasOrIndex.Alias : alias.getClass().getName();
1050+
((AliasOrIndex.Alias) alias).addIndex(indexMetaData);
1051+
return alias;
1052+
}
1053+
});
10451054
}
10461055
}
10471056
aliasAndIndexLookup = Collections.unmodifiableSortedMap(aliasAndIndexLookup);
1057+
// build all concrete indices arrays:
1058+
// TODO: I think we can remove these arrays. it isn't worth the effort, for operations on all indices.
1059+
// When doing an operation across all indices, most of the time is spent on actually going to all shards and
1060+
// do the required operations, the bottleneck isn't resolving expressions into concrete indices.
1061+
String[] allIndicesArray = allIndices.toArray(new String[allIndices.size()]);
1062+
String[] allOpenIndicesArray = allOpenIndices.toArray(new String[allOpenIndices.size()]);
1063+
String[] allClosedIndicesArray = allClosedIndices.toArray(new String[allClosedIndices.size()]);
1064+
10481065
return new MetaData(clusterUUID, version, transientSettings, persistentSettings, indices.build(), templates.build(),
1049-
customs.build(), allIndices, allOpenIndices, allClosedIndices, aliasAndIndexLookup);
1066+
customs.build(), allIndicesArray, allOpenIndicesArray, allClosedIndicesArray, aliasAndIndexLookup);
10501067
}
10511068

10521069
public static String toXContent(MetaData metaData) throws IOException {

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2727
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
2828
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
29-
import org.elasticsearch.common.io.stream.StreamInput;
3029
import org.elasticsearch.common.settings.Settings;
3130
import org.elasticsearch.common.xcontent.ToXContent;
3231
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -36,9 +35,14 @@
3635
import org.elasticsearch.test.ESTestCase;
3736

3837
import java.io.IOException;
38+
import java.util.HashMap;
39+
import java.util.HashSet;
40+
import java.util.Map;
41+
import java.util.Set;
3942

4043
import static org.hamcrest.Matchers.equalTo;
4144
import static org.hamcrest.Matchers.is;
45+
import static org.hamcrest.Matchers.startsWith;
4246

4347
public class MetaDataTests extends ESTestCase {
4448

@@ -52,7 +56,42 @@ public void testIndexAndAliasWithSameName() {
5256
MetaData.builder().put(builder).build();
5357
fail("exception should have been thrown");
5458
} catch (IllegalStateException e) {
55-
assertThat(e.getMessage(), equalTo("index and alias names need to be unique, but alias [index] and index [index] have the same name"));
59+
assertThat(e.getMessage(), equalTo("index and alias names need to be unique, but the following duplicates were found [index (alias of [index])]"));
60+
}
61+
}
62+
63+
public void testAliasCollidingWithAnExistingIndex() {
64+
int indexCount = randomIntBetween(10, 100);
65+
Set<String> indices = new HashSet<>(indexCount);
66+
for (int i = 0; i < indexCount; i++) {
67+
indices.add(randomAlphaOfLength(10));
68+
}
69+
Map<String, Set<String>> aliasToIndices = new HashMap<>();
70+
for (String alias: randomSubsetOf(randomIntBetween(1, 10), indices)) {
71+
aliasToIndices.put(alias, new HashSet<>(randomSubsetOf(randomIntBetween(1, 3), indices)));
72+
}
73+
int properAliases = randomIntBetween(0, 3);
74+
for (int i = 0; i < properAliases; i++) {
75+
aliasToIndices.put(randomAlphaOfLength(5), new HashSet<>(randomSubsetOf(randomIntBetween(1, 3), indices)));
76+
}
77+
MetaData.Builder metaDataBuilder = MetaData.builder();
78+
for (String index : indices) {
79+
IndexMetaData.Builder indexBuilder = IndexMetaData.builder(index)
80+
.settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT))
81+
.numberOfShards(1)
82+
.numberOfReplicas(0);
83+
aliasToIndices.forEach((key, value) -> {
84+
if (value.contains(index)) {
85+
indexBuilder.putAlias(AliasMetaData.builder(key).build());
86+
}
87+
});
88+
metaDataBuilder.put(indexBuilder);
89+
}
90+
try {
91+
metaDataBuilder.build();
92+
fail("exception should have been thrown");
93+
} catch (IllegalStateException e) {
94+
assertThat(e.getMessage(), startsWith("index and alias names need to be unique"));
5695
}
5796
}
5897

0 commit comments

Comments
 (0)