- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
FileBasedLog,InMemoryLog,LevelDBLog,LogCache
The interface for a persistent log. See doc/design/Log.txt for details.
- Since:
- 0.1
- Author:
- Bela Ban
-
Method Summary
Modifier and TypeMethodDescriptionlongappend(long index, LogEntries entries) Append the entries starting at index.longReturns the current commit index.commitIndex(long new_index) Sets commitIndex to a new valuelongReturns the current termcurrentTerm(long new_term) Sets the current termvoiddelete()Remove the persistent store, e.g.voiddeleteAllEntriesStartingFrom(long start_index) Delete all entries starting from start_index (including the entry at start_index).longReturns the index of the first log entryvoidforEach(ObjLongConsumer<LogEntry> function) Applies a function to all elements in range [first_appended .. last_appended]voidforEach(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 logvoidCalled after the instance has been createdlongReturns the index of the last append entry
This value is set byappend(long,LogEntries)voidreinitializeTo(long index, LogEntry entry) Clears all entries and sets first_appended/last_appended/commit_index to index and appends entry at index.voidStores a snapshot in the log.default longsize()The number of entries in the loglongvoidtruncate(long index_exclusive) Truncates the log up to (and excluding) index.booleanuseFsync()useFsync(boolean f) Do not cache a change (e.g.org.jgroups.AddressvotedFor()Returns the address of the candidate that this node voted for in the current termvotedFor(org.jgroups.Address member) Sets the address of the member this node voted for in the current term.
-
Method Details
-
init
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>.logargs- A hashmap of configuration information (impl-dependent) to configure itself. May be null- Throws:
Exception
-
useFsync
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
Remove the persistent store, e.g. DB table, or file- Throws:
Exception
-
currentTerm
long currentTerm()Returns the current term -
currentTerm
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
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
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 byappend(long,LogEntries) -
setSnapshot
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
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 appendedentries- The entries to append- Returns:
- long The index of the last appended entry
-
get
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
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 indexentry- 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
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 appliedstart_index- The start index. If smaller than first_appended, first_appended will be usedend_index- The end index. If greater than last_appended, last_appended will be used
-
forEach
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()
-