SaltDns. Leader
(Salt v1.0.0)
Which node drives Knot, decided by a lease row in Postgres
(SaltDns.Leader.Lease).
Without config :salt_dns, SaltDns.Leader, node_id: "..." (the environment
variable SALT_DNS_NODE_ID) there is no cluster: the node is always the
leader (role/0 is :single) and nothing here costs anything.
With a node id, every node runs this process. Each renew_ms it either
renews its lease (leader) or tries to take a free one (follower). The lease
lasts ttl_ms; a leader that dies is replaced after at most
ttl_ms + renew_ms.
Fencing
The epoch of the lease is a fencing token, and the leader keeps its right to
write to Knot only while the last renewal is fresh: fence_ok?/0 is true
only if the lease was confirmed within stale_ms (default 1.5 x renew_ms)
by the local monotonic clock. It needs no database call, so a leader that
loses Postgres stops writing to Knot within one renew interval, long
before another node can take the lease. When the lease is lost (renewal
refused, or never confirmed for ttl_ms) the node is a follower again.
Role changes are announced on PubSub topic salt_dns:leader as
{:leader_event, :promoted | :demoted, epoch}; SaltDns.Knot.Failover acts
on them.
Summary
Functions
Returns a specification to start this module under a supervisor.
The address other replicas reach this replica's Knot on when none is configured: the first non-loopback IPv4 address of this host, port 5300 (the transfer port of the example stacks), or nil.
True when a node id is configured, i.e. leadership is decided by the lease.
The epoch of the lease this node holds, or nil.
True when this node may write to Knot as the primary: always without a cluster; otherwise only as leader with a lease confirmed recently enough.
An identity for a replica that was not given one: the host name (the pod name in Kubernetes) if there is one, else a random id. It is only a label for logs and metrics; what identifies a process in the lease is this label plus a token made at boot, so two replicas with the same label never share a lease.
What identifies this process in the lease row: the node id and a random token made at boot. Two replicas with the same label therefore never share a lease: each is just another contender.
Alias of fence_ok?/0 for callers that mean "am I the leader".
The lease row (who leads, at which epoch, how to reach that node's Knot), or nil.
Age in milliseconds of the last confirmed lease renewal, or nil when this node does not hold the lease.
This node's id.
:single without a cluster, else :leader or :follower (a stale leader counts as a follower).
Status for /ready and telemetry.
Subscribes the caller to role changes.
Problems with a lease timing (milliseconds), as sentences; [] when sound.
renew must be positive and below half of ttl; the fence time stale
(how long a confirmed renewal keeps writing allowed) must be at least
renew (or the fence would close between two renewals) and at most
ttl - renew (or the lease could expire for others before this node stops).
Checks against the database that this node holds the lease at its epoch (one
query). For the steps that change roles; the cheap fence_ok?/0 guards the
rest.
Types
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec default_knot_address() :: String.t() | nil
The address other replicas reach this replica's Knot on when none is configured: the first non-loopback IPv4 address of this host, port 5300 (the transfer port of the example stacks), or nil.
@spec enabled?() :: boolean()
True when a node id is configured, i.e. leadership is decided by the lease.
@spec epoch() :: pos_integer() | nil
The epoch of the lease this node holds, or nil.
@spec fence_ok?() :: boolean()
True when this node may write to Knot as the primary: always without a cluster; otherwise only as leader with a lease confirmed recently enough.
An identity for a replica that was not given one: the host name (the pod name in Kubernetes) if there is one, else a random id. It is only a label for logs and metrics; what identifies a process in the lease is this label plus a token made at boot, so two replicas with the same label never share a lease.
@spec holder_id() :: String.t() | nil
What identifies this process in the lease row: the node id and a random token made at boot. Two replicas with the same label therefore never share a lease: each is just another contender.
@spec leader?() :: boolean()
Alias of fence_ok?/0 for callers that mean "am I the leader".
@spec lease() :: SaltDns.Leader.Lease.lease() | nil
The lease row (who leads, at which epoch, how to reach that node's Knot), or nil.
@spec lease_age_ms() :: non_neg_integer() | nil
Age in milliseconds of the last confirmed lease renewal, or nil when this node does not hold the lease.
@spec node_id() :: String.t() | nil
This node's id.
@spec role() :: role()
:single without a cluster, else :leader or :follower (a stale leader counts as a follower).
@spec status() :: map()
Status for /ready and telemetry.
@spec subscribe() :: :ok | {:error, term()}
Subscribes the caller to role changes.
Problems with a lease timing (milliseconds), as sentences; [] when sound.
renew must be positive and below half of ttl; the fence time stale
(how long a confirmed renewal keeps writing allowed) must be at least
renew (or the fence would close between two renewals) and at most
ttl - renew (or the lease could expire for others before this node stops).
@spec validate_epoch() :: :ok | {:error, :not_leader}
Checks against the database that this node holds the lease at its epoch (one
query). For the steps that change roles; the cheap fence_ok?/0 guards the
rest.