Slap.Cluster.Host (slap_cluster v0.4.0)

Copy Markdown View Source

Starts and stops the shards placed on this node, for the strategy.

Opens and closes run in tasks, so several run at once. If one of these tasks fails, the host stops: the shard may or may not be running, and the cluster's supervisor then stops every shard and starts over.

Each shard runs under its own supervisor: the process that owns the database, then the application's shard children. The host watches them and reports every shard that stops to the strategy's Slap.Cluster.Strategy.handle_shard_down/3: :fenced, :crashed or :stopped. It never restarts a shard by itself; that is the strategy's decision.

Summary

Functions

Returns a specification to start this module under a supervisor.

The shards running on this node.

start_shard/3 and stop_shard/2 without waiting: the request is sent from the calling process and added to requests (a :gen_server request id collection) under label; its reply is a message, for :gen_server.check_response/3. The host handles the requests of one process in the order they were sent, so a stop sent after a start is handled after it.

Opens shard n on this node and starts its children. Options: :generation, the strategy's lease generation (default nil).

Stops shard n on this node: its children first, then the database is closed, which flushes it. A shard that is still opening counts as stopping at once (Slap.Cluster.lookup/2 no longer finds it here), and is closed once it has opened. Returns :ok or {:error, :not_running}. Stops run concurrently.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

local_shards(cluster)

@spec local_shards(module()) :: [non_neg_integer()]

The shards running on this node.

send_start(cluster, n, opts, label, requests)

start_shard/3 and stop_shard/2 without waiting: the request is sent from the calling process and added to requests (a :gen_server request id collection) under label; its reply is a message, for :gen_server.check_response/3. The host handles the requests of one process in the order they were sent, so a stop sent after a start is handled after it.

send_stop(cluster, n, label, requests)

See send_start/5.

start_shard(cluster, n, opts \\ [])

@spec start_shard(module(), non_neg_integer(), keyword()) ::
  {:ok, pid()} | {:error, term()}

Opens shard n on this node and starts its children. Options: :generation, the strategy's lease generation (default nil).

Returns {:ok, pid}, {:error, :already_started}, {:error, :stopping} (it is still closing), {:error, :fenced} (another node opened it while it was opening here), {:error, :stopped} (stop_shard/2 was called while it was opening; it is closed, and was never :running since) or {:error, reason} (for example when the database cannot be opened). Opens run concurrently.

stop_shard(cluster, n)

@spec stop_shard(module(), non_neg_integer()) :: :ok | {:error, :not_running}

Stops shard n on this node: its children first, then the database is closed, which flushes it. A shard that is still opening counts as stopping at once (Slap.Cluster.lookup/2 no longer finds it here), and is closed once it has opened. Returns :ok or {:error, :not_running}. Stops run concurrently.