Slap.Cluster.Strategy.LeaseLoop.Store behaviour (slap_cluster v0.2.0)

Copy Markdown View Source

Where a lease strategy keeps its leases and heartbeats. Each callback gets the store's state and returns it, possibly changed. A lease is {owner, generation} while unexpired; owners/1 returns only those. Every call must return within the :timeout given to init/2.

Summary

Callbacks

Claims up to count free or expired leases; returns [{shard, generation}].

Frees every lease of this node and removes its heartbeat (clean stop).

The owner of every unexpired lease.

Frees this node's lease of shard, if it still holds it.

Renews the leases of shards held by this node; returns those renewed. The others are lost. If some calls failed, it returns {:error, reason, renewed, failed}: failed are the shards whose lease may or may not still be held (the next renewal tries them again), and the others not renewed are lost.

Creates what is missing. A transient error is retried. Return {:error, {:unsupported, reason}} when the store cannot meet the lease protocol's requirements.

Types

ctx()

@type ctx() :: %{
  cluster: module(),
  cluster_name: String.t(),
  me: String.t(),
  ttl: pos_integer(),
  shards: pos_integer(),
  timeout: pos_integer()
}

shard()

@type shard() :: non_neg_integer()

state()

@type state() :: term()

Callbacks

claim(state, count)

@callback claim(state(), count :: pos_integer()) ::
  {{:ok, [{shard(), non_neg_integer()}]} | {:error, term()}, state()}

Claims up to count free or expired leases; returns [{shard, generation}].

heartbeat(state)

@callback heartbeat(state()) :: {:ok | {:error, term()}, state()}

init(opts, ctx)

@callback init(opts :: keyword(), ctx()) :: state()

leave(state)

@callback leave(state()) :: state()

Frees every lease of this node and removes its heartbeat (clean stop).

live_nodes(state)

@callback live_nodes(state()) :: {{:ok, pos_integer()} | {:error, term()}, state()}

owners(state)

@callback owners(state()) ::
  {{:ok, %{required(shard()) => String.t()}} | {:error, term()}, state()}

The owner of every unexpired lease.

release(state, shard)

@callback release(state(), shard()) :: state()

Frees this node's lease of shard, if it still holds it.

renew(state, list)

@callback renew(state(), [shard()]) ::
  {{:ok, [shard()]} | {:error, term(), [shard()], [shard()]}, state()}

Renews the leases of shards held by this node; returns those renewed. The others are lost. If some calls failed, it returns {:error, reason, renewed, failed}: failed are the shards whose lease may or may not still be held (the next renewal tries them again), and the others not renewed are lost.

setup state

@callback setup(state()) :: {:ok | {:error, term()}, state()}

Creates what is missing. A transient error is retried. Return {:error, {:unsupported, reason}} when the store cannot meet the lease protocol's requirements.