wasm_storage_guest (wasm_storage v0.1.0)

View Source

One 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:

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

open_failure()

-type open_failure() ::
          {reply, not_found | {error, map()} | {trap, term(), binary() | none}} |
          {exit, term()} |
          timeout.

open_result()

-type open_result() :: {ok, pid(), [binary()]} | {error, pid(), open_failure()}.

opening()

-opaque opening()

step()

-type step() :: {non_neg_integer(), iodata()}.

Functions

call(Pid, Op, Payload, Timeout)

-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/3

-spec close(pid(), step(), timeout()) -> ok.

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.

devices(Pid)

-spec devices(pid()) -> [{non_neg_integer(), wasm_storage:info()}].

wasm_storage:info/1 of every device the guest can reach, by id.

load(App, File)

-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.

memory_pages(Pid)

-spec memory_pages(pid()) -> {ok, non_neg_integer()}.

The size of the guest's linear memory, in 64 KiB pages.

module(App, File)

-spec module(atom(), file:filename()) -> {ok, wasm:module_()} | {error, not_loaded}.

The module load/2 loaded.

open_cancel/1

-spec open_cancel(opening()) -> ok.

Stop an open in progress; the owner is gone when it returns.

open_guest/1

-spec open_guest(opening()) -> pid().

The owner an open runs in.

open_message/2

-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.

open_start/3

-spec open_start(map(), [step(), ...], timeout()) -> {ok, opening()} | {error, term()}.

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.

open_wait/1

-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_link(Opts)

-spec start_link(map()) -> {ok, pid()} | {error, term()}.

Start the owner process. Options:

  • module (required): from module/2.
  • devices: [{Id, Spec, OpenOpts}], opened in this process with wasm_storage:open/2 and registered as device Id, in order.
  • locks: #{Id => LockTable}, a wasm_storage_lock process for each device that other guests share. Without one, the device's lock import answers ENOTSUP.
  • namespace: {Spec, Opts} of a wasm_storage_ns root, opened in this process, for guests that open files by name. Adds wasm_storage_host:namespace_imports/0.
  • imports: more imports, merged over wasm_storage_host:imports/0.
  • instance: options for wasm:instantiate/3, merged over the defaults: compile => true, fuel => infinity, max_host_calls => infinity, max_depth => 8192. A fuel limit 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's code_cache_dir to 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. 0 keeps OTP's default.
  • hibernate_after: milliseconds without a call after which the process hibernates and gives its heap back, default 1000.

stop(Pid)

-spec stop(pid()) -> ok.

Stop the owner, destroying the instance and closing its devices.

stop(Pid, Timeout)

-spec stop(pid(), timeout()) -> ok.

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_linked/2

-spec stop_linked(pid(), timeout() | kill) -> ok.

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.