Class InMemoryLog
- All Implemented Interfaces:
Closeable, AutoCloseable, Log
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 Summary
Fields -
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.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 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.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Log
findCapability, size
-
Field Details
-
logs
-
name
-
voted_for
protected volatile org.jgroups.Address voted_for -
snapshot
-
-
Constructor Details
-
InMemoryLog
public InMemoryLog()
-
-
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
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
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
-
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. -
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
-
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
-
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
-
snapshotSize
public long snapshotSize()Description copied from interface:LogReturns the size of the snapshot.- Specified by:
snapshotSizein interfaceLog- Returns:
- returns the size of the stored snapshot.
-
readSnapshotRegion
public int readSnapshotRegion(long offset, byte[] dst, int dstOffset, int length) 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
-
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. -
get
-
truncate
public void truncate(long index_exclusive) 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. -
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
-
deleteAllEntriesStartingFrom
public void deleteAllEntriesStartingFrom(long start_index) 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)
-
forEach
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()
-
forEach
Description copied from interface:LogApplies the function to every entry in the log. Equivalent toforEach(function, firstAppended(), lastAppended()). -
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
-
toString
-