Alternera.PluginWasmFramework.Slots (alternera_plugin_wasm_framework v0.9.0)

Copy Markdown View Source

The node's limit on guests running at once, shared fairly between tenants. How many may run is the configured runtime's (Runtime.max_running/0): Runtime.Wasmex lets one fewer run than its threads, so one always stays free for its epoch tick (a guest keeps its thread until the tick, and the tick is a task on the same threads: on a two-thread node two endless scripts at once stopped every language step); Runtime.Port lets as many run as the node has processors, since each guest is an OS process of its own with its ticker on its own thread.

A slot is held while a guest may run. Its owner is a process: the wasmex worker, or the caller of Runtime.Port.call/5; the slot goes back when the owner exits, so a killed process cannot leak one. The caller gives it back while the guest waits on a host call (the guest is off the processor then, and a pipeline.call into another language step needs a slot of its own) and takes one again before the guest resumes (resume/4), ahead of its tenant's guests not yet started; a guest of a called pipeline takes its first slot that way too (acquire_first/5). A call that cannot get one before its deadline is a time-out: waiting counts against its time.

The slots are shared fairly between tenants: when one frees, it goes to the waiting request whose tenant holds the fewest slots, and among equals to a resuming guest before a new one, then to the one that waited longest. A lone tenant uses every slot; a second one gets the next slot to free, so one tenant's slow modules cannot queue every other tenant's steps. No slot is taken back. A call that names no tenant (a check on save or upload) counts under nil, a tenant of its own.

The application env's :max_running_calls replaces the runtime's number. On a one-thread node wasmex's limit is still one, and an endless script can hold that thread past its deadline.

Summary

Functions

Takes a slot for owner, running for tenant, waiting until deadline (monotonic milliseconds). Answers the slot, or :busy when none came in time.

Takes the first slot of a guest: with resume/4 when it runs in a called pipeline (nested?, the step's call_depth above 0), since the request that called it is half way through and its caller waits on it; else with acquire/4.

Returns a specification to start this module under a supervisor.

How many guests may run at once: the application env's :max_running_calls, else the configured runtime's (Runtime.max_running/0), and at least one.

The processors this node may run on, held to a container's CPU quota: what wasmex sizes its threads by, and what Runtime.Port lets run.

Gives a slot back, or withdraws a request still waiting.

Takes a slot back for a guest that gave its own up for a host call, as acquire/4 does, ahead of the tenant's guests not yet started: a guest half way through holds its store and the request's memory, and under a burst it would otherwise wait behind every request that came after it at each host call, while those start and hold stores of their own.

Starts the limit: :name (this module) and :limit (limit/0), which tests set for an instance of their own. Without :limit it follows :follow (limit/0) as it changes, checked on each request.

Functions

acquire(owner, tenant, deadline, server \\ __MODULE__)

@spec acquire(pid(), term(), integer(), GenServer.server()) ::
  {:ok, reference()} | :busy

Takes a slot for owner, running for tenant, waiting until deadline (monotonic milliseconds). Answers the slot, or :busy when none came in time.

acquire_first(owner, tenant, deadline, nested?, server \\ __MODULE__)

@spec acquire_first(pid(), term(), integer(), boolean(), GenServer.server()) ::
  {:ok, reference()} | :busy

Takes the first slot of a guest: with resume/4 when it runs in a called pipeline (nested?, the step's call_depth above 0), since the request that called it is half way through and its caller waits on it; else with acquire/4.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

limit()

@spec limit() :: pos_integer()

How many guests may run at once: the application env's :max_running_calls, else the configured runtime's (Runtime.max_running/0), and at least one.

processors()

@spec processors() :: pos_integer()

The processors this node may run on, held to a container's CPU quota: what wasmex sizes its threads by, and what Runtime.Port lets run.

release(slot, server \\ __MODULE__)

@spec release(reference(), GenServer.server()) :: :ok

Gives a slot back, or withdraws a request still waiting.

resume(owner, tenant, deadline, server \\ __MODULE__)

@spec resume(pid(), term(), integer(), GenServer.server()) ::
  {:ok, reference()} | :busy

Takes a slot back for a guest that gave its own up for a host call, as acquire/4 does, ahead of the tenant's guests not yet started: a guest half way through holds its store and the request's memory, and under a burst it would otherwise wait behind every request that came after it at each host call, while those start and hold stores of their own.

start_link(opts)

Starts the limit: :name (this module) and :limit (limit/0), which tests set for an instance of their own. Without :limit it follows :follow (limit/0) as it changes, checked on each request.