Class Role

java.lang.Object
io.krabka.streams.coordination.Role
All Implemented Interfaces:
Comparable<Role>

public final class Role extends Object implements Comparable<Role>
The validated name of one coordination role, such as the name of an active controller.

A role becomes a Kafka transactional.id. The transaction coordinator mints one producer epoch for that id, so the role names the fenced group of members that compete for the same leadership. All records of one role go to one partition of CoordinationCodec.TOPIC, and RolePartitioner computes that partition from the name.

of(String) is the only way to build a role, so every Role value is proof that the name is well formed. The factory rejects an empty name and a name longer than MAX_LENGTH bytes.

Example

Role controller = Role.of("controller");
producerSettings.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, controller.name());
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The maximum length of a role name, in bytes.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    Returns the UTF-8 bytes of the role name.
    int
    compareTo(Role other)
    Orders two roles by their names.
    boolean
    equals(Object other)
    Reports whether another object is a role with the same name.
    int
    Returns a hash of the role name.
    Returns the role name.
    static Role
    of(String name)
    Parses a role name.
    Returns the role name.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • MAX_LENGTH

      public static final int MAX_LENGTH
      The maximum length of a role name, in bytes.

      The bound is the bound Kafka puts on a topic name, so a role stays short enough to log and to compare.

      See Also:
  • Method Details

    • of

      public static Role of(String name)
      Parses a role name.
      Parameters:
      name - the role name, in UTF-8
      Returns:
      the validated role
      Throws:
      CoordinationException - if the name is empty or holds more than MAX_LENGTH bytes
      NullPointerException - if the name is null
    • name

      public String name()
      Returns the role name.
      Returns:
      the name this role carries
    • bytes

      public byte[] bytes()
      Returns the UTF-8 bytes of the role name.

      RolePartitioner hashes these bytes, and CoordinationCodec writes them into a key.

      Returns:
      a fresh array of the name bytes
    • compareTo

      public int compareTo(Role other)
      Orders two roles by their names.
      Specified by:
      compareTo in interface Comparable<Role>
      Parameters:
      other - the role to compare against
      Returns:
      a negative number, zero, or a positive number as this name sorts before, with, or after the other name
    • equals

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

      public int hashCode()
      Returns a hash of the role name.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code of the name
    • toString

      public String toString()
      Returns the role name.
      Overrides:
      toString in class Object
      Returns:
      the name this role carries