Interface ReplicatedLog
- All Known Implementing Classes:
AbstractReplicatedLog
@NonNullByDefault
public interface ReplicatedLog
Represents the ReplicatedLog that needs to be kept in sync by the RaftActor.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final record
A combination ofEntryMeta
and indicator of whether the entry is stable. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Appends an entry to the log if its index is already included in the log.boolean
appendReceived
(LogEntry entry, @Nullable Consumer<LogEntry> callback) Appends an entry received by a follower to the in-memory log and persists it as well, returning an indication whether or not a snapshot should be taken.boolean
appendSubmitted
(long index, long term, Payload command, Consumer<ReplicatedLogEntry> callback) Appends an entry submitted on the leader to the in-memory log and persists it as well.void
captureSnapshotIfReady
(EntryMeta lastEntry) Determines if a snapshot needs to be captured based on the count/memory consumed and initiates the capture.void
clear()
Deprecated, for removal: This API element is subject to removal in a future version.void
clear
(int startIndex, int endIndex) Clears the journal entries with startIndex (inclusive) and endIndex (exclusive).int
dataSize()
Returns the size of the data in the log (in bytes).entryAt
(long offset) Returns the entry and specified offset.long
Returns the journal index corresponding to the first entry in the journal.long
Returns the index of highest log entry known to be committed.getFrom
(long index) Returns a list of log entries starting from the given index to the end of the log.getFrom
(long index, int maxEntries, long maxDataSize) Returns a list of log entries starting from the given index up to the given maximum of entries or the given maximum accumulated size, whichever comes first.long
Returns index of highest log entry applied to state machine.default long
getLogEntryIndex
(long index) Returns the actual index of the entry in replicated log for the given index or -1 if not found.default long
getLogEntryOrSnapshotTerm
(long index) Returns the actual term of the entry in the replicated log for the given index or, if not present, returns the snapshot term if the given index is in the snapshot or -1 otherwise.default long
getLogEntryTerm
(long index) Returns the actual term of the entry in the replicated log for the given index or -1 if not found.long
Returns the index from which the snapshot was created,-1
otherwise.long
Returns the term of the index from which the snapshot was created, -1 otherwise.void
increaseJournalLogCapacity
(int amount) Optimization method to increase the capacity of the journal log prior to appending entries.boolean
isInSnapshot
(long index) Checks if the entry is present in a snapshot.default boolean
isLogEntryPresent
(long index) boolean
isPresent
(long index) Checks if the entry at the specified index is present or not.@Nullable LogEntry
last()
Return the last replicated log entry in the log or null of not found.long
Returns thejournalIndex
corresponding togetLastApplied()
.default long
Return the index of the last entry in the log or -1 if the log is empty.default @Nullable EntryMeta
lastMeta()
Return the last replicated log entry in the log or null of not found.default long
lastTerm()
Return the term of the last entry in the log or -1 if the log is empty.@Nullable LogEntry
lookup
(long index) Return the replicated log entry at the specified index.default @Nullable EntryMeta
lookupMeta
(long index) Return metadata about a replicated entry.@Nullable ReplicatedLog.StoredEntryMeta
lookupStoredMeta
(long index) ReturnReplicatedLog.StoredEntryMeta
a replicated entry.void
Mark the current valuegetLastApplied()
for recovery purposes.newCaptureSnapshot
(@Nullable EntryMeta lastLogEntry, long replicatedToAllIndex, boolean mandatoryTrim, boolean hasFollowers) Constructs aSnapshotManager.CaptureSnapshot
instance.void
resetToLog
(ReplicatedLog prev) Reset interal state to match specifiedReplicatedLog
.void
resetToSnapshot
(Snapshot snapshot) Reset internal state to specifiedSnapshot
.void
setCommitIndex
(long commitIndex) Sets the index of highest log entry known to be committed.void
setFirstJournalIndex
(long newFirstJournalIndex) Sets thefirstJournalIndex()
value.void
setLastApplied
(long lastApplied) Sets index of highest log entry applied to state machine.void
setSnapshotIndex
(long snapshotIndex) Sets the snapshot index in the replicated log.void
setSnapshotTerm
(long snapshotTerm) Sets snapshot term.boolean
shouldCaptureSnapshot
(long logIndex) Determines if a snapshot should be captured based on the count/memory consumed.long
size()
Returns the number of entries in the journal.default void
Sets the Replicated log to state after snapshot success.void
snapshotCommit
(boolean updateDataSize) Sets the Replicated log to state after snapshot success.void
snapshotPreCommit
(long snapshotCapturedIndex, long snapshotCapturedTerm) Handles all the bookkeeping in order to perform a rollback in the event of SaveSnapshotFailure.void
Restores the replicated log to a state in the event of a save snapshot failure.boolean
trimToReceive
(long nextIndex) Removes entries all entries from the log starting at the given index, resettinglastIndex()
tonextIndex - 1
.
-
Method Details
-
entryAt
Returns the entry and specified offset.- Parameters:
offset
- the offset- Returns:
- the
LogEntry
- Throws:
IndexOutOfBoundsException
- if the offset is out of range (offset < 0 || offset >= size()
)
-
lookup
Return the replicated log entry at the specified index.- Parameters:
index
- the index of the log entry- Returns:
- the ReplicatedLogEntry if found, otherwise null if the adjusted index less than 0 or greater than the size of the in-memory journal
-
lookupMeta
Return metadata about a replicated entry.- Parameters:
index
- the index of the log entry- Returns:
- the
EntryMeta
if found, otherwise null if the adjusted index less than 0 or greater than the size of the in-memory journal
-
lookupStoredMeta
ReturnReplicatedLog.StoredEntryMeta
a replicated entry.- Parameters:
index
- the index of the log entry- Returns:
- the
ReplicatedLog.StoredEntryMeta
if found, otherwise null if the adjusted index less than 0 or greater than the size of the in-memory journal
-
last
@Nullable LogEntry last()Return the last replicated log entry in the log or null of not found.- Returns:
- the last replicated log entry in the log or null of not found.
-
lastMeta
Return the last replicated log entry in the log or null of not found.- Returns:
- the last replicated log entry in the log or null of not found.
-
lastIndex
default long lastIndex()Return the index of the last entry in the log or -1 if the log is empty.- Returns:
- the index of the last entry in the log or -1 if the log is empty.
-
lastTerm
default long lastTerm()Return the term of the last entry in the log or -1 if the log is empty.- Returns:
- the term of the last entry in the log or -1 if the log is empty.
-
getCommitIndex
long getCommitIndex()Returns the index of highest log entry known to be committed.- Returns:
- index of highest log entry known to be committed.
-
setCommitIndex
void setCommitIndex(long commitIndex) Sets the index of highest log entry known to be committed.- Parameters:
commitIndex
- new commit index
-
getLastApplied
long getLastApplied()Returns index of highest log entry applied to state machine.- Returns:
- index of highest log entry applied to state machine.
-
setLastApplied
void setLastApplied(long lastApplied) Sets index of highest log entry applied to state machine.- Parameters:
lastApplied
- the new applied index.
-
markLastApplied
void markLastApplied()Mark the current valuegetLastApplied()
for recovery purposes. -
trimToReceive
boolean trimToReceive(long nextIndex) Removes entries all entries from the log starting at the given index, resettinglastIndex()
tonextIndex - 1
.- Parameters:
nextIndex
- the index of the first log entry to remove- Returns:
true
if the operation succeeds
-
append
Appends an entry to the log if its index is already included in the log.- Parameters:
entry
- the entry to append- Returns:
true
if the entry was successfully appended,false
otherwise.
-
increaseJournalLogCapacity
void increaseJournalLogCapacity(int amount) Optimization method to increase the capacity of the journal log prior to appending entries.- Parameters:
amount
- the amount to increase by
-
appendReceived
Appends an entry received by a follower to the in-memory log and persists it as well, returning an indication whether or not a snapshot should be taken.- Parameters:
entry
- the entry to appendcallback
- optional callback to be notified when persistence is complete- Returns:
true
if the journal requires trimming and a snapshot needs to be taken
-
appendSubmitted
boolean appendSubmitted(long index, long term, Payload command, Consumer<ReplicatedLogEntry> callback) Appends an entry submitted on the leader to the in-memory log and persists it as well.- Parameters:
index
- the indexterm
- the termcommand
- the commandcallback
- the callback to be notified when persistence is complete- Returns:
true
if the entry was successfully appended, false otherwise
-
getFrom
Returns a list of log entries starting from the given index to the end of the log.- Parameters:
index
- the index of the first log entry to get.- Returns:
- the List of entries
-
getFrom
Returns a list of log entries starting from the given index up to the given maximum of entries or the given maximum accumulated size, whichever comes first.- Parameters:
index
- the index of the first log entry to getmaxEntries
- the maximum number of entries to getmaxDataSize
- the maximum accumulated size of the log entries to get, negative means no limit- Returns:
- the List of entries meeting the criteria.
-
size
long size()Returns the number of entries in the journal.- Returns:
- the number of entries
-
isPresent
boolean isPresent(long index) Checks if the entry at the specified index is present or not.- Parameters:
index
- the index of the log entry- Returns:
- true if the entry is present in the in-memory journal
-
isInSnapshot
boolean isInSnapshot(long index) Checks if the entry is present in a snapshot.- Parameters:
index
- the index of the log entry- Returns:
- true if the entry is in the snapshot. false if the entry is not in the snapshot even if the entry may be present in the replicated log
-
firstJournalIndex
long firstJournalIndex()Returns the journal index corresponding to the first entry in the journal.- Returns:
- the journal index corresponding to the first entry in the journal
-
setFirstJournalIndex
void setFirstJournalIndex(long newFirstJournalIndex) Sets thefirstJournalIndex()
value.- Parameters:
newFirstJournalIndex
- the new value offirstJournalIndex()
.
-
lastAppliedJournalIndex
long lastAppliedJournalIndex()Returns thejournalIndex
corresponding togetLastApplied()
.- Returns:
- the
journalIndex
corresponding togetLastApplied()
-
getSnapshotIndex
long getSnapshotIndex()Returns the index from which the snapshot was created,-1
otherwise.- Returns:
- the index from which the snapshot was created,
-1
otherwise
-
getSnapshotTerm
long getSnapshotTerm()Returns the term of the index from which the snapshot was created, -1 otherwise.- Returns:
- the term of the index from which the snapshot was created, -1 otherwise
-
setSnapshotIndex
void setSnapshotIndex(long snapshotIndex) Sets the snapshot index in the replicated log.- Parameters:
snapshotIndex
- the index to set
-
setSnapshotTerm
void setSnapshotTerm(long snapshotTerm) Sets snapshot term.- Parameters:
snapshotTerm
- the term to set
-
getLogEntryIndex
default long getLogEntryIndex(long index) Returns the actual index of the entry in replicated log for the given index or -1 if not found.- Returns:
- the log entry index or -1 if not found
-
getLogEntryTerm
default long getLogEntryTerm(long index) Returns the actual term of the entry in the replicated log for the given index or -1 if not found.- Returns:
- the log entry term or -1 if not found
-
getLogEntryOrSnapshotTerm
default long getLogEntryOrSnapshotTerm(long index) Returns the actual term of the entry in the replicated log for the given index or, if not present, returns the snapshot term if the given index is in the snapshot or -1 otherwise.- Returns:
- the term or -1 otherwise
-
isLogEntryPresent
default boolean isLogEntryPresent(long index) -
clear
Deprecated, for removal: This API element is subject to removal in a future version.UseresetToSnapshot(Snapshot)
instead.Clears all entries. -
clear
void clear(int startIndex, int endIndex) Clears the journal entries with startIndex (inclusive) and endIndex (exclusive).- Parameters:
startIndex
- the start index (inclusive)endIndex
- the end index (exclusive)
-
snapshotPreCommit
void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm) Handles all the bookkeeping in order to perform a rollback in the event of SaveSnapshotFailure.- Parameters:
snapshotCapturedIndex
- the new snapshot indexsnapshotCapturedTerm
- the new snapshot term
-
snapshotCommit
default void snapshotCommit()Sets the Replicated log to state after snapshot success. This method is equivalent tosnapshotCommit(true)
. -
snapshotCommit
void snapshotCommit(boolean updateDataSize) Sets the Replicated log to state after snapshot success. Most users will want to usesnapshotCommit()
instead.- Parameters:
updateDataSize
- true ifdataSize()
should also be updated
-
snapshotRollback
void snapshotRollback()Restores the replicated log to a state in the event of a save snapshot failure. -
dataSize
int dataSize()Returns the size of the data in the log (in bytes).- Returns:
- the size of the data in the log (in bytes)
-
captureSnapshotIfReady
Determines if a snapshot needs to be captured based on the count/memory consumed and initiates the capture.- Parameters:
lastEntry
- the last log entry.
-
shouldCaptureSnapshot
boolean shouldCaptureSnapshot(long logIndex) Determines if a snapshot should be captured based on the count/memory consumed.- Parameters:
logIndex
- the log index to use to determine if the log count has exceeded the threshold- Returns:
- true if a snapshot should be captured, false otherwise
-
resetToLog
Reset interal state to match specifiedReplicatedLog
.- Parameters:
prev
- the log to reset to
-
resetToSnapshot
Reset internal state to specifiedSnapshot
.- Parameters:
snapshot
- snapshot to reset to
-
newCaptureSnapshot
SnapshotManager.CaptureSnapshot newCaptureSnapshot(@Nullable EntryMeta lastLogEntry, long replicatedToAllIndex, boolean mandatoryTrim, boolean hasFollowers) Constructs aSnapshotManager.CaptureSnapshot
instance.- Parameters:
lastLogEntry
- the last log entry for the snapshot.replicatedToAllIndex
- the index of the last entry replicated to all followers.- Returns:
- a new CaptureSnapshot instance.
-
resetToSnapshot(Snapshot)
instead.