Class FileBasedLog
java.lang.Object
org.jgroups.protocols.raft.FileBasedLog
- All Implemented Interfaces:
Closeable, AutoCloseable, Log
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongappend(long index, LogEntries entries) Appends entries starting at the given index and advancesLog.lastAppended()accordingly.voidclose()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.<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.longReturns the approximate size of the log data in bytes.longReturns the size of the snapshot.toString()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 value) 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.
-
Constructor Details
-
FileBasedLog
public FileBasedLog()
-
-
Method Details
-
init
Description copied from interface:LogInitializes this log with the given identity and optional configuration. Must be called exactly once before any other method.- Specified by:
initin interfaceLog- 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
-
useFsync
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
currentTerm
public long currentTerm()Description copied from interface:LogReturns the current Raft term.- Specified by:
currentTermin interfaceLog- Returns:
- the current term, or 0 if not yet set
-
currentTerm
Description copied from interface:LogPersists the current Raft term. The Raft protocol guarantees that terms are monotonically increasing.- Specified by:
currentTermin interfaceLog- Parameters:
new_term- the term to persist- Returns:
- this log
- Throws:
IOException- if the term cannot be persisted
-
votedFor
-
votedFor
Description copied from interface:LogPersists 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.- Specified by:
votedForin interfaceLog- Parameters:
member- the candidate address, ornullto clear the vote- Returns:
- this log
- Throws:
IOException- if the vote cannot be persisted
-
commitIndex
public long commitIndex()Description copied from interface:LogReturns the current commit index.- Specified by:
commitIndexin interfaceLog- Returns:
- the commit index, or 0 if no entries have been committed
-
commitIndex
Description copied from interface:LogPersists the commit index.- Specified by:
commitIndexin interfaceLog- Parameters:
new_index- the new commit index- Returns:
- this log
- Throws:
IOException- if the commit index cannot be persisted
-
firstAppended
public long firstAppended()Description copied from interface:LogReturns the index of the first entry in the log.- Specified by:
firstAppendedin interfaceLog- Returns:
- the first entry index, or 0 if the log is empty
-
lastAppended
public long lastAppended()Description copied from interface:LogReturns the index of the last appended entry.- Specified by:
lastAppendedin interfaceLog- Returns:
- the last appended index, or 0 if the log is empty
-
setSnapshot
Description copied from interface:LogStores a snapshot of the state machine, replacing any previously stored snapshot.- Specified by:
setSnapshotin interfaceLog- Parameters:
sn- the snapshot data- Throws:
IOException- if the snapshot cannot be stored
-
setSnapshot
Description copied from interface:LogStores a snapshot of the state machine, replacing any previously stored snapshot.- Specified by:
setSnapshotin interfaceLog- Parameters:
input- an input stream to read the snapshot data from- Throws:
IOException- if the snapshot cannot be stored
-
getSnapshot
Description copied from interface:LogReturns the most recently stored snapshot.- Specified by:
getSnapshotin interfaceLog- Returns:
- the snapshot data, or
nullif no snapshot has been stored - Throws:
IOException- if the snapshot cannot be read
-
snapshotSize
Description copied from interface:LogReturns the size of the snapshot.- Specified by:
snapshotSizein interfaceLog- Returns:
- returns the size of the stored snapshot.
- Throws:
IOException- if not possible to extract the snapshot size
-
readSnapshotRegion
public int readSnapshotRegion(long offset, byte[] dst, int dstOffset, int length) throws IOException Description copied from interface:LogReads up to length bytes from the snapshot data starting at offset.- Specified by:
readSnapshotRegionin interfaceLog- 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
Description copied from interface:LogAppends entries starting at the given index and advancesLog.lastAppended()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.- Specified by:
appendin interfaceLog- 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
Description copied from interface:LogReturns the entry at the given index.- Specified by:
getin interfaceLog- 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
Description copied from interface:LogRemoves all entries before the given index. After this call,Log.firstAppended()equalsindex_exclusive.If
index_exclusiveexceedsLog.commitIndex(), the commit index is used instead to prevent truncating committed entries.- Specified by:
truncatein interfaceLog- Parameters:
index_exclusive- entries strictly before this index are removed- Throws:
IOException- if the truncation cannot be completed
-
reinitializeTo
Description copied from interface:LogResets the log to contain only the given entry at the given index. SetsLog.firstAppended(),Log.lastAppended(), andLog.commitIndex()toindex. The next entry will be appended atindex + 1.Typically called after installing a snapshot received from the leader.
- Specified by:
reinitializeToin interfaceLog- Parameters:
index- the new starting indexentry- the single entry to store- Throws:
IOException- if the log cannot be reinitialized
-
deleteAllEntriesStartingFrom
Description copied from interface:LogDeletes all entries at and afterstart_index. UpdatesLog.lastAppended()andLog.currentTerm()to reflect the remaining log.- Specified by:
deleteAllEntriesStartingFromin interfaceLog- Parameters:
start_index- the first index to delete (inclusive)- Throws:
IOException- if the deletion cannot be completed
-
forEach
public void forEach(ObjLongConsumer<LogEntry> function, long start_index, long end_index) throws IOException Description copied from interface:LogApplies the function to each entry in the range [max(start_index, firstAppended)..min(end_index, lastAppended)], inclusive.- Specified by:
forEachin interfaceLog- Parameters:
function- invoked with each entry and its indexstart_index- the start of the range (inclusive), clamped toLog.firstAppended()end_index- the end of the range (inclusive), clamped toLog.lastAppended()- Throws:
IOException- if an entry cannot be read
-
forEach
Description copied from interface:LogApplies the function to every entry in the log. Equivalent toforEach(function, firstAppended(), lastAppended()).- Specified by:
forEachin interfaceLog- Parameters:
function- invoked with each entry and its index- Throws:
IOException- if an entry cannot be read
-
sizeInBytes
public long sizeInBytes()Description copied from interface:LogReturns the approximate size of the log data in bytes.- Specified by:
sizeInBytesin interfaceLog- Returns:
- the size in bytes
-
findCapability
Description copied from interface:LogLooks 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.- Specified by:
findCapabilityin interfaceLog- Type Parameters:
T- the capability type- Parameters:
capability- the capability interface to look up- Returns:
- the capability instance, or
nullif not available
-
toString
-