wasm_redb_db (wasm_redb v0.1.0)

View Source

The process behind one open database.

Callers talk to this process; the redb guest runs in a linked wasm_storage_guest process, so that a deadline can stop it by killing that process while this one, and the pid callers hold, stay. Each guest call is synchronous: while one runs, this process waits for it, up to that call's deadline. A reopen runs in the background, and this process answers meanwhile. It:

  • holds exclusive BEAM-local ownership of the storage (wasm_storage_owner);
  • serializes guest calls, and gives each one the caller's deadline;
  • lets many read transactions and one write transaction be open at once. Writers (explicit ones and implicit single writes) wait in a FIFO queue for the write slot;
  • commits implicit writes in groups: the writes given the database that wait in the queue when it is their turn share one write transaction and one sync, up to 256 of them. A write that finds nobody ahead of it runs alone. If one write of a group fails, the group is abandoned and each of its writes runs alone, so that every caller gets the outcome of its own write;
  • ends a transaction when the process that opened it exits;
  • replaces the guest after a deadline, a trap, or a storage error, and reopens the database so that redb runs its recovery. Every transaction open at that point is lost, and says so.

The outcome a caller gets back is always one of: the operation's result; {error, Reason} when it is known not to have happened; or {error, {commit_unknown, Why}} when a commit was interrupted and may or may not have reached the file. A reopen reads the file and settles it.

With readers => N, the writing guest opens the file in redb's SingleWriter mode and N wasm_redb_reader processes each run a guest that opens it read-only. A lock table (wasm_storage_lock) stands between them. Reads and read transactions then go to the readers, which run in parallel, and this process keeps the write path. wasm_redb sends reads to the readers directly; reads that reach this process (a database named or given as a pid) are passed on to the least busy reader.

Use it through wasm_redb; the messages are internal.

Where each part lives, in the order a request meets them:

  • requests and routing: handle_call/3, ends_txn/1, request/4, route/4, writer_request/4, guest_request/4;
  • transactions: with_txn/4, finish/4, ending/4, end_reader/3, forget_transactions/1;
  • the write slot and its queue: slot/4, enqueue/4, purge_expired/1, take_slot/4, next_writer/1;
  • group commit: schedule_run/1, run_writes/1, take_group/3, run_group/2, together/2, apply_steps/5, commit_group/5, alone/2, abandon/3;
  • guest calls and failures: guest/4, call_guest/4, failed/4, lost/4, restart/2;
  • opening and reopening: init/2, open_now/1, reopen_soon/0, start_open/1, opened/2, open_failed/2, cancel_open/1, with the callers that wait in park/4, replay/1 and expire_waiting/1;
  • readers: readers/1, start_readers/2, reader_for/2, forward/5, drop_readers_shared/3;
  • the read transaction implicit reads share: shared_txn/2, drop_shared/1, changed/1, bump/1;
  • idle closing and info: info_msg/2 (idle_check), close_guest/1, info/1.