Juggernaut.Peer.Snapshot (Juggernaut v0.4.0)

Copy Markdown View Source

The state a job may change and a worker puts back after it (design notes §10.5, plan I18): the environment of every loaded application, the OS environment, and the :persistent_term keys the project owns.

take/1 records it once, after boot; restore/1 compares the live state with it and writes only what differs, so a job that changed nothing costs no writes (a persistent_term write starts a global GC, docs/dependencies/code-loading.md). Values a job changed are set back, keys it added are deleted, keys it deleted come back.

A module a job loaded from memory (no file on disk: a module a test defines, a fixture a test compiles into the VM, the :elixir_compiler_N modules Code leaves loaded) is deleted and purged, so it neither reaches the next job nor reads as drift in Juggernaut.Peer.Health (a suite that defines modules would otherwise recycle every worker after every job). A file a job required (Code.require_file/2) is unrequired, so a later job that requires it compiles it again instead of loading nothing. A module of the boot set that a job removed is not put back: the health check reports it.

A persistent_term key is the project's when it is one of the owners atoms (the project's application names and modules) or a tuple whose first element is one. Other keys belong to libraries (telemetry, Elixir itself) or to Juggernaut and are never touched: take/1 drops the worker-side harness modules (harness/0) from the owners, so when the project is Juggernaut itself a restore never erases the boot state that Juggernaut.Peer.Boot writes after the snapshot.

ETS tables of the project (owned by a process of one of apps that runs one of the owners modules, or by the application's start process) are recorded with their contents; a library process in the app's tree (Ecto's pool queue, a Registry partition) owns state its library manages, which is left out. After a job, a public table whose contents differ is put back as it was at boot (every object deleted, the boot objects inserted again): a memo a test filled, or a wrong value a mutant wrote, never reaches the next job. A protected table can be written only by its owner, so it is not put back; nor is the state of a project server (a worker of the apps' supervision trees whose module is one of owners, read with :sys.get_state/2), which may hold pids, refs and timers. residue/1 names what of either differs from boot, so that the controller runs a survivor found on such a worker again on a fresh one (Juggernaut.Runner). Private tables cannot be read and are left out (a known limit).

A server whose module is one of the lasting modules (the lasting_servers setting) is not recorded, so its state is never residue: the project says that what tests leave there is never read by a later test (state keyed by each test's own fresh ids, a debug ring). A memo must not be listed: a value it kept could spare a mutant.

This module runs inside workers and is never mutated.

Summary

Types

t()

An application's ETS table at boot: its protection and its objects, sorted.

Functions

The changes that turn now into wanted: {:put, key, value} for a key missing or different in now, {:delete, key} for a key only in now, sorted by key.

The worker-side modules whose persistent_term keys are Juggernaut's, never owned.

Whether the persistent_term key key belongs to one of owners.

What differs from snapshot that restore/1 does not put back, as readable reasons ([] when nothing): an application's ETS table whose contents changed, and a project server whose state changed or could not be read in time. A table or server gone since boot is left to Juggernaut.Peer.Health.

Puts back what differs from snapshot and returns what it put back.

The state of the server pid: {:ok, state}, :timeout when it did not answer within 200 ms (or is not a :sys process), :gone when it is dead.

Records the state. owners are the atoms whose persistent_term keys are put back and whose modules' servers are watched, but for the servers of the lasting modules; apps the applications whose ETS tables and supervision trees are recorded.

Types

restored()

@type restored() :: %{
  app_env: [{atom(), atom()}],
  os_env: [String.t()],
  terms: [term()],
  modules: [module()],
  required: [Path.t()],
  tables: [atom() | :ets.tid()]
}

t()

@type t() :: %{
  app_env: %{required(atom()) => %{required(atom()) => term()}},
  os_env: %{required(String.t()) => String.t()},
  owners: MapSet.t(atom()),
  terms: %{required(term()) => term()},
  modules: MapSet.t(module()),
  required: MapSet.t(Path.t()),
  tables: %{required(:ets.table()) => table()},
  servers: %{required(pid()) => {module(), term()}}
}

table()

@type table() :: %{protection: :public | :protected, objects: [tuple()]}

An application's ETS table at boot: its protection and its objects, sorted.

Functions

changes(wanted, now)

@spec changes(map(), map()) :: [{:put, term(), term()} | {:delete, term()}]

The changes that turn now into wanted: {:put, key, value} for a key missing or different in now, {:delete, key} for a key only in now, sorted by key.

harness()

@spec harness() :: [module()]

The worker-side modules whose persistent_term keys are Juggernaut's, never owned.

owned?(key, owners)

@spec owned?(term(), MapSet.t(atom())) :: boolean()

Whether the persistent_term key key belongs to one of owners.

residue(snapshot)

@spec residue(t()) :: [String.t()]

What differs from snapshot that restore/1 does not put back, as readable reasons ([] when nothing): an application's ETS table whose contents changed, and a project server whose state changed or could not be read in time. A table or server gone since boot is left to Juggernaut.Peer.Health.

restore(snapshot)

@spec restore(t()) :: restored()

Puts back what differs from snapshot and returns what it put back.

server_state(pid)

@spec server_state(pid()) :: {:ok, term()} | :timeout | :gone

The state of the server pid: {:ok, state}, :timeout when it did not answer within 200 ms (or is not a :sys process), :gone when it is dead.

take(owners, apps \\ [], lasting \\ [])

@spec take([atom()], [atom()], [module()]) :: t()

Records the state. owners are the atoms whose persistent_term keys are put back and whose modules' servers are watched, but for the servers of the lasting modules; apps the applications whose ETS tables and supervision trees are recorded.