SuperCache.Cluster.TxnRegistry (SuperCache v1.3.0)

Copy Markdown View Source

In-memory transaction log for the three-phase commit protocol.

Each in-flight transaction is stored as a map entry keyed by its txn_id binary. The log is held in an ETS table owned by this GenServer so that:

  • Reads are lock-free (:public + :read_concurrency).
  • The table survives coordinator or participant crashes as long as the owning VM is alive (the ETS table is linked to this GenServer's lifetime, not to the caller's).
  • On node restart, SuperCache.Cluster.ThreePhaseCommit.recover/0 can iterate the table to resolve in-doubt transactions.

Transaction states

StateMeaning
:preparedPREPARE accepted; waiting for PRE_COMMIT or ABORT
:pre_committedPRE_COMMIT acknowledged; commit is safe even after crash
:committedCOMMIT applied; entry removed immediately after
:abortedABORT received; entry removed immediately after

Memory management

Committed and aborted entries are deleted synchronously by ThreePhaseCommit after each successful/failed transaction. Stale :prepared entries (e.g. from a coordinator crash before PRE_COMMIT) are cleaned up by recover/0 on the next startup.

Summary

Functions

Returns a specification to start this module under a supervisor.

Return the number of in-flight transactions (useful for tests).

Look up a transaction by id. Returns the entry map or nil.

Return all transaction entries as [{txn_id, entry}].

Transition txn_id to :pre_committed.

Register a new transaction in state :prepared.

Remove a transaction entry (called after commit or abort).

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

count()

@spec count() :: non_neg_integer()

Return the number of in-flight transactions (useful for tests).

get(txn_id)

@spec get(binary()) :: map() | nil

Look up a transaction by id. Returns the entry map or nil.

list_all()

@spec list_all() :: [{binary(), map()}]

Return all transaction entries as [{txn_id, entry}].

mark_pre_committed(txn_id)

@spec mark_pre_committed(binary()) :: :ok

Transition txn_id to :pre_committed.

register(txn_id, partition_idx, ops, replicas)

@spec register(binary(), non_neg_integer(), list(), [node()]) :: :ok

Register a new transaction in state :prepared.

replicas is the list of participant nodes (may be empty on participants themselves — they only need ops and partition_idx).

remove(txn_id)

@spec remove(binary()) :: :ok

Remove a transaction entry (called after commit or abort).

start_link(opts)