RaftEx.LogSegmentWriter
(raft_ex v0.1.0)
View Source
Background process that manages immutable segment files for long-term log storage.
Segments are immutable files containing a range of log entries. Once a segment is sealed, it cannot be modified. This provides efficient sequential reads and enables compaction of old entries.
Segment File Format
Each segment file contains:
- Header: magic bytes, version, first_index, last_index, entry count
- Entries: serialized log entries with checksums
- Footer: checksum of the entire file
Lifecycle
- Active Segment: Receives new entries from WAL
- Sealed Segment: Closed when max size/entries reached
- Compacted: Merged with other segments during cleanup
Summary
Functions
Returns a specification to start this module under a supervisor.
Deletes segments up to the given index (used after snapshot).
Returns the first and last index across all segments.
Returns overview of segment state.
Reads entries from a segment by index range.
Reads a single entry by index.
Recovers segments from disk.
Seals the current active segment and starts a new one.
Starts the segment writer process.
Truncates segments from the given index onwards.
Writes entries to the current active segment or creates a new one.
Types
@type segment() :: %{ id: segment_id(), path: String.t(), range: segment_range(), entry_count: non_neg_integer(), size_bytes: non_neg_integer(), sealed: boolean(), fd: reference() | nil }
@type segment_id() :: non_neg_integer()
@type segment_range() :: {first_index :: non_neg_integer(), last_index :: non_neg_integer()}
@type t() :: %RaftEx.LogSegmentWriter{ active_segment: segment() | nil, config: map(), data_dir: String.t(), max_entries: non_neg_integer(), max_size_bytes: non_neg_integer(), segments: [segment()] }
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec delete_up_to(pid(), non_neg_integer()) :: :ok | {:error, term()}
Deletes segments up to the given index (used after snapshot).
@spec index_range(pid()) :: {non_neg_integer(), non_neg_integer()}
Returns the first and last index across all segments.
Returns overview of segment state.
@spec read(pid(), non_neg_integer(), non_neg_integer()) :: {:ok, [RaftEx.Types.log_entry()]} | {:error, term()}
Reads entries from a segment by index range.
@spec read_entry(pid(), non_neg_integer()) :: {:ok, RaftEx.Types.log_entry()} | {:error, :not_found}
Reads a single entry by index.
Recovers segments from disk.
Seals the current active segment and starts a new one.
@spec start_link(map()) :: GenServer.on_start()
Starts the segment writer process.
@spec truncate_from(pid(), non_neg_integer()) :: :ok | {:error, term()}
Truncates segments from the given index onwards.
@spec write(pid(), [RaftEx.Types.log_entry()]) :: :ok | {:error, term()}
Writes entries to the current active segment or creates a new one.