TwoPhaseCommit (two_phase_commit v0.1.0) View Source

A simple state machine representing the state of a two-phase commit, as well as providing next steps to be taken in the case of a coordinator recovery after a crash.

Link to this section Summary

Functions

Records a participant's "abort" vote and moves the transaction to the rolling_back state.

Adds a participant to the transaction while it's in the interactive phase.

Notes that a participant has committed, moves to the committed state when all have committed.

Creates a new TwoPhaseCommit state structs with the given participants.

Indicates what actions need to be performed next in order to move the transaction forward.

Returns a list of all participants.

Moves the transaction to the voting phase.

Records a participant's "prepared" vote, moves to the committing phase when all are prepared.

Notes that a participant has rolled back, moves to the aborted state when all have rolled back.

Link to this section Types

Specs

client() :: any()

Specs

id() :: any()

Specs

next_action() ::
  :write_data | {:vote | :roll_back | :commit, participants()} | nil

Specs

opt() :: {:client, client()} | {:id, id()}

Specs

opts() :: [opt()]

Specs

participant() :: any()

Specs

participants() :: [participant()]

Specs

state() ::
  :interactive
  | {:voting | :rolling_back | :committing, MapSet.t(participant())}
  | :aborted
  | :committed

Specs

t() :: %TwoPhaseCommit{
  client: client(),
  id: id(),
  participants: MapSet.t(),
  state: state()
}

Link to this section Functions

Link to this function

aborted(two_phase_commit, participant)

View Source

Specs

aborted(t(), participant()) :: t() | {:error, :unknown_participant}

Records a participant's "abort" vote and moves the transaction to the rolling_back state.

Link to this function

add_participant(two_phase_commit, participant)

View Source

Specs

add_participant(t(), participant()) :: t()

Adds a participant to the transaction while it's in the interactive phase.

Link to this function

committed(two_phase_commit, participant)

View Source

Specs

committed(t(), participant()) :: t() | {:error, :unknown_participant}

Notes that a participant has committed, moves to the committed state when all have committed.

Link to this function

new(participants, opts \\ [])

View Source

Specs

new(participants(), opts()) :: t()

Creates a new TwoPhaseCommit state structs with the given participants.

The following keyword parameters can be provided:

:id - stores the transaction id (globally unique) :client - stores an arbitrary identifying the requesting entity (a pid, etc)

Link to this function

next_action(two_phase_commit)

View Source

Specs

next_action(t()) :: next_action()

Indicates what actions need to be performed next in order to move the transaction forward.

This is useful for the coordinator to determine where the transaction left off after recovering from a crash.

Link to this function

participants(two_phase_commit)

View Source

Specs

participants(t()) :: participants()

Returns a list of all participants.

Link to this function

prepare(two_phase_commit)

View Source

Specs

prepare(t()) :: t() | no_return()

Moves the transaction to the voting phase.

Link to this function

prepared(two_phase_commit, participant)

View Source

Specs

prepared(t(), participant()) :: t() | {:error, :unknown_participant}

Records a participant's "prepared" vote, moves to the committing phase when all are prepared.

Link to this function

rolled_back(two_phase_commit, participant)

View Source

Specs

rolled_back(t(), participant()) :: t() | {:error, :unknown_participant}

Notes that a participant has rolled back, moves to the aborted state when all have rolled back.