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

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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.
    • join

      public CompletableFuture<Void> join(Collection<MemberId> cluster)
      Specified by:
      join in interface RaftCluster
    • 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.
    • 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.
    • getMemberContext

      public RaftMemberContext getMemberContext(MemberId id)
      Returns the context for a given member.
    • getQuorumFor

      public <T extends Comparable<T>> Optional<T> getQuorumFor(Function<RaftMemberContext,T> calculateMemberValue)
      Calculates the smallest value that is reported for a majority of this cluster, assuming that the local node always has the highest value.
      Parameters:
      calculateMemberValue - a function that calculates a value for a given member. Will be evaluated at least once for every remote member.
      Returns:
      empty when no remote members are present, otherwise the smallest value that is reported by enough remote members to form a quorum with the local member.
    • isSingleMemberCluster

      public boolean isSingleMemberCluster()
      Returns:
      true if the cluster has no remote active members and only the local member is active.
    • getVotingMembers

      public Set<RaftMember> getVotingMembers()
      Returns:
      A list remote members which participate in voting, i.e. are active.
    • getReplicationTargets

      public Set<RaftMemberContext> getReplicationTargets()
      Returns:
      A list of remote members that a leader should replicate to.
    • isMember

      public boolean isMember(MemberId memberId)
      Returns:
      true if the given member is part of the cluster, false otherwise
    • inJointConsensus

      public boolean inJointConsensus()
      Returns:
      true if the current configuration is a join consensus configuration.
    • getConfiguration

      public Configuration getConfiguration()
    • getContext

      public RaftContext getContext()
    • getVoteQuorum

      public VoteQuorum getVoteQuorum(Consumer<Boolean> callback)
    • reset

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

      public void configure(Configuration configuration)
      Configures the cluster state.
      Parameters:
      configuration - The cluster configuration.
    • commitCurrentConfiguration

      public void commitCurrentConfiguration()
      Commit the current configuration to disk.
    • close

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