Class ZkStateReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable, org.apache.solr.common.SolrCloseable

    public class ZkStateReader
    extends Object
    implements org.apache.solr.common.SolrCloseable
    • Constructor Detail

      • ZkStateReader

        public ZkStateReader​(SolrZkClient zkClient)
      • ZkStateReader

        public ZkStateReader​(String zkServerAddress,
                             int zkClientTimeout,
                             int zkClientConnectTimeout)
      • ZkStateReader

        public ZkStateReader​(String zkServerAddress,
                             int zkClientTimeout,
                             int zkClientConnectTimeout,
                             boolean canUseZkACLs)
    • Method Detail

      • from

        public static ZkStateReader from​(org.apache.solr.client.solrj.impl.CloudSolrClient solrClient)
        Gets the ZkStateReader inside a ZK based SolrClient.
        Throws:
        IllegalArgumentException - if solrClient isn't ZK based.
      • forciblyRefreshAllClusterStateSlow

        public void forciblyRefreshAllClusterStateSlow()
                                                throws org.apache.zookeeper.KeeperException,
                                                       InterruptedException
        Forcibly refresh cluster state from ZK. Do this only to avoid race conditions because it's expensive.

        It is cheaper to call forceUpdateCollection(String) on a single collection if you must.

        Throws:
        org.apache.zookeeper.KeeperException
        InterruptedException
        NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
      • forceUpdateCollection

        public void forceUpdateCollection​(String collection)
                                   throws org.apache.zookeeper.KeeperException,
                                          InterruptedException
        Forcibly refresh a collection's internal state from ZK. Try to avoid having to resort to this when a better design is possible.
        Throws:
        org.apache.zookeeper.KeeperException
        InterruptedException
      • updateLiveNodes

        public void updateLiveNodes()
                             throws org.apache.zookeeper.KeeperException,
                                    InterruptedException
        Refresh the set of live nodes.
        Throws:
        org.apache.zookeeper.KeeperException
        InterruptedException
      • compareStateVersions

        public Integer compareStateVersions​(String coll,
                                            int version)
      • createClusterStateWatchersAndUpdate

        public void createClusterStateWatchersAndUpdate()
                                                 throws org.apache.zookeeper.KeeperException,
                                                        InterruptedException
        Throws:
        org.apache.zookeeper.KeeperException
        InterruptedException
      • registerCloudCollectionsListener

        public void registerCloudCollectionsListener​(CloudCollectionsListener cloudCollectionsListener)
        Register a CloudCollectionsListener to be called when the set of collections within a cloud changes.
      • removeCloudCollectionsListener

        public void removeCloudCollectionsListener​(CloudCollectionsListener cloudCollectionsListener)
        Remove a registered CloudCollectionsListener.
      • getCurrentCollections

        public Set<String> getCurrentCollections()
      • registerLiveNodesListener

        public void registerLiveNodesListener​(LiveNodesListener listener)
      • removeLiveNodesListener

        public void removeLiveNodesListener​(LiveNodesListener listener)
      • getClusterState

        public org.apache.solr.common.cloud.ClusterState getClusterState()
        Returns:
        information about the cluster from ZooKeeper
      • getUpdateLock

        public Object getUpdateLock()
      • isClosed

        public boolean isClosed()
        Specified by:
        isClosed in interface org.apache.solr.common.SolrCloseable
      • getLeader

        public org.apache.solr.common.cloud.Replica getLeader​(Set<String> liveNodes,
                                                              org.apache.solr.common.cloud.DocCollection docCollection,
                                                              String shard)
      • getLeader

        public org.apache.solr.common.cloud.Replica getLeader​(String collection,
                                                              String shard)
      • isNodeLive

        public boolean isNodeLive​(String node)
      • getShardLeadersPath

        public static String getShardLeadersPath​(String collection,
                                                 String shardId)
        Get path where shard leader properties live in zookeeper.
      • getShardLeadersElectPath

        public static String getShardLeadersElectPath​(String collection,
                                                      String shardId)
        Get path where shard leader elections ephemeral nodes are.
      • getReplicaProps

        public List<org.apache.solr.common.cloud.ZkCoreNodeProps> getReplicaProps​(String collection,
                                                                                  String shardId,
                                                                                  String thisCoreNodeName)
      • getReplicaProps

        public List<org.apache.solr.common.cloud.ZkCoreNodeProps> getReplicaProps​(String collection,
                                                                                  String shardId,
                                                                                  String thisCoreNodeName,
                                                                                  org.apache.solr.common.cloud.Replica.State mustMatchStateFilter)
      • getReplicaProps

        public List<org.apache.solr.common.cloud.ZkCoreNodeProps> getReplicaProps​(String collection,
                                                                                  String shardId,
                                                                                  String thisCoreNodeName,
                                                                                  org.apache.solr.common.cloud.Replica.State mustMatchStateFilter,
                                                                                  org.apache.solr.common.cloud.Replica.State mustNotMatchStateFilter)
      • getReplicaProps

        public List<org.apache.solr.common.cloud.ZkCoreNodeProps> getReplicaProps​(String collection,
                                                                                  String shardId,
                                                                                  String thisCoreNodeName,
                                                                                  org.apache.solr.common.cloud.Replica.State mustMatchStateFilter,
                                                                                  org.apache.solr.common.cloud.Replica.State mustNotMatchStateFilter,
                                                                                  EnumSet<org.apache.solr.common.cloud.Replica.Type> acceptReplicaType)
      • getClusterProperty

        public <T> T getClusterProperty​(String key,
                                        T defaultValue)
        Get a cluster property

        N.B. Cluster properties are updated via ZK watchers, and so may not necessarily be completely up-to-date. If you need to get the latest version, then use a ClusterProperties instance.

        Type Parameters:
        T - the type of the property
        Parameters:
        key - the property to read
        defaultValue - a default value to use if no such property exists
        Returns:
        the cluster property, or a default if the property is not set
      • getClusterProperty

        public <T> T getClusterProperty​(List<String> keyPath,
                                        T defaultValue)
        Same as the above but allows a full json path as a list of parts
        Parameters:
        keyPath - path to the property example ["collectionDefauls", "numShards"]
        defaultValue - a default value to use if no such property exists
        Returns:
        the cluster property, or a default if the property is not set
      • getClusterProperties

        public Map<String,​Object> getClusterProperties()
        Get all cluster properties for this cluster

        N.B. Cluster properties are updated via ZK watchers, and so may not necessarily be completely up-to-date. If you need to get the latest version, then use a ClusterProperties instance.

        Returns:
        a Map of cluster properties
      • getCollectionProperties

        public Map<String,​String> getCollectionProperties​(String collection)
        Get collection properties for a given collection. If the collection is watched, simply return it from the cache, otherwise fetch it directly from zookeeper. This is a convenience for getCollectionProperties(collection,0)
        Parameters:
        collection - the collection for which properties are desired
        Returns:
        a map representing the key/value properties for the collection.
      • getCollectionProperties

        public Map<String,​String> getCollectionProperties​(String collection,
                                                                long cacheForMillis)
        Get and cache collection properties for a given collection. If the collection is watched, or still cached simply return it from the cache, otherwise fetch it directly from zookeeper and retain the value for at least cacheForMillis milliseconds. Cached properties are watched in zookeeper and updated automatically. This version of getCollectionProperties should be used when properties need to be consulted frequently in the absence of an active CollectionPropsWatcher.
        Parameters:
        collection - The collection for which properties are desired
        cacheForMillis - The minimum number of milliseconds to maintain a cache for the specified collection's properties. Setting a CollectionPropsWatcher will override this value and retain the cache for the life of the watcher. A lack of changes in zookeeper may allow the caching to remain for a greater duration up to the cycle time of CacheCleaner. Passing zero for this value will explicitly remove the cached copy if and only if it is due to expire and no watch exists. Any positive value will extend the expiration time if required.
        Returns:
        a map representing the key/value properties for the collection.
      • getSecurityProps

        public ZkStateReader.ConfigData getSecurityProps​(boolean getFresh)
        Returns the content of /security.json from ZooKeeper as a Map If the files doesn't exist, it returns null.
      • getBaseUrlForNodeName

        public String getBaseUrlForNodeName​(String nodeName)
        Returns the baseURL corresponding to a given node's nodeName -- NOTE: does not (currently) imply that the nodeName (or resulting baseURL) exists in the cluster.
        WARNING: This API is experimental and might change in incompatible ways in the next release.
      • getCollectionLive

        public org.apache.solr.common.cloud.DocCollection getCollectionLive​(String coll)
      • registerCore

        public void registerCore​(String collection)
        Notify this reader that a local Core is a member of a collection, and so that collection state should be watched.

        Not a public API. This method should only be called from ZkController.

        The number of cores per-collection is tracked, and adding multiple cores from the same collection does not increase the number of watches.

        Parameters:
        collection - the collection that the core is a member of
        See Also:
        unregisterCore(String)
      • unregisterCore

        public void unregisterCore​(String collection)
        Notify this reader that a local core that is a member of a collection has been closed.

        Not a public API. This method should only be called from ZkController.

        If no cores are registered for a collection, and there are no CollectionStateWatchers for that collection either, the collection watch will be removed.

        Parameters:
        collection - the collection that the core belongs to
      • registerDocCollectionWatcher

        public void registerDocCollectionWatcher​(String collection,
                                                 DocCollectionWatcher stateWatcher)
        Register a DocCollectionWatcher to be called when the cluster state for a collection changes.

        The Watcher will automatically be removed when it's onStateChanged returns true

      • waitForState

        public org.apache.solr.common.cloud.DocCollection waitForState​(String collection,
                                                                       long wait,
                                                                       TimeUnit unit,
                                                                       Predicate<org.apache.solr.common.cloud.DocCollection> predicate)
                                                                throws InterruptedException,
                                                                       TimeoutException
        Block until a Predicate returns true, or the wait times out

        Note that the predicate may be called again even after it has returned true, so implementors should avoid changing state within the predicate call itself. The predicate may also be called concurrently when multiple state changes are seen in rapid succession.

        Parameters:
        collection - the collection to watch
        wait - how long to wait
        unit - the units of the wait parameter
        predicate - the predicate to call on state changes
        Returns:
        the state of the doc collection after the predicate succeeds
        Throws:
        InterruptedException - on interrupt
        TimeoutException - on timeout
      • waitForLiveNodes

        public void waitForLiveNodes​(long wait,
                                     TimeUnit unit,
                                     LiveNodesPredicate predicate)
                              throws InterruptedException,
                                     TimeoutException
        Block until a LiveNodesStatePredicate returns true, or the wait times out

        Note that the predicate may be called again even after it has returned true, so implementors should avoid changing state within the predicate call itself.

        Parameters:
        wait - how long to wait
        unit - the units of the wait parameter
        predicate - the predicate to call on state changes
        Throws:
        InterruptedException - on interrupt
        TimeoutException - on timeout
      • getAliases

        public org.apache.solr.common.cloud.Aliases getAliases()
        Get an immutable copy of the present state of the aliases. References to this object should not be retained in any context where it will be important to know if aliases have changed.
        Returns:
        The current aliases, Aliases.EMPTY if not solr cloud, or no aliases have existed yet. Never returns null.
      • getCollection

        public org.apache.solr.common.cloud.DocCollection getCollection​(String collection)