8000 docs(durability): qualify the WAL statements and freeze v8 within 3.x (#941) by adityamukho · Pull Request #953 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions docs/DURABILITY.md
8000 8000
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ A file-backed database is two files in one directory:
| `app.graph` | Every checkpointed fact: four indexes, a dictionary, long strings, a free list, and two meta pages | Permanent |
| `app.graph.wal` | Every transaction committed since the last checkpoint, one CRC32-protected entry each | Created by the first write after a checkpoint, deleted by the next checkpoint |

A transaction is **committed** when `execute("(transact ...)")`, `execute("(retract ...)")` or `WriteTransaction::commit()` returns `Ok`. It is then in the WAL and in memory. Each call is one WAL entry, so a transaction is never applied in part: after a crash it is there in full or not at all. A `WriteTransaction` that is rolled back or dropped without `commit()` writes nothing.
A transaction is **committed** when `execute("(transact ...)")`, `execute("(retract ...)")` or `WriteTransaction::commit()` returns `Ok`. On the ordinary path, where staged facts stay at or below `transaction_chunk_facts`, each commit writes one WAL entry and applies the facts in memory (a checkpoint may then move them into `app.graph`). A transaction is never applied in part: after a crash it is there in full or not at all. An ordinary `WriteTransaction` that is rolled back or dropped without `commit()` writes nothing. [Large transactions](#large-transactions) write working pages before commit and commit directly to `app.graph`.

A **checkpoint** copies the committed transactions from memory into `app.graph` and deletes the WAL. It runs:

- after every `wal_checkpoint_threshold` WAL entries (default 1,000), inside the write that reaches the threshold;
- when `checkpoint()` is called;
- when the last clone of the handle is dropped (a clean close).

`wal_checkpoint_threshold(usize::MAX)` turns off the first and the last: only `checkpoint()` writes `app.graph`, and the WAL holds everything else.
`wal_checkpoint_threshold(usize::MAX)` turns off the first and the last: for ordinary transactions at or below `transaction_chunk_facts`, only `checkpoint()` writes their facts to `app.graph`, and the WAL holds everything else. [Large transactions](#large-transactions) still write `app.graph` and commit as checkpoints regardless of this setting.

A checkpoint is copy-on-write. It writes new pages only where the current meta page does not point, syncs them, then writes the other meta page and syncs again. That meta page is the only commit point. A crash at any moment leaves the previous checkpoint intact, and the WAL still holds what the interrupted checkpoint was writing. Every page carries a CRC32, its own page id and the checkpoint generation that wrote it, checked on every read.
A checkpoint is copy-on-write. It writes new pages only where the current meta page does not point, syncs them, then writes the other meta page and syncs again. That meta page is the only commit point. A crash at any moment leaves the previous checkpoint intact, and transactions already in the WAL can be replayed. Every page carries a CRC32, its own page id and the checkpoint generation that wrote it, checked on every read.

Rules (`(rule ...)`) and registered aggregates and predicates live in memory, not in the file. Register them again after every open (rule persistence is #241).

Expand All @@ -37,11 +37,11 @@ Rules (`(rule ...)`) and registered aggregates and predicates live in memory, no

A crash during a checkpoint is no different: the file keeps the previous checkpoint, and the WAL is replayed.

**`SyncMode::Full`** calls `fdatasync` on the WAL after every entry before the write returns. When it creates the WAL, it syncs the header and the parent directory, so the new file's name survives a power loss too (#389). A committed transaction survives a power loss.
For ordinary transactions at or below `transaction_chunk_facts`, **`SyncMode::Full`** calls `fdatasync` on the WAL after every entry before the write returns. When it creates the WAL, it syncs the header and the parent directory, so the new file's name survives a power loss too (#389). A committed transaction survives a power loss.

**`SyncMode::Normal`** writes the WAL entry with no sync. The entry is in the operating system's page cache, which survives the process but not the machine. It reaches the disk at the next checkpoint, which syncs `app.graph` in both modes. After a power loss, replay stops at the first damaged or missing entry, so what comes back is a prefix of the committed transactions, never a transaction in part and never a later one without an earlier one. Use it for bulk loads that can be re-run from a known point, and call `checkpoint()` at the points you would re-run from. With `SyncMode::Normal` and `wal_checkpoint_threshold(usize::MAX)`, nothing is synced until you call `checkpoint()`.
On that ordinary path, **`SyncMode::Normal`** writes the WAL entry with no sync. The entry is in the operating system's page cache, which survives the process but not the machine. It reaches the disk at the next checkpoint, which syncs `app.graph` in both modes. After a power loss, replay stops at the first damaged or missing entry, so what comes back is a prefix of the committed transactions, never a transaction in part and never a later one without an earlier one. Use it for bulk loads that can be re-run from a known point, and call `checkpoint()` at the points you would re-run from. With `SyncMode::Normal` and `wal_checkpoint_threshold(usize::MAX)`, ordinary transaction entries are not synced unless a later `Full` commit syncs the WAL or a checkpoint makes the facts durable in `app.graph`. Creating the WAL still syncs its header and parent directory; [large transactions](#large-transactions) still commit durably in either mode.

Batching is the other lever: `begin_write()`, several `execute()` calls and one `commit()` write one WAL entry with one sync, in either mode.
Batching is the other lever: `begin_write()`, several `execute()` calls and one `commit()` write one WAL entry while the staged facts stay at or below `transaction_chunk_facts`, with one entry sync in `Full` mode and none in `Normal`. Crossing that threshold takes the [large-transaction path](#large-transactions).

### Per-transaction mode

Expand All @@ -50,10 +50,18 @@ Batching is the other lever: `begin_write()`, several `execute()` calls and one
- A **process crash** keeps every committed transaction, in either mode.
- A **power loss** keeps a prefix of the commit order, never a transaction in part and never a later one without an earlier one. The prefix includes every transaction up to the last `Full` commit, the last checkpoint, or whichever is later: a `Full` commit syncs the whole WAL file, so the `Normal` entries before it become durable with it. Entries committed with `Normal` after the last `Full` commit may be lost.
- A `Normal` commit never weakens a transaction committed before it.
- A **large transaction** (more facts than `transaction_chunk_facts`) commits as a checkpoint, with no WAL entry, so it is durable in both modes; the header of the WAL it creates before its first page write is always synced. The per-transaction mode has no effect on it.
- A [large transaction](#large-transactions) commits as a checkpoint, so it is durable in both modes. The per-transaction mode has no effect on it.

For example, a database opened `Full` can run a bulk load as `Normal` transactions and end it with one `Full` transaction (or a `checkpoint()`), which makes the whole load durable at once.

### Large transactions

On a file-backed database, a transaction becomes **large** when its staged facts would exceed `transaction_chunk_facts` (default 65,536, or the `TxOptions::chunk_facts` override for a `WriteTransaction`). It writes chunks into copy-on-write working trees in `app.graph`. Earlier committed facts still pending in memory join those trees first. Commit syncs the pages, writes the next meta page, syncs again, then deletes the WAL. It writes no WAL entry for the large transaction and is durable in either sync mode, regardless of `wal_checkpoint_threshold`.

Before the first working page write, the transaction ensures a WAL exists as a recovery guard. If it creates the WAL, it writes and syncs the header and syncs the parent directory, even in `Normal` mode; an existing WAL retains its earlier entries.

A large `WriteTransaction` rolled back or dropped without `commit()` may therefore have created that WAL and written index and dictionary pages, long-value pages, and temporary claims-tree pages used to track triples replaced by a statement. These writes may include copies of earlier committed, uncheckpointed facts as well as the transaction's own facts. Rollback publishes no meta page and leaves the committed state unchanged: the written pages are free in the active generation or beyond its page count. It does not erase those bytes or delete the WAL guard. The handle is marked dirty so the next checkpoint writes a meta page before removing the WAL. A crash before the large transaction's commit point likewise leaves its facts uncommitted; only earlier transactions in the WAL are replayed.

**What durability assumes.** A sync is only as good as the storage under it. Minigraf assumes that when `fdatasync`/`fsync` (`FlushFileBuffers` on Windows) returns, the data is on stable storage. Disks or controllers with a volatile write cache that ignore flushes, virtual machines with an "unsafe" disk cache mode, and some network or FUSE filesystems break that assumption, and a power loss there can lose committed transactions or tear a page. A torn or rotted page is detected by its checksum on the next read (`STG-029`); it is not silently read.

**Browser IndexedDB (`BrowserDb.open`).** There is no WAL. Each `transact` and `retract` is a checkpoint whose pages, meta page and freed page keys are written in one IndexedDB `readwrite` transaction, and the returned promise resolves after that transaction completes. Closing the tab before then loses that write, whole. How durable a completed IndexedDB transaction is on a power loss is up to the browser.
Expand Down Expand Up @@ -272,7 +280,7 @@ Some query results change in v3 for existing data; check them against [Upgrading

### Within v3

Patch and minor releases of v3 read every v8 file. A later v3.x release may add an optional on-disk structure and mark it in the meta page's feature bits; an older v3.x then refuses that file with `STG-034` rather than misread it, so going back to an older library after such a release needs a backup from before it. Formats v1–v6 are not readable by v3 (`STG-028`): open them once with v2.x first.
The v8 format is frozen from 3.0.0-alpha: every 3.x release reads and writes files every other 3.x release can read. Under [PHILOSOPHY §7](../PHILOSOPHY.md#7-stability--backwards-compatibility), a format change never ships in a minor or patch release; a future major that changes the format bumps the version in the file header. `STG-034` is a defensive check for unknown bits in the meta page's `required_features`, which a released 3.x should never hit. No 3.x minor or patch may introduce a new required bit. Formats v1–v6 are not readable by v3 (`STG-028`): open them once with v2.x first.

## Errors: retry, reopen, or restore

Expand All @@ -283,7 +291,8 @@ Every error has a code (`MinigrafError::code()`); the [error reference](https://
| **Retry later.** Nothing is wrong with the file | `STG-026` (another process holds the database: wait for it to exit) |
| **Fix the setup.** The file is fine | `STG-025` (this process already has the file open: reuse that handle), `STG-027` (the filesystem cannot lock), `STG-042` (no file at the path, read-only open), `STG-043` (the log writer's target exists), `WAL-006` (the WAL could not be deleted after a checkpoint, usually directory permissions; nothing is lost) |
| **Reopen.** The handle refuses writes; every committed transaction is in the WAL | `STG-046`, `WAL-007`, an I/O error from a write or checkpoint (fix the cause first), `API-001` (a thread panicked while holding the write lock) |
| **Use another library version.** The file is not modified | `STG-006` (format newer than this library), `STG-034` (feature bits from a newer v3.x), `STG-028` (format older than v7: open with v2.x first), `WAL-002` (WAL from another version: open with that version and `checkpoint()`) |
| **Use another library version.** The file is not modified | `STG-006` (format newer than this library), `STG-028` (format older than v7: open with v2.x first), `WAL-002` (WAL from another version: open with that version and `checkpoint()`) |
| **Check the file's origin.** The file is not modified | `STG-034` (unknown `required_features` bits: a defensive check that a released 3.x should never hit; see [Within v3](#within-v3)) |
| **Repair.** Found by `verify()` | `STG-035`, `STG-038`, `STG-039` → `rebuild_indexes()`. `STG-036`, `STG-040` → restore |
| **Restore from backup.** The file is damaged or is not this database; a failed open leaves it unmodified | `STG-029`, `STG-030`, `STG-031` (a damaged page; inside an index, try `rebuild_indexes()` first), `STG-032`, `STG-033`, `STG-041`, `STG-044`, `STG-045`, `STG-002` (not a Minigraf file: check the path) |
| **Restore, or accept losing the WAL** | `WAL-001` (the `.wal` file is not a WAL), `STG-047` (the `.wal` is from a later state than `app.graph`, usually a restore that left the current WAL in place: move the WAL aside, or restore the `app.graph` it belongs to). Copy both files first. Deleting the `.wal` opens the database without the transactions since the last checkpoint |
Expand Down
6 changes: 3 additions & 3 deletions docs/ERROR_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2992,12 +2992,12 @@ its meta later rots. Native recovery keeps this refusal unchanged.

**Error text**: `Unsupported file feature bits {}: the file was written by a newer Minigraf version`

**Cause**: The meta page's `required_features` bitmask has bits this version does not know. A newer Minigraf v3.x release wrote structures this version cannot read.
**Cause**: The meta page's `required_features` bitmask has bits this version does not know. This is a defensive check that a released 3.x should never hit: the v8 format is frozen from 3.0.0-alpha, and every 3.x release reads and writes files every other 3.x release can read. Under [PHILOSOPHY §7](../PHILOSOPHY.md#7-stability--backwards-compatibility), no minor or patch release may introduce a new required bit; a future major that changes the format bumps the file header version instead.

**Resolution**:
- Open the file with the Minigraf version that wrote it, or a newer one. The file is not modified by the failed open.
- Check the file's origin, including whether an unreleased or modified build wrote it. Upgrading between released 3.x versions is not a remedy for unknown required bits. The file is not modified by the failed open.

**Scenario**: A file written by a later v3.x release that enabled an optional on-disk structure, opened with v3.0.0.
**Scenario**: A v8 meta page with a valid checksum contains an unknown required feature bit.

### STG-035 Free-list inconsistency

Expand Down
Loading
0