Interface ReplicatedLog
- All Known Implementing Classes:
AbstractReplicatedLogImpl
public interface ReplicatedLog
Represents the ReplicatedLog that needs to be kept in sync by the RaftActor.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionboolean
append
(ReplicatedLogEntry replicatedLogEntry) Appends an entry to the log.boolean
appendAndPersist
(@NonNull ReplicatedLogEntry replicatedLogEntry, @Nullable Consumer<ReplicatedLogEntry> callback, boolean doAsync) Appends an entry to the in-memory log and persists it as well.void
captureSnapshotIfReady
(ReplicatedLogEntry replicatedLogEntry) Determines if a snapshot needs to be captured based on the count/memory consumed and initiates the capture.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).@Nullable ReplicatedLogEntry
get
(long index) Return the replicated log entry at the specified index.@NonNull List<ReplicatedLogEntry>
getFrom
(long index) Returns a list of log entries starting from the given index to the end of the log.@NonNull List<ReplicatedLogEntry>
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 the index of the snapshot.long
Returns the term of the snapshot.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.boolean
isPresent
(long index) Checks if the entry at the specified index is present or not.@Nullable ReplicatedLogEntry
last()
Return the last replicated log entry in the log or null of not found.long
Return the index of the last entry in the log or -1 if the log is empty.long
lastTerm()
Return the term of the last entry in the log or -1 if the log is empty.long
removeFrom
(long index) Removes entries from the in-memory log starting at the given index.boolean
removeFromAndPersist
(long index) Removes entries from the in-memory log and the persisted log starting at the given index.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.
-
Field Details
-
NO_MAX_SIZE
static final long NO_MAX_SIZE- See Also:
-
-
Method Details
-
get
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
-
last
@Nullable ReplicatedLogEntry 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.
-
lastIndex
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
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.
-
removeFrom
long removeFrom(long index) Removes entries from the in-memory log starting at the given index.- Parameters:
index
- the index of the first log entry to remove- Returns:
- the adjusted index of the first log entry removed or -1 if the log entry is not found.
-
removeFromAndPersist
boolean removeFromAndPersist(long index) Removes entries from the in-memory log and the persisted log starting at the given index.The persisted information would then be used during recovery to properly reconstruct the state of the in-memory replicated log
- Parameters:
index
- the index of the first log entry to remove- Returns:
- true if entries were removed, false otherwise
-
append
Appends an entry to the log.- Parameters:
replicatedLogEntry
- the entry to append- Returns:
- true if the entry was successfully appended, false otherwise. An entry can fail to append if the index is already included in the log.
-
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
-
appendAndPersist
boolean appendAndPersist(@NonNull ReplicatedLogEntry replicatedLogEntry, @Nullable Consumer<ReplicatedLogEntry> callback, boolean doAsync) Appends an entry to the in-memory log and persists it as well.- Parameters:
replicatedLogEntry
- the entry to appendcallback
- the callback to be notified when persistence is complete (optional).doAsync
- if true, the persistent actor can receive subsequent messages to process in between the persist call and the execution of the associated callback. If false, subsequent messages are stashed and get delivered after persistence is complete and the associated callback is executed. In either case the callback is guaranteed to execute in the context of the actor associated with this log.- 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- 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
-
getSnapshotIndex
long getSnapshotIndex()Returns the index of the snapshot.- Returns:
- the index from which the snapshot was created. -1 otherwise.
-
getSnapshotTerm
long getSnapshotTerm()Returns the term of the snapshot.- 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
-
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:
replicatedLogEntry
- 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
-