abstract class Raft::Log

Overview

Abstract base class for the Raft replicated log.

The log stores an ordered sequence of entries, persistent metadata (current term and voted-for), and snapshots. Entries are 1-indexed — index 0 means "no entry" (empty log).

Two implementations are provided:

log = Raft::Log::InMemory.new      # for testing
log = Raft::Log::File.new("/data") # for production

Direct Known Subclasses

Defined in:

raft/log.cr
raft/log/entry.cr

Instance Method Summary

Instance Method Detail

abstract def append(entries : Array(Entry)) : Nil #

Appends entries to the log with conflict detection.

If an existing entry at the same index has a different term, all entries from that index onward are truncated before appending. Entries with matching index and term are skipped (idempotent).


[View source]
abstract def close : Nil #

Releases any resources held by the log (e.g., file handles).


[View source]
abstract def get(index : UInt64) : Entry | Nil #

Returns the entry at the given 1-based index, or nil if not present.


[View source]
abstract def last_index : UInt64 #

Returns the index of the last entry, or 0 if the log is empty.


[View source]
abstract def last_term : UInt64 #

Returns the term of the last entry, or 0 if the log is empty.


[View source]
abstract def load_metadata : Metadata #

Loads the persisted metadata, or returns defaults (term 0, no vote).


[View source]
abstract def load_snapshot : Tuple(UInt64, UInt64, Bytes) | Nil #

Loads the most recent snapshot, or returns nil if none exists.

Returns a tuple of {last_included_index, last_included_term, snapshot_data}.


[View source]
abstract def save_metadata(meta : Metadata) : Nil #

Persists the node's current term and voted-for state.


[View source]
abstract def save_snapshot(last_index : UInt64, last_term : UInt64, data : Bytes) : Nil #

Saves a snapshot and compacts the log up to last_index.

All entries with index <= last_index are removed from the log.


[View source]
abstract def slice(from : UInt64, to : UInt64) : Array(Entry) #

Returns entries in the inclusive range [from, to].


[View source]
abstract def term_at(index : UInt64) : UInt64 | Nil #

Returns the term of the entry at index, or nil if not present.


[View source]
abstract def truncate_from(index : UInt64) : Nil #

Removes all entries at index and beyond (inclusive).


[View source]