Interface Log
- All Superinterfaces:
AutoCloseable, Closeable
- All Known Implementing Classes:
FileBasedLog, InMemoryLog, LogCache
Implementations provide the persistence layer that backs the Raft consensus algorithm. Each Raft node owns exactly one
Log instance for the lifetime of the node. The log stores three categories of data:
- Entries: an ordered, 1-based sequence of
LogEntryrecords appended during replication. - Election metadata: the current term (
currentTerm(long)) and the candidate voted for in that term (votedFor(Address)). These values must survive restarts to preserve Raft's single-vote-per-term invariant. - Snapshot: a point-in-time capture of the state machine, used to compact the log.
Lifecycle
A Log instance must be initialized before any other method is called and
closed when no longer needed. After close(), behavior of all other methods is undefined.
Durability and failure
Mutating operations declare IOException to signal storage failures. When an IOException is thrown, the
operation did not complete and the log's state may be inconsistent. Callers must not assume partial progress unless a
method's contract explicitly defines it (see append(long, LogEntries)).
When fsync is enabled, each mutating operation guarantees that data has reached stable storage
before the call returns.
Thread safety
Implementations may assume single-writer semantics: at most one thread performs mutating operations at any given
time. However, read-only accessors (currentTerm(), votedFor(), commitIndex(),
firstAppended(), lastAppended()) must be safe to call concurrently from any thread while a mutating
operation is in progress.
Delegation
Implementations that wrap another Log (e.g., caching or adapter layers) should delegate findCapability(Class)
to the wrapped log so that the full delegation chain is discoverable.
- Since:
- 0.1
- Author:
- Bela Ban
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionlongappend(long index, LogEntries entries) Appends entries starting at the given index and advanceslastAppended()accordingly.longReturns the current commit index.commitIndex(long new_index) Persists the commit index.longReturns the current Raft term.currentTerm(long new_term) Persists the current Raft term.voiddeleteAllEntriesStartingFrom(long start_index) Deletes all entries at and afterstart_index.default <T extends LogCapability>
TfindCapability(Class<T> capability) Looks up an optional capability provided by this log or any log in the delegation chain.longReturns the index of the first entry in the log.voidforEach(ObjLongConsumer<LogEntry> function) Applies the function to every entry in the log.voidforEach(ObjLongConsumer<LogEntry> function, long start_index, long end_index) Applies the function to each entry in the range [max(start_index, firstAppended)..get(long index) Returns the entry at the given index.Returns the most recently stored snapshot.voidInitializes this log with the given identity and optional configuration.longReturns the index of the last appended entry.intreadSnapshotRegion(long offset, byte[] dst, int dstOffset, int length) Reads up to length bytes from the snapshot data starting at offset.voidreinitializeTo(long index, LogEntry entry) Resets the log to contain only the given entry at the given index.voidsetSnapshot(InputStream input) Stores a snapshot of the state machine, replacing any previously stored snapshot.voidStores a snapshot of the state machine, replacing any previously stored snapshot.default longsize()Returns the number of entries in the log.longReturns the approximate size of the log data in bytes.longReturns the size of the snapshot.voidtruncate(long index_exclusive) Removes all entries before the given index.booleanuseFsync()Returns whether writes are forced to stable storage before mutating operations return.useFsync(boolean f) Controls whether mutating operations force data to stable storage (fsync) before returning.org.jgroups.AddressvotedFor()Returns the candidate this node voted for in the current term.votedFor(org.jgroups.Address member) Persists the vote for the given candidate in the current term.
-
Method Details
-
init
Initializes this log with the given identity and optional configuration. Must be called exactly once before any other method.- 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 benull- Throws:
Exception- if initialization fails (missing directory, permission denied, corrupt storage, etc.)
-
useFsync
Controls whether mutating operations force data to stable storage (fsync) before returning.- Parameters:
f-trueto force all writes to stable storage- Returns:
- this log
-
useFsync
boolean useFsync()Returns whether writes are forced to stable storage before mutating operations return.- Returns:
trueif fsync is enabled
-
currentTerm
long currentTerm()Returns the current Raft term.- Returns:
- the current term, or 0 if not yet set
-
currentTerm
Persists the current Raft term. The Raft protocol guarantees that terms are monotonically increasing.- Parameters:
new_term- the term to persist- Returns:
- this log
- Throws:
IOException- if the term cannot be persisted
-
votedFor
org.jgroups.Address votedFor()Returns the candidate this node voted for in the current term.- Returns:
- the address of the voted-for candidate, or
nullif no vote has been cast in this term
-
votedFor
Persists the vote for the given candidate in the current term. The Raft protocol guarantees at most one vote per term. Passingnullclears the vote, typically at term boundaries.- Parameters:
member- the candidate address, ornullto clear the vote- Returns:
- this log
- Throws:
IOException- if the vote cannot be persisted
-
commitIndex
long commitIndex()Returns the current commit index.- Returns:
- the commit index, or 0 if no entries have been committed
-
commitIndex
Persists the commit index.- Parameters:
new_index- the new commit index- Returns:
- this log
- Throws:
IOException- if the commit index cannot be persisted
-
firstAppended
long firstAppended()Returns the index of the first entry in the log.- Returns:
- the first entry index, or 0 if the log is empty
-
lastAppended
long lastAppended()Returns the index of the last appended entry.- Returns:
- the last appended index, or 0 if the log is empty
-
setSnapshot
Stores a snapshot of the state machine, replacing any previously stored snapshot.- Parameters:
sn- the snapshot data- Throws:
IOException- if the snapshot cannot be stored
-
setSnapshot
Stores a snapshot of the state machine, replacing any previously stored snapshot.- Parameters:
input- an input stream to read the snapshot data from- Throws:
IOException- if the snapshot cannot be stored
-
getSnapshot
Returns the most recently stored snapshot.- Returns:
- the snapshot data, or
nullif no snapshot has been stored - Throws:
IOException- if the snapshot cannot be read
-
snapshotSize
Returns the size of the snapshot.- Returns:
- returns the size of the stored snapshot.
- Throws:
IOException- if not possible to extract the snapshot size
-
readSnapshotRegion
Reads up to length bytes from the snapshot data starting at offset.- Parameters:
offset- offset to start reading fromdst- buffer to store the read bytesdstOffset- offset to start writing the bytes at the destination bufferlength- how many bytes to try to read- Returns:
- the number of actual read bytes
- Throws:
IOException- if not possible to read the snapshot
-
append
Appends entries starting at the given index and advanceslastAppended()accordingly.On partial failure,
lastAppendedmust reflect the last successfully written entry rather than reverting to the value before the call. For example, iflastAppendedwas 1 and 100 entries were submitted but the operation failed at entry 51,lastAppendedmust be 50 after the exception.- Parameters:
index- the index at which to begin appendingentries- the entries to append- Returns:
- the index of the last appended entry
- Throws:
IOException- if one or more entries cannot be written to storage
-
get
Returns the entry at the given index.- Parameters:
index- the log index to retrieve- Returns:
- the entry, or
nullif no entry exists at that index - Throws:
IOException- if the entry cannot be read from storage
-
truncate
Removes all entries before the given index. After this call,firstAppended()equalsindex_exclusive.If
index_exclusiveexceedscommitIndex(), the commit index is used instead to prevent truncating committed entries.- Parameters:
index_exclusive- entries strictly before this index are removed- Throws:
IOException- if the truncation cannot be completed
-
reinitializeTo
Resets the log to contain only the given entry at the given index. SetsfirstAppended(),lastAppended(), andcommitIndex()toindex. The next entry will be appended atindex + 1.Typically called after installing a snapshot received from the leader.
- Parameters:
index- the new starting indexentry- the single entry to store- Throws:
IOException- if the log cannot be reinitialized
-
deleteAllEntriesStartingFrom
Deletes all entries at and afterstart_index. UpdateslastAppended()andcurrentTerm()to reflect the remaining log.- Parameters:
start_index- the first index to delete (inclusive)- Throws:
IOException- if the deletion cannot be completed
-
forEach
void forEach(ObjLongConsumer<LogEntry> function, long start_index, long end_index) throws IOException Applies the function to each entry in the range [max(start_index, firstAppended)..min(end_index, lastAppended)], inclusive.- Parameters:
function- invoked with each entry and its indexstart_index- the start of the range (inclusive), clamped tofirstAppended()end_index- the end of the range (inclusive), clamped tolastAppended()- Throws:
IOException- if an entry cannot be read
-
forEach
Applies the function to every entry in the log. Equivalent toforEach(function, firstAppended(), lastAppended()).- Parameters:
function- invoked with each entry and its index- Throws:
IOException- if an entry cannot be read
-
size
default long size()Returns the number of entries in the log.- Returns:
- the entry count
-
sizeInBytes
long sizeInBytes()Returns the approximate size of the log data in bytes.- Returns:
- the size in bytes
-
findCapability
Looks up an optional capability provided by this log or any log in the delegation chain.Implementations that wrap another
Logshould delegate to the wrapped log when they do not provide the requested capability themselves.- Type Parameters:
T- the capability type- Parameters:
capability- the capability interface to look up- Returns:
- the capability instance, or
nullif not available
-