Package io.aeron.cluster
Class RecordingLog
java.lang.Object
io.aeron.cluster.RecordingLog
- All Implemented Interfaces:
AutoCloseable
A log of recordings which make up the history of a Raft log across leadership terms. Entries are in order.
The log is made up of entries of leadership terms or snapshots to roll up state as of a log position within a leadership term.
The latest state is made up of the latest snapshot followed by any leadership log which follows. It is possible that a snapshot is taken midterm and therefore the latest state is the snapshot plus the log of messages which got appended to the log after the snapshot was taken.
Record layout as follows:
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Recording ID | | | +---------------------------------------------------------------+ | Leadership Term ID | | | +---------------------------------------------------------------+ | Log Position at beginning of term | | | +---------------------------------------------------------------+ | Log Position when entry was created or committed | | | +---------------------------------------------------------------+ | Timestamp when entry was created | | | +---------------------------------------------------------------+ | Service ID when a Snapshot | +---------------------------------------------------------------+ |R| Entry Type (Log or Snapshot) | +---------------------------------------------------------------+ | Archive Endpoint (length) | +---------------------------------------------------------------+ | Archive Endpoint (length) | | Archive Endpoint ... ... (variable) | +---------------------------------------------------------------+ | | | ... ... Repeats to the end of the log | | | +---------------------------------------------------------------+
The reserved bit on the entry type indicates whether the entry was marked invalid.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresentation of the entry in theRecordingLog.static final classRepresentation of a log entry in theRecordingLog.static final classThe snapshots and steps to recover the state of a cluster.static final classRepresentation of a snapshot entry in theRecordingLog. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe offset at which the endpoint of the remote snapshot is held.static final intThe flag used to determine if the entry has been marked with invalid.static final intThe offset at which the type of the entry is stored.static final intThe log entry is for a recording of a snapshot of state taken as of a position in the log.static final intThe log entry is for a recording of a snapshot of state taken as of a position in the log on another machine.static final intThe log entry is for a recording of messages within a leadership term to the log.static final intThe offset at which the leadership term id for the entry is stored.static final intThe offset at which the log position is stored.static final intMaximum possible entry length.static final intThe offset at which the recording id for the entry is stored.static final StringFilename for the history of leadership log terms and snapshot recordings.static final intThe offset at which the service id is recorded.static final intThe offset at which the log position as of the beginning of the term for the entry is stored.static final intThe offset at which the timestamp for the entry is stored. -
Constructor Summary
ConstructorsConstructorDescriptionRecordingLog(File parentDir, boolean createNew) Create a log that appends to an existing log or creates a new one. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendSnapshot(long recordingId, long leadershipTermId, long termBaseLogPosition, long logPosition, long timestamp, int serviceId) Append a log entry for a snapshot.voidappendStandbySnapshot(long recordingId, long leadershipTermId, long termBaseLogPosition, long logPosition, long timestamp, int serviceId, String archiveEndpoint) Append a log entry for a snapshot.voidappendTerm(long recordingId, long leadershipTermId, long termBaseLogPosition, long timestamp) Append a log entry for a leadership term.voidclose()voidcommitLogPosition(long leadershipTermId, long logPosition) Commit the log position reached in a leadership term.createRecoveryPlan(AeronArchive archive, int serviceCount, long replicatedRecordingId) Create a recovery plan for the cluster so that when the steps are replayed the plan will bring the cluster back to the latest stable state.entries()List of currently loaded entries.Find the last leadership term in the recording log.longFind the last recording id used for a leadership term.findTermEntry(long leadershipTermId) Find the termRecordingLog.Entryfor a given leadership term id.voidforce(int fileSyncLevel) Force the file to durable storage.getLatestSnapshot(int serviceId) Get the latest snapshotRecordingLog.Entryin the log.getTermEntry(long leadershipTermId) Get the termRecordingLog.Entryfor a given leadership term id.longgetTermTimestamp(long leadershipTermId) Get theRecordingLog.Entry.timestampfor a term.booleanInvalidate the last snapshot taken by the cluster so that on restart it can revert to the previous one.booleanisUnknown(long leadershipTermId) Is the given leadershipTermId unknown for the log?latestStandbySnapshots(int serviceCount) Return a collection of the most recent remote snapshots grouped by the archiveEndpoint where the snapshot is stored.intGet the next index to be used when appending an entry to the log.voidreload()Reload the recording log from disk.toString()
-
Field Details
-
RECORDING_LOG_FILE_NAME
Filename for the history of leadership log terms and snapshot recordings.- See Also:
-
ENTRY_TYPE_TERM
public static final int ENTRY_TYPE_TERMThe log entry is for a recording of messages within a leadership term to the log.- See Also:
-
ENTRY_TYPE_SNAPSHOT
public static final int ENTRY_TYPE_SNAPSHOTThe log entry is for a recording of a snapshot of state taken as of a position in the log.- See Also:
-
ENTRY_TYPE_STANDBY_SNAPSHOT
public static final int ENTRY_TYPE_STANDBY_SNAPSHOTThe log entry is for a recording of a snapshot of state taken as of a position in the log on another machine. Entries of this time should have an endpoint for an archive associated with them.- See Also:
-
ENTRY_TYPE_INVALID_FLAG
public static final int ENTRY_TYPE_INVALID_FLAGThe flag used to determine if the entry has been marked with invalid.- See Also:
-
RECORDING_ID_OFFSET
public static final int RECORDING_ID_OFFSETThe offset at which the recording id for the entry is stored.- See Also:
-
LEADERSHIP_TERM_ID_OFFSET
public static final int LEADERSHIP_TERM_ID_OFFSETThe offset at which the leadership term id for the entry is stored.- See Also:
-
TERM_BASE_LOG_POSITION_OFFSET
public static final int TERM_BASE_LOG_POSITION_OFFSETThe offset at which the log position as of the beginning of the term for the entry is stored.- See Also:
-
LOG_POSITION_OFFSET
public static final int LOG_POSITION_OFFSETThe offset at which the log position is stored.- See Also:
-
TIMESTAMP_OFFSET
public static final int TIMESTAMP_OFFSETThe offset at which the timestamp for the entry is stored.- See Also:
-
SERVICE_ID_OFFSET
public static final int SERVICE_ID_OFFSETThe offset at which the service id is recorded.- See Also:
-
ENTRY_TYPE_OFFSET
public static final int ENTRY_TYPE_OFFSETThe offset at which the type of the entry is stored.- See Also:
-
ENDPOINT_OFFSET
public static final int ENDPOINT_OFFSETThe offset at which the endpoint of the remote snapshot is held.- See Also:
-
MAX_ENTRY_LENGTH
public static final int MAX_ENTRY_LENGTHMaximum possible entry length. Include the entry plus a variable length endpoint string.- See Also:
-
-
Constructor Details
-
RecordingLog
Create a log that appends to an existing log or creates a new one.- Parameters:
parentDir- in which the log will be created.createNew- create a new recording log if one does not exist.
-
-
Method Details
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
force
public void force(int fileSyncLevel) Force the file to durable storage. Same as callingFileChannel.force(boolean)with true.- Parameters:
fileSyncLevel- as defined byConsensusModule.Configuration.FILE_SYNC_LEVEL_PROP_NAME.
-
entries
List of currently loaded entries.- Returns:
- the list of currently loaded entries.
-
nextEntryIndex
public int nextEntryIndex()Get the next index to be used when appending an entry to the log.- Returns:
- the next index to be used when appending an entry to the log.
-
reload
public void reload()Reload the recording log from disk. -
findLastTermRecordingId
public long findLastTermRecordingId()Find the last recording id used for a leadership term. If not found thenRecordingPos.NULL_RECORDING_ID.- Returns:
- the last leadership term recording id or
RecordingPos.NULL_RECORDING_IDif not found.
-
findLastTerm
Find the last leadership term in the recording log.- Returns:
- the last leadership term in the recording log.
-
getTermEntry
Get the termRecordingLog.Entryfor a given leadership term id.- Parameters:
leadershipTermId- to getRecordingLog.Entryfor.- Returns:
- the
RecordingLog.Entryif found or throwIllegalArgumentExceptionif no entry exists for term.
-
findTermEntry
Find the termRecordingLog.Entryfor a given leadership term id.- Parameters:
leadershipTermId- to getRecordingLog.Entryfor.- Returns:
- the
RecordingLog.Entryif found or null if not found.
-
getLatestSnapshot
Get the latest snapshotRecordingLog.Entryin the log.- Parameters:
serviceId- for the snapshot.- Returns:
- the latest snapshot
RecordingLog.Entryin the log or null if no snapshot exists.
-
invalidateLatestSnapshot
public boolean invalidateLatestSnapshot()Invalidate the last snapshot taken by the cluster so that on restart it can revert to the previous one.- Returns:
- true if the latest snapshot was found and marked as invalid, so it will not be reloaded.
-
getTermTimestamp
public long getTermTimestamp(long leadershipTermId) Get theRecordingLog.Entry.timestampfor a term.- Parameters:
leadershipTermId- to getRecordingLog.Entry.timestampfor.- Returns:
- the timestamp or
Aeron.NULL_VALUEif not found.
-
createRecoveryPlan
public RecordingLog.RecoveryPlan createRecoveryPlan(AeronArchive archive, int serviceCount, long replicatedRecordingId) Create a recovery plan for the cluster so that when the steps are replayed the plan will bring the cluster back to the latest stable state.- Parameters:
archive- to lookup recording descriptors.serviceCount- of services that may have snapshots.replicatedRecordingId- leader's recordingId used for replicating- Returns:
- a new
RecordingLog.RecoveryPlanfor the cluster.
-
isUnknown
public boolean isUnknown(long leadershipTermId) Is the given leadershipTermId unknown for the log?- Parameters:
leadershipTermId- to check.- Returns:
- true if term has not yet been appended otherwise false.
-
appendTerm
public void appendTerm(long recordingId, long leadershipTermId, long termBaseLogPosition, long timestamp) Append a log entry for a leadership term. Terms must be appended in ascending order.- Parameters:
recordingId- of the log in the archive.leadershipTermId- for the appended term.termBaseLogPosition- for the beginning of the term.timestamp- at the beginning of the appended term.
-
appendSnapshot
public void appendSnapshot(long recordingId, long leadershipTermId, long termBaseLogPosition, long logPosition, long timestamp, int serviceId) Append a log entry for a snapshot. Snapshots must be for the current term.- Parameters:
recordingId- in the archive for the snapshot.leadershipTermId- for the current termtermBaseLogPosition- at the beginning of the leadership term.logPosition- within the current term or accumulated length for the log.timestamp- at which the snapshot was taken.serviceId- for which the snapshot is recorded.
-
appendStandbySnapshot
public void appendStandbySnapshot(long recordingId, long leadershipTermId, long termBaseLogPosition, long logPosition, long timestamp, int serviceId, String archiveEndpoint) Append a log entry for a snapshot. Snapshots must be for the current term.- Parameters:
recordingId- in the archive for the snapshot.leadershipTermId- for the current termtermBaseLogPosition- at the beginning of the leadership term.logPosition- within the current term or accumulated length for the log.timestamp- at which the snapshot was taken.serviceId- for which the snapshot is recorded.archiveEndpoint- endpoint for the archive where
-
commitLogPosition
public void commitLogPosition(long leadershipTermId, long logPosition) Commit the log position reached in a leadership term.- Parameters:
leadershipTermId- for committing the term position reached.logPosition- reached in the leadership term.
-
latestStandbySnapshots
Return a collection of the most recent remote snapshots grouped by the archiveEndpoint where the snapshot is stored.- Parameters:
serviceCount- to ensure that we have a complete set of snapshots.- Returns:
- collection of snapshots.
-
toString
-