RaftEx.Infra.Persistence.LogWal (raft_ex v0.3.1)

View Source

Write-ahead log process for RaftEx.

Accepts batched append requests from server processes and writes them durably before acknowledging. The WAL provides:

  • Batched writes: Multiple entries are grouped and written together
  • Configurable fsync: Supports :datasync, :fsync, or :none
  • Recovery: Replays WAL entries on startup
  • Truncation: Removes entries up to a given index after snapshotting
  • Size limits: Rotates when WAL exceeds configured size

Architecture

A single WAL process serves every server in a system, but each server (identified by its :uid) owns its own WAL file, its own buffer and its own index range. Servers may not share a WAL file: entries are only indexed by their per-server log index, so interleaving two servers in one file would make recovery impossible.

Files live at <wal_data_dir>/<uid>/wal.dat, which is exactly where RaftEx.Infra.Persistence.Log looks when it recovers on startup.

The WAL maintains an in-memory buffer of pending entries per file. When a write request arrives, entries are added to the buffer. A flush is triggered when:

  • The buffer reaches wal_max_batch_size
  • A synchronous write is requested
  • A periodic timer fires

Entries are written to a WAL file with a simple binary format:

<<magic::32, version::8, entry_count::32, entries::binary, crc::32>>

Integration

Started by RaftEx.Infra.Persistence.LogWalSupervisor as part of the system supervision tree. The Log module sends {:append, uid, entries} messages and receives {:wal_written, first_index, last_index} notifications.

Summary

Functions

Append entries to a server's WAL.

Asynchronously append entries to a server's WAL.

Returns a specification to start this module under a supervisor.

Get a server's current WAL state.

Get WAL overview for metrics and monitoring.

Read entries from a server's WAL buffer (in-memory only).

Recover entries from a server's WAL file on startup.

Start the WAL GenServer.

Truncate a server's WAL, removing all entries up to and including index.

Types

file_state()

@type file_state() :: %{
  path: Path.t(),
  fd: :file.io_device() | nil,
  buffer: [
    {RaftEx.Domain.Types.index(), RaftEx.Domain.Types.term_num(), term()}
  ],
  pending_writes: [{pid(), reference()}],
  first_index: RaftEx.Domain.Types.index(),
  last_index: RaftEx.Domain.Types.index(),
  file_size: non_neg_integer()
}

t()

@type t() :: %RaftEx.Infra.Persistence.LogWal{
  config: map(),
  files: %{optional(term()) => file_state()},
  flush_timer: reference() | nil
}

Functions

append(wal_name, uid, entries, timeout \\ 5000)

Append entries to a server's WAL.

Returns {:ok, first_index, last_index} when entries are durably written.

append_async(wal_name, uid, entries)

@spec append_async(
  atom(),
  term(),
  [{RaftEx.Domain.Types.index(), RaftEx.Domain.Types.term_num(), term()}]
) :: :ok

Asynchronously append entries to a server's WAL.

The caller will receive {:wal_written, first_index, last_index} when complete.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

info(wal_name, uid)

Get a server's current WAL state.

Returns {first_index, last_index, buffer_size}.

overview(wal_name)

@spec overview(atom()) :: map()

Get WAL overview for metrics and monitoring.

read(wal_name, uid, from_index, to_index)

Read entries from a server's WAL buffer (in-memory only).

Returns entries in the range [from_index, to_index].

recover(config)

Recover entries from a server's WAL file on startup.

config may be %{data_dir: dir} pointing at the server directory (as Log.init/1 does) or a full WAL config containing :dir/:data_dir plus a :uid.

Returns {first_index, last_index, entries}.

start_link(config)

@spec start_link(map()) :: GenServer.on_start()

Start the WAL GenServer.

Options

  • :names - Map of process names including :wal
  • :dir / :data_dir - Base directory; one wal.dat per server uid is kept under <dir>/<uid>/
  • :wal_max_size_bytes - Maximum WAL file size before rotation
  • :wal_max_batch_size - Maximum entries per batch write
  • :wal_sync_method - :datasync, :fsync, or :none
  • :wal_compute_checksums - Whether to compute CRC32 checksums

truncate(wal_name, uid, index, timeout \\ 5000)

@spec truncate(atom(), term(), RaftEx.Domain.Types.index(), timeout()) ::
  :ok | {:error, term()}

Truncate a server's WAL, removing all entries up to and including index.

Used after installing a snapshot to discard old entries.