Package io.atomix.raft.impl
Class RaftContext
java.lang.Object
io.atomix.raft.impl.RaftContext
- All Implemented Interfaces:
HealthMonitorable,AutoCloseable
Manages the volatile state and state transitions of a Raft server.
This class is the primary vehicle for managing the state of a server. All state that is shared across roles (i.e. follower, candidate, leader) is stored in the cluster state. This includes Raft-specific state like the current leader and term, the log, and the cluster configuration.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final RaftClusterContextprotected final ClusterMembershipServiceprotected final Stringprotected final RaftServerProtocolprotected final RaftStorageprotected final ThreadContext -
Constructor Summary
ConstructorsConstructorDescriptionRaftContext(String name, int partitionId, MemberId localMemberId, ClusterMembershipService membershipService, RaftServerProtocol protocol, RaftStorage storage, RaftThreadContextFactory threadContextFactory, Supplier<Random> randomFactory, RaftElectionConfig electionConfig, RaftPartitionConfig partitionConfig) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCommitListener(RaftCommitListener commitListener) Adds a new commit listener, which will be notified whenever the commit position changes.voidaddCommittedEntryListener(RaftCommittedEntryListener raftCommittedEntryListener) Adds a new committed entry listener, which will be notified when the Leader commits a new entry.voidaddFailureListener(FailureListener listener) Adds a failure listener which will be invoked when an uncaught exception occursvoidaddLeaderElectionListener(Consumer<RaftMember> listener) Adds a leader election listener.voidaddRoleChangeListener(RaftRoleChangeListener listener) Adds a role change listener.voidaddSnapshotReplicationListener(SnapshotReplicationListener snapshotReplicationListener) Adds a new snapshot replication listener, which will be notified before and after a new snapshot is received from a leader.voidaddStateChangeListener(Consumer<RaftContext.State> listener) Adds a state change listener.anoint()Attempts to become the leader.voidawaitState(RaftContext.State state, Consumer<RaftContext.State> listener) Awaits a state change.voidChecks that the current thread is the state context thread.voidclose()compact()Compacts the server logs.flushLog()Ensures everything written to the log until this point, is flushed to disk.Returns the cluster state.longReturns the commit index.longReturns the election timeout.Returns the entry validator to be called when an entry is appended.longReturns the first commit index.Returns the heartbeat interval.longReturns the state last voted for candidate.Returns the state leader.getLog()Returns the server log.Returns the server state machine.intintReturns the cluster service.Returns the server metadata store.intgetName()Returns the server name.intReturns the server snapshot store.intReturns the server protocol.Returns the current server state.getRole()Returns the current server role.getState()Returns the current server state.Returns the server storage.longgetTerm()Returns the state term.Returns the execution context.booleanisLeader()Returns a boolean indicating whether this server is the current leader.booleanvoidnotifyCommitListeners(long lastCommitIndex) Notifies all listeners of the latest entry.voidnotifyCommittedEntryListeners(IndexedRaftLogEntry committedEntry) Notifies all listeners of the latest entry.voidvoidvoidremoveCommitListener(RaftCommitListener commitListener) Removes registered commit listenervoidremoveCommittedEntryListener(RaftCommittedEntryListener raftCommittedEntryListener) Removes registered committedEntryListenervoidremoveFailureListener(FailureListener listener) Remove a failure listenervoidremoveLeaderElectionListener(Consumer<RaftMember> listener) Removes a leader election listener.voidRemoves a role change listener.voidremoveSnapshotReplicationListener(SnapshotReplicationListener snapshotReplicationListener) Removes registered snapshot replication listenervoidremoveStateChangeListener(Consumer<RaftContext.State> listener) Removes a state change listener.voidlongsetCommitIndex(long commitIndex) Sets the commit index.voidsetEntryValidator(EntryValidator validator) Sets the entry validator to be called when an entry is appended.voidsetFirstCommitIndex(long firstCommitIndex) Sets the first commit index.voidsetLastHeartbeat(long lastHeartbeat) voidsetLastVotedFor(MemberId candidate) Sets the state last voted for candidate.voidSets the state leader.voidsetPreferSnapshotReplicationThreshold(int snapshotReplicationThreshold) voidsetTerm(long term) Sets the state term.toString()voidtransition(RaftMember.Type type) Transitions the server to the base state for the given member type.voidtransition(RaftServer.Role role) Transition handler.
-
Field Details
-
name
-
threadContext
-
membershipService
-
cluster
-
protocol
-
storage
-
-
Constructor Details
-
RaftContext
public RaftContext(String name, int partitionId, MemberId localMemberId, ClusterMembershipService membershipService, RaftServerProtocol protocol, RaftStorage storage, RaftThreadContextFactory threadContextFactory, Supplier<Random> randomFactory, RaftElectionConfig electionConfig, RaftPartitionConfig partitionConfig)
-
-
Method Details
-
getMaxAppendBatchSize
public int getMaxAppendBatchSize() -
getMaxAppendsPerFollower
public int getMaxAppendsPerFollower() -
addRoleChangeListener
Adds a role change listener. If there isn't currently a transition ongoing the listener is called immediately after adding the listener.- Parameters:
listener- The role change listener.
-
removeRoleChangeListener
Removes a role change listener.- Parameters:
listener- The role change listener.
-
awaitState
Awaits a state change.- Parameters:
state- the state for which to waitlistener- the listener to call when the next state change occurs
-
addStateChangeListener
Adds a state change listener.- Parameters:
listener- The state change listener.
-
removeStateChangeListener
Removes a state change listener.- Parameters:
listener- The state change listener.
-
addCommitListener
Adds a new commit listener, which will be notified whenever the commit position changes. Note that it will be called on the Raft thread, and as such should not perform any heavy computation.- Parameters:
commitListener- the listener to add
-
removeCommitListener
Removes registered commit listener- Parameters:
commitListener- the listenere to remove
-
addCommittedEntryListener
Adds a new committed entry listener, which will be notified when the Leader commits a new entry. If RAFT runs currently in a Follower role this listeners are not called.Note that it will be called on the Raft thread, and as such should not perform any heavy computation.
- Parameters:
raftCommittedEntryListener- the listener to add
-
removeCommittedEntryListener
Removes registered committedEntryListener- Parameters:
raftCommittedEntryListener- the listener to remove
-
notifyCommitListeners
public void notifyCommitListeners(long lastCommitIndex) Notifies all listeners of the latest entry.- Parameters:
lastCommitIndex- index of the most recently committed entry
-
notifyCommittedEntryListeners
Notifies all listeners of the latest entry.- Parameters:
committedEntry- the most recently committed entry
-
setCommitIndex
public long setCommitIndex(long commitIndex) Sets the commit index.- Parameters:
commitIndex- The commit index.- Returns:
- the previous commit index
-
addSnapshotReplicationListener
Adds a new snapshot replication listener, which will be notified before and after a new snapshot is received from a leader. Note that it will be called on the Raft thread, and hence should not perform any heavy computation.- Parameters:
snapshotReplicationListener- the listener to add
-
removeSnapshotReplicationListener
public void removeSnapshotReplicationListener(SnapshotReplicationListener snapshotReplicationListener) Removes registered snapshot replication listener- Parameters:
snapshotReplicationListener- the listener to remove
-
notifySnapshotReplicationStarted
public void notifySnapshotReplicationStarted() -
notifySnapshotReplicationCompleted
public void notifySnapshotReplicationCompleted() -
compact
Compacts the server logs.- Returns:
- a future to be completed once the logs have been compacted
-
flushLog
Ensures everything written to the log until this point, is flushed to disk. If default raft flush is enabled, then this will not flush because the logs are flushed when necessary to achieve expected consistency guarantees.- Returns:
- a future to be completed once the log is flushed to disk
-
anoint
Attempts to become the leader. -
addLeaderElectionListener
Adds a leader election listener.- Parameters:
listener- The leader election listener.
-
removeLeaderElectionListener
Removes a leader election listener.- Parameters:
listener- The leader election listener.
-
getCluster
Returns the cluster state.- Returns:
- The cluster state.
-
getLeader
Returns the state leader.- Returns:
- The state leader.
-
transition
Transition handler. -
checkThread
public void checkThread()Checks that the current thread is the state context thread. -
transition
Transitions the server to the base state for the given member type. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
toString
-
getCommitIndex
public long getCommitIndex()Returns the commit index.- Returns:
- The commit index.
-
getElectionTimeout
Returns the election timeout.- Returns:
- The election timeout.
-
getFirstCommitIndex
public long getFirstCommitIndex()Returns the first commit index.- Returns:
- The first commit index.
-
setFirstCommitIndex
public void setFirstCommitIndex(long firstCommitIndex) Sets the first commit index.- Parameters:
firstCommitIndex- The first commit index.
-
getHeartbeatInterval
Returns the heartbeat interval.- Returns:
- The heartbeat interval.
-
getEntryValidator
Returns the entry validator to be called when an entry is appended.- Returns:
- The entry validator.
-
setEntryValidator
Sets the entry validator to be called when an entry is appended.- Parameters:
validator- The entry validator.
-
getLastVotedFor
Returns the state last voted for candidate.- Returns:
- The state last voted for candidate.
-
setLastVotedFor
Sets the state last voted for candidate.- Parameters:
candidate- The candidate that was voted for.
-
getLog
Returns the server log.- Returns:
- The server log.
-
getMembershipService
Returns the cluster service.- Returns:
- the cluster service
-
getMetaStore
Returns the server metadata store.- Returns:
- The server metadata store.
-
getName
Returns the server name.- Specified by:
getNamein interfaceHealthMonitorable- Returns:
- The server name.
-
getHealthReport
- Specified by:
getHealthReportin interfaceHealthMonitorable
-
addFailureListener
Adds a failure listener which will be invoked when an uncaught exception occurs- Specified by:
addFailureListenerin interfaceHealthMonitorable
-
removeFailureListener
Remove a failure listener- Specified by:
removeFailureListenerin interfaceHealthMonitorable
-
getProtocol
Returns the server protocol.- Returns:
- The server protocol.
-
getRaftRole
Returns the current server state.- Returns:
- The current server state.
-
getRaftRoleMetrics
-
getRole
Returns the current server role.- Returns:
- The current server role.
-
getLogCompactor
Returns the server state machine.- Returns:
- The log compactor.
-
getPersistedSnapshotStore
Returns the server snapshot store.- Returns:
- The server snapshot store.
-
getState
Returns the current server state.- Returns:
- the current server state
-
getStorage
Returns the server storage.- Returns:
- The server storage.
-
getTerm
public long getTerm()Returns the state term.- Returns:
- The state term.
-
setTerm
public void setTerm(long term) Sets the state term.- Parameters:
term- The state term.
-
getThreadContext
Returns the execution context.- Returns:
- The execution context.
-
isLeader
public boolean isLeader()Returns a boolean indicating whether this server is the current leader.- Returns:
- Indicates whether this server is the leader.
-
setLeader
Sets the state leader.- Parameters:
leader- The state leader.
-
getCurrentSnapshot
-
getCurrentSnapshotIndex
public long getCurrentSnapshotIndex() -
isRunning
public boolean isRunning() -
getReplicationMetrics
-
getRandom
-
getLastHeartbeat
public long getLastHeartbeat() -
setLastHeartbeat
public void setLastHeartbeat(long lastHeartbeat) -
resetLastHeartbeat
public void resetLastHeartbeat() -
getMinStepDownFailureCount
public int getMinStepDownFailureCount() -
getMaxQuorumResponseTimeout
-
getPreferSnapshotReplicationThreshold
public int getPreferSnapshotReplicationThreshold() -
setPreferSnapshotReplicationThreshold
public void setPreferSnapshotReplicationThreshold(int snapshotReplicationThreshold) -
getPartitionId
public int getPartitionId()
-