-
Notifications
You must be signed in to change notification settings - Fork 58
feat(drive): time-range index TTL — O(1) flat-drop drainage and ephemeral-bytes fees #4581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
QuantumExplorer
wants to merge
19
commits into
v4.2-dev
Choose a base branch
from
claude/time-range-ttl
base: v4.2-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
67573e9
feat(drive): time-range index TTL — lazy bucket expiry with walker-ex…
QuantumExplorer 4707009
feat(drive): TTL drainage on grovedb's flat-subtree drop — O(1) steps…
QuantumExplorer 6323f4d
chore: bump grovedb to develop d548e282 (flat-subtree drop #849 merged)
QuantumExplorer 614a123
fix(drive): unbilled TTL drainage preserves the fee-estimation invari…
QuantumExplorer 3ddb505
chore: fix whitespace runs in the ttl lower-bound message; derive the…
QuantumExplorer 141b9c2
fix(drive): updates drain expired TTL buckets too
QuantumExplorer 526f826
feat(drive): TTL ephemeral-bytes fee reclassification — index bytes b…
QuantumExplorer 947de2a
fix(drive): drain TTL levels once per write, before queuing — and on …
QuantumExplorer 220a27a
Merge branch 'claude/time-range-ttl' into claude/ttl-ephemeral-fees
QuantumExplorer 0e74512
chore: drop unneeded mut on the delete-only ttl test closure
QuantumExplorer 532cf01
Merge branch 'claude/time-range-ttl' into claude/ttl-ephemeral-fees
QuantumExplorer 56455e3
test: pin one-drain-budget-per-write by asserting the bucket survives…
QuantumExplorer adc87a0
Merge branch 'claude/time-range-ttl' into claude/ttl-ephemeral-fees
QuantumExplorer 26dc21d
fix(drive)!: strip flags at the ephemeral choke point instead of a v1…
QuantumExplorer b1e2214
test: assert the ttl ephemeral rate is visible to FeeStorageVersion e…
QuantumExplorer 28f6946
refactor(platform-version): fold the TTL limits into the unreleased S…
QuantumExplorer 13d001c
refactor(platform-version): collapse FEE_VERSION3 into the shared fee…
QuantumExplorer ed1c3c9
fix(drive): reject byStart queries past the ttl horizon; raise the dr…
QuantumExplorer 0d82cab
Merge remote-tracking branch 'origin/v4.2-dev' into claude/time-range…
QuantumExplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| # Time-Range Index TTL | ||
|
|
||
| Design document. Status: **implemented**. The grovedb primitive landed as | ||
| the flat-subtree drop | ||
| ([dashpay/grovedb#848](https://github.com/dashpay/grovedb/issues/848), | ||
| grovedb PR #849); see [the dependency section](#grovedb-dependency-flat-subtree-drop) | ||
| for how the shipped shape differs from the original two-phase sketch. | ||
|
QuantumExplorer marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Problem | ||
|
|
||
| A `timeRange` index stores every document once per containing window, and | ||
| a ranked one additionally rewrites a per-window secondary on every write. | ||
| All of those bytes are billed as **storage** — a price that prepays | ||
| ~perpetual retention through the epoch-distribution model — even though | ||
| windowed data is intrinsically ephemeral: a "posts liked this hour" | ||
| bucket is worthless once the trending surface has moved past it. The | ||
| result is that the flagship use case (likes feeding a trending index) | ||
| pays perpetuity prices for state with a useful life measured in days, | ||
| multiplied by the grid's overlap factor. | ||
|
|
||
| Nobody cleans this up, either. Deletion costs the deleter processing, | ||
| refunds accrue to owners who have no reason to come back for entries this | ||
| small, and the state lingers forever. | ||
|
|
||
| ## Proposal | ||
|
|
||
| A `timeRange` index may declare a **time to live**: | ||
|
|
||
| ```json | ||
| "timeRange": { "on": "$createdAt", "range": 3600, "step": 3600, "ttl": 604800 } | ||
| ``` | ||
|
|
||
| Semantics, in one paragraph: entries under this index exist for at most | ||
| `ttl` seconds past their bucket's start. Everything written under the | ||
| index's grid-qualified level is billed as **processing, not storage** — | ||
| including the transitional bytes — at an ephemeral-bytes rate. Expired | ||
|
QuantumExplorer marked this conversation as resolved.
Outdated
|
||
| buckets are dropped **lazily, on write**: the state transition whose | ||
| document creates a *new* bucket also drops up to a capped number of | ||
| buckets whose start has fallen behind `block_time − ttl`. Nothing about | ||
|
QuantumExplorer marked this conversation as resolved.
Outdated
QuantumExplorer marked this conversation as resolved.
Outdated
|
||
| the query surface changes: an expired window is provably absent, exactly | ||
| like a window that never held documents. | ||
|
|
||
| ### Why the fee reclassification is honest, not a subsidy | ||
|
|
||
| Storage fees prepay retention distributed across future epochs — decades | ||
| of it. A byte that provably lives at most one week consumes on the order | ||
| of **1/2,600th** of that retention. The real resource cost of a TTL'd | ||
| write is compute and write amplification (already processing) plus a | ||
| week of disk occupancy, which a flat per-byte processing surcharge covers | ||
| safely *because `ttl` is capped*. Version 1 caps it at **one week** | ||
| (`SystemLimits::max_time_range_ttl_seconds = 604 800`). | ||
|
|
||
| The load-bearing simplification: **TTL'd subtrees never create | ||
| refundable storage.** No `StorageFlags`, no owner/epoch refund entries. | ||
| That single property pays off three times: | ||
|
|
||
| 1. the fee reroute needs no refund-ledger reconciliation; | ||
| 2. cleanup owes nobody anything; | ||
| 3. deletion needs no byte metering for consensus — which is what makes | ||
| O(1) bucket drops possible at all (see the grovedb dependency). | ||
|
|
||
| ## Grammar and validation | ||
|
|
||
| - `ttl` is an optional key of the `timeRange` map, in seconds, parsed | ||
| into the transform. It is **not part of the grid identity**: | ||
| [`TimeRangeTransform::storage_key`] excludes it, so declaring or | ||
| changing a TTL never forks the storage level, and query-side grid | ||
| matching ([`TimeRangeGridSpec`]) continues to compare | ||
| `(range, step, phase)` only. | ||
| - **`ttl ≥ range`.** `$createdAt` is consensus-assigned from block time, | ||
| so writes only ever target windows containing *now*; this invariant | ||
| guarantees no bucket that can still receive entries (or serve as the | ||
| `oldest` selector's window) is ever dropped. | ||
| - **`ttl ≤ SystemLimits::max_time_range_ttl_seconds`** (one week in v1). | ||
| The cap is what makes the flat ephemeral-byte rate safe. | ||
| - **One TTL per grid per field.** Two indexes bucketing the same field | ||
| with the same grid share one storage level; a differing `ttl` would | ||
| give the shared subtree two conflicting lifecycles. Rejected at | ||
| contract validation. | ||
| - Composes with everything the grid already composes with: `countable`, | ||
| the range axes, ranked levels below the bucket, `unique` | ||
| (`range == step`, `$createdAt`), indexOnly document types. | ||
| `preallocated` stays banned with `timeRange` for the pre-existing | ||
| structural reason. | ||
|
|
||
| ## Cleanup | ||
|
|
||
| **Trigger** — deterministic and write-amortized: **every write** into a | ||
| TTL'd index continues drainage of the oldest expired bucket (start | ||
| `< block_time − ttl`), deepest-first, spending at most | ||
| `SystemLimits::max_time_range_ttl_drop_operations_per_write` O(1) drop | ||
| operations and resuming exactly where the previous write's budget ran | ||
| out. When nothing is expired, the check is a single bounded range read. | ||
| The operation count of a full bucket scales with its distinct groups, | ||
| and write volume scales with group volume, so drainage keeps pace | ||
| roughly one window behind; after a quiet spell the backlog amortizes | ||
| across subsequent writes instead of dumping a week of demolition on the | ||
| first like after a lull. | ||
|
|
||
| **Residue** — an index that never receives another write keeps its final | ||
| `ttl` of buckets indefinitely. This is bounded garbage that owes nobody | ||
| a refund. If it ever matters, the backstop is an epoch-transition sweep | ||
| riding the existing scheduled-cleanup pattern | ||
| (`check_for_ended_vote_polls` / `clean_up_after_vote_polls_end`); | ||
| deliberately **out of scope for v1**. | ||
|
|
||
| **User deletes and updates of expired documents** — handled at | ||
| **full-path granularity**, because a bucket drains piecewise: an entry | ||
| whose bucket (or whose group's trees inside a standing bucket) the drain | ||
| already took is skipped as cleanly removed; one whose trees still stand | ||
| is removed normally, so a not-yet-drained expired bucket never carries | ||
| dangling references. Every check is deterministic — it reads consensus | ||
| state plus the carried `$createdAt` and block time. Writes never target | ||
| expired windows, so an update of a fully expired document simply leaves | ||
| it without entries under the TTL'd index. | ||
|
|
||
| **Per-index semantics** — TTL removes entries from *this index only*. | ||
| An indexOnly like whose windowed entries expire keeps counting in the | ||
| all-time ranked `byPost` and in `byLiker`; permanence lives where the | ||
| contract declares it. Ranked per-window secondaries die with their | ||
| bucket — which also caps live leaderboard state at ~`ttl / step` windows | ||
| per index. | ||
|
|
||
| ## grovedb dependency: flat-subtree drop | ||
|
|
||
| Dropping a bucket must never put user-scaled work on the consensus path. | ||
| The primitive that landed (grovedb PR #849) is the **flat-subtree drop**: | ||
| O(1) consensus removal of a subtree *declared to contain no child | ||
| subtrees* — an ordinary parent-Merk element delete whose cost is | ||
| independent of the subtree's contents — staging a durable redo record | ||
| (atomically, outside the root hash) that names every storage prefix the | ||
| drop orphaned: the subtree's own and, for indexed primaries, its three | ||
| per-axis secondary prefixes. Reclamation is DB-level range tombstones, | ||
| drained by `GroveDb::flush_pending_prefix_drops` — idempotent, | ||
| crash-safe, snapshot-correct, and never part of consensus cost. | ||
|
|
||
| A time-range bucket is *not* flat, so the platform drains it | ||
| **deepest-first, one flat unit at a time** (`drain_expired_time_range_buckets`): | ||
|
|
||
| 1. each group's `[0]` reference tree — flat by construction, and where | ||
| the mass lives — is flat-dropped; | ||
| 2. the emptied group value tree leaves through the flat drop — or, under | ||
| a ranked (indexed-primary) property-name tree, through grovedb's | ||
| dedicated indexed-tree delete, which mirrors the group out of the | ||
| ranking secondary; | ||
| 3. the drained property-name tree is flat-dropped (dooming its secondary | ||
| prefixes when ranked); | ||
| 4. the emptied bucket is flat-dropped. | ||
|
|
||
| Every step is O(1); the *number* of steps scales with the window's | ||
| distinct groups, and that count is what | ||
| `SystemLimits::max_time_range_ttl_drop_operations_per_write` bounds. | ||
| **Every write** into a TTL'd index continues drainage where the previous | ||
| budget stopped (when nothing is expired, the check is one bounded range | ||
| read); write volume scales with group volume, so drainage keeps pace | ||
| roughly one window behind. Between writes a bucket may stand partially | ||
| drained — within TTL semantics (entries live *at most* `ttl`) — and the | ||
| removal walkers handle those states at full-path granularity: a | ||
| document whose group the drain already took deletes as a clean skip, | ||
| one whose group still stands is removed normally. | ||
|
|
||
| The flat-drop path-reuse contract (never re-create a dropped path before | ||
| its record drains) holds by construction: bucket paths embed their | ||
| window start, and writes never target expired windows. The host side: | ||
| drive-abci calls `flush_pending_prefix_drops` after committing each | ||
| block's transaction and once at startup, completing reclamation a crash | ||
| may have interrupted. | ||
|
|
||
| ## Fee mechanics | ||
|
|
||
| Write operations targeting a TTL'd index's subtrees are classified | ||
| **ephemeral**: their added bytes bill to processing at an | ||
| ephemeral-bytes rate (a fee-version constant) instead of to storage, and | ||
| the elements carry no storage flags. Deletion (both the TTL drop and a | ||
| user delete of a not-yet-expired document) generates no refunds — there | ||
| is nothing to refund. Cost estimation mirrors the same classification so | ||
| estimated and actual fees stay in the same class. | ||
|
|
||
| ## Queries | ||
|
|
||
| Unchanged. An expired window is a provable empty answer through every | ||
| surface (document, count/sum/avg, ranked, having-range). One documented | ||
| consequence: on a TTL'd index, `byStart` addresses historic windows | ||
| *within the TTL horizon* — beyond it, absence is the (correct, provable) | ||
| answer. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Versioning | ||
|
|
||
| Everything rides the still-unreleased PV14 grammar: the `ttl` key joins | ||
| the meta-schema v3 `timeRange` map, the two limits join a new | ||
| `SystemLimits` version, and the fee constant joins the PV14 fee table. | ||
| No migration story exists or is needed. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.