Class RoleState

java.lang.Object
io.krabka.streams.coordination.RoleState

public final class RoleState extends Object
The state that a reader folds out of the records of one role.

The roster is in offset order, which is registration order. The lease is the last lease record of the role, and it is absent when no member holds the role or when a tombstone cleared it.

Build a state with fromRecords(Role, Iterable), or fold records one at a time with RoleStateBuilder.

Example

RoleState state = RoleState.fromRecords(role, transport.readRoleRecords(role));
OptionalInt rank = state.rankOf(me);
Optional<MemberId> holder = state.holder();
  • Method Details

    • empty

      public static RoleState empty()
      Returns the state of a role that has no record.
      Returns:
      a state with an empty roster and no lease
    • fromRecords

      public static RoleState fromRecords(Role role, Iterable<CoordinationEntry> entries)
      Folds the records of one role into a role state.

      The caller reads the partition of the role in offset order and passes every record. The fold gives the same answer for another order, because it keeps the record of the highest offset for every key. It drops a record of another role, so a caller folds a partition that holds several roles and needs no filter of its own.

      Parameters:
      role - the role to collect
      entries - the decoded records of the partition
      Returns:
      the folded state of that role
      Throws:
      NullPointerException - if the role or the entries are null
    • roster

      public List<RosterEntry> roster()
      Returns the candidates of the role, in registration order.
      Returns:
      the roster, oldest registration first
    • lease

      public Optional<Lease> lease()
      Returns the lease of the role.
      Returns:
      the last lease record, and empty when no member holds the role
    • holder

      public Optional<MemberId> holder()
      Returns the member the lease names.

      The holder of an expired lease is still the holder. Ask LeaseTiming.liveAt(long) whether the lease is live.

      Returns:
      the holder, and empty when the role has no lease
    • entry

      public Optional<RosterEntry> entry(MemberId member)
      Returns the roster entry of one member.
      Parameters:
      member - the member to look up
      Returns:
      the entry, and empty when the member did not register
    • rankOf

      public OptionalInt rankOf(MemberId member)
      Returns the challenge rank of one member.

      The rank is the index of the member in the roster after the removal of the current holder, so the first standby takes rank 0. The holder itself keeps rank 0. A holder reaches the rank test only after its own lease expired, and it still owns the newest epoch at that point, so it reclaims the role for less churn than a failover costs.

      Parameters:
      member - the member to rank
      Returns:
      the rank, and empty when the member did not register
    • equals

      public boolean equals(Object other)
      Reports whether another object is a state with the same roster and lease.
      Overrides:
      equals in class Object
      Parameters:
      other - the object to compare against
      Returns:
      true when the other object carries this roster and this lease
    • hashCode

      public int hashCode()
      Returns a hash of the roster and the lease.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code of the two components
    • toString

      public String toString()
      Returns the roster and the lease.
      Overrides:
      toString in class Object
      Returns:
      a description of this state