Skip to content

Commit 52d4c89

Browse files
Speed up Building Indices Lookup in Metadata (#83241)
We can speed this up by almost a third in case of a large index count where most indices are part of a datastream by avoiding the double lookup for the ds abstraction in the way done here. Also, simplified the loop iteration a little to use the slightly faster cursor and removed some needless conditional in the loop.
1 parent 0cbadbd commit 52d4c89

File tree

1 file changed

+10
-18
lines changed
  • server/src/main/java/org/elasticsearch/cluster/metadata

1 file changed

+10
-18
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ static SortedMap<String, IndexAbstraction> buildIndicesLookup(
17921792
ImmutableOpenMap<String, IndexMetadata> indices
17931793
) {
17941794
SortedMap<String, IndexAbstraction> indicesLookup = new TreeMap<>();
1795-
Map<String, DataStream> indexToDataStreamLookup = new HashMap<>();
1795+
Map<String, IndexAbstraction.DataStream> indexToDataStreamLookup = new HashMap<>();
17961796
// If there are no indices, then skip data streams. This happens only when metadata is read from disk
17971797
if (dataStreamMetadata != null && indices.size() > 0) {
17981798
Map<String, List<String>> dataStreamToAliasLookup = new HashMap<>();
@@ -1817,35 +1817,27 @@ static SortedMap<String, IndexAbstraction> buildIndicesLookup(
18171817
assert dataStream.getIndices().isEmpty() == false;
18181818

18191819
List<String> aliases = dataStreamToAliasLookup.getOrDefault(dataStream.getName(), List.of());
1820+
final IndexAbstraction.DataStream dsAbstraction = new IndexAbstraction.DataStream(dataStream, aliases);
18201821
IndexAbstraction existing = indicesLookup.put(
18211822
dataStream.getName(),
18221823
new IndexAbstraction.DataStream(dataStream, aliases)
18231824
);
18241825
assert existing == null : "duplicate data stream for " + dataStream.getName();
18251826

18261827
for (Index i : dataStream.getIndices()) {
1827-
indexToDataStreamLookup.put(i.getName(), dataStream);
1828+
indexToDataStreamLookup.put(i.getName(), dsAbstraction);
18281829
}
18291830
}
18301831
}
18311832

18321833
Map<String, List<IndexMetadata>> aliasToIndices = new HashMap<>();
1833-
for (var indexMetadata : indices.values()) {
1834-
ConcreteIndex index;
1835-
DataStream parent = indexToDataStreamLookup.get(indexMetadata.getIndex().getName());
1836-
if (parent != null) {
1837-
assert parent.getIndices()
1838-
.stream()
1839-
.map(Index::getName)
1840-
.collect(Collectors.toList())
1841-
.contains(indexMetadata.getIndex().getName())
1842-
: "Expected data stream [" + parent.getName() + "] to contain index " + indexMetadata.getIndex();
1843-
index = new ConcreteIndex(indexMetadata, (IndexAbstraction.DataStream) indicesLookup.get(parent.getName()));
1844-
} else {
1845-
index = new ConcreteIndex(indexMetadata);
1846-
}
1847-
1848-
IndexAbstraction existing = indicesLookup.put(indexMetadata.getIndex().getName(), index);
1834+
for (var entry : indices) {
1835+
final String name = entry.key;
1836+
final IndexMetadata indexMetadata = entry.value;
1837+
final IndexAbstraction.DataStream parent = indexToDataStreamLookup.get(name);
1838+
assert parent == null || parent.getIndices().stream().anyMatch(index -> name.equals(index.getName()))
1839+
: "Expected data stream [" + parent.getName() + "] to contain index " + indexMetadata.getIndex();
1840+
IndexAbstraction existing = indicesLookup.put(name, new ConcreteIndex(indexMetadata, parent));
18491841
assert existing == null : "duplicate for " + indexMetadata.getIndex();
18501842

18511843
for (var aliasCursor : indexMetadata.getAliases()) {

0 commit comments

Comments
 (0)