Alternera.PluginHosts.Pool.Manager (alternera_plugin_hosts v0.9.0)

Copy Markdown View Source

Starts and tracks the members of one PluginHosts.Pool.

The members run under the pool's DynamicSupervisor, restart: :temporary: the manager, not the supervisor, decides when one starts. It starts min at boot and starts another whenever fewer than min run. A member holds a slot, a number from 1 to max: its place in the pool's :counters (its calls in flight, read by Pool.call/4 with no message), its name (<pool>/<slot>) and its log file (host-<slot>.log). A new member takes the lowest free slot, its counter set to 0, so a member that died with calls in flight leaves no count behind.

A Host restarts its own OS process; a member that stops is a Host process that crashed or was stopped, and the manager learns it from its monitor. A member that stopped normally (Host.retire/1) is replaced at once when fewer than min run. One that crashed is replaced after a backoff that doubles with each crash of the last minute, from 1 s to 30 s, so a member that crashes as it starts costs a start every 30 s, not a loop.

Idle shrink

Each member writes the time of its last activity, a call starting or ending or its becoming ready, into the pool's last_use atomics at its slot (Host); a new member's slot starts at its start time. Every min(idle_ms / 4, 30 s) the manager looks at the members beyond min: one with nothing in flight whose last activity is at least idle_ms old is retired (Host.retire/1: it leaves the registry and :pg first, so no call picks it, finishes what it took meanwhile, and stops), the longest idle first, one at a time; once it is gone the manager looks again at once. A ready member is retired only while min other members are ready, so an idle member is not stopped while the ones that would serve instead are still starting; a member that is not ready is retired only when it is down or unavailable, never while it starts. A member that crashes while it is retired counts as a crash (logged, backoff).

The sweep does not wait for Pool.rolling_restart/2: the restart passes over a member that stopped meanwhile.

Start on demand

With min: 0 no member starts at boot, and the last one stops once idle. A caller that finds no ready member asks await_ready/2. It waits only when a start is under way or due: no member runs (the manager starts one), or one is starting, draining for a restart, or being retired (idle), which a waiting caller has replaced on its stop. Members that are all down between crashes (:down, in their Host's backoff) or :unavailable (a start that failed the Host's max_failures times; the member stays and keeps retrying at its longest backoff until the idle sweep stops it) are answered at once, {:error, :host_restarting} or {:error, :host_unavailable}: a host in an outage fails its calls fast rather than hold each for its whole deadline. A caller that waits is answered when a member is ready (:ok), when every member has become :unavailable, or at its own deadline ({:error, :timeout}). While anyone waits the manager keeps at least one member, so one start serves every waiter; a start after a crash waits out the crash backoff as any other. The manager is in the pool's registry under {:manager, name}, so a caller finds it by the pool's name.

Growing

A member whose calls in flight reach the pool's grow_at from below sends :busy (Host; once, until it has dropped to half of grow_at, so a member hovering at the threshold does not send one per call). The manager then starts one more member when every ready member has at least grow_at in flight (read from the pool's :counters), fewer than max run, no member is starting (one that is neither ready, unavailable nor being retired: a replacement, a start for a waiting caller, a restart's extra member, an earlier growth) and no start is waiting out a backoff: one start at a time, so a burst does not start max members at once. The calls go on to the members that run; none waits for the start. The manager looks again whenever a member becomes ready, unavailable or stops, and when a retry is due, so load that stays above grow_at grows the pool step by step without another crossing; the sweep looks too. A start that fails waits 1 s before the next try, as fill does. At max it logs a warning, at most once a minute. The idle sweep stops the extra members once the load is gone. With max == min the pool cannot grow, and its members send no :busy.

In a cluster

The manager joins the :pg group {:host, name} of the pool's :scope for as long as it lives (Cluster.announce/3), so other nodes know this one configures the host even while no member runs, and forward to it; a forwarded call waits for a start as a call of this node's own. While no member can serve and none is on its way (every one that runs is unavailable, or none runs and a start waits out a crash backoff) it is also in {:outage, name}, and other nodes forward here last.

The members tell the manager when they become ready, unavailable, busy or reload (Host), and it keeps the last catalogue a member had: summary/1 and catalogue/1 answer it while none runs, without starting one.

Summary

Functions

Starts one more member, beyond min but not past max: {:ok, pid}, or {:error, :full} when max run, or the start's error.

Waits up to timeout_ms for a ready member, starting one when none is running or starting (see the moduledoc): :ok, or {:error, :timeout | :host_restarting | :host_unavailable}. :ok at once when a member is ready already.

The last catalogue a member had, or nil before any was ready; starts nothing.

Returns a specification to start this module under a supervisor.

The running members' pids, by slot.

%{running, min, max, idle_ms, grow_at, waiting, catalogue, members}, each member %{slot, pid, idle_ms} by slot, idle_ms the time since its last activity; waiting the callers waiting for a start, catalogue the last a member had (nil before any was ready).

Functions

add(manager)

@spec add(GenServer.server()) :: {:ok, pid()} | {:error, term()}

Starts one more member, beyond min but not past max: {:ok, pid}, or {:error, :full} when max run, or the start's error.

await_ready(manager, timeout_ms)

@spec await_ready(GenServer.server(), non_neg_integer()) ::
  :ok | {:error, :timeout | :host_restarting | :host_unavailable}

Waits up to timeout_ms for a ready member, starting one when none is running or starting (see the moduledoc): :ok, or {:error, :timeout | :host_restarting | :host_unavailable}. :ok at once when a member is ready already.

catalogue(manager)

@spec catalogue(GenServer.server()) :: [map()] | nil

The last catalogue a member had, or nil before any was ready; starts nothing.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

members(manager)

@spec members(GenServer.server()) :: [pid()]

The running members' pids, by slot.

start_link(opts)

summary(manager)

@spec summary(GenServer.server()) :: map()

%{running, min, max, idle_ms, grow_at, waiting, catalogue, members}, each member %{slot, pid, idle_ms} by slot, idle_ms the time since its last activity; waiting the callers waiting for a start, catalogue the last a member had (nil before any was ready).