Class ClusterMember

java.lang.Object
io.aeron.cluster.ClusterMember

public final class ClusterMember extends Object
Represents a member of the cluster that participates in consensus for storing state from the perspective of any single member. It is not a global view of the cluster, perspectives only exist from a vantage point.
  • Constructor Details

    • ClusterMember

      public ClusterMember(int id, String ingressEndpoint, String consensusEndpoint, String logEndpoint, String catchupEndpoint, String archiveEndpoint, String endpoints)
      Construct a new member of the cluster.
      Parameters:
      id - unique id for the member.
      ingressEndpoint - address and port endpoint to which cluster clients send ingress.
      consensusEndpoint - address and port endpoint to which other cluster members connect.
      logEndpoint - address and port endpoint to which the log is replicated.
      catchupEndpoint - address and port endpoint to which a stream is replayed for catchup to the leader.
      archiveEndpoint - address and port endpoint to which the archive control channel can be reached.
      endpoints - comma separated list of endpoints.
    • ClusterMember

      public ClusterMember(int id, String ingressEndpoint, String consensusEndpoint, String logEndpoint, String catchupEndpoint, String archiveEndpoint, String archiveResponseEndpoint, String egressResponseEndpoint, String endpoints)
      Construct a new member of the cluster.
      Parameters:
      id - unique id for the member.
      ingressEndpoint - address and port endpoint to which cluster clients send ingress.
      consensusEndpoint - address and port endpoint to which other cluster members connect.
      logEndpoint - address and port endpoint to which the log is replicated.
      catchupEndpoint - address and port endpoint to which a stream is replayed for catchup to the leader.
      archiveEndpoint - address and port endpoint to which the archive control channel can be reached.
      archiveResponseEndpoint - address and port endpoint to which the archive control response channel can be reached.
      egressResponseEndpoint - address and port endpoint to which the egress response channel can be reached.
      endpoints - comma separated list of endpoints.
  • Method Details

    • reset

      public void reset()
      Reset the state of a cluster member, so it can be canvassed and reestablished.
    • isLeader

      public ClusterMember isLeader(boolean isLeader)
      Set if this member should be leader.
      Parameters:
      isLeader - value.
      Returns:
      this for a fluent API.
    • isLeader

      public boolean isLeader()
      Is this member currently the leader?
      Returns:
      true if this member is currently the leader otherwise false.
    • isBallotSent

      public ClusterMember isBallotSent(boolean isBallotSent)
      Is the ballot for the current election sent to this member?
      Parameters:
      isBallotSent - is the ballot for the current election sent to this member?
      Returns:
      this for a fluent API.
    • isBallotSent

      public boolean isBallotSent()
      Is the ballot for the current election sent to this member?
      Returns:
      true if the ballot has been sent for this member in the current election.
    • hasTerminated

      public ClusterMember hasTerminated(boolean hasTerminated)
      Set if this member has terminated.
      Parameters:
      hasTerminated - in notification to the leader.
      Returns:
      this for a fluent API.
    • hasTerminated

      public boolean hasTerminated()
      Has this member notified that it has terminated?
      Returns:
      has this member notified that it has terminated?
    • id

      public ClusterMember id(int id)
      Set the unique id for this member of the cluster.
      Parameters:
      id - for this member of the cluster.
      Returns:
      this for a fluent API.
    • id

      public int id()
      Unique identity for this member in the cluster.
      Returns:
      the unique identity for this member in the cluster.
    • vote

      public ClusterMember vote(Boolean vote)
      Set the result of the vote for this member. Boolean.TRUE means they voted for this member, Boolean.FALSE means they voted against this member, and null means no vote was received.
      Parameters:
      vote - for this member in the election.
      Returns:
      this for a fluent API.
    • vote

      public Boolean vote()
      The status of the vote for this member in an election. Boolean.TRUE means they voted for this member, Boolean.FALSE means they voted against this member, and null means no vote was received.
      Returns:
      the status of the vote for this member in an election.
    • leadershipTermId

      public ClusterMember leadershipTermId(long leadershipTermId)
      The leadership term reached for the cluster member.
      Parameters:
      leadershipTermId - leadership term reached for the cluster member.
      Returns:
      this for a fluent API.
    • leadershipTermId

      public long leadershipTermId()
      The leadership term reached for the cluster member.
      Returns:
      The leadership term reached for the cluster member.
    • logPosition

      public ClusterMember logPosition(long logPosition)
      The log position this member has persisted.
      Parameters:
      logPosition - this member has persisted.
      Returns:
      this for a fluent API.
    • logPosition

      public long logPosition()
      The log position this member has persisted.
      Returns:
      the log position this member has persisted.
    • candidateTermId

      public ClusterMember candidateTermId(long candidateTermId)
      The candidate term id used when voting.
      Parameters:
      candidateTermId - used when voting.
      Returns:
      this for a fluent API.
    • candidateTermId

      public long candidateTermId()
      The candidate term id used when voting.
      Returns:
      the candidate term id used when voting.
    • catchupReplaySessionId

      public ClusterMember catchupReplaySessionId(long replaySessionId)
      The session id for the replay when catching up to the leader.
      Parameters:
      replaySessionId - for the replay when catching up to the leader.
      Returns:
      this for a fluent API.
    • catchupReplaySessionId

      public long catchupReplaySessionId()
      The session id for the replay when catching up to the leader.
      Returns:
      the session id for the replay when catching up to the leader.
    • catchupReplayCorrelationId

      public ClusterMember catchupReplayCorrelationId(long correlationId)
      The correlation id for the replay when catching up to the leader.
      Parameters:
      correlationId - for the replay when catching up to the leader.
      Returns:
      this for a fluent API.
    • catchupReplayCorrelationId

      public long catchupReplayCorrelationId()
      The correlation id for the replay when catching up to the leader.
      Returns:
      the correlation id for the replay when catching up to the leader.
    • correlationId

      public ClusterMember correlationId(long correlationId)
      Correlation id assigned to the current action undertaken by the cluster member.
      Parameters:
      correlationId - assigned to the current action undertaken by the cluster member.
      Returns:
      this for a fluent API.
    • correlationId

      public long correlationId()
      Correlation id assigned to the current action undertaken by the cluster member.
      Returns:
      correlation id assigned to the current action undertaken by the cluster member.
    • timeOfLastAppendPositionNs

      public ClusterMember timeOfLastAppendPositionNs(long timeNs)
      Time (in ns) of last received appendPosition.
      Parameters:
      timeNs - of the last received appendPosition.
      Returns:
      this for a fluent API.
    • timeOfLastAppendPositionNs

      public long timeOfLastAppendPositionNs()
      Time (in ns) of last received appendPosition.
      Returns:
      time (in ns) of last received appendPosition or Aeron.NULL_VALUE if none received.
    • consensusEndpoint

      public String consensusEndpoint()
      The address:port endpoint for this cluster member that other members connect to for achieving consensus.
      Returns:
      the address:port endpoint for this cluster member that other members will connect to for consensus.
    • ingressEndpoint

      public String ingressEndpoint()
      The address:port endpoint for this cluster member that clients send ingress to.
      Returns:
      the address:port endpoint for this cluster member that listens for ingress.
    • logEndpoint

      public String logEndpoint()
      The address:port endpoint for this cluster member that the log is replicated to.
      Returns:
      the address:port endpoint for this cluster member that the log is replicated to.
    • catchupEndpoint

      public String catchupEndpoint()
      The address:port endpoint for this cluster member to which a stream is replayed for catchup to the leader.

      It is recommended a port of 0 is used, so it is system allocated to avoid potential clashes.

      Returns:
      the address:port endpoint for this cluster member to which a stream is replayed for catchup to the leader.
    • archiveEndpoint

      public String archiveEndpoint()
      The address:port endpoint for this cluster member that the archive can be reached.
      Returns:
      the address:port endpoint for this cluster member that the archive can be reached.
    • archiveResponseEndpoint

      public String archiveResponseEndpoint()
      The address:port endpoint for this cluster member to use on the archive to set up a response channel for control, replay and replication.
      Returns:
      the address:port endpoint for the archive response channel to be used by archive clients within the cluster.
    • egressResponseEndpoint

      public String egressResponseEndpoint()
      The address:port endpoint for the cluster member that will serve as the control address for the egress response channel that client can connect to.
      Returns:
      the endpoint used for the egress response channel.
    • endpoints

      public String endpoints()
      The string of endpoints for this member in a comma separated list in the same order they are parsed.
      Returns:
      list of endpoints for this member in a comma separated list.
      See Also:
    • publication

      public ExclusivePublication publication()
      The Publication used for send status updates to the member.
      Returns:
      Publication used for send status updates to the member.
    • publication

      public void publication(ExclusivePublication publication)
      Publication used for send status updates to the member.
      Parameters:
      publication - used for send status updates to the member.
    • closePublication

      public void closePublication(ErrorHandler errorHandler)
      Close consensus publication and null out reference.
      Parameters:
      errorHandler - to capture errors during close.
    • parse

      public static ClusterMember[] parse(String value)
      Parse the details for a cluster members from a string.

      member-id,ingress:port,consensus:port,log:port,catchup:port,archive:port|1,...

      Parameters:
      value - of the string to be parsed.
      Returns:
      An array of cluster members.
    • parseEndpoints

      public static ClusterMember parseEndpoints(int id, String endpoints)
      Parse a string containing the endpoints for a cluster node and passing to ClusterMember(int, String, String, String, String, String, String).
      Parameters:
      id - of the member node.
      endpoints - comma separated.
      Returns:
      the ClusterMember with the endpoints set.
    • encodeAsString

      public static String encodeAsString(ClusterMember[] clusterMembers)
      Encode member endpoints from a cluster members array to a String.
      Parameters:
      clusterMembers - to fill the details from.
      Returns:
      String representation suitable for use with parse(String).
    • encodeAsString

      public static String encodeAsString(List<ClusterMember> clusterMembers)
      Encode member endpoints from a cluster members List to a String.
      Parameters:
      clusterMembers - to fill the details from.
      Returns:
      String representation suitable for use with parse(String).
    • addConsensusPublications

      public static void addConsensusPublications(ClusterMember[] members, ClusterMember thisMember, String channelTemplate, int streamId, boolean bindConsensusControl, Aeron aeron, ErrorHandler errorHandler)
      Add the publications for sending consensus messages to the other members of the cluster.
      Parameters:
      members - of the cluster.
      thisMember - this member when adding publications.
      channelTemplate - for the publications.
      streamId - for the publications.
      bindConsensusControl - if the control endpoint should be bound for the publication.
      aeron - to add the publications to.
      errorHandler - to log registration exceptions to.
    • addConsensusPublication

      public static void addConsensusPublication(ClusterMember thisMember, ClusterMember otherMember, String channelTemplate, int streamId, boolean bindConsensusControl, Aeron aeron, ErrorHandler errorHandler)
      Add an exclusive Publication for communicating to a member on the consensus channel.
      Parameters:
      thisMember - from which the publication is addressed.
      otherMember - to which the publication is addressed.
      channelTemplate - for the target member.
      streamId - for the target member.
      bindConsensusControl - if the control endpoint should be bound for the publication.
      aeron - from which the publication will be created.
      errorHandler - to log registration exceptions to.
    • tryAddPublication

      public static void tryAddPublication(ClusterMember member, int streamId, Aeron aeron, ErrorHandler errorHandler)
      Try and add an exclusive Publication for communicating to a member on the consensus channel.
      Parameters:
      member - to which the publication is added.
      streamId - for the target member.
      aeron - from which the publication will be created.
      errorHandler - to log registration exceptions to.
    • closeConsensusPublications

      public static void closeConsensusPublications(ErrorHandler errorHandler, ClusterMember[] clusterMembers)
      Close the publications associated with members of the cluster used for the consensus protocol.
      Parameters:
      errorHandler - to capture errors during close.
      clusterMembers - to close the publications for.
    • addClusterMemberIds

      public static void addClusterMemberIds(ClusterMember[] clusterMembers, Int2ObjectHashMap<ClusterMember> clusterMemberByIdMap)
      Populate map of ClusterMembers which can be looked up by id.
      Parameters:
      clusterMembers - to populate the map.
      clusterMemberByIdMap - to be populated.
    • hasActiveQuorum

      public static boolean hasActiveQuorum(ClusterMember[] clusterMembers, long nowNs, long timeoutNs)
      Check if the cluster leader has an active quorum of cluster followers.
      Parameters:
      clusterMembers - for the current cluster.
      nowNs - for the current time.
      timeoutNs - after which a member is not considered active.
      Returns:
      true if quorum of cluster members are considered active.
    • quorumThreshold

      public static int quorumThreshold(int memberCount)
      The threshold of clusters members required to achieve quorum given a count of cluster members.
      Parameters:
      memberCount - for the cluster
      Returns:
      the threshold for achieving quorum.
    • quorumPosition

      public static long quorumPosition(ClusterMember[] members, long[] rankedPositions, long nowNs, long timeoutNs)
      Calculate the position reached by a quorum of cluster members.
      Parameters:
      members - of the cluster.
      rankedPositions - temp array to be used for sorting the positions to avoid allocation.
      nowNs - for the current time.
      timeoutNs - after which a member is not considered active.
      Returns:
      the position reached by a quorum of active cluster members.
    • hasQuorumAtPosition

      public static boolean hasQuorumAtPosition(ClusterMember[] clusterMembers, long leadershipTermId, long position, long nowNs, long timeoutNs)
      Has a quorum of members of appended a position to their local log.
      Parameters:
      clusterMembers - to check.
      leadershipTermId - expected of the members.
      position - to compare the logPosition() against.
      nowNs - for the current time.
      timeoutNs - after which a member is not considered active.
      Returns:
      true if a quorum of members reached this position otherwise false.
    • reset

      public static void reset(ClusterMember[] members)
      Reset the state of all cluster members.
      Parameters:
      members - to reset.
    • becomeCandidate

      public static void becomeCandidate(ClusterMember[] members, long candidateTermId, int candidateMemberId)
      Become a candidate by voting for yourself and resetting the other votes to Aeron.NULL_VALUE.
      Parameters:
      members - to reset the votes for.
      candidateTermId - for the candidacy.
      candidateMemberId - for the election.
    • isUnanimousLeader

      public static boolean isUnanimousLeader(ClusterMember[] clusterMembers, long candidateTermId, int gracefulClosedLeaderId)
      Is a member considered unanimously to be leader after voting.

      If a leader has been gracefully closed then it is not included in the membership for considering a unanimous position but will be considered in the membership for quorum.

      Note: all members are considered (i.e. no isActive(long, long) check), because this method is called during ElectionState.CANDIDATE_BALLOT phase before append position is notified from the followers.

      Parameters:
      clusterMembers - to check for votes.
      candidateTermId - for the vote.
      gracefulClosedLeaderId - id of a leader if gracefully closed otherwise Aeron.NULL_VALUE.
      Returns:
      true if all members voted positively.
    • isQuorumLeader

      public static boolean isQuorumLeader(ClusterMember[] clusterMembers, long candidateTermId)
      Is this member considered leader by a quorum of members by having positive votes being counted for a majority and no negative votes.

      Note: all members are considered (i.e. no isActive(long, long) check), because this method is called during ElectionState.CANDIDATE_BALLOT phase before append position is notified from the followers.

      Parameters:
      clusterMembers - to check for votes.
      candidateTermId - for the vote.
      Returns:
      true if sufficient positive votes being counted for a majority and no negative votes.
    • determineMember

      public static ClusterMember determineMember(ClusterMember[] clusterMembers, int memberId, String memberEndpoints)
      Determine which member of a cluster this is and check endpoints.
      Parameters:
      clusterMembers - for the current cluster which can be null.
      memberId - for this member.
      memberEndpoints - for this member.
      Returns:
      the ClusterMember determined.
    • validateMemberEndpoints

      public static void validateMemberEndpoints(ClusterMember member, String memberEndpoints)
      Check the member with the memberEndpoints.
      Parameters:
      member - to check memberEndpoints against.
      memberEndpoints - to check member against.
      See Also:
    • areSameEndpoints

      public static boolean areSameEndpoints(ClusterMember lhs, ClusterMember rhs)
      Are two cluster members using the same endpoints?
      Parameters:
      lhs - to compare for equality.
      rhs - to compare for equality.
      Returns:
      true if both are using the same endpoints or false if not.
    • isUnanimousCandidate

      public static boolean isUnanimousCandidate(ClusterMember[] clusterMembers, ClusterMember candidate, int gracefulClosedLeaderId)
      Is the member considered a candidate by a unanimous view to be a suitable candidate in an election.

      If a leader has been gracefully closed then it is not included in the membership for considering a unanimous position but will be considered in the membership for quorum.

      Parameters:
      clusterMembers - to compare the candidate against.
      candidate - for leadership.
      gracefulClosedLeaderId - id of a leader if gracefully closed otherwise Aeron.NULL_VALUE.
      Returns:
      true if the candidate is suitable otherwise false.
    • isQuorumCandidate

      public static boolean isQuorumCandidate(ClusterMember[] clusterMembers, ClusterMember candidate)
      Has the member achieved a quorum view to be a suitable candidate in an election.
      Parameters:
      clusterMembers - to compare the candidate against.
      candidate - for leadership.
      Returns:
      true if the candidate is suitable otherwise false.
    • compareLog

      public static int compareLog(long lhsLogLeadershipTermId, long lhsLogPosition, long rhsLogLeadershipTermId, long rhsLogPosition)
      The result is positive if lhs has the more recent log, zero if logs are equal, and negative if rhs has the more recent log.
      Parameters:
      lhsLogLeadershipTermId - term for which the position is most recent.
      lhsLogPosition - reached in the provided term.
      rhsLogLeadershipTermId - term for which the position is most recent.
      rhsLogPosition - reached in the provided term.
      Returns:
      positive if lhs has the more recent log, zero if logs are equal, and negative if rhs has the more recent log.
    • compareLog

      public static int compareLog(ClusterMember lhs, ClusterMember rhs)
      The result is positive if lhs has the more recent log, zero if logs are equal, and negative if rhs has the more recent log.
      Parameters:
      lhs - member to compare.
      rhs - member to compare.
      Returns:
      positive if lhs has the more recent log, zero if logs are equal, and negative if rhs has the more recent log.
    • findMember

      public static ClusterMember findMember(ClusterMember[] clusterMembers, int memberId)
      Find a ClusterMember with a given id.
      Parameters:
      clusterMembers - to search.
      memberId - to search for.
      Returns:
      the ClusterMember if found otherwise null.
    • ingressEndpoints

      public static String ingressEndpoints(ClusterMember[] members)
      Create a string of ingress endpoints by member id in format id=endpoint,id=endpoint, ....
      Parameters:
      members - for which the ingress endpoints string will be generated.
      Returns:
      a string of ingress endpoints by id.
    • setIsLeader

      public static void setIsLeader(ClusterMember[] clusterMembers, int leaderMemberId)
      Run through the list of cluster members and set the isLeader field based on the supplied leaderMemberId.
      Parameters:
      clusterMembers - list of cluster members.
      leaderMemberId - memberId of the current leader.
    • toString

      public String toString()
      Overrides:
      toString in class Object