Class Message

java.lang.Object
io.permazen.kv.raft.msg.Message
Direct Known Subclasses:
AppendRequest, AppendResponse, CommitRequest, CommitResponse, GrantVote, InstallSnapshot, PingRequest, PingResponse, RequestVote

public abstract class Message extends Object
Support superclass for Raft messages.
  • Constructor Details

    • Message

      protected Message(byte type, int clusterId, String senderId, String recipientId, long term)
    • Message

      protected Message(byte type, ByteBuffer buf, int version)
  • Method Details

    • getClusterId

      public int getClusterId()
      Get the cluster ID of the sender.
      Returns:
      sender's cluster ID
    • getSenderId

      public String getSenderId()
      Get the identity of the sender.
      Returns:
      sender's unique identity string
    • getRecipientId

      public String getRecipientId()
      Get the identity of the recipient.
      Returns:
      recipient's unique identity string
    • getTerm

      public long getTerm()
      Get the term of the sender of this message.
      Returns:
      requester's unique ID
    • isLeaderMessage

      public boolean isLeaderMessage()
      Determine whether this message is only sent by leaders.
      Returns:
      true if receipt of this message implies sender is a leader
    • visit

      public abstract void visit(MessageSwitch handler)
      Apply the visitor pattern based on this instance's type.
      Parameters:
      handler - target for visit
    • getCurrentProtocolVersion

      public static int getCurrentProtocolVersion()
      Get the current (i.e., maximum known) protocol version number.
      Returns:
      latest message protocol version
    • decodeProtocolVersion

      public static int decodeProtocolVersion(ByteBuffer buf)
      Read the protocol version number header from the message encoded in the given input.
      Parameters:
      buf - source for encoded message
      Returns:
      decoded message protocol version
    • decode

      public static Message decode(ByteBuffer buf, int version)
      Decode a message from the given input. The protocol version number header should already have been read.

      Note that data is not necessarily copied out of buf, so the returned instance may become invalid if the data in buf gets overwritten.

      Parameters:
      buf - source for encoded message
      version - message version
      Returns:
      decoded message
      Throws:
      BufferUnderflowException - if there is not enough data
      IllegalArgumentException - if version is bogus
      IllegalArgumentException - if encoded message is bogus
      IllegalArgumentException - if there is trailing garbage
    • encode

      public ByteBuffer encode(int version)
      Serialize this instance.
      Parameters:
      version - protocol encoding version number
      Returns:
      encoded message
      Throws:
      IllegalArgumentException - if version is bogus
    • writeTo

      public void writeTo(ByteBuffer buf, int version)
      Serialize this instance into the given buffer.
      Parameters:
      buf - destination for encoded data
      version - protocol encoding version number
      Throws:
      BufferOverflowException - if data overflows buf
      IllegalArgumentException - if version is bogus
    • calculateSize

      protected int calculateSize(int version)
      Calculate an upper bound on the number of bytes required by writeTo().
      Parameters:
      version - protocol encoding version
      Returns:
      an upper bound on the number of encoded bytes
    • toString

      public abstract String toString()
      Overrides:
      toString in class Object
    • putByteBuffer

      protected static void putByteBuffer(ByteBuffer dest, ByteBuffer buf)
      Serialize a ByteBuffer into the buffer.
      Parameters:
      dest - destination for encoded data
      buf - data to encode
      Throws:
      ReadOnlyBufferException - if dest is read only
      BufferOverflowException - if dest overflows
      IllegalArgumentException - if buf has more than 2^31 bytes remaining
      IllegalArgumentException - if either parameter is null
    • getByteBuffer

      protected static ByteBuffer getByteBuffer(ByteBuffer buf)
      Deserialize a ByteBuffer previously serialized by putByteBuffer() from the buffer.
      Parameters:
      buf - source for encoded data
      Returns:
      decoded data
      Throws:
      BufferUnderflowException - if buf underflows
      IllegalArgumentException - if input is bogus
      IllegalArgumentException - if buf is null
    • calculateSize

      protected static int calculateSize(ByteBuffer buf)
    • putString

      protected static void putString(ByteBuffer dest, String string)
      Serialize a String into the buffer.
      Parameters:
      dest - destination for encoded data
      string - string to encode
      Throws:
      ReadOnlyBufferException - if dest is read only
      BufferOverflowException - if dest overflows
      IllegalArgumentException - if either parameter is null
    • getString

      protected static String getString(ByteBuffer buf)
      Deserialize a String previously serialized by putString() from the buffer.
      Parameters:
      buf - source for encoded data
      Returns:
      decoded string, never null
      Throws:
      BufferUnderflowException - if buf underflows
      IllegalArgumentException - if input is bogus
    • calculateSize

      protected static int calculateSize(String string)
    • putBoolean

      protected static void putBoolean(ByteBuffer dest, boolean value)
      Serialize a boolean value into the buffer.
      Parameters:
      dest - destination for encoded data
      value - value to encode
      Throws:
      ReadOnlyBufferException - if dest is read only
      BufferOverflowException - if dest overflows
      IllegalArgumentException - if dest is null
    • getBoolean

      protected static boolean getBoolean(ByteBuffer buf)
      Deserialize a boolean value previously serialized by putBoolean() from the buffer.
      Parameters:
      buf - source for encoded data
      Returns:
      decoded value
      Throws:
      BufferUnderflowException - if buf underflows
      IllegalArgumentException - if input is bogus
    • putTimestamp

      protected static void putTimestamp(ByteBuffer dest, Timestamp timestamp, int version)
      Serialize a Timestamp value into the buffer.
      Parameters:
      dest - destination for encoded data
      timestamp - value to encode
      version - protocol encoding version
      Throws:
      ReadOnlyBufferException - if dest is read only
      BufferOverflowException - if dest overflows
      IllegalArgumentException - if dest or timestamp is null
    • getTimestamp

      protected static Timestamp getTimestamp(ByteBuffer buf, int version)
      Deserialize a Timestamp value previously serialized by putTimestamp() from the buffer.
      Parameters:
      buf - source for encoded data
      version - protocol encoding version
      Returns:
      decoded value
      Throws:
      BufferUnderflowException - if buf underflows
      IllegalArgumentException - if input is bogus
    • calculateSize

      protected static int calculateSize(Timestamp timestamp, int version)