Juggernaut.Pool.Worker (Juggernaut v0.4.0)

Copy Markdown View Source

One warm worker: a GenServer that owns a :peer node booted in the sandbox and runs jobs in it, one at a time (design notes §10.5).

The peer is started with connection: :standard_io (no distribution, no epmd) and peer_down: :crash, so a peer that dies (a mutant calling System.halt/1) is an exit this process sees, never a silent normal one (docs/dependencies/peer.md). Each peer is started by a holder: a process of its own, linked to the worker, that starts the peer, parks it (Juggernaut.Peer.Boot.park/1), tells the worker, and lives as long as the peer; a peer that dies takes its holder with it, which is the exit the worker sees. Everything the peer prints goes to a Juggernaut.Pool.Output kept by the worker, never to the controller's stdout. The peer is booted right after start_link/1 returns; calls wait for the boot, so ready/2 answers once it is done.

A worker is recycled (its peer stopped, a fresh one started and booted) after a job that reports drift, after a job past its deadline (the peer is stopped mid-job), when its peer dies, when a canary job fails or drifts, or on recycle/2. Each recycle is recorded with its reason. A boot that fails leaves the worker failed: every later call answers the boot's infrastructure error (exit 3), so a broken setup is never retried in a loop.

Spares

With :spare, the worker keeps a spare peer parked beside its own: a fresh VM with the project loaded, which has touched no database and started no app (Boot.park/1), so it holds no database connection and can wait beside a peer that uses the same partition. A boot takes the spare, if there is one, and boots it the rest of the way (Boot.resume/1) once the old peer has stopped, then parks the next spare in the background: a recycle skips starting a VM and loading the project, and nothing the spare did can differ from what a fresh boot would have done. :after_recycle keeps a spare once the worker has recycled (a worker that recycles once tends to again); :always keeps one from the start. A reload (reload/1) replaces the spare, which loaded the project before the beams changed.

Held workers

