SuperCache.Cluster.DistributedHelpers (SuperCache v1.7.0)

Copy Markdown View Source

Shared helpers for distributed read/write operations.

Extracts common patterns used across SuperCache.KeyValue, SuperCache.Queue, SuperCache.Stack, and SuperCache.Struct for:

Quorum reads

Quorum reads use SuperCache.Cluster.Quorum.await/4 with selective receive and a single deadline. As soon as a strict majority agrees on a result, remaining tasks are killed. This is more efficient than Task.async_stream, which always waits for all tasks to complete before computing consensus, and it counts nil as a valid value.

Summary

Functions

Applies a batch of write operations with the configured replication strategy.

Returns true when the cluster is running in distributed mode.

Returns true if the current node holds a replica (primary or secondary) for the given partition index.

Run a read function, converting a distributed read failure into {:error, exception} instead of raising.

Reads from the primary replica for the given partition.

Reads from all replicas and returns the result that achieves quorum (majority).

Routes a read operation according to the requested read mode.

Routes a write operation to the primary replica for the given partition.

Functions

apply_write(idx, partition, ops)

@spec apply_write(non_neg_integer(), term(), [
  {:put, term()}
  | {:delete, term()}
  | {:delete_match, term()}
  | {:delete_all, term()}
  | {:increment, term(), term(), term()}
]) :: :ok | {:error, SuperCache.Cluster.Error.t()}

Applies a batch of write operations with the configured replication strategy.

  • Strong replication (:strong): delegates to ThreePhaseCommit.commit/2 for atomic distributed commits via the WAL-based 3PC protocol.
  • Async / sync replication: applies each operation to local storage first, then replicates to replica nodes via Replicator.

Operations

Each operation is a tuple:

OperationDescription
{:put, record}Insert or update a record
{:delete, key}Delete a record by key
{:delete_match, pattern}Delete records matching a pattern
{:delete_all, _}Delete all records in the partition

Returns

  • :ok on success
  • {:error, reason} when 3PC commit fails (strong mode only)

distributed?()

@spec distributed?() :: boolean()

Returns true when the cluster is running in distributed mode.

Inlined by the compiler for hot-path performance.

has_partition?(partition_idx)

@spec has_partition?(non_neg_integer()) :: boolean()

Returns true if the current node holds a replica (primary or secondary) for the given partition index.

read_or_error(fun)

@spec read_or_error((-> term())) :: term() | {:error, SuperCache.Cluster.Error.t()}

Run a read function, converting a distributed read failure into {:error, exception} instead of raising.

Non-bang read APIs use this wrapper so a failed :primary or :quorum read with no usable local copy returns the documented error tuple. Local errors that are not distributed failures still propagate unchanged.

read_primary(caller, fun, args, partition_idx)

@spec read_primary(module(), atom(), [term()], non_neg_integer()) :: term()

Reads from the primary replica for the given partition.

If the current node is the primary, the function is applied locally. Otherwise an :erpc.call is made to the primary node. When the primary cannot be reached, the read falls back to the local replica (stale data allowed) with a warning rather than crashing the caller — reads follow eventual-consistency semantics.

read_quorum(caller, fun, args, partition_idx)

@spec read_quorum(module(), atom(), [term()], non_neg_integer()) :: term()

Reads from all replicas and returns the result that achieves quorum (majority).

Uses Quorum.await/4 with early termination: as soon as a strict majority of nodes agree on a result, remaining tasks are killed immediately. nil is a valid value and can win a quorum.

If no result reaches quorum, falls back to the primary node's result.

route_read(caller, fun, args, partition_idx, opts)

@spec route_read(module(), atom(), [term()], non_neg_integer(), keyword()) :: term()

Routes a read operation according to the requested read mode.

Read modes

  • :local — read from the local node. If the local node does not hold a replica, automatically escalates to :primary.
  • :primary — read from the primary replica.
  • :quorum — read from all replicas and return the majority result.

Parameters

  • caller — the module that owns the function being called
  • fun — the function name (atom)
  • args — the function arguments (list)
  • partition_idx — the partition index
  • opts — keyword list, supports :read_mode (default :local)

Returns

The return value of the called function.

route_write(caller, fun, args, partition_idx)

@spec route_write(module(), atom(), [term()], non_neg_integer()) :: term()

Routes a write operation to the primary replica for the given partition.

If the current node is the primary, the function is applied locally. Otherwise, an :erpc.call is made to the primary node.

Parameters

  • caller — the module that owns the function being called (replaces __MODULE__)
  • fun — the function name (atom)
  • args — the function arguments (list)
  • partition_idx — the partition index used to look up the primary node

Returns

The return value of the called function. Transport failures raise SuperCache.Cluster.Error rather than leaking a raw :erpc exit.