RaftEx.Domain.Invariant (raft_ex v0.6.0)

View Source

Explicit persistence/recovery index invariants for RaftEx.

The five indices involved are:

  • :snapshot_index - highest index captured in the latest snapshot
  • :wal_persisted_index - highest index durably written to the WAL
  • :log_last_index - highest index present in the in-memory log
  • :commit_index - highest index known to be committed
  • :last_applied - highest index applied to the state machine

Invariants (all must hold):

  1. snapshot_index <= wal_persisted_index
  2. wal_persisted_index <= log_last_index
  3. commit_index <= log_last_index
  4. last_applied <= commit_index
  5. snapshot_index <= last_applied (when a snapshot exists, i.e. snapshot_index > 0)

Checked via check/1 (returns :ok or {:error, reason}) and check!/1 (raises RaftEx.Domain.Invariant.Violation when checks are enabled). Checks are gated by Application.get_env(:raft_ex, :invariant_checks) (:off by default, :test/:debug to enable).

Summary

Functions

Pure check: returns :ok or {:error, reason}. Enforces all five documented invariants including WAL<=log.

Raises on violation when checks are enabled; no-op when :off. Uses check/1.

Recovery-relevant check: WAL may legitimately be ahead of the in-memory log (e.g. after truncation of a conflicting suffix before WAL truncation), so only the ordering that must hold post-recovery is enforced.

Raises on violation when checks are enabled; no-op when :off. Uses check_recovery/1.

Convenience: build an indices map from a keyword list.

Returns the current check mode.

Types

indices()

@type indices() :: %{
  optional(:snapshot_index) => non_neg_integer(),
  optional(:wal_persisted_index) => non_neg_integer(),
  optional(:log_last_index) => non_neg_integer(),
  optional(:commit_index) => non_neg_integer(),
  optional(:last_applied) => non_neg_integer()
}

Functions

check(indices)

@spec check(indices()) :: :ok | {:error, term()}

Pure check: returns :ok or {:error, reason}. Enforces all five documented invariants including WAL<=log.

check!(indices)

@spec check!(indices()) :: :ok

Raises on violation when checks are enabled; no-op when :off. Uses check/1.

check_recovery(indices)

@spec check_recovery(indices()) :: :ok | {:error, term()}

Recovery-relevant check: WAL may legitimately be ahead of the in-memory log (e.g. after truncation of a conflicting suffix before WAL truncation), so only the ordering that must hold post-recovery is enforced.

check_recovery!(indices)

@spec check_recovery!(indices()) :: :ok

Raises on violation when checks are enabled; no-op when :off. Uses check_recovery/1.

from(kw)

@spec from(keyword()) :: indices()

Convenience: build an indices map from a keyword list.

mode()

@spec mode() :: :off | :test | :debug

Returns the current check mode.