Package org.apache.solr.common.cloud

## Class ClusterState

- java.lang.Object
- - org.apache.solr.common.cloud.ClusterState

All Implemented Interfaces:
  `MapSerializable`, `MapWriter`, `NavigableObject`, `org.noggit.JSONWriter.Writable`

    public class ClusterState
    extends Object
    implements MapWriter

Immutable state of the cloud. Normally you can get the state by using `ZkStateReader#getClusterState()`.

However, the `list of live nodes` is updated when nodes go up and down.

WARNING: This API is experimental and might change in incompatible ways in the next release.

### Nested Class Summary

Nested Classes
`static class` `ClusterState.CollectionRef`

### Nested classes/interfaces inherited from interface org.apache.solr.common.MapWriter

`MapWriter.EntryWriter`

### Field Summary

Fields
`static String` `URL_SCHEME` Cluster Prop that is http or https.

### Fields inherited from interface org.apache.solr.common.MapWriter

`EMPTY`

### Constructor Summary

Constructors
`ClusterState​(Map<String,​ClusterState.CollectionRef> collectionStates, Set<String> liveNodes)` Use this if all the collection states are not readily available and some needs to be lazily loaded (parameter order different from constructor above to have different erasures)
`ClusterState​(Set<String> liveNodes, Map<String,​DocCollection> collectionStates)` Use this constr when ClusterState is meant for consumption.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
`static DocCollection` `collectionFromObjects​(String name, Map<String,​Object> objs, int version, Instant creationTime, DocCollection.PrsSupplier prsSupplier)`
`Stream<DocCollection>` `collectionStream()` Streams the resolved `DocCollection`s, which will often fetch from ZooKeeper for each one for a many-collection scenario.
`ClusterState` `copyWith​(String collectionName, DocCollection collection)` Returns a new cluster state object modified with the given collection.
`static ClusterState` `createFromCollectionMap​(int version, Map<String,​Object> stateMap, Set<String> liveNodes)` Deprecated.
`static ClusterState` `createFromCollectionMap​(int version, Map<String,​Object> stateMap, Set<String> liveNodes, Instant creationTime, DocCollection.PrsSupplier prsSupplier)` Deprecated.
`static ClusterState` `createFromJson​(int version, byte[] bytes, Set<String> liveNodes)` Deprecated.
`static ClusterState` `createFromJson​(int version, byte[] bytes, Set<String> liveNodes, Instant creationTime, DocCollection.PrsSupplier prsSupplier)` Deprecated.
`boolean` `equals​(Object obj)`
`void` `forEachCollection​(Consumer<DocCollection> consumer)` Deprecated. see `collectionStream()`
`DocCollection` `getCollection​(String collection)` Get the named DocCollection object, or throw an exception if it doesn't exist.
`Collection<String>` `getCollectionNames()` The collection names.
`DocCollection` `getCollectionOrNull​(String collectionName)` Returns the corresponding `DocCollection` object for the given collection name if such a collection exists.
`DocCollection` `getCollectionOrNull​(String collectionName, boolean allowCached)` Returns the corresponding `DocCollection` object for the given collection name if such a collection exists.
`ClusterState.CollectionRef` `getCollectionRef​(String coll)`
`Map<String,​DocCollection>` `getCollectionsMap()` Deprecated. see `collectionStream()`
`Map<String,​ClusterState.CollectionRef>` `getCollectionStates()` Deprecated. see `collectionStream()`
`Set<String>` `getLiveNodes()` Get names of the currently live nodes.
`Map<String,​List<Replica>>` `getReplicaNamesPerCollectionOnNode​(String nodeName)` Deprecated.
`String` `getShardId​(String nodeName, String coreName)` Deprecated.
`String` `getShardId​(String collectionName, String nodeName, String coreName)` Deprecated.
`boolean` `hasCollection​(String collectionName)` Returns true if the specified collection name exists, false otherwise.
`int` `hashCode()`
`boolean` `liveNodesContain​(String name)` Check if node is alive.
`static void` `setStrInternerParser​(Function<org.noggit.JSONParser,​org.noggit.ObjectBuilder> fun)`
`int` `size()` The approximate number of collections.
`String` `toString()`
`void` `writeMap​(MapWriter.EntryWriter ew)`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.apache.solr.common.MapWriter

`append, jsonStr, toMap, write`

### Methods inherited from interface org.apache.solr.common.NavigableObject

`_forEachEntry, _forEachEntry, _forEachEntry, _get, _get, _get, _getStr, _getStr, _getStr, _size`

### Field Detail

#### URL_SCHEME

    public static final String URL_SCHEME

Cluster Prop that is http or https.

See Also:
  Constant Field Values

### Constructor Detail

#### ClusterState

    public ClusterState​(Set<String> liveNodes,
                        Map<String,​DocCollection> collectionStates)

Use this constr when ClusterState is meant for consumption.

#### ClusterState

    public ClusterState​(Map<String,​ClusterState.CollectionRef> collectionStates,
                        Set<String> liveNodes)

Use this if all the collection states are not readily available and some needs to be lazily loaded (parameter order different from constructor above to have different erasures)

### Method Detail

#### copyWith

    public ClusterState copyWith​(String collectionName,
                                 DocCollection collection)

Returns a new cluster state object modified with the given collection.

Parameters:
  `collectionName` - the name of the modified (or deleted) collection
  `collection` - the collection object. A null value deletes the collection from the state
Returns:
  the updated cluster state which preserves the current live nodes
NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

#### hasCollection

    public boolean hasCollection​(String collectionName)

Returns true if the specified collection name exists, false otherwise.

