About the immutableOpenmap in elasticsearch

I would like to ask what consideration is the immutableOpenmap in Elasticsearch designed for? What kind of utility does it have? It is used in both aliasmetadata and indexmetadata. My personal guess is that it has something to do with atomicity, but I don't get the full understanding from the code.

Hi @xingzhao ,

it's key property is that it is immutable. This makes it easy to share among multiple threads. Since it can never mutate, there is no need for any inter-thread synchronization when accessing the map.

Immutability is used widely in Elasticsearch (and Lucene) to avoid having to synchronize access to such shared resources.

got it, thank you very much!

I have a related question. If two threads add a alias into openmap and use the build function to build a new immutableopenmap, how to ensure the final result is correct and it won't happened that one add action override the other?

First of all, ImmutableOpenMap.Builder can only be used by one thread at a time, it is not thread-safe.

The way Elasticsearch handles this is that every change to cluster state goes through a single master thread that applies the changes. So if two users add an alias, this will (in principle) be two cluster state updates, each executing one strictly after the other.

We do use batching of cluster state updates, but that is for performance more than conceptual.

thank you very much

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.