wasm_storage_guest (wasm_storage v0.1.0)
View SourceOne guest instance, owned by one process, spoken to through the request-buffer ABI.
Both database applications run their guest through this module. The process
opens the storage devices the guest will use (so it owns their handles),
instantiates the module with the wasm_storage imports, and then serves
calls one at a time:
{ok, Mod} = wasm_storage_guest:module(wasm_redb, "wasm_redb.wasm"),
{ok, G} = wasm_storage_guest:start_link(
#{module => Mod,
devices => [{0, {wasm_storage_memory, new}, #{}}]}),
{ok, Body} = wasm_storage_guest:call(G, 0, <<>>, 5000).call/4 returns what the guest answered: {ok, Body}, not_found, or
{error, #{code, message, in_commit, storage}}. in_commit says the error
came from a commit, whose outcome is then unknown. storage is the last
device failure (wasm_storage_host:last_error/0) or none.
A trap means the guest's state can no longer be trusted. The call returns
{trap, Error, PanicMessage} and the process stops with
{shutdown, {trap, Error}}. A caller that wants to go on starts a new
instance; the database layers reopen the database, which runs the engine's
own recovery.
The process never limits time itself. A caller that needs a deadline uses
the Timeout of call/4 and kills the process when it passes: the guest
is then stopped wherever it was, exactly like a crash.
What runs where:
- In the owner process: the guest itself, the storage imports
(
wasm_storage_host) and the devices they use.start_link/1starts it;call/4,memory_pages/1anddevices/1ask it. - In the caller's process, a database server:
stop/2,stop_linked/2andclose/3, which stop an owner, andopen_start/3,open_message/2,open_wait/1andopen_cancel/1, which open a guest without blocking the caller. That open is a small state machine the caller drives from its own mailbox. - Node-wide:
load/2andmodule/2keep each guest module inpersistent_term, loaded once by its application.
Summary
Functions
Run operation Op with request Payload.
Close the guest with the call Close (its engine's close operation), then
stop the owner as stop_linked/2 does. Each step waits at most Timeout
milliseconds; a guest that does not close in time is stopped anyway.
wasm_storage:info/1 of every device the guest can reach, by id.
Load priv/File of App and keep the module for the life of the calling
process. Call it from the application's start/2: erlang_wasm drops a module
from its cache when the process that loaded it exits.
The size of the guest's linear memory, in 64 KiB pages.
The module load/2 loaded.
Stop an open in progress; the owner is gone when it returns.
The owner an open runs in.
Offer Msg to the open. no: it is not the open's. {waiting, Opening}:
the open goes on. {done, {ok, Pid, Bodies}}: the guest is open, Bodies
being the steps' responses in order. {done, {error, Pid, Why}}: a step
failed, the owner exited, or the open ran out of time; the owner is gone.
Start an owner, as start_link/1 does, and send it Steps, the calls that
open its guest, one after the other, without waiting for them. The open
must end within Timeout milliseconds, or the owner is killed.
Wait for the open to end, within its own deadline. For an open the caller cannot answer anybody before, its first one.
Start the owner process. Options
Stop the owner, destroying the instance and closing its devices.
Stop the owner within Timeout milliseconds. One that does not stop in time,
for example a guest blocked in a host call, is killed.
Stop an owner the caller started with start_link/1 and whose exits it
traps, and take its exit message. Timeout is as in stop/2; kill kills
it at once.
Types
-type open_result() :: {ok, pid(), [binary()]} | {error, pid(), open_failure()}.
-opaque opening()
-type step() :: {non_neg_integer(), iodata()}.
Functions
-spec call(pid(), non_neg_integer(), iodata(), timeout()) -> {ok, binary()} | not_found | {error, map()} | {trap, term(), binary() | none}.
Run operation Op with request Payload.
Close the guest with the call Close (its engine's close operation), then
stop the owner as stop_linked/2 does. Each step waits at most Timeout
milliseconds; a guest that does not close in time is stopped anyway.
-spec devices(pid()) -> [{non_neg_integer(), wasm_storage:info()}].
wasm_storage:info/1 of every device the guest can reach, by id.
-spec load(atom(), file:filename()) -> {ok, wasm:module_()} | {error, term()}.
Load priv/File of App and keep the module for the life of the calling
process. Call it from the application's start/2: erlang_wasm drops a module
from its cache when the process that loaded it exits.
-spec memory_pages(pid()) -> {ok, non_neg_integer()}.
The size of the guest's linear memory, in 64 KiB pages.
-spec module(atom(), file:filename()) -> {ok, wasm:module_()} | {error, not_loaded}.
The module load/2 loaded.
-spec open_cancel(opening()) -> ok.
Stop an open in progress; the owner is gone when it returns.
The owner an open runs in.
-spec open_message(term(), opening()) -> no | {waiting, opening()} | {done, open_result()}.
Offer Msg to the open. no: it is not the open's. {waiting, Opening}:
the open goes on. {done, {ok, Pid, Bodies}}: the guest is open, Bodies
being the steps' responses in order. {done, {error, Pid, Why}}: a step
failed, the owner exited, or the open ran out of time; the owner is gone.
Start an owner, as start_link/1 does, and send it Steps, the calls that
open its guest, one after the other, without waiting for them. The open
must end within Timeout milliseconds, or the owner is killed.
The caller, which must trap exits, hands every message it receives to
open_message/2 until the open is done, or waits for it with
open_wait/1. A database server opens this way so that a long open (a
repair) neither holds it up nor is cut short by a caller's deadline.
-spec open_wait(opening()) -> {done, open_result()}.
Wait for the open to end, within its own deadline. For an open the caller cannot answer anybody before, its first one.
Start the owner process. Options:
module(required): frommodule/2.devices:[{Id, Spec, OpenOpts}], opened in this process withwasm_storage:open/2and registered as deviceId, in order.locks:#{Id => LockTable}, awasm_storage_lockprocess for each device that other guests share. Without one, the device'slockimport answersENOTSUP.namespace:{Spec, Opts}of awasm_storage_nsroot, opened in this process, for guests that open files by name. Addswasm_storage_host:namespace_imports/0.imports: more imports, merged overwasm_storage_host:imports/0.instance: options forwasm:instantiate/3, merged over the defaults:compile => true,fuel => infinity,max_host_calls => infinity,max_depth => 8192. Afuellimit turns the compiled tier off. The first compile of a guest module runs in the background and takes minutes (about 230 s for redb's, 217 s for SQLite's, on the test machine); set erlang_wasm'scode_cache_dirto pay it once per release.min_heap_size: the process heap floor in words, default 262144 (2 MiB). Guest code allocates fast; with OTP's default floor a single redb commit runs about 75 collections on erlang_wasm 0.16, a third of them full sweeps.0keeps OTP's default.hibernate_after: milliseconds without a call after which the process hibernates and gives its heap back, default 1000.
-spec stop(pid()) -> ok.
Stop the owner, destroying the instance and closing its devices.
Stop the owner within Timeout milliseconds. One that does not stop in time,
for example a guest blocked in a host call, is killed.
Stop an owner the caller started with start_link/1 and whose exits it
traps, and take its exit message. Timeout is as in stop/2; kill kills
it at once.
The link stays until the owner is gone: if the caller is killed meanwhile, the owner goes with it instead of living on, holding its devices.