Implementation note: This method resolves the collection reference by calling `ClusterState.CollectionRef.get()` which can make a call to ZooKeeper. This is necessary because the semantics of how collection list is loaded have changed in SOLR-6629.

#### getCollection

    public DocCollection getCollection​(String collection)

Get the named DocCollection object, or throw an exception if it doesn't exist.

#### getCollectionRef

    public ClusterState.CollectionRef getCollectionRef​(String coll)

#### getCollectionOrNull

    public DocCollection getCollectionOrNull​(String collectionName)

Returns the corresponding `DocCollection` object for the given collection name if such a collection exists. Returns null otherwise. Equivalent to getCollectionOrNull(collectionName, false)

#### getCollectionOrNull

    public DocCollection getCollectionOrNull​(String collectionName,
                                             boolean allowCached)

Returns the corresponding `DocCollection` object for the given collection name if such a collection exists. Returns null otherwise.

Parameters:
  `collectionName` - Name of the collection
  `allowCached` - allow LazyCollectionRefs to use a time-based cached value

  Implementation note: This method resolves the collection reference by calling `ClusterState.CollectionRef.get()` which may make a call to ZooKeeper. This is necessary because the semantics of how collection list is loaded have changed in SOLR-6629.

#### getCollectionNames

    public Collection<String> getCollectionNames()

The collection names. Like a Set but might not implement it. Immutable; non-null. Some names returned might not resolve via `getCollectionOrNull(String)`, so consider this a close approximation.

#### getCollectionsMap

    @Deprecated
    public Map<String,​DocCollection> getCollectionsMap()

Deprecated.
see `collectionStream()`
Get a map of collection name vs DocCollection objects

Implementation note: This method resolves the collection reference by calling `ClusterState.CollectionRef.get()` which can make a call to ZooKeeper. This is necessary because the semantics of how collection list is loaded have changed in SOLR-6629.

Returns:
  a map of collection name vs DocCollection object

#### getLiveNodes

    public Set<String> getLiveNodes()

Get names of the currently live nodes.

#### getShardId

    @Deprecated
    public String getShardId​(String nodeName,
                             String coreName)

Deprecated.

#### getShardId

    @Deprecated
    public String getShardId​(String collectionName,
                             String nodeName,
                             String coreName)

Deprecated.

#### getReplicaNamesPerCollectionOnNode

    @Deprecated
    public Map<String,​List<Replica>> getReplicaNamesPerCollectionOnNode​(String nodeName)

Deprecated.

#### liveNodesContain

    public boolean liveNodesContain​(String name)

Check if node is alive.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

#### createFromJson

    @Deprecated
    public static ClusterState createFromJson​(int version,
                                              byte[] bytes,
                                              Set<String> liveNodes,
                                              Instant creationTime,
                                              DocCollection.PrsSupplier prsSupplier)

Deprecated.
Create a ClusterState from Json. This method doesn't support legacy configName location and thus don't call it where that's important

Parameters:
  `bytes` - a byte array of a Json representation of a mapping from collection name to the Json representation of a `DocCollection` as written by `MapWriter.write(JSONWriter)`. It can represent one or more collections.
  `liveNodes` - list of live nodes
  `creationTime` - assigns this date to all `DocCollection` referenced by the returned `ClusterState`
Returns:
  the ClusterState

#### createFromJson

    @Deprecated
    public static ClusterState createFromJson​(int version,
                                              byte[] bytes,
                                              Set<String> liveNodes)

Deprecated.

#### createFromCollectionMap

    @Deprecated
    public static ClusterState createFromCollectionMap​(int version,
                                                       Map<String,​Object> stateMap,
                                                       Set<String> liveNodes,
                                                       Instant creationTime,
                                                       DocCollection.PrsSupplier prsSupplier)

Deprecated.

#### createFromCollectionMap

    @Deprecated
    public static ClusterState createFromCollectionMap​(int version,
                                                       Map<String,​Object> stateMap,
                                                       Set<String> liveNodes)

Deprecated.

#### collectionFromObjects

    public static DocCollection collectionFromObjects​(String name,
                                                      Map<String,​Object> objs,
                                                      int version,
                                                      Instant creationTime,
                                                      DocCollection.PrsSupplier prsSupplier)

NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

#### writeMap

    public void writeMap​(MapWriter.EntryWriter ew)
                  throws IOException

Specified by:
  `writeMap` in interface `MapWriter`
Throws:
  `IOException`

#### hashCode

    public int hashCode()

Overrides:
  `hashCode` in class `Object`

#### equals

    public boolean equals​(Object obj)

Overrides:
  `equals` in class `Object`

#### getCollectionStates

    @Deprecated
    public Map<String,​ClusterState.CollectionRef> getCollectionStates()

Deprecated.
see `collectionStream()`
Be aware that this may return collections which may not exist now. You can confirm that this collection exists after verifying CollectionRef.get() != null

#### collectionStream

    public Stream<DocCollection> collectionStream()

Streams the resolved `DocCollection`s, which will often fetch from ZooKeeper for each one for a many-collection scenario. Use this sparingly; some users have thousands of collections!

#### forEachCollection

    @Deprecated
    public void forEachCollection​(Consumer<DocCollection> consumer)

Deprecated.
see `collectionStream()`
Calls `consumer` with a resolved `DocCollection`s for all collections. Use this sparingly in case there are many collections.

#### size

    public int size()

The approximate number of collections.

#### setStrInternerParser

    public static void setStrInternerParser​(Function<org.noggit.JSONParser,​org.noggit.ObjectBuilder> fun)

NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.