With hold: true the worker boots nothing at start: it parks its spare and waits. reboot/2 stops its peer, if any, and boots one with options merged over its :boot (a boot-lane job's switch and progress file), from the spare; retire/1 stops the peer. A failed boot only fails that boot: the worker stays held, ready for the next. A held worker is the boot lane's per-slot worker (Juggernaut.Runner.BootLane), which boots once per job, so each job finds its VM already started and its project loaded. A held worker recycles as a pool worker does, booting again with the same overrides (the same mutant on), as a new boot-lane worker per job did.

Options:

  • :index (required) - the worker index, or :boot;
  • :sandbox (required), :deps_path (default the sandbox's dependency mirror);
  • :env - rendered {name, value} pairs (Juggernaut.Pool.Env.render/3);
  • :schedulers - +S for the peer (default 2);
  • :boot - options of Juggernaut.Peer.Boot.boot/1 other than the sandbox;
  • :boot_timeout_ms (default 600 000), :slack_ms added to each job deadline (2 000);
  • :canary - a job run every :canary_every jobs (default 200) to prove the worker still passes what passed at baseline; none by default (set_canary/2).
  • :spare - :never (default), :after_recycle or :always (see Spares);
  • :hold - false (default) or true (see Held workers);
  • :cover - a directory: the worker-side modules (Juggernaut.Peer.*) are cover-compiled in the peer and their coverage is exported there after the boot and after each job, for Juggernaut's own test coverage. Off by default.

Summary

Functions

The canary's deadline (ms): its per-test :timeout_ms (60 000 when unset) for each selected test, as the baseline gives a shard.

Whether a canary runs after job number jobs.

Why a canary's result recycles its worker, or nil: a canary proves the worker only when it passes and leaves no drift.

Waits for the boot: {:ok, info} (Juggernaut.Peer.Boot) or the boot's error. A boot still running after timeout, or a worker that is not running, is an infrastructure error too: the caller never exits.

Boots a held worker's peer (see Held workers) with overrides merged over its :boot options: its old peer stopped first, its spare taken. {:ok, info} or the boot's error, as ready/2 gives them; the worker stays held either way. A worker that is not running is an infrastructure error.

Recycles the worker now, recording reason.

Why a job's result recycles its worker, or nil.

Loads into the peer the beams that changed under the sandbox since they were loaded (Juggernaut.Peer.Reload), as the server does before a run on its warm pool. A module whose old code is still running, or a load that fails, recycles the worker instead. :ok, or the error of a worker that failed to boot again.

Stops a held worker's peer, keeping its spare. :ok.

Runs job (Juggernaut.Peer.Runner) with a deadline of deadline_ms plus the slack. {:ok, result} carries the runner's result, or %{status: :timeout} past the deadline, or %{status: :down, reason: reason} when the peer died during the job.

Sets the canary job.

Whether a worker with the :spare option spare and its recycles keeps a spare.

Starts a worker; its peer boots right after. Unknown options raise.

The worker's index, job count, recycles, boots (peers booted the whole way), spare_boots (how many of those boots took a spare), the spare's state (nil, :parking or :parked), boot info and the end of its peer's output.

Types

recycle()

@type recycle() :: %{
  worker: pos_integer() | :boot,
  reason: String.t(),
  after_job: non_neg_integer()
}

Functions

canary_deadline(canary)

@spec canary_deadline(map()) :: pos_integer()

The canary's deadline (ms): its per-test :timeout_ms (60 000 when unset) for each selected test, as the baseline gives a shard.

canary_due?(arg1, jobs, every)

@spec canary_due?(map() | nil, non_neg_integer(), pos_integer()) :: boolean()

Whether a canary runs after job number jobs.

canary_reason(result)

@spec canary_reason(map()) :: String.t() | nil

Why a canary's result recycles its worker, or nil: a canary proves the worker only when it passes and leaves no drift.

ready(worker, timeout \\ :infinity)

@spec ready(GenServer.server(), timeout()) ::
  {:ok, map()} | {:error, Juggernaut.Error.t()}

Waits for the boot: {:ok, info} (Juggernaut.Peer.Boot) or the boot's error. A boot still running after timeout, or a worker that is not running, is an infrastructure error too: the caller never exits.

reboot(worker, overrides)

@spec reboot(GenServer.server(), map()) ::
  {:ok, map()} | {:error, Juggernaut.Error.t()}

Boots a held worker's peer (see Held workers) with overrides merged over its :boot options: its old peer stopped first, its spare taken. {:ok, info} or the boot's error, as ready/2 gives them; the worker stays held either way. A worker that is not running is an infrastructure error.

recycle(worker, reason)

@spec recycle(GenServer.server(), String.t()) :: :ok | {:error, Juggernaut.Error.t()}

Recycles the worker now, recording reason.

recycle_reason(arg1)

@spec recycle_reason(map()) :: String.t() | nil

Why a job's result recycles its worker, or nil.

reload(worker)

@spec reload(GenServer.server()) :: :ok | {:error, Juggernaut.Error.t()}

Loads into the peer the beams that changed under the sandbox since they were loaded (Juggernaut.Peer.Reload), as the server does before a run on its warm pool. A module whose old code is still running, or a load that fails, recycles the worker instead. :ok, or the error of a worker that failed to boot again.

retire(worker)

@spec retire(GenServer.server()) :: :ok

Stops a held worker's peer, keeping its spare. :ok.

run(worker, job, deadline_ms)

@spec run(GenServer.server(), map(), pos_integer()) ::
  {:ok, map()} | {:error, Juggernaut.Error.t()}

Runs job (Juggernaut.Peer.Runner) with a deadline of deadline_ms plus the slack. {:ok, result} carries the runner's result, or %{status: :timeout} past the deadline, or %{status: :down, reason: reason} when the peer died during the job.

set_canary(worker, job)

@spec set_canary(GenServer.server(), map()) :: :ok

Sets the canary job.

spare?(atom, recycles)

@spec spare?(:never | :after_recycle | :always, list()) :: boolean()

Whether a worker with the :spare option spare and its recycles keeps a spare.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts a worker; its peer boots right after. Unknown options raise.

stats(worker)

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

The worker's index, job count, recycles, boots (peers booted the whole way), spare_boots (how many of those boots took a spare), the spare's state (nil, :parking or :parked), boot info and the end of its peer's output.