Class RaftContext

java.lang.Object
io.atomix.raft.impl.RaftContext
All Implemented Interfaces:
HealthMonitorable, AutoCloseable

public class RaftContext extends Object implements AutoCloseable, HealthMonitorable
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.

  • Field Details

  • Constructor Details

  • Method Details

    • getMaxAppendBatchSize

      public int getMaxAppendBatchSize()
    • getMaxAppendsPerFollower

      public int getMaxAppendsPerFollower()
    • addRoleChangeListener

      public void addRoleChangeListener(RaftRoleChangeListener listener)
      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

      public void removeRoleChangeListener(RaftRoleChangeListener listener)
      Removes a role change listener.
      Parameters:
      listener - The role change listener.
    • addStateChangeListener

      public void addStateChangeListener(Consumer<RaftContext.State> listener)
      Adds a state change listener.
      Parameters:
      listener - The state change listener.
    • removeStateChangeListener

      public void removeStateChangeListener(Consumer<RaftContext.State> listener)
      Removes a state change listener.
      Parameters:
      listener - The state change listener.
    • addCommitListener

      public void addCommitListener(RaftCommitListener commitListener)
      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

      public void removeCommitListener(RaftCommitListener commitListener)
      Removes registered commit listener
      Parameters:
      commitListener - the listener to remove
    • addCommittedEntryListener

      public void addCommittedEntryListener(RaftApplicationEntryCommittedPositionListener raftApplicationEntryCommittedPositionListener)
      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:
      raftApplicationEntryCommittedPositionListener - the listener to add
    • removeCommittedEntryListener

      public void removeCommittedEntryListener(RaftApplicationEntryCommittedPositionListener raftApplicationEntryCommittedPositionListener)
      Removes registered committedEntryListener
      Parameters:
      raftApplicationEntryCommittedPositionListener - 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
    • notifyApplicationEntryCommittedPositionListeners

      public void notifyApplicationEntryCommittedPositionListeners(long committedEntry)
      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

      public void addSnapshotReplicationListener(SnapshotReplicationListener snapshotReplicationListener)
      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()
    • flushLog

      public CompletableFuture<Void> 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
    • addLeaderElectionListener

      public void addLeaderElectionListener(Consumer<RaftMember> listener)
      Adds a leader election listener.
      Parameters:
      listener - The leader election listener.
    • removeLeaderElectionListener

      public void removeLeaderElectionListener(Consumer<RaftMember> listener)
      Removes a leader election listener.
      Parameters:
      listener - The leader election listener.
    • getCluster

      public RaftClusterContext getCluster()
      Returns the cluster state.
      Returns:
      The cluster state.
    • getLeader

      public DefaultRaftMember getLeader()
      Returns the state leader.
      Returns:
      The state leader.
    • transition

      public void transition(RaftServer.Role role)
      Transition handler.
    • checkThread

      public void checkThread()
      Checks that the current thread is the state context thread.
    • transition

      public void transition(RaftMember.Type type)
      Transitions the server to the base state for the given member type.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getCommitIndex

      public long getCommitIndex()
      Returns the commit index.
      Returns:
      The commit index.
    • getElectionTimeout

      public Duration 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

      public Duration getHeartbeatInterval()
      Returns the heartbeat interval.
      Returns:
      The heartbeat interval.
    • getEntryValidator

      public EntryValidator getEntryValidator()
      Returns the entry validator to be called when an entry is appended.
      Returns:
      The entry validator.
    • setEntryValidator

      public void setEntryValidator(EntryValidator validator)
      Sets the entry validator to be called when an entry is appended.
      Parameters:
      validator - The entry validator.
    • getLastVotedFor

      public MemberId getLastVotedFor()
      Returns the state last voted for candidate.
      Returns:
      The state last voted for candidate.
    • setLastVotedFor

      public void setLastVotedFor(MemberId candidate)
      Sets the state last voted for candidate.
      Parameters:
      candidate - The candidate that was voted for.
    • getLog

      public RaftLog getLog()
      Returns the server log.
      Returns:
      The server log.
    • getMembershipService

      public ClusterMembershipService getMembershipService()
      Returns the cluster service.
      Returns:
      the cluster service
    • getMetaStore

      public MetaStore getMetaStore()
      Returns the server metadata store.
      Returns:
      The server metadata store.
    • getName

      public String getName()
      Returns the server name.
      Returns:
      The server name.
    • componentName

      public String componentName()
      Specified by:
      componentName in interface HealthMonitorable
    • getHealthReport

      public HealthReport getHealthReport()
      Specified by:
      getHealthReport in interface HealthMonitorable
    • addFailureListener

      public void addFailureListener(FailureListener listener)
      Adds a failure listener which will be invoked when an uncaught exception occurs
      Specified by:
      addFailureListener in interface HealthMonitorable
    • removeFailureListener

      public void removeFailureListener(FailureListener listener)
      Remove a failure listener
      Specified by:
      removeFailureListener in interface HealthMonitorable
    • getProtocol

      public RaftServerProtocol getProtocol()
      Returns the server protocol.
      Returns:
      The server protocol.
    • getRaftRole

      public RaftRole getRaftRole()
      Returns the current server state.
      Returns:
      The current server state.
    • getRaftRoleMetrics

      public RaftRoleMetrics getRaftRoleMetrics()
    • getRole

      public RaftServer.Role getRole()
      Returns the current server role.
      Returns:
      The current server role.
    • getLogCompactor

      public LogCompactor getLogCompactor()
      Returns the server state machine.
      Returns:
      The log compactor.
    • getPersistedSnapshotStore

      public ReceivableSnapshotStore getPersistedSnapshotStore()
      Returns the server snapshot store.
      Returns:
      The server snapshot store.
    • getState

      public RaftContext.State getState()
      Returns the current server state.
      Returns:
      the current server state
    • getStorage

      public RaftStorage 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

      public ThreadContext 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

      public void setLeader(MemberId leader)
      Sets the state leader.
      Parameters:
      leader - The state leader.
    • getCurrentSnapshot

      public PersistedSnapshot getCurrentSnapshot()
    • updateCurrentSnapshot

      public void updateCurrentSnapshot()
    • getCurrentSnapshotIndex

      public long getCurrentSnapshotIndex()
    • getCurrentConfigurationIndex

      public long getCurrentConfigurationIndex()
      Returns:
      the current configuration index or -1 if there is no configuration yet.
    • isRunning

      public boolean isRunning()
    • getReplicationMetrics

      public RaftReplicationMetrics getReplicationMetrics()
    • getRandom

      public Random getRandom()
    • getLastHeartbeat

      public long getLastHeartbeat()
    • setLastHeartbeat

      public void setLastHeartbeat(long lastHeartbeat)
    • resetLastHeartbeat

      public void resetLastHeartbeat()
    • getMinStepDownFailureCount

      public int getMinStepDownFailureCount()
    • getMaxQuorumResponseTimeout

      public Duration getMaxQuorumResponseTimeout()
    • getPreferSnapshotReplicationThreshold

      public int getPreferSnapshotReplicationThreshold()
    • setPreferSnapshotReplicationThreshold

      public void setPreferSnapshotReplicationThreshold(int snapshotReplicationThreshold)
    • reconfigurePriority

      public CompletableFuture<Void> reconfigurePriority(int newPriority)
    • getPartitionId

      public int getPartitionId()
    • updateState

      public void updateState(RaftContext.State newState)
    • getSnapshotChunkSize

      public int getSnapshotChunkSize()
    • getTailSegments

      public CompletableFuture<Collection<Path>> getTailSegments(long index)