Class StateLog

  • Direct Known Subclasses:
    DiskStateLog, DurableStateLog

    public class StateLog
    extends java.lang.Object
    This classes serves as a log for the state associated with the last checkpoint, and the message batches received since the same checkpoint until the present. The state associated with the last checkpoint together with all the batches of messages received so far, comprises this replica current state
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected org.slf4j.Logger logger  
    • Constructor Summary

      Constructors 
      Constructor Description
      StateLog​(int id, byte[] initialState, byte[] initialHash)  
      StateLog​(int id, int k)
      Constructs a State log
      StateLog​(int id, int k, byte[] initialState, byte[] initialHash)
      Constructs a State log
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addMessageBatch​(byte[][] commands, MessageContext[] msgCtx, int lastConsensusId)
      Adds a message batch to the log.
      DefaultApplicationState getApplicationState​(int cid, boolean setState)
      Constructs a TransferableState using this log information
      int getLastCheckpointCID()
      Retrieves the consensus ID for the last checkpoint
      int getLastCID()
      Retrieves the consensus ID for the last messages batch delivered to the application
      java.util.TreeMap<java.lang.Integer,​TOMMessage> getLastReplies()  
      byte[] getLastRepliesHash()
      Retrieves the last replies hash
      CommandsInfo getMessageBatch​(int cid)
      Returns a batch of messages, given its correspondent consensus ID
      CommandsInfo[] getMessageBatches()
      Retrieves all the stored batches kept since the last checkpoint
      int getNumBatches()
      Retrieves the total number of stored batches kept since the last checkpoint
      byte[] getState()
      Retrieves the state associated with the last checkpoint
      byte[] getStateHash()
      Retrieves the hash of the state associated with the last checkpoint
      void newCheckpoint​(byte[] state, byte[] stateHash, int lastConsensusId)
      Sets the state associated with the last checkpoint, and updates the consensus ID associated with it
      void setLastCheckpointCID​(int lastCheckpointCID)
      Sets the consensus ID for the last checkpoint
      void setLastCID​(int lastCID)
      Sets the consensus ID for the last messages batch delivered to the application
      void setLastReplies​(java.util.TreeMap<java.lang.Integer,​TOMMessage> lastReplies)
      Retrieves the last replies sent to clients
      void setLastRepliesHash​(byte[] lastRepliesHash)
      Sets the last replies hash
      void update​(DefaultApplicationState transState)
      Updates this log, according to the information contained in the TransferableState object
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        protected org.slf4j.Logger logger
    • Constructor Detail

      • StateLog

        public StateLog​(int id,
                        int k,
                        byte[] initialState,
                        byte[] initialHash)
        Constructs a State log
        Parameters:
        id -
        k - The checkpoint period
        initialState -
        initialHash -
      • StateLog

        public StateLog​(int id,
                        int k)
        Constructs a State log
        Parameters:
        id -
        k - The checkpoint period
      • StateLog

        public StateLog​(int id,
                        byte[] initialState,
                        byte[] initialHash)
    • Method Detail

      • newCheckpoint

        public void newCheckpoint​(byte[] state,
                                  byte[] stateHash,
                                  int lastConsensusId)
        Sets the state associated with the last checkpoint, and updates the consensus ID associated with it
        Parameters:
        state - State associated with the last checkpoint
        stateHash -
        lastConsensusId -
      • setLastCheckpointCID

        public void setLastCheckpointCID​(int lastCheckpointCID)
        Sets the consensus ID for the last checkpoint
        Parameters:
        lastCheckpointCID - Consensus ID for the last checkpoint
      • getLastCheckpointCID

        public int getLastCheckpointCID()
        Retrieves the consensus ID for the last checkpoint
        Returns:
        Consensus ID for the last checkpoint, or -1 if none was obtained
      • setLastCID

        public void setLastCID​(int lastCID)
        Sets the consensus ID for the last messages batch delivered to the application
        Parameters:
        lastCID - the consensus ID for the last messages batch delivered to the application
      • getLastCID

        public int getLastCID()
        Retrieves the consensus ID for the last messages batch delivered to the application
        Returns:
        Consensus ID for the last messages batch delivered to the application
      • getState

        public byte[] getState()
        Retrieves the state associated with the last checkpoint
        Returns:
        State associated with the last checkpoint
      • getStateHash

        public byte[] getStateHash()
        Retrieves the hash of the state associated with the last checkpoint
        Returns:
        Hash of the state associated with the last checkpoint
      • addMessageBatch

        public void addMessageBatch​(byte[][] commands,
                                    MessageContext[] msgCtx,
                                    int lastConsensusId)
        Adds a message batch to the log. This batches should be added to the log in the same order in which they are delivered to the application. Only the 'k' batches received after the last checkpoint are supposed to be kept
        Parameters:
        commands - The batch of messages to be kept.
        msgCtx - The message contexts related to the commands
        lastConsensusId -
      • getLastReplies

        public java.util.TreeMap<java.lang.Integer,​TOMMessage> getLastReplies()
      • setLastReplies

        public void setLastReplies​(java.util.TreeMap<java.lang.Integer,​TOMMessage> lastReplies)
        Retrieves the last replies sent to clients
        Parameters:
        lastReplies - last replies to clients
      • getLastRepliesHash

        public byte[] getLastRepliesHash()
        Retrieves the last replies hash
      • setLastRepliesHash

        public void setLastRepliesHash​(byte[] lastRepliesHash)
        Sets the last replies hash
        Parameters:
        lastRepliesHash - last replies hash
      • getMessageBatch

        public CommandsInfo getMessageBatch​(int cid)
        Returns a batch of messages, given its correspondent consensus ID
        Parameters:
        cid - Consensus ID associated with the batch to be fetched
        Returns:
        The batch of messages associated with the batch correspondent consensus ID
      • getMessageBatches

        public CommandsInfo[] getMessageBatches()
        Retrieves all the stored batches kept since the last checkpoint
        Returns:
        All the stored batches kept since the last checkpoint
      • getNumBatches

        public int getNumBatches()
        Retrieves the total number of stored batches kept since the last checkpoint
        Returns:
        The total number of stored batches kept since the last checkpoint
      • getApplicationState

        public DefaultApplicationState getApplicationState​(int cid,
                                                           boolean setState)
        Constructs a TransferableState using this log information
        Parameters:
        cid - Consensus ID correspondent to desired state
        setState -
        Returns:
        TransferableState Object containing this log information
      • update

        public void update​(DefaultApplicationState transState)
        Updates this log, according to the information contained in the TransferableState object
        Parameters:
        transState - TransferableState object containing the information which is used to updated this log