# `Slap.Cluster.Strategy`
[🔗](https://github.com/mjrusso/slap/blob/slap_cluster-v0.4.0/slap_cluster/lib/slap/cluster/strategy.ex#L1)

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 `c: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.

# `location`

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

# `reason`

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

# `shard`

```elixir
@type shard() :: non_neg_integer()
```

# `child_spec`

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

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

# `handle_shard_down`

```elixir
@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`

```elixir
@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`
*optional* 

```elixir
@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`
*optional* 

```elixir
@callback validate_options(opts :: keyword()) :: :ok
```

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

---

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