Class ZkStateWriter

  • public class ZkStateWriter
    extends Object
    ZkStateWriter is responsible for writing updates to the cluster state stored in ZooKeeper for collections each of which gets their own individual state.json in ZK.

    Updates to the cluster state are specified using the enqueueUpdate(ClusterState, List, ZkWriteCallback) method. The class buffers updates to reduce the number of writes to ZK. The buffered updates are flushed during enqueueUpdate automatically if necessary. The writePendingUpdates() can be used to force flush any pending updates.

    If either enqueueUpdate(ClusterState, List, ZkWriteCallback) or writePendingUpdates() throws a KeeperException.BadVersionException then the internal buffered state of the class is suspect and the current instance of the class should be discarded and a new instance should be created and used for any future updates.

    • Field Detail

      • reader

        protected final reader
      • stats

        protected final Stats stats
      • clusterState

        protected clusterState
      • lastUpdatedTime

        protected long lastUpdatedTime
      • invalidState

        protected boolean invalidState
        Set to true if we ever get a BadVersionException so that we can disallow future operations with this instance
    • Constructor Detail

      • ZkStateWriter

        public ZkStateWriter​( zkStateReader,
                             Stats stats)
    • Method Detail

      • updateClusterState

        public void updateClusterState​(Function<,​> fun)
        if any collection is updated not through this class (directly written to ZK, then it needs to be updated locally)
      • enqueueUpdate

        public enqueueUpdate​( prevState,
                                                                       List<ZkWriteCommand> cmds,
                                                                       ZkStateWriter.ZkWriteCallback callback)
                                                                throws IllegalStateException,
        Applies the given ZkWriteCommand on the prevState. The modified ClusterState is returned and it is expected that the caller will use the returned cluster state for the subsequent invocation of this method.

        The modified state may be buffered or flushed to ZooKeeper depending on the internal buffering logic of this class. The hasPendingUpdates() method may be used to determine if the last enqueue operation resulted in buffered state. The method writePendingUpdates() can be used to force an immediate flush of pending cluster state changes.

        prevState - the cluster state information on which the given cmd is applied
        cmds - the list of ZkWriteCommand which specifies the change to be applied to cluster state in atomic
        callback - a ZkStateWriter.ZkWriteCallback object to be used for any callbacks
        modified cluster state created after applying cmd to prevState . If cmd is a no-op (NO_OP) then the prevState is returned unmodified.
        IllegalStateException - if the current instance is no longer usable. The current instance must be discarded.
        Exception - on an error in ZK operations or callback. If a flush to ZooKeeper results in a KeeperException.BadVersionException this instance becomes unusable and must be discarded
      • hasPendingUpdates

        public boolean hasPendingUpdates()
      • writePendingUpdates

        public writePendingUpdates()
                                                                      throws org.apache.zookeeper.KeeperException,
      • writePendingUpdates

        public writePendingUpdates​(Map<String,​ZkWriteCommand> updates,
                                                                             boolean resetPendingUpdateCounters)
                                                                      throws IllegalStateException,
        Writes all pending updates to ZooKeeper and returns the modified cluster state
        the modified cluster state
        IllegalStateException - if the current instance is no longer usable and must be discarded
        org.apache.zookeeper.KeeperException - if any ZooKeeper operation results in an error
        InterruptedException - if the current thread is interrupted
      • resetPendingUpdateCounters

        public void resetPendingUpdateCounters()
      • getClusterState

        public getClusterState()
        the most up-to-date cluster state until the last enqueueUpdate operation