Slap.Cluster.Strategy behaviour (slap_cluster v0.4.0)

Copy Markdown View Source

Decides which node owns which shard.

A strategy is a process started under the cluster's supervisor, after the host. It starts and stops shards on this node only through Slap.Cluster.Host.start_shard/3 and stop_shard/2, and the host reports every shard that stops through handle_shard_down/3.

SlateDB's fencing keeps at most one effective writer per shard whatever the strategy does, so a strategy only has to be mostly right: two nodes that both open a shard cost errors and a restart, not data.

Summary

Callbacks

The strategy's child spec. opts includes :cluster.

Called by the host, from the host process, when a shard on this node stops: :fenced (another writer opened it), :crashed (it failed more often than its supervisor allows) or :stopped (stop_shard/2). Must not block; send a message to the strategy's process instead.

Where shard is owned, as far as the strategy knows. The result may be stale.

Refreshes the strategy's view of where shards are, after call/4 found it stale. Optional. It must return within timeout ms.

Validates strategy options before the cluster starts. Raise ArgumentError for invalid options. Optional for custom strategies.

Types

location()

@type location() :: {:local, pid()} | {:remote, node()}

reason()

@type reason() :: :fenced | :crashed | :stopped

shard()

@type shard() :: non_neg_integer()

Callbacks

child_spec(opts)

@callback child_spec(opts :: keyword()) :: Supervisor.child_spec()

The strategy's child spec. opts includes :cluster.

handle_shard_down(cluster, shard, reason)

@callback handle_shard_down(cluster :: module(), shard(), reason()) :: :ok

Called by the host, from the host process, when a shard on this node stops: :fenced (another writer opened it), :crashed (it failed more often than its supervisor allows) or :stopped (stop_shard/2). Must not block; send a message to the strategy's process instead.

lookup(cluster, shard)

@callback lookup(cluster :: module(), shard()) ::
  {:ok, location()} | {:error, :unassigned}

Where shard is owned, as far as the strategy knows. The result may be stale.

refresh(cluster, timeout)

(optional)
@callback refresh(cluster :: module(), timeout :: timeout()) :: :ok

Refreshes the strategy's view of where shards are, after call/4 found it stale. Optional. It must return within timeout ms.

validate_options(opts)

(optional)
@callback validate_options(opts :: keyword()) :: :ok

Validates strategy options before the cluster starts. Raise ArgumentError for invalid options. Optional for custom strategies.