History retention: retention
retention removes old versions of facts. It requires database, and removes
history from the database storage. Settings name the fields to prune, and an
entity can carry its own policy in stardust/history. Without retention,
the database keeps every version of every fact.
| Setting | Accepts | Purpose | Default |
|---|---|---|---|
fields | A map from a field name to {retain …} | The history to keep of each field. | No fields |
debounce | A #dur | Time without commits before a removal pass starts. | #dur "PT2S" |
max-wait | A #dur | Longest time after a commit before a removal pass starts. | #dur "PT30S" |
recheck | A #dur | Time between passes without commits, for a #dur or #utc retain, and after a pass that failed or that a history floor held back. | #dur "PT1M" |
Each field takes one retain value:
retain | Kept history |
|---|---|
| An integer N, 1 or more | The newest N values of each fact, and the value before them. |
A positive #dur | The values written within that time before now, and the value before them. |
A #utc | The values written at or after that time, and the value before them. |
Fields that the settings do not name keep all of their history. The plugin
refuses an invalid retain value or an unknown setting when the database
opens.
Dependencies
Configuration
Select this plugin, and database with a path, when opening the database:
{plugins {database {path "app.db"} retention {fields {status {retain 10} audit {retain #dur "P30D"}} debounce #dur "PT2S"}}}
Usage
Removal runs in the background, in small steps that each commit on their own.
A commit that writes facts starts a pass after debounce without more
commits, and not later than max-wait after that commit. A database that
opens starts a pass after debounce, so removal continues after a restart.
Without storage, a pass removes nothing.
Removal never changes the current value of a fact. An as-of read before the
oldest kept value reads the oldest kept value, and no read fails. List values
release their storage chunks, and the database deletes the chunks that no kept
value uses.
Removal never waits for readers, and readers never see it: each request reads
one snapshot. A session opened with as-of, or pinned by namespaces/load with
as-of, reads one snapshot until it closes or resets. While such a session is
open, the database cannot reuse the pages of its snapshot, and the file can
grow.
Entity policies
A patch gives an entity its own policy in the stardust/history field:
{method "transact"
params {code {patch {_order {status "placed" stardust/history {retain 5 cascade true}}}}}}
The policy is {retain …} with an optional boolean cascade, and nothing
else. Other values are refused with #err {code <invalid value>}. null
removes the policy.
retain | Kept history of each field of the entity |
|---|---|
| An integer N, 1 or more | The facts of the newest N transactions that wrote a field of the entity, and the value before them. |
A positive #dur | The facts written within that time before now, and the value before them. |
A #utc | The facts written at or after that time, and the value before them. |
With cascade true, the policy also applies to the descendants of the entity
through stardust/path/parent. A descendant with its own policy uses it. An
own policy without cascade applies to that entity only, so its children
still use the cascading policy above it. The policy map merges into an entity
that stardust/history links to, as every map in a patch does, so a patch
that gives only retain keeps the earlier cascade. If a direct edit makes
the linked policy invalid, pruning stops and retention/status reports
last-error until the policy is repaired.
When a field setting and an entity policy apply to the same fact, the one that keeps more history applies.
History floors
Another plugin can hold history back with a history floor
(Spec.history_floor). A removal step then keeps every fact that a read at
or after the lowest floor needs. A pass that a floor held back sets
held-back, and the plugin runs a pass again after recheck.
Status
The retention/status request reports what removal did. It takes no params
and starts no pass.
| Field | Meaning |
|---|---|
runs | Passes since the database opened. |
last-run | When the last pass ended, a #utc, or null before the first pass. |
entities | The distinct entities whose versions the last pass changed. When memory ran out while counting them, an upper bound, and last-error says so. |
deleted | Versions deleted since the database opened. |
rewritten | Versions that took the oldest kept value since the database opened. |
removed-pairs | Facts with no versions left since the database opened. |
freed-chunks | List chunks deleted since the database opened. |
held-back | A history floor held the last pass back. |
last-error | Why the last pass failed, or null when it did not fail. |
pending | A commit waits for a pass, or a pass runs. |
A pass that fails on a field or an entity records last-error, continues with
the other work, and runs again after recheck.
Contributions
Fields
Patches write these fields only while this plugin is selected.
| Field | Writes |
|---|---|
stardust/history | Any patch, in the shape this page gives |
Requests
retention/status