Class LogCache

java.lang.Object
org.jgroups.raft.util.LogCache
All Implemented Interfaces:
Closeable, AutoCloseable, Log

public class LogCache extends Object implements Log
Bounded caching Log facade. Reads are returned from the cache (if available), writes invalidate the corresponding entries. Keeps the last N entries only (N is configurable).
Since:
1.0.8
Author:
Bela Ban
  • Field Details

    • log

      protected final Log log
    • cache

      protected ArrayRingBuffer<LogEntry> cache
    • max_size

      protected int max_size
    • current_term

      protected long current_term
    • commit_index

      protected long commit_index
    • first_appended

      protected long first_appended
    • last_appended

      protected long last_appended
    • voted_for

      protected org.jgroups.Address voted_for
    • num_trims

      protected int num_trims
    • num_hits

      protected int num_hits
    • num_misses

      protected int num_misses
  • Constructor Details

    • LogCache

      public LogCache(Log log)
    • LogCache

      public LogCache(Log log, int max_size)
  • Method Details

    • maxSize

      public int maxSize()
    • maxSize

      public Log maxSize(int s)
    • cacheSize

      public int cacheSize()
    • log

      public Log log()
    • numTrims

      public int numTrims()
    • numAccesses

      public int numAccesses()
    • hitRatio

      public double hitRatio()
    • resetStats

      public Log resetStats()
    • useFsync

      public Log useFsync(boolean f)
      Description copied from interface: Log
      Do not cache a change (e.g. AppendEntriesRequest, or setting the commit index), but force a write to disk (fsync), if true.
      Specified by:
      useFsync in interface Log
    • useFsync

      public boolean useFsync()
      Specified by:
      useFsync in interface Log
    • init

      public void init(String log_name, Map<String,String> args) throws Exception
      Description copied from interface: Log
      Called after the instance has been created
      Specified by:
      init in interface Log
      Parameters:
      log_name - The name of the log. Implementations can create a DB or file named after this, e.g. /tmp/<log_name></log_name>.log
      args - A hashmap of configuration information (impl-dependent) to configure itself. May be null
      Throws:
      Exception
    • delete

      public void delete() throws Exception
      Description copied from interface: Log
      Remove the persistent store, e.g. DB table, or file
      Specified by:
      delete in interface Log
      Throws:
      Exception
    • currentTerm

      public long currentTerm()
      Description copied from interface: Log
      Returns the current term
      Specified by:
      currentTerm in interface Log
    • currentTerm

      public Log currentTerm(long new_term)
      Description copied from interface: Log
      Sets the current term
      Specified by:
      currentTerm in interface Log
    • votedFor

      public org.jgroups.Address votedFor()
      Description copied from interface: Log
      Returns the address of the candidate that this node voted for in the current term
      Specified by:
      votedFor in interface Log
    • votedFor

      public Log votedFor(org.jgroups.Address member)
      Description copied from interface: Log
      Sets the address of the member this node voted for in the current term. Only invoked once per term
      Specified by:
      votedFor in interface Log
    • commitIndex

      public long commitIndex()
      Description copied from interface: Log
      Returns the current commit index. (May get removed as the RAFT paper has this as in-memory attribute)
      Specified by:
      commitIndex in interface Log
    • commitIndex

      public Log commitIndex(long new_index)
      Description copied from interface: Log
      Sets commitIndex to a new value
      Specified by:
      commitIndex in interface Log
      Parameters:
      new_index - The new index to set commitIndex to. May throw an exception if new_index > lastApplied()
      Returns:
      the log
    • firstAppended

      public long firstAppended()
      Description copied from interface: Log
      Returns the index of the first log entry
      Specified by:
      firstAppended in interface Log
    • lastAppended

      public long lastAppended()
      Description copied from interface: Log
      Returns the index of the last append entry
      This value is set by Log.append(long,LogEntries)
      Specified by:
      lastAppended in interface Log
    • setSnapshot

      public void setSnapshot(ByteBuffer sn)
      Description copied from interface: Log
      Stores a snapshot in the log.
      Specified by:
      setSnapshot in interface Log
      Parameters:
      sn - The snapshot data
    • getSnapshot

      public ByteBuffer getSnapshot()
      Description copied from interface: Log
      Gets the snapshot from the log
      Specified by:
      getSnapshot in interface Log
      Returns:
      The snapshot, or null if not existing
    • append

      public long append(long index, LogEntries entries)
      Description copied from interface: Log
      Append the entries starting at index. Advance last_appended by the number of entries appended.
      If the operation fails, then last_appended needs to be the index of the last successful append. E.g. if last_appended is 1, and we attempt to appened 100 entries, but fail at 51, then last_appended must be 50 (not 1!).
      Specified by:
      append in interface Log
      Parameters:
      index - The index at which to append the entries. Should be the same as lastAppended. LastAppended needs to be incremented by the number of entries appended
      entries - The entries to append
      Returns:
      long The index of the last appended entry
    • get

      public LogEntry get(long index)
      Description copied from interface: Log
      Gets the entry at start_index. Updates current_term and last_appended accordingly
      Specified by:
      get in interface Log
      Parameters:
      index - The index
      Returns:
      The LogEntry, or null if none is present at index.
    • truncate

      public void truncate(long index_exclusive)
      Description copied from interface: Log
      Truncates the log up to (and excluding) index. All entries < index are removed. First = index.
      Specified by:
      truncate in interface Log
      Parameters:
      index_exclusive - If greater than commit_index, commit_index will be used instead
    • reinitializeTo

      public void reinitializeTo(long index, LogEntry le) throws Exception
      Description copied from interface: Log
      Clears all entries and sets first_appended/last_appended/commit_index to index and appends entry at index. The next entry will be appended at last_appended+1.
      Use when a snapshot has been received by a follower, after setting the snapshot, to basically create a new log
      Specified by:
      reinitializeTo in interface Log
      Parameters:
      index - The new index
      le - The entry to append
      Throws:
      Exception - Thrown if this operation failed
    • deleteAllEntriesStartingFrom

      public void deleteAllEntriesStartingFrom(long start_index)
      Description copied from interface: Log
      Delete all entries starting from start_index (including the entry at start_index). Updates current_term and last_appended accordingly
      Specified by:
      deleteAllEntriesStartingFrom in interface Log
      Parameters:
      start_index -
    • forEach

      public void forEach(ObjLongConsumer<LogEntry> function, long start_index, long end_index)
      Description copied from interface: Log
      Applies function to all elements of the log in range [max(start_index,first_appended) .. min(last_appended,end_index)].
      Specified by:
      forEach in interface Log
      Parameters:
      function - The function to be applied
      start_index - The start index. If smaller than first_appended, first_appended will be used
      end_index - The end index. If greater than last_appended, last_appended will be used
    • forEach

      public void forEach(ObjLongConsumer<LogEntry> function)
      Description copied from interface: Log
      Applies a function to all elements in range [first_appended .. last_appended]
      Specified by:
      forEach in interface Log
    • sizeInBytes

      public long sizeInBytes()
      Specified by:
      sizeInBytes in interface Log
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • clear

      public LogCache clear()
    • trim

      public Log trim()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toStringDetails

      public String toStringDetails()