|
@@ -1287,7 +1287,7 @@ public class Metadata implements Iterable<IndexMetadata>, Diffable<Metadata>, To
|
|
|
final List<String> visibleOpenIndices = new ArrayList<>();
|
|
|
final List<String> allClosedIndices = new ArrayList<>();
|
|
|
final List<String> visibleClosedIndices = new ArrayList<>();
|
|
|
- final Set<String> duplicateAliasesIndices = new HashSet<>();
|
|
|
+ final Set<String> allAliases = new HashSet<>();
|
|
|
for (ObjectCursor<IndexMetadata> cursor : indices.values()) {
|
|
|
final IndexMetadata indexMetadata = cursor.value;
|
|
|
final String name = indexMetadata.getIndex().getName();
|
|
@@ -1308,23 +1308,55 @@ public class Metadata implements Iterable<IndexMetadata>, Diffable<Metadata>, To
|
|
|
visibleClosedIndices.add(name);
|
|
|
}
|
|
|
}
|
|
|
- indexMetadata.getAliases().keysIt().forEachRemaining(duplicateAliasesIndices::add);
|
|
|
+ indexMetadata.getAliases().keysIt().forEachRemaining(allAliases::add);
|
|
|
}
|
|
|
- duplicateAliasesIndices.retainAll(allIndices);
|
|
|
- if (duplicateAliasesIndices.isEmpty() == false) {
|
|
|
+
|
|
|
+ final Set<String> allDataStreams = new HashSet<>();
|
|
|
+ DataStreamMetadata dataStreamMetadata = (DataStreamMetadata) this.customs.get(DataStreamMetadata.TYPE);
|
|
|
+ if (dataStreamMetadata != null) {
|
|
|
+ for (DataStream dataStream : dataStreamMetadata.dataStreams().values()) {
|
|
|
+ allDataStreams.add(dataStream.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ final Set<String> aliasDuplicatesWithIndices = new HashSet<>(allAliases);
|
|
|
+ aliasDuplicatesWithIndices.retainAll(allIndices);
|
|
|
+ ArrayList<String> duplicates = new ArrayList<>();
|
|
|
+ if (aliasDuplicatesWithIndices.isEmpty() == false) {
|
|
|
// iterate again and constructs a helpful message
|
|
|
- ArrayList<String> duplicates = new ArrayList<>();
|
|
|
for (ObjectCursor<IndexMetadata> cursor : indices.values()) {
|
|
|
- for (String alias : duplicateAliasesIndices) {
|
|
|
+ for (String alias : aliasDuplicatesWithIndices) {
|
|
|
if (cursor.value.getAliases().containsKey(alias)) {
|
|
|
- duplicates.add(alias + " (alias of " + cursor.value.getIndex() + ")");
|
|
|
+ duplicates.add(alias + " (alias of " + cursor.value.getIndex() + ") conflicts with index");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- assert duplicates.size() > 0;
|
|
|
- throw new IllegalStateException("index and alias names need to be unique, but the following duplicates were found ["
|
|
|
- + Strings.collectionToCommaDelimitedString(duplicates) + "]");
|
|
|
+ }
|
|
|
|
|
|
+ final Set<String> aliasDuplicatesWithDataStreams = new HashSet<>(allAliases);
|
|
|
+ aliasDuplicatesWithDataStreams.retainAll(allDataStreams);
|
|
|
+ if (aliasDuplicatesWithDataStreams.isEmpty() == false) {
|
|
|
+ // iterate again and constructs a helpful message
|
|
|
+ for (ObjectCursor<IndexMetadata> cursor : indices.values()) {
|
|
|
+ for (String alias : aliasDuplicatesWithDataStreams) {
|
|
|
+ if (cursor.value.getAliases().containsKey(alias)) {
|
|
|
+ duplicates.add(alias + " (alias of " + cursor.value.getIndex() + ") conflicts with data stream");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ final Set<String> dataStreamDuplicatesWithIndices = new HashSet<>(allDataStreams);
|
|
|
+ dataStreamDuplicatesWithIndices.retainAll(allIndices);
|
|
|
+ if (dataStreamDuplicatesWithIndices.isEmpty() == false) {
|
|
|
+ for (String dataStream : dataStreamDuplicatesWithIndices) {
|
|
|
+ duplicates.add("data stream [" + dataStream + "] conflicts with index");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (duplicates.size() > 0) {
|
|
|
+ throw new IllegalStateException("index, alias, and data stream names need to be unique, but the following duplicates " +
|
|
|
+ "were found [" + Strings.collectionToCommaDelimitedString(duplicates) + "]");
|
|
|
}
|
|
|
|
|
|
SortedMap<String, IndexAbstraction> indicesLookup = Collections.unmodifiableSortedMap(buildIndicesLookup());
|
|
@@ -1349,10 +1381,40 @@ public class Metadata implements Iterable<IndexMetadata>, Diffable<Metadata>, To
|
|
|
|
|
|
private SortedMap<String, IndexAbstraction> buildIndicesLookup() {
|
|
|
SortedMap<String, IndexAbstraction> indicesLookup = new TreeMap<>();
|
|
|
+ Map<String, DataStream> indexToDataStreamLookup = new HashMap<>();
|
|
|
+ DataStreamMetadata dataStreamMetadata = (DataStreamMetadata) this.customs.get(DataStreamMetadata.TYPE);
|
|
|
+ // If there are no indices, then skip data streams. This happens only when metadata is read from disk
|
|
|
+ if (dataStreamMetadata != null && indices.size() > 0) {
|
|
|
+ for (DataStream dataStream : dataStreamMetadata.dataStreams().values()) {
|
|
|
+ List<IndexMetadata> backingIndices = dataStream.getIndices().stream()
|
|
|
+ .map(index -> indices.get(index.getName()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ assert backingIndices.isEmpty() == false;
|
|
|
+ assert backingIndices.contains(null) == false;
|
|
|
+
|
|
|
+ IndexAbstraction existing = indicesLookup.put(dataStream.getName(),
|
|
|
+ new IndexAbstraction.DataStream(dataStream, backingIndices));
|
|
|
+ assert existing == null : "duplicate data stream for " + dataStream.getName();
|
|
|
+
|
|
|
+ for (Index i : dataStream.getIndices()) {
|
|
|
+ indexToDataStreamLookup.put(i.getName(), dataStream);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
for (ObjectCursor<IndexMetadata> cursor : indices.values()) {
|
|
|
IndexMetadata indexMetadata = cursor.value;
|
|
|
- IndexAbstraction existing =
|
|
|
- indicesLookup.put(indexMetadata.getIndex().getName(), new IndexAbstraction.Index(indexMetadata));
|
|
|
+
|
|
|
+ IndexAbstraction.Index index;
|
|
|
+ DataStream parent = indexToDataStreamLookup.get(indexMetadata.getIndex().getName());
|
|
|
+ if (parent != null) {
|
|
|
+ assert parent.getIndices().contains(indexMetadata.getIndex());
|
|
|
+ index = new IndexAbstraction.Index(indexMetadata, (IndexAbstraction.DataStream) indicesLookup.get(parent.getName()));
|
|
|
+ } else {
|
|
|
+ index = new IndexAbstraction.Index(indexMetadata);
|
|
|
+ }
|
|
|
+
|
|
|
+ IndexAbstraction existing = indicesLookup.put(indexMetadata.getIndex().getName(), index);
|
|
|
assert existing == null : "duplicate for " + indexMetadata.getIndex();
|
|
|
|
|
|
for (ObjectObjectCursor<String, AliasMetadata> aliasCursor : indexMetadata.getAliases()) {
|
|
@@ -1369,28 +1431,6 @@ public class Metadata implements Iterable<IndexMetadata>, Diffable<Metadata>, To
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- DataStreamMetadata dataStreamMetadata = (DataStreamMetadata) this.customs.get(DataStreamMetadata.TYPE);
|
|
|
- // If there are no indices then it doesn't make sense to to add data streams to indicesLookup,
|
|
|
- // since there no concrete indices that a data stream can point to.
|
|
|
- // (This occurs when only Metadata is read from disk.)
|
|
|
- if (dataStreamMetadata != null && indices.size() > 0) {
|
|
|
- for (Map.Entry<String, DataStream> entry : dataStreamMetadata.dataStreams().entrySet()) {
|
|
|
- DataStream dataStream = entry.getValue();
|
|
|
- List<IndexMetadata> backingIndices = dataStream.getIndices().stream()
|
|
|
- .map(index -> indices.get(index.getName()))
|
|
|
- .collect(Collectors.toList());
|
|
|
- assert backingIndices.isEmpty() == false;
|
|
|
- assert backingIndices.contains(null) == false;
|
|
|
-
|
|
|
- IndexAbstraction existing = indicesLookup.put(dataStream.getName(),
|
|
|
- new IndexAbstraction.DataStream(dataStream, backingIndices));
|
|
|
- if (existing != null) {
|
|
|
- throw new IllegalStateException("data stream [" + dataStream.getName() +
|
|
|
- "] conflicts with existing " + existing.getType().getDisplayName() + " [" + existing.getName() + "]");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
indicesLookup.values().stream()
|
|
|
.filter(aliasOrIndex -> aliasOrIndex.getType() == IndexAbstraction.Type.ALIAS)
|
|
|
.forEach(alias -> ((IndexAbstraction.Alias) alias).computeAndValidateAliasProperties());
|