Package org.apache.solr.cloud.overseer
Class ZkStateWriter
- java.lang.Object
-
- org.apache.solr.cloud.overseer.ZkStateWriter
-
public class ZkStateWriter extends Object
ZkStateWriter is responsible for writing updates to the cluster state stored in ZooKeeper for both stateFormat=1 collection (stored in shared /clusterstate.json in ZK) and stateFormat=2 collections each of which get their own individual state.json in ZK. Updates to the cluster state are specified using theenqueueUpdate(ClusterState, List, ZkWriteCallback)method. The class buffers updates to reduce the number of writes to ZK. The buffered updates are flushed duringenqueueUpdateautomatically if necessary. ThewritePendingUpdates()can be used to force flush any pending updates. If eitherenqueueUpdate(ClusterState, List, ZkWriteCallback)orwritePendingUpdates()throws aKeeperException.BadVersionExceptionthen 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceZkStateWriter.ZkWriteCallback
-
Field Summary
Fields Modifier and Type Field Description protected ClusterStateclusterStateprotected booleaninvalidStateSet to true if we ever get a BadVersionException so that we can disallow future operations with this instanceprotected booleanisClusterStateModifiedprotected longlastUpdatedTimestatic ZkWriteCommandNO_OPRepresents a no-opZkWriteCommandwhich will result in no modification to cluster stateprotected ZkStateReaderreaderprotected Statsstatsprotected Map<String,DocCollection>updates
-
Constructor Summary
Constructors Constructor Description ZkStateWriter(ZkStateReader zkStateReader, Stats stats)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ClusterStateenqueueUpdate(ClusterState prevState, List<ZkWriteCommand> cmds, ZkStateWriter.ZkWriteCallback callback)Applies the givenZkWriteCommandon theprevState.ClusterStategetClusterState()booleanhasPendingUpdates()ClusterStatewritePendingUpdates()Writes all pending updates to ZooKeeper and returns the modified cluster state
-
-
-
Field Detail
-
NO_OP
public static ZkWriteCommand NO_OP
Represents a no-opZkWriteCommandwhich will result in no modification to cluster state
-
reader
protected final ZkStateReader reader
-
stats
protected final Stats stats
-
updates
protected Map<String,DocCollection> updates
-
clusterState
protected ClusterState clusterState
-
isClusterStateModified
protected boolean isClusterStateModified
-
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 zkStateReader, Stats stats)
-
-
Method Detail
-
enqueueUpdate
public ClusterState enqueueUpdate(ClusterState prevState, List<ZkWriteCommand> cmds, ZkStateWriter.ZkWriteCallback callback) throws IllegalStateException, Exception
Applies the givenZkWriteCommandon theprevState. The modifiedClusterStateis 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 methodwritePendingUpdates()can be used to force an immediate flush of pending cluster state changes.- Parameters:
prevState- the cluster state information on which the givencmdis appliedcmds- the list ofZkWriteCommandwhich specifies the change to be applied to cluster state in atomiccallback- aZkStateWriter.ZkWriteCallbackobject to be used for any callbacks- Returns:
- modified cluster state created after applying
cmdtoprevState. Ifcmdis a no-op (NO_OP) then theprevStateis returned unmodified. - Throws:
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 aKeeperException.BadVersionExceptionthis instance becomes unusable and must be discarded
-
hasPendingUpdates
public boolean hasPendingUpdates()
-
writePendingUpdates
public ClusterState writePendingUpdates() throws IllegalStateException, org.apache.zookeeper.KeeperException, InterruptedException
Writes all pending updates to ZooKeeper and returns the modified cluster state- Returns:
- the modified cluster state
- Throws:
IllegalStateException- if the current instance is no longer usable and must be discardedorg.apache.zookeeper.KeeperException- if any ZooKeeper operation results in an errorInterruptedException- if the current thread is interrupted
-
getClusterState
public ClusterState getClusterState()
- Returns:
- the most up-to-date cluster state until the last enqueueUpdate operation
-
-