Class InMemoryLog

java.lang.Object
org.jgroups.protocols.raft.InMemoryLog
All Implemented Interfaces:
Closeable, AutoCloseable, Log

public class InMemoryLog extends Object implements Log
An in-memory Log implementation without any persistence.

The actual LogEntry are stored with a ArrayRingBuffer, resizing as necessary. The entries are only freed from memory on a Log.truncate(long) operation, so it needs a proper configuration to avoid OOM.

Warning: This implementation does not tolerate restarts, meaning all internal states will be lost. If data must survive restarts, use another implementation.

Since:
0.2
Author:
Bela Ban
See Also:
  • Field Details

    • logs

      public static final Map<String,Log> logs
    • name

      protected String name
    • voted_for

      protected volatile org.jgroups.Address voted_for
    • snapshot

      protected volatile ByteBuffer snapshot
  • Constructor Details

    • InMemoryLog

      public InMemoryLog()
  • Method Details

    • 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
    • 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
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • delete

      public void delete()
      Description copied from interface: Log
      Remove the persistent store, e.g. DB table, or file
      Specified by:
      delete in interface Log
    • 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 entry)
      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
      entry - The entry to append
    • 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
    • toString

      public String toString()
      Overrides:
      toString in class Object