Class DefaultRaftServer

java.lang.Object
io.atomix.raft.impl.DefaultRaftServer
All Implemented Interfaces:
RaftServer

public class DefaultRaftServer extends Object implements RaftServer
Provides a standalone implementation of the Raft consensus algorithm.
See Also:
  • Field Details

  • Constructor Details

    • DefaultRaftServer

      public DefaultRaftServer(RaftContext context)
  • Method Details

    • toString

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

      public String name()
      Description copied from interface: RaftServer
      Returns the server name.

      The server name is provided to the server via the builder configuration. The name is used internally to manage the server's on-disk state. Log, snapshot, and configuration files stored on disk use the server name as the prefix.

      Specified by:
      name in interface RaftServer
      Returns:
      The server name.
    • cluster

      public RaftCluster cluster()
      Description copied from interface: RaftServer
      Returns the server's cluster configuration.

      The RaftCluster is representative of the server's current view of the cluster configuration. The first time the server is started, the cluster configuration will be initialized using the MemberId list provided to the server builder

      Specified by:
      cluster in interface RaftServer
      Returns:
      The server's cluster configuration.
    • addRoleChangeListener

      public void addRoleChangeListener(RaftRoleChangeListener listener)
      Description copied from interface: RaftServer
      Adds a role change listener.
      Specified by:
      addRoleChangeListener in interface RaftServer
      Parameters:
      listener - The role change listener that consumes the role and the raft term.
    • removeRoleChangeListener

      public void removeRoleChangeListener(RaftRoleChangeListener listener)
      Description copied from interface: RaftServer
      Removes a role change listener.
      Specified by:
      removeRoleChangeListener in interface RaftServer
      Parameters:
      listener - The role change listener to remove.
    • addFailureListener

      public void addFailureListener(FailureListener listener)
      Description copied from interface: RaftServer
      Adds a failure listener
      Specified by:
      addFailureListener in interface RaftServer
    • removeFailureListener

      public void removeFailureListener(FailureListener listener)
      Description copied from interface: RaftServer
      Removes a failure listener
      Specified by:
      removeFailureListener in interface RaftServer
    • bootstrap

      public CompletableFuture<RaftServer> bootstrap(Collection<MemberId> cluster)
      Description copied from interface: RaftServer
      Bootstraps the cluster using the provided cluster configuration.

      Bootstrapping the cluster results in a new cluster being formed with the provided configuration. The initial nodes in a cluster must always be bootstrapped. This is necessary to prevent split brain. If the provided configuration is empty, the local server will form a single-node cluster.

      Only RaftMember.Type.ACTIVE members can be included in a bootstrap configuration. If the local server is not initialized as an active member, it cannot be part of the bootstrap configuration for the cluster.

      When the cluster is bootstrapped, the local server will be transitioned into the active state and begin participating in the Raft consensus algorithm. When the cluster is first bootstrapped, no leader will exist. The bootstrapped members will elect a leader amongst themselves.

      It is critical that all servers in a bootstrap configuration be started with the same exact set of members. Bootstrapping multiple servers with different configurations may result in split brain.

      The CompletableFuture returned by this method will be completed once the cluster has been bootstrapped, a leader has been elected, and the leader has been notified of the local server's client configurations.

      Specified by:
      bootstrap in interface RaftServer
      Parameters:
      cluster - The bootstrap cluster configuration.
      Returns:
      A completable future to be completed once the cluster has been bootstrapped.
    • join

      Description copied from interface: RaftServer
      Starts this raft server by joining an existing replication group. A JoinRequest is sent to an arbitrary member of the cluster.
      Specified by:
      join in interface RaftServer
      Parameters:
      cluster - a list of member ids that are part of the cluster and assist in joining.
      Returns:
      A completable future to be completed once the server has joined the cluster.
    • leave

      public CompletableFuture<RaftServer> leave()
      Description copied from interface: RaftServer
      Requests to leave the replication group by sending a LeaveRequest to an arbitrary member of the cluster, as provided by the ClusterMembershipService.
      Specified by:
      leave in interface RaftServer
      Returns:
      A future to be completed successfully once the server has left the cluster.
    • promote

      public CompletableFuture<RaftServer> promote()
      Description copied from interface: RaftServer
      Promotes the server to leader if possible.
      Specified by:
      promote in interface RaftServer
      Returns:
      a future to be completed once the server has been promoted
    • forceConfigure

      public CompletableFuture<RaftServer> forceConfigure(Map<MemberId,RaftMember.Type> membersToRetain)
      Description copied from interface: RaftServer
      Force configure the partition to remove all members which are not part of the given membersToRetain.

      This method is typically called to remove a set of unreachable members when there is no leader.

      Specified by:
      forceConfigure in interface RaftServer
      Parameters:
      membersToRetain - The members to retain in the partition
      Returns:
      a future to be completed once the server has been force configured
    • reconfigurePriority

      public CompletableFuture<Void> reconfigurePriority(int newPriority)
      Description copied from interface: RaftServer
      Update priority of this server used for priority election. If priority election is not enabled, this method has no effect. To get the desired result, priority of all replicas must be updated accordingly. This method only updates the local server's priority.
      Specified by:
      reconfigurePriority in interface RaftServer
      Parameters:
      newPriority - the priority to be set
      Returns:
      a future to be completed when the new priority is applied
    • flushLog

      public CompletableFuture<Void> flushLog()
      Description copied from interface: RaftServer
      Ensures that all records written to the log are flushed to disk
      Specified by:
      flushLog in interface RaftServer
      Returns:
      a future which will be completed after the log is flushed to disk
    • shutdown

      public CompletableFuture<Void> shutdown()
      Shuts down the server without leaving the Raft cluster.
      Specified by:
      shutdown in interface RaftServer
      Returns:
      A completable future to be completed once the server has been shutdown.
    • getContext

      public RaftContext getContext()
      Description copied from interface: RaftServer
      Returns the current Raft context.
      Specified by:
      getContext in interface RaftServer
      Returns:
      the current Raft context
    • getTerm

      public long getTerm()
      Description copied from interface: RaftServer
      Returns the server's term.
      Specified by:
      getTerm in interface RaftServer
      Returns:
      the server's term
    • getRole

      public RaftServer.Role getRole()
      Description copied from interface: RaftServer
      Returns the server role.

      The initial state of a Raft server is RaftServer.Role.INACTIVE. Once the server is started and until it is explicitly shutdown, the server will be in one of the active states - RaftServer.Role.PASSIVE, RaftServer.Role.FOLLOWER, RaftServer.Role.CANDIDATE, or RaftServer.Role.LEADER.

      Specified by:
      getRole in interface RaftServer
      Returns:
      The server role.
    • isRunning

      public boolean isRunning()
      Returns a boolean indicating whether the server is running.
      Specified by:
      isRunning in interface RaftServer
      Returns:
      Indicates whether the server is running.
    • stepDown

      public CompletableFuture<Void> stepDown()
      Description copied from interface: RaftServer
      Steps down from the current leadership, which means tries to transition directly to follower.
      Specified by:
      stepDown in interface RaftServer