wasm_storage_fault (wasm_storage v0.1.0)

View Source

A memory device that fails on purpose, deterministically.

Use it to test what an engine does when storage misbehaves, and what its file looks like after a crash. The device is a controller you create in your test process, wrapping a wasm_storage_memory device:

{ok, Mem} = wasm_storage_memory:new(#{}),
{ok, F} = wasm_storage_fault:new(Mem, #{plan => [{at, 12, crash}]}),
%% Hand {wasm_storage_fault, F} to the engine as its storage, run a
%% transaction; operation 12 does not happen and every later one fails
%% with `crashed'. Then rebuild what a disk could hold after power loss:
{ok, Image} = wasm_storage_fault:crash_image(F, {random, 42}),
%% and reopen the engine on {wasm_storage_memory, Image}.

Every device call counts as one operation, numbered from 1 in call order (ops/1). A plan is a list of rules, checked in order on every operation; the first that matches applies:

  • {at, N, Action}: operation number N.
  • {nth, Kind, K, Action}: the K-th operation of Kind (size, pread, pwrite, resize or sync).
  • {from, N, Action}: operation N and every one after it.
  • {enospc_after, Bytes}: a write or resize that would make the device larger than Bytes fails with enospc.

Actions:

  • {error, Reason}: the call fails and has no effect.
  • {short_write, Bytes}: a write stores only its first Bytes, then fails with eio.
  • {torn_write, Seed}: a write stores a random subset of its sectors, then fails with eio.
  • crash: the call does not happen, and every later call fails with crashed, as if power was cut.
  • {stall, Ms}: the call waits Ms milliseconds in the calling process, then runs as if no rule matched. For tests of deadlines and of storage that stops answering.

Writes and resizes are applied to the wrapped device at once, so reads see them. Until a sync succeeds they are also recorded as unsynced. A failed sync leaves them unsynced. crash_image/2 builds a new memory device from the content as of the last successful sync plus a choice of the unsynced operations:

  • drop_unsynced: none of them.
  • keep_unsynced: all of them.
  • {random, Seed}: each one independently is dropped, kept, or (a write) torn at sector granularity. Any subset of unsynced writes may reach the disk in any order, so this covers reordering too.

The device is volatile. The controller's state lives in a public ETS table owned by the process that called new/2.

Several devices can share one plan and one operation counter: create a group with group/1 and pass it as group to new/2. A database that keeps several files (SQLite and its journal) can then be failed at every operation it makes, whichever file the operation touches. set_plan/2 and ops/1 accept the group. Each device keeps its own unsynced operations, and crash_image/2 is taken per device: writes to different files may reach the disk in any order between syncs, so independent choices per file are possible states.

Summary

Functions

A new memory device holding what a disk could hold after a crash now.

Free the controller and its durable copy. The wrapped device is left alone.

A plan and an operation counter that several devices share.

Wrap memory device Mem. Its current content is the starting durable state. Options: plan (default []), sector_size (default 512), group (from group/1; the group's plan and counter are used instead of the device's).

How many operations ran, in total and per kind.

Replace the plan. Operation counters keep counting.

The unsynced operations, oldest first.

Types

action()

-type action() ::
          {error, term()} |
          {short_write, non_neg_integer()} |
          {torn_write, integer()} |
          crash |
          {stall, non_neg_integer()}.

fault()

-opaque fault()

rule()

-type rule() ::
          {at, pos_integer(), action()} |
          {nth, atom(), pos_integer(), action()} |
          {from, pos_integer(), action()} |
          {enospc_after, non_neg_integer()}.

Functions

crash_image/2

-spec crash_image(fault(), drop_unsynced | keep_unsynced | {random, integer()}) ->
                     {ok, wasm_storage_memory:device()}.

A new memory device holding what a disk could hold after a crash now.

delete/1

-spec delete(fault()) -> ok.

Free the controller and its durable copy. The wrapped device is left alone.

group(Opts)

-spec group(map()) -> {ok, fault()}.

A plan and an operation counter that several devices share.

new(Mem, Opts)

-spec new(wasm_storage_memory:device(), map()) -> {ok, fault()}.

Wrap memory device Mem. Its current content is the starting durable state. Options: plan (default []), sector_size (default 512), group (from group/1; the group's plan and counter are used instead of the device's).

ops/1

-spec ops(fault()) -> #{total := non_neg_integer(), atom() => non_neg_integer()}.

How many operations ran, in total and per kind.

set_plan/2

-spec set_plan(fault(), [rule()]) -> ok.

Replace the plan. Operation counters keep counting.

unsynced/1

-spec unsynced(fault()) -> [{write, non_neg_integer(), binary()} | {resize, non_neg_integer()}].

The unsynced operations, oldest first.