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
      Initializes this log with the given identity and optional configuration. Must be called exactly once before any other method.
      Specified by:
      init in interface Log
      Parameters:
      log_name - a name used to derive the storage identity (file name, database table, etc.). Two logs initialized with the same name in the same environment share state.
      args - implementation-specific configuration parameters, may be null
      Throws:
      Exception - if initialization fails (missing directory, permission denied, corrupt storage, etc.)
    • useFsync

      public Log useFsync(boolean f)
      Description copied from interface: Log
      Controls whether mutating operations force data to stable storage (fsync) before returning.
      Specified by:
      useFsync in interface Log
      Parameters:
      f - true to force all writes to stable storage
      Returns:
      this log
    • useFsync

      public boolean useFsync()
      Description copied from interface: Log
      Returns whether writes are forced to stable storage before mutating operations return.
      Specified by:
      useFsync in interface Log
      Returns:
      true if fsync is enabled
    • close

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

      public long currentTerm()
      Description copied from interface: Log
      Returns the current Raft term.
      Specified by:
      currentTerm in interface Log
      Returns:
      the current term, or 0 if not yet set
    • currentTerm

      public Log currentTerm(long new_term)
      Description copied from interface: Log
      Persists the current Raft term. The Raft protocol guarantees that terms are monotonically increasing.
      Specified by:
      currentTerm in interface Log
      Parameters:
      new_term - the term to persist
      Returns:
      this log
    • votedFor

      public org.jgroups.Address votedFor()
      Description copied from interface: Log
      Returns the candidate this node voted for in the current term.
      Specified by:
      votedFor in interface Log
      Returns:
      the address of the voted-for candidate, or null if no vote has been cast in this term
    • votedFor

      public Log votedFor(org.jgroups.Address member)
      Description copied from interface: Log
      Persists the vote for the given candidate in the current term. The Raft protocol guarantees at most one vote per term. Passing null clears the vote, typically at term boundaries.
      Specified by:
      votedFor in interface Log
      Parameters:
      member - the candidate address, or null to clear the vote
      Returns:
      this log
    • commitIndex

      public long commitIndex()
      Description copied from interface: Log
      Returns the current commit index.
      Specified by:
      commitIndex in interface Log
      Returns:
      the commit index, or 0 if no entries have been committed
    • commitIndex

      public Log commitIndex(long new_index)
      Description copied from interface: Log
      Persists the commit index.
      Specified by:
      commitIndex in interface Log
      Parameters:
      new_index - the new commit index
      Returns:
      this log
    • firstAppended

      public long firstAppended()
      Description copied from interface: Log
      Returns the index of the first entry in the log.
      Specified by:
      firstAppended in interface Log
      Returns:
      the first entry index, or 0 if the log is empty
    • lastAppended

      public long lastAppended()
      Description copied from interface: Log
      Returns the index of the last appended entry.
      Specified by:
      lastAppended in interface Log
      Returns:
      the last appended index, or 0 if the log is empty
    • setSnapshot

      public void setSnapshot(ByteBuffer sn)
      Description copied from interface: Log
      Stores a snapshot of the state machine, replacing any previously stored snapshot.
      Specified by:
      setSnapshot in interface Log
      Parameters:
      sn - the snapshot data
    • setSnapshot

      public void setSnapshot(InputStream input) throws IOException
      Description copied from interface: Log
      Stores a snapshot of the state machine, replacing any previously stored snapshot.
      Specified by:
      setSnapshot in interface Log
      Parameters:
      input - an input stream to read the snapshot data from
      Throws:
      IOException - if the snapshot cannot be stored
    • getSnapshot

      public ByteBuffer getSnapshot()
      Description copied from interface: Log
      Returns the most recently stored snapshot.
      Specified by:
      getSnapshot in interface Log
      Returns:
      the snapshot data, or null if no snapshot has been stored
    • snapshotSize

      public long snapshotSize()
      Description copied from interface: Log
      Returns the size of the snapshot.
      Specified by:
      snapshotSize in interface Log
      Returns:
      returns the size of the stored snapshot.
    • readSnapshotRegion

      public int readSnapshotRegion(long offset, byte[] dst, int dstOffset, int length)
      Description copied from interface: Log
      Reads up to length bytes from the snapshot data starting at offset.
      Specified by:
      readSnapshotRegion in interface Log
      Parameters:
      offset - offset to start reading from
      dst - buffer to store the read bytes
      dstOffset - offset to start writing the bytes at the destination buffer
      length - how many bytes to try to read
      Returns:
      the number of actual read bytes
    • append

      public long append(long index, LogEntries entries)
      Description copied from interface: Log
      Appends entries starting at the given index and advances Log.lastAppended() accordingly.

      On partial failure, lastAppended must reflect the last successfully written entry rather than reverting to the value before the call. For example, if lastAppended was 1 and 100 entries were submitted but the operation failed at entry 51, lastAppended must be 50 after the exception.

      Specified by:
      append in interface Log
      Parameters:
      index - the index at which to begin appending
      entries - the entries to append
      Returns:
      the index of the last appended entry
    • get

      public LogEntry get(long index)
      Description copied from interface: Log
      Returns the entry at the given index.
      Specified by:
      get in interface Log
      Parameters:
      index - the log index to retrieve
      Returns:
      the entry, or null if no entry exists at that index
    • truncate

      public void truncate(long index_exclusive)
      Description copied from interface: Log
      Removes all entries before the given index. After this call, Log.firstAppended() equals index_exclusive.

      If index_exclusive exceeds Log.commitIndex(), the commit index is used instead to prevent truncating committed entries.

      Specified by:
      truncate in interface Log
      Parameters:
      index_exclusive - entries strictly before this index are removed
    • reinitializeTo

      public void reinitializeTo(long index, LogEntry entry)
      Description copied from interface: Log
      Resets the log to contain only the given entry at the given index. Sets Log.firstAppended(), Log.lastAppended(), and Log.commitIndex() to index. The next entry will be appended at index + 1.

      Typically called after installing a snapshot received from the leader.

      Specified by:
      reinitializeTo in interface Log
      Parameters:
      index - the new starting index
      entry - the single entry to store
    • deleteAllEntriesStartingFrom

      public void deleteAllEntriesStartingFrom(long start_index)
      Description copied from interface: Log
      Deletes all entries at and after start_index. Updates Log.lastAppended() and Log.currentTerm() to reflect the remaining log.
      Specified by:
      deleteAllEntriesStartingFrom in interface Log
      Parameters:
      start_index - the first index to delete (inclusive)
    • forEach

      public void forEach(ObjLongConsumer<LogEntry> function, long start_index, long end_index)
      Description copied from interface: Log
      Applies the function to each entry in the range [max(start_index, firstAppended) .. min(end_index, lastAppended)], inclusive.
      Specified by:
      forEach in interface Log
      Parameters:
      function - invoked with each entry and its index
      start_index - the start of the range (inclusive), clamped to Log.firstAppended()
      end_index - the end of the range (inclusive), clamped to Log.lastAppended()
    • forEach

      public void forEach(ObjLongConsumer<LogEntry> function)
      Description copied from interface: Log
      Applies the function to every entry in the log. Equivalent to forEach(function, firstAppended(), lastAppended()).
      Specified by:
      forEach in interface Log
      Parameters:
      function - invoked with each entry and its index
    • sizeInBytes

      public long sizeInBytes()
      Description copied from interface: Log
      Returns the approximate size of the log data in bytes.
      Specified by:
      sizeInBytes in interface Log
      Returns:
      the size in bytes
    • toString

      public String toString()
      Overrides:
      toString in class Object