SuperCache.Cluster.Quorum (SuperCache v1.7.0)

Copy Markdown View Source

Deadline-bound quorum collection for distributed reads and writes.

await/4 starts one task per {node, task_fun} entry under a dedicated Task.Supervisor, then uses selective receive to count results as they arrive. It returns as soon as required entries report the same value, or when the deadline expires.

Semantics

  • Every return value counts, including nil. nil is a valid cached value and can win a quorum.
  • Each node is counted once. Duplicate node entries are rejected before any task starts.
  • Early termination. Remaining tasks are killed and their messages are flushed when quorum is reached.
  • Deadline. Every remaining task is killed at the deadline; the caller never waits per task and never blocks past timeout_ms.
  • Crash isolation. Tasks run under Task.Supervisor.async_nolink/2, so a crashing task is reported as a failed vote and cannot crash the caller.
  • Clean mailbox. Result and monitor messages produced by this call are consumed before it returns.

Return values

{:ok, value, voters}
{:error, reason, results}

voters lists the nodes that reported the winning value, in arrival order. results maps each node that completed to {:ok, value} or {:error, reason}; nodes still running at the deadline appear as :timeout.

The main error reasons are :quorum_not_reached (all tasks finished without a majority), :timeout (the deadline expired), and {:duplicate_nodes, nodes}.

Example

entries = [
  {:primary@host, fn -> read(:primary) end},
  {:replica1@host, fn -> read(:replica1) end},
  {:replica2@host, fn -> read(:replica2) end}
]

Quorum.await(entries, 2, 1_000)
# => {:ok, [record], [:primary@host, :replica1@host]}

Summary

Functions

Collect a quorum from entries within timeout_ms milliseconds.

Types

entry()

@type entry() :: {node(), (-> term())}

result()

@type result() ::
  {:ok, term(), [node()]}
  | {:error, term(),
     %{required(node()) => {:ok, term()} | {:error, term()} | :timeout}}

Functions

await(entries, required, timeout_ms, opts \\ [])

@spec await([entry()], pos_integer(), non_neg_integer(), keyword()) :: result()

Collect a quorum from entries within timeout_ms milliseconds.

entries must be unique {node, task_fun} pairs. required is the number of matching votes needed for success. opts supports :pool, the Task.Supervisor name; it defaults to the application-managed SuperCache.Cluster.Quorum.Pool.