Juggernaut.Pool (Juggernaut v0.4.0)

Copy Markdown View Source

The warm pool: a supervisor of Juggernaut.Pool.Workers, one :peer each, all booted in the sandbox at once (design notes §10.5).

Each worker gets its own environment from the configured env templates (Juggernaut.Pool.Env.render/3), so with the default template every worker has its own MIX_TEST_PARTITION and its own test database. await/2 waits for every boot and then proves the partitions (check_partitions/2): every worker must see exactly the environment it was given, and no two workers may use the same database. A boot that fails, or a partition that does not hold, is an infrastructure error (exit 3).

Options of start_link/1 (validated; a wrong one raises NimbleOptions.ValidationError naming it):

  • :size (pos_integer/0) - Required. The number of workers (a run takes it from options/2, within the budget).

  • :templates (list of tuple of String.t/0, String.t/0 values) - The env templates, rendered per worker. The default value is [{"MIX_TEST_PARTITION", "${MIX_TEST_PARTITION}_jg{worker}"}].

  • :controller_env (map of String.t/0 keys and String.t/0 values) - The controller's environment the templates read (default System.get_env/0).

Every other option is passed to each worker (Juggernaut.Pool.Worker), which validates it.

Summary

Functions

Waits for every worker's boot and checks the partitions. {:ok, infos} gives each worker's boot info by index; otherwise the first infrastructure error.

{:ok, infos} when every worker saw the environment it was given (expected, by index) and no database is used by two workers; otherwise an infrastructure error naming the workers.

Returns a specification to start this module under a supervisor.

The pool options a run's config gives: size within the connection budget (Juggernaut.Pool.Env.size/3, a budget below one worker is a config error; reserve peers' room kept out of it), the env templates and each peer's schedulers. Merge the sandbox and worker options in and pass the result to start_link/1. With spares on, each worker keeps a parked spare once it has recycled (Juggernaut.Pool.Worker, spare: :after_recycle).

Starts the pool and every worker's boot. Use await/2 before running jobs.

The workers, by index.

Functions

await(pool, timeout \\ :infinity)

@spec await(Supervisor.supervisor(), timeout()) ::
  {:ok, %{required(pos_integer()) => map()}} | {:error, Juggernaut.Error.t()}

Waits for every worker's boot and checks the partitions. {:ok, infos} gives each worker's boot info by index; otherwise the first infrastructure error.

check_partitions(infos, expected)

@spec check_partitions(%{required(pos_integer()) => map()}, %{
  required(pos_integer()) => [{String.t(), String.t()}]
}) :: {:ok, map()} | {:error, Juggernaut.Error.t()}

{:ok, infos} when every worker saw the environment it was given (expected, by index) and no database is used by two workers; otherwise an infrastructure error naming the workers.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

options(config, schedulers, reserve \\ 0)

@spec options(Juggernaut.Config.t(), pos_integer(), non_neg_integer()) ::
  {:ok, keyword()} | {:error, Juggernaut.Error.t()}

The pool options a run's config gives: size within the connection budget (Juggernaut.Pool.Env.size/3, a budget below one worker is a config error; reserve peers' room kept out of it), the env templates and each peer's schedulers. Merge the sandbox and worker options in and pass the result to start_link/1. With spares on, each worker keeps a parked spare once it has recycled (Juggernaut.Pool.Worker, spare: :after_recycle).

start_link(opts)

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

Starts the pool and every worker's boot. Use await/2 before running jobs.

workers(pool)

@spec workers(Supervisor.supervisor()) :: [{pos_integer(), pid()}]

The workers, by index.