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:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 4C 70617468 | txt "path" |
| 009 | 6C 6170702E6462 | txt "app.db" |
| 010 | 0F 0A 04000000 0900 0000 | map leaf, size 10: @4 → @9 |
| 01A | 6C 72657461696E | txt "retain" |
| 021 | 02 0A000000000000 00 | i64 10 |
| 02A | 0F 0A 1A000000 2100 0000 | map leaf, size 10: @1A → @21 |
| 034 | 4C 23647572 | txt "#dur" |
| 039 | 4C 50333044 | txt "P30D" |
| 03E | 0F 0A 34000000 3900 0000 | map leaf, size 10: @34 → @39 |
| 048 | 6C 72657461696E | txt "retain" |
| 04F | 0F 0A 48000000 3E00 0000 | map leaf, size 10: @48 → @3E |
| 059 | 6C 737461747573 | txt "status" |
| 060 | 0F 0A 59000000 2A00 0000 | map leaf, size 10: @59 → @2A |
| 06A | 5C 6175646974 | txt "audit" |
| 070 | 0F 0A 6A000000 4F00 0000 | map leaf, size 10: @6A → @4F |
| 07A | 07 0E 01100000 6000 0000 70000000 | map branch, size 14, depth 0 (root), slots 0, 12: @60 → @70 |
| 088 | 4C 23647572 | txt "#dur" |
| 08D | 4C 50543253 | txt "PT2S" |
| 092 | 0F 0A 88000000 8D00 0000 | map leaf, size 10: @88 → @8D |
| 09C | 8C 6465626F756E63 65 | txt "debounce" |
| 0A5 | 0F 0A 9C000000 9200 0000 | map leaf, size 10: @9C → @92 |
| 0AF | 6C 6669656C6473 | txt "fields" |
| 0B6 | 0F 0A AF000000 7A00 0000 | map leaf, size 10: @AF → @7A |
| 0C0 | 07 0E 0C000000 A500 0000 B6000000 | map branch, size 14, depth 0 (root), slots 2, 3: @A5 → @B6 |
| 0CE | 9C 726574656E7469 6F6E | txt "retention" |
| 0D8 | 0F 0A CE000000 C000 0000 | map leaf, size 10: @CE → @C0 |
| 0E2 | 8C 64617461626173 65 | txt "database" |
| 0EB | 0F 0A E2000000 1000 0000 | map leaf, size 10: @E2 → @10 |
| 0F5 | 07 0E 08100000 D800 0000 EB000000 | map branch, size 14, depth 0 (root), slots 3, 12: @D8 → @EB |
| 103 | 7C 706C7567696E73 | txt "plugins" |
| 10B | 0F 0A 03010000 F500 0000 | map leaf, size 10: @103 → @F5 |
| 115 | 0B010000 00000000 | root → @10B; previous: none |
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:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 00 | 54 52 4F 4E | magic TRON |
| 04 | 6C 72657461696E | txt "retain" |
| 0B | 02 05000000000000 00 | i64 5 |
| 14 | 0F 0A 04000000 0B00 0000 | map leaf, size 10: @4 → @B |
| 1E | 7C 63617363616465 | txt "cascade" |
| 26 | 09 | bit true |
| 27 | 0F 0A 1E000000 2600 0000 | map leaf, size 10: @1E → @26 |
| 31 | 07 0E 80010000 1400 0000 27000000 | map branch, size 14, depth 0 (root), slots 7, 8: @14 → @27 |
| 3F | 6C 737461747573 | txt "status" |
| 46 | 6C 706C61636564 | txt "placed" |
| 4D | 0F 0A 3F000000 4600 0000 | map leaf, size 10: @3F → @46 |
| 57 | 14 10 737461726475 73742F686973746F 7279 | txt "stardust/history" |
| 69 | 0F 0A 57000000 3100 0000 | map leaf, size 10: @57 → @31 |
| 73 | 07 0E 01020000 4D00 0000 69000000 | map branch, size 14, depth 0 (root), slots 0, 9: @4D → @69 |
| 81 | 6C 5F6F72646572 | txt "_order" |
| 88 | 0F 0A 81000000 7300 0000 | map leaf, size 10: @81 → @73 |
| 92 | 5C 7061746368 | txt "patch" |
| 98 | 0F 0A 92000000 8800 0000 | map leaf, size 10: @92 → @88 |
| A2 | 4C 636F6465 | txt "code" |
| A7 | 0F 0A A2000000 9800 0000 | map leaf, size 10: @A2 → @98 |
| B1 | 6C 6D6574686F64 | txt "method" |
| B8 | 8C 7472616E736163 74 | txt "transact" |
| C1 | 0F 0A B1000000 B800 0000 | map leaf, size 10: @B1 → @B8 |
| CB | 6C 706172616D73 | txt "params" |
| D2 | 0F 0A CB000000 A700 0000 | map leaf, size 10: @CB → @A7 |
| DC | 07 0E 80010000 C100 0000 D2000000 | map branch, size 14, depth 0 (root), slots 7, 8: @C1 → @D2 |
| EA | DC000000 00000000 | root → @DC; previous: none |
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