Class RaftClusterContext

java.lang.Object
io.atomix.raft.cluster.impl.RaftClusterContext
All Implemented Interfaces:
RaftCluster, AutoCloseable

public final class RaftClusterContext extends Object implements RaftCluster, AutoCloseable
Manages the persistent state of the Raft cluster from the perspective of a single server.
  • Constructor Details

  • Method Details

    • getMember

      public DefaultRaftMember getMember(MemberId id)
      Description copied from interface: RaftCluster
      Returns a member by ID.

      The returned RaftMember is referenced by the unique RaftMember.memberId().

      Specified by:
      getMember in interface RaftCluster
      Parameters:
      id - The member ID.
      Returns:
      The member or null if no member with the given id exists.
    • bootstrap

      public CompletableFuture<Void> bootstrap(Collection<MemberId> cluster)
      Description copied from interface: RaftCluster
      Bootstraps the cluster.

      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 RaftCluster
      Parameters:
      cluster - The bootstrap cluster configuration.
      Returns:
      A completable future to be completed once the cluster has been bootstrapped.
    • getLeader

      public RaftMember getLeader()
      Description copied from interface: RaftCluster
      Returns the current cluster leader.

      If no leader has been elected for the current term, the leader will be null. Once a leader is elected, the leader must be known to the local server's configuration. If the returned RaftMember is null then that does not necessarily indicate that no leader yet exists for the current term, only that the local server has not learned of a valid leader for the term.

      Specified by:
      getLeader in interface RaftCluster
      Returns:
      The current cluster leader or null if no leader is known for the current term.
    • getLocalMember

      public RaftMember getLocalMember()
      Description copied from interface: RaftCluster
      Returns the local cluster member.
      Specified by:
      getLocalMember in interface RaftCluster
      Returns:
      The local cluster member.
    • getMembers

      public Collection<RaftMember> getMembers()
      Description copied from interface: RaftCluster
      Returns a collection of all cluster members.

      The returned members are representative of the last configuration known to the local server. Over time, the cluster configuration may change. In the event of a membership change, the returned Collection will not be modified, but instead a new collection will be created. Similarly, modifying the returned collection will have no impact on the cluster membership.

      Specified by:
      getMembers in interface RaftCluster
      Returns:
      A collection of all cluster members.
    • getTerm

      public long getTerm()
      Description copied from interface: RaftCluster
      Returns the current cluster term.

      The term is representative of the epoch determined by the underlying Raft consensus algorithm. The term is a monotonically increasing number used by Raft to represent a point in logical time. The term is guaranteed to be unique and monotonically increasing even across cluster restarts. Additionally, for any given term, Raft guarantees that only a single leader can be elected.

      Specified by:
      getTerm in interface RaftCluster
      Returns:
      The current cluster term.
    • getRemoteMember

      public DefaultRaftMember getRemoteMember(MemberId id)
      Returns a member by ID.
      Parameters:
      id - The member ID.
      Returns:
      The member.
    • getMemberState

      public RaftMemberContext getMemberState(MemberId id)
      Returns a member state by ID.
      Parameters:
      id - The member ID.
      Returns:
      The member state.
    • getActiveMemberStates

      public List<RaftMemberContext> getActiveMemberStates(Comparator<RaftMemberContext> comparator)
      Returns a list of active members.
      Parameters:
      comparator - A comparator with which to sort the members list.
      Returns:
      The sorted members list.
    • getActiveMemberStates

      public List<RaftMemberContext> getActiveMemberStates()
      Returns a list of active members.
      Returns:
      A list of active members.
    • getRemoteMemberStates

      public List<RaftMemberContext> getRemoteMemberStates(RaftMember.Type type)
      Returns a list of member states for the given type.
      Parameters:
      type - The member type.
      Returns:
      A list of member states for the given type.
    • reset

      public RaftClusterContext reset()
      Resets the cluster state to the persisted state.
      Returns:
      The cluster state.
    • configure

      public RaftClusterContext configure(Configuration configuration)
      Configures the cluster state.
      Parameters:
      configuration - The cluster configuration.
      Returns:
      The cluster state.
    • commit

      public RaftClusterContext commit()
      Commit the current configuration to disk.
      Returns:
      The cluster state.
    • close

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

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

      public Configuration getConfiguration()
      Returns the cluster configuration.
      Returns:
      The cluster configuration.
    • getContext

      public RaftContext getContext()
      Returns the parent context.
      Returns:
      The parent context.
    • getQuorum

      public int getQuorum()
      Returns the remote quorum count.
      Returns:
      The remote quorum count.
    • getRemoteMemberStates

      public List<RaftMemberContext> getRemoteMemberStates()
      Returns a list of all member states.
      Returns:
      A list of all member states.