# `Alternera.PluginHosts.Cluster`
[🔗](https://github.com/10e-ab/alternera-camel/blob/v0.9.0/alternera_plugin_hosts/lib/alternera/plugin_hosts/cluster.ex#L1)

A plugin host on some nodes of a cluster. A node runs the hosts its own
configuration lists (`PluginHosts`); no node is named anywhere. A host
is known by its key, `{tenant_id, name}` (`PluginHosts.key/2`): host
names are per tenant, so two tenants' hosts of one name are two hosts.
Three `:pg` groups of its kind's scope (`scope/1`) say where a host is:

  * `{:host, key}`: the pool's manager joins it for as long as the pool
    lives (`announce/3`), so the group is the nodes that configure the
    host, whether a member runs there or not (`min: 0`);
  * `{:pool, key}`: each ready member joins it, and leaves when it stops
    being ready or dies, so the group is the cluster's ready members;
  * `{:outage, key}`: the manager joins it while no member of its pool
    can serve and none is on its way (`outage/4`).

## The directory

This process monitors the scope. When a manager on another node joins
`{:host, key}`, it asks that node what the host is
(`PluginHosts.shared_entry/2`: name, protocol, tenant, written-route
rules, reload; never the entry's command, paths or credentials) and
writes the answer to an ETS table, by key and node; when it leaves (its
pool stopped), the row goes. A node that does not answer is asked again
2 seconds later. So reading what other nodes configure (`entry/2`,
`keys/1`, `nodes/3`) is an ETS lookup in the caller, with no call, and
the table holds only hosts some other node configures now.

Nodes may still describe one host differently (two configurations out
of step during a rollout): `entry/2` is the first node's by node name,
and `nodes/3` picks, when asked, among those whose description has the
protocol wanted.

## Forwarding

A node without a pool of a host forwards to one that configures it, with
one `:erpc` call (`forward/5`): a step's call, a written document's
check, the editor's catalogue and Try. `nodes/3` gives the nodes with a
ready member first, in random order; then those that run none, in an
order that is the same on every node for a host (by a hash of host and
node), so that while no node has a member ready the calls of the whole
cluster start one on one node, not one on each; then the nodes in an
outage, in random order. It tries them in that order, so a node that
must start a member is asked only when no node has one ready, and a node
in an outage only when no other can take the call. A node that cannot be
reached or fails the call is logged and passed over for the next. A node
that does not answer in time ends it with `:timeout`, since the call may
have run. The receiving node waits for its start within the call's own
deadline, as for a call of its own, so the forward allows that deadline
plus a hop: a start slower than the deadline is that node's refusal
(`:no_ready_host`, `:not_ready`), not a `:timeout` here.

# `announce`

```elixir
@spec announce(atom() | nil, term(), pid()) :: :ok
```

Joins `pid`, the manager of a pool of host `key`, to `{:host, key}`:
this node configures the host. It leaves the group when it dies. A no-op
without the scope.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `entry`

```elixir
@spec entry(module(), term()) :: map() | nil
```

`kind`'s host `key` as the first other node by name describes it, or nil
when no other node configures it.

# `forward`

```elixir
@spec forward(
  [node()],
  String.t(),
  atom() | {module(), atom()},
  list(),
  non_neg_integer()
) ::
  {:ok, term()} | {:error, :no_ready_host | :timeout | :remote_failed}
```

Runs `Alternera.PluginHosts.<fun>(args)`, or `module.fun(args)` when
`fun` is `{module, fun}`, on one of `nodes` (see `nodes/3`; `label`
names the host in the log), within
`timeout_ms` plus a hop. `{:ok, result}`; `{:error, :no_ready_host}` when
there is no node; `{:error, :timeout}` when the node did not answer in
time; `{:error, :remote_failed}` when every node failed it (each logged).

# `join`

```elixir
@spec join(atom() | nil, term(), pid()) :: :ok
```

Joins `pid`, a ready member of host `key`, to its group; a no-op without the scope.

# `keys`

```elixir
@spec keys(module()) :: [{String.t(), String.t()}]
```

The keys, `{tenant_id, name}`, of `kind`'s hosts another node configures.

# `leave`

```elixir
@spec leave(atom() | nil, term(), pid()) :: :ok
```

Takes `pid` out of host `key`'s group.

# `nodes`

```elixir
@spec nodes(module(), term(), (map() -&gt; boolean()) | nil) :: [node()]
```

The other nodes that configure `kind`'s host `key`, those whose entry
`accept` accepts when given. The nodes with a ready member come first,
in random order; then those that run none (they start one when called),
in an order by host and node that every node shares; then those in an
outage, in random order (see the moduledoc).

# `outage`

```elixir
@spec outage(atom() | nil, term(), pid(), boolean()) :: :ok
```

Puts `pid`, the manager of a pool of host `key`, in `{:outage, key}`
(`true`) or takes it out (`false`). A no-op without the scope.

# `pg_child_spec`

The `:pg` scope's child spec.

# `scope`

```elixir
@spec scope(module()) :: atom()
```

The `:pg` scope `kind`'s ready members join.

# `start_link`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
