Interface Log

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
FileBasedLog, InMemoryLog, LevelDBLog, LogCache

public interface Log extends Closeable
The interface for a persistent log. See doc/design/Log.txt for details.
Since:
0.1
Author:
Bela Ban
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    append(long index, LogEntries entries)
    Append the entries starting at index.
    long
    Returns the current commit index.
    commitIndex(long new_index)
    Sets commitIndex to a new value
    long
    Returns the current term
    currentTerm(long new_term)
    Sets the current term
    void
    Remove the persistent store, e.g.
    void
    deleteAllEntriesStartingFrom(long start_index)
    Delete all entries starting from start_index (including the entry at start_index).
    long
    Returns the index of the first log entry
    void
    Applies a function to all elements in range [first_appended .. last_appended]
    void
    forEach(ObjLongConsumer<LogEntry> function, long start_index, long end_index)
    Applies function to all elements of the log in range [max(start_index,first_appended) .. min(last_appended,end_index)].
    get(long index)
    Gets the entry at start_index.
    Gets the snapshot from the log
    void
    init(String log_name, Map<String,String> args)
    Called after the instance has been created
    long
    Returns the index of the last append entry
    This value is set by append(long,LogEntries)
    void
    reinitializeTo(long index, LogEntry entry)
    Clears all entries and sets first_appended/last_appended/commit_index to index and appends entry at index.
    void
    Stores a snapshot in the log.
    default long
    The number of entries in the log
    long
     
    void
    truncate(long index_exclusive)
    Truncates the log up to (and excluding) index.
    boolean
     
    useFsync(boolean f)
    Do not cache a change (e.g.
    org.jgroups.Address
    Returns the address of the candidate that this node voted for in the current term
    votedFor(org.jgroups.Address member)
    Sets the address of the member this node voted for in the current term.

    Methods inherited from interface java.io.Closeable

    close
  • Method Details

    • init

      void init(String log_name, Map<String,String> args) throws Exception
      Called after the instance has been created
      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

      Log useFsync(boolean f)
      Do not cache a change (e.g. AppendEntriesRequest, or setting the commit index), but force a write to disk (fsync), if true.
    • useFsync

      boolean useFsync()
    • delete

      void delete() throws Exception
      Remove the persistent store, e.g. DB table, or file
      Throws:
      Exception
    • currentTerm

      long currentTerm()
      Returns the current term
    • currentTerm

      Log currentTerm(long new_term)
      Sets the current term
    • votedFor

      org.jgroups.Address votedFor()
      Returns the address of the candidate that this node voted for in the current term
    • votedFor

      Log votedFor(org.jgroups.Address member)
      Sets the address of the member this node voted for in the current term. Only invoked once per term
    • commitIndex

      long commitIndex()
      Returns the current commit index. (May get removed as the RAFT paper has this as in-memory attribute)
    • commitIndex

      Log commitIndex(long new_index)
      Sets commitIndex to a new value
      Parameters:
      new_index - The new index to set commitIndex to. May throw an exception if new_index > lastApplied()
      Returns:
      the log
    • firstAppended

      long firstAppended()
      Returns the index of the first log entry
    • lastAppended

      long lastAppended()
      Returns the index of the last append entry
      This value is set by append(long,LogEntries)
    • setSnapshot

      void setSnapshot(ByteBuffer sn)
      Stores a snapshot in the log.
      Parameters:
      sn - The snapshot data
    • getSnapshot

      ByteBuffer getSnapshot()
      Gets the snapshot from the log
      Returns:
      The snapshot, or null if not existing
    • append

      long append(long index, LogEntries entries)
      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!).
      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

      LogEntry get(long index)
      Gets the entry at start_index. Updates current_term and last_appended accordingly
      Parameters:
      index - The index
      Returns:
      The LogEntry, or null if none is present at index.
    • truncate

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

      void reinitializeTo(long index, LogEntry entry) throws Exception
      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
      Parameters:
      index - The new index
      entry - The entry to append
      Throws:
      Exception - Thrown if this operation failed
    • deleteAllEntriesStartingFrom

      void deleteAllEntriesStartingFrom(long start_index)
      Delete all entries starting from start_index (including the entry at start_index). Updates current_term and last_appended accordingly
      Parameters:
      start_index -
    • forEach

      void forEach(ObjLongConsumer<LogEntry> function, long start_index, long end_index)
      Applies function to all elements of the log in range [max(start_index,first_appended) .. min(last_appended,end_index)].
      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

      void forEach(ObjLongConsumer<LogEntry> function)
      Applies a function to all elements in range [first_appended .. last_appended]
    • size

      default long size()
      The number of entries in the log
    • sizeInBytes

      long sizeInBytes()