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
7 changes: 7 additions & 0 deletions .changeset/personal-categories-ahead.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@bounda-dev/core": minor
"@bounda-dev/postgresql": minor
"@bounda-dev/cloudflare": minor
---

The store keeps a catalogue of the personal-data categories a key was ever given in, `bounda_personal_categories`, which `personalKeys.add` records in its own transaction and a storage migration fills from the keys already there; `PersonalKeyStore` gains `recordedCategories()`, which an adapter of your own implements, and inspection lists it as the `personalCategories` store. An app reads it at start, so a category newer code adds while a deploy rolls is never forgotten by an instance of the older code, which cannot tell whether that code keeps it: a forget of a person as a whole, and `forgetTenant` whatever `despiteLegalObligations` says, fail with the new transient `CategoryAheadError` (`SCHEMA_AHEAD`, naming the category), forgetting nothing, which `@bounda-dev/react-router` and `@bounda-dev/cloudflare` answer with 503; a reaction's `forget` tries again every `dispatcher.backoff.baseDelay` and the retention holds the person to try again later, neither spending an attempt; the retention keeps what a forgetting kept in that category. The forget ledger's reconciliation waits to forget such a category again, a forget of other categories goes ahead, and a category the store held at start that the app no longer declares is forgotten as before. A `CategoryAheadError` thrown inside a Durable Object reaches the Worker as itself. Roll forward: once the newer code runs on every instance, the forget goes through.
10 changes: 10 additions & 0 deletions .changeset/stored-event-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@bounda-dev/core": minor
"@bounda-dev/sqlite": minor
"@bounda-dev/postgresql": minor
"@bounda-dev/cloudflare": minor
"@bounda-dev/react-router": minor
"@bounda-dev/cli": minor
---

The store keeps a catalogue of the kinds of event it holds, `bounda_event_types` (aggregate type, type and schema version), which every append records in its own transaction and a storage migration fills from the events already there. An app checks it at start and refuses, with a `ConfigurationError` naming each, a store holding an event newer code wrote: a schema version past the one its module reads, or an event of the runtime's own a newer Bounda wrote. An event newer code writes while the app runs is refused by every read with the new transient `SchemaAheadError` (`SCHEMA_AHEAD`), naming the event and the version this code reads, and never read in a shape it was not written in: a command on its stream fails with it, which `@bounda-dev/react-router` and `@bounda-dev/cloudflare` answer with 503; a policy, process, recipient notice or scheduled command tries again every `dispatcher.backoff.baseDelay`, spending none of its attempts, and is never dead-lettered, whatever its retry says; each subscriber stops right before the event on its own, its lag naming it; a rebuild pauses, to go on once that code runs; `app.exportPersonalData`, a redaction, a restriction and `bounda forgotten` refuse it. An event of a kind no module reads that the catalogue held at start is read as stored, as before; one no Bounda writer recorded fails with a `ConfigurationError`. `EventStore` gains `types()`, which an adapter of your own implements, as `InboxLedger` gains `release()`, which settles a claim as failed and takes back the attempt it counted; `EventStore.reseal` takes, for each event, `from`, what the caller read it as, writes the event only while it still holds that, and resolves to the positions it left, so the backfill of personal data written before its marks never writes back an event another instance wrote again while it read it, in a newer shape or sealed, and reads it again on its next pass; inspection lists the catalogue as the `eventTypes` store. `RUNTIME_SCHEMA_VERSION` and `SCHEDULED_COMMAND_FAILED_EVENT` are exported, and an event named `ScheduledCommandFailed` is refused by validation and by `bounda generate`, since the catalogue could not tell it from the runtime's. A `SchemaAheadError` thrown inside a Durable Object reaches the Worker as itself, with every field. Deploy a change to an event's schema by rolling forward: an instance running the older code waits for the newer one instead of misreading what it wrote.
2 changes: 2 additions & 0 deletions docs/src/content/docs/adapters/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ it brings them up to the version the installed Bounda expects
| Table | Holds |
| --- | --- |
| `bounda_events` | Every event, with its stream version, a global position and the person it names, if any, which [`exportPersonalData`](/guides/personal-data/#exporting-a-persons-data) reads by |
| `bounda_event_types` | Each kind of event the store holds, by aggregate type, type and schema version, which every append records in its own transaction and a start [checks against the code](/guides/changing-events/#what-the-runtime-checks) |
| `bounda_checkpoints` | How far each projection, the policy runner, the process runner, the retention and the [forget ledger](/guides/personal-data/#backups)'s copy have read, where a paused rebuild stands, the time of the last ledger entry the database applied, when the last reconciliation ran and the wait of one that failed, when the [key rotation](/guides/personal-data/#rotating-the-tenants-key) was last found complete, the leases of the [background jobs](/guides/deployment/#more-than-one-instance), the counters that tell a [restore](/guides/personal-data/#restores), and, for a read model in a database of its own, the event each of its checkpoints stands at, there, and how far it reached, in the storage's |
| `bounda_inbox` | Which handler already completed for which event, so a retry does not run it again |
| `bounda_scheduled_commands` | Scheduled commands, process deadlines and timeouts, and delayed policy runs, each with when it was scheduled |
Expand All @@ -97,6 +98,7 @@ it brings them up to the version the installed Bounda expects
| `bounda_personal_keys` | One key per person, which their [personal fields](/guides/personal-data/) are encrypted with, wrapped under the [tenant's key](/guides/personal-data/#where-the-keys-live) whose version it names, and the key's fingerprint, and the wait of a rewrap that failed; `forget` deletes it |
| `bounda_index_keys` | The database's key, which derives the [blind indexes](/guides/personal-data/#sealed-at-rest) of the database's read models and seals snapshots and the remembered answers that name nobody, wrapped as a person's key is, and its fingerprint |
| `bounda_superseded_keys` | Each person's key a [reseal](/guides/personal-data/#recovering-from-a-full-compromise) replaced, with the fingerprint of the key that took its place, which a read opens values sealed under it with until the reseal destroys it, keeping only its fingerprint, so a [restored copy](/guides/backups/#what-is-guaranteed) still forgets whoever a forgetting since named |
| `bounda_personal_categories` | Each [category](/guides/personal-data/#categories) a key was ever given, which every new key records in its own transaction and a start reads to tell a category the app stopped declaring from one only [newer code](/guides/deployment/#deploying-a-change-to-events) declares |
| `bounda_tenancy` | The [tenant](/guides/personal-data/#tenants) the database belongs to, the random id its tenant's key names it by, when it was bound and when the tenant was [forgotten](/guides/personal-data/#forgetting-a-tenant) |
| `bounda_tenant_fence` | The database's [fence](/guides/personal-data/#freezing-a-tenant), no row while it is open: whether its tenant is frozen or forgotten, and since when, which every write checks in its own transaction |
| `bounda_persons` | One row per person the runtime knows something about: since when their oldest [restriction](/guides/personal-data/#restricting-processing) holds, `NULL` while none does, and, with a [retention](/guides/personal-data/#retention), when they were last active and last forgotten, the position of the last event recorded for them, and the wait of a forget the retention could not do |
Expand Down
36 changes: 34 additions & 2 deletions docs/src/content/docs/guides/changing-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,47 @@ reason, rather than deriving them from today's event.
current shape, so the fields marked today are sealed under the names they have today, once no
instance of the previous code runs.
- An event written with a version this code does not know, because a newer deploy wrote it,
is refused with an error naming the event. Roll forward, never back, once new events exist, or
once events were rewritten in a newer shape.
is never read in a shape this code does not know: see
[what the runtime checks](#what-the-runtime-checks). Roll forward, never back, once new events
exist, or once events were rewritten in a newer shape.

The types keep you honest in one direction: the last upcaster must return the current payload, so
a change to `payload` in the event module fails to compile until the upcast module catches up.
They do not check the links in between: `Event.Upcasts` does not verify that each upcaster accepts
what the previous one returns. Annotating every step's parameter and return type, as above, is
what keeps the chain straight.

## What the runtime checks

Every append records the kind of each event it writes, its aggregate type, type and schema
version, in the store's catalogue of event types (`bounda_event_types`), in the same transaction.
A database written before the catalogue has it filled from its events when it migrates.

- **At start.** An app, a [rebuild](/guides/deployment/#rebuilding-a-read-model) and the commands
of the CLI that open the store read the catalogue, and refuse to start, with a
`ConfigurationError` naming each kind, while it holds an event this code cannot read: a schema
version past the one its upcasters reach, or a type of the runtime's own events, or of a
process's lifecycle, that this Bounda does not know.
- **While it runs.** An event newer code wrote after the start, in a schema version past this
code's or of a type it has no module for, is never read in a shape it does not know: every read
that meets it rejects with a [`SchemaAheadError`](/reference/errors/#codes), `SCHEMA_AHEAD`.
- A command fails, and the hosts answer 503: the same request passes on an instance that runs
the newer code.
- A policy, a process, a notice to a recipient, a scheduled command, a deadline and a delayed
policy try again every `dispatcher.backoff.baseDelay`, spending none of their attempts, and
are never given up or dead-lettered for it.
Retrying a dead letter by hand rejects, and the letter stays `failed`.
- A projection, and every other subscriber, stops before the event: the events before it go
through, its lag shows where it waits, and its last failure names the event.
- A rebuild stops, keeping what it built for the next one; an export stops whole, never in part.
- Nothing rewrites it in place: neither the backfill of personal data, a redaction nor a
reseal of keys.
- **An event no Bounda wrote.** One this code cannot read whose kind the catalogue does not hold
was written by something other than Bounda: reads reject with a `ConfigurationError` instead.

A kind of event the catalogue held at the start whose module the code no longer has is read as
stored, as before the catalogue: see [what is not covered yet](#what-is-not-covered-yet).

## When to write one, and when not to

- **A field renamed, split, merged or re-typed**: an upcaster.
Expand Down
23 changes: 23 additions & 0 deletions docs/src/content/docs/guides/deployment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,29 @@ column, or an index for a field newly `unique()` or `index()`; one that loses a
type, moves its primary key or drops `unique()` needs the rebuild below
([evolving a read model](/adapters/#evolving-a-read-model)).

## Deploying a change to events

A deploy that adds an upcaster, a new event type or a new version of Bounda's own events can roll
across instances while they run, unlike one that migrates the tables: the store's catalogue of
event types tells each instance which events it can read
([what the runtime checks](/guides/changing-events/#what-the-runtime-checks)).

- An instance of the previous code that meets an event the new code wrote never reads it in a
shape it does not know. A command that meets it fails with
[`SCHEMA_AHEAD`](/reference/errors/#codes), which the hosts answer with a 503, so a load
balancer sends it again elsewhere; reactions and scheduled work try again every
`dispatcher.backoff.baseDelay`, spending none of their attempts; a projection stops before the event, and its lag in `bounda status` and
`app.status()` grows until an instance of the new code projects it.
- An instance of the previous code that starts once the store holds such an event is refused,
with a `ConfigurationError` naming each kind it cannot read.
- A deploy that adds a personal-data category rolls the same way: an instance of the previous code
never forgets keys the new code gave in it, refusing a forget with `SCHEMA_AHEAD` and holding
its reactions and retention until the new code runs
([changing a category](/guides/personal-data/#changing-a-category)).
- Roll forward, never back: once the new code wrote an event, only code that reads it can serve.
To take the change back, deploy code that still reads the events, with the upcaster or the type
kept, rather than the code before it.

## Rebuilding a read model

A read model is derived data: when its projection had a bug, or its view lost a field or changed a
Expand Down
21 changes: 20 additions & 1 deletion docs/src/content/docs/guides/personal-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@ value no key opens any more, its
person forgotten in its old category, stays as it is: it was forgotten already. Going back to
earlier marks walks the stream again too.

A category a deploy adds is one the previous code does not know while the deploy rolls across
instances, so it cannot tell whether the new code keeps a key given in it. The store records every
category a key was ever given in, and an instance reads that record when it starts: a category
recorded then that the app does not declare is one removed, forgotten as above, and one recorded
after the start is the new code's. Its keys are never taken by the previous code: a forget of a
person as a whole, and a tenant's forget whatever `despiteLegalObligations` says, fails with
[`SCHEMA_AHEAD`](/reference/errors/#codes) (`CategoryAheadError`, naming the category), forgetting
nothing; a reaction's `forget` tries again every `dispatcher.backoff.baseDelay`; the
[retention](#retention) holds the person to try again later and keeps what a forgetting kept in
that category; and the forget ledger's reconciliation waits for the new code to forget again what
it forgot in that category, all spending none of their attempts. A forget of other categories goes
ahead. Once the new code runs on every instance, the forget goes through: roll forward, never back.

## In a command's payload

A command is stored too when it waits: dispatched with `delay`, retried by the scheduler after a
Expand Down Expand Up @@ -525,6 +538,8 @@ running the previous code in a rolling deploy, which may not know the current ve
left in their stored shape and sealed as their marks say, and the walk goes back to the first it
left once that instance has stopped; `caughtUp` waits for it. From then on an instance of the
previous code refuses those events, as it refuses the events a newer deploy writes: roll forward.
An event another instance wrote again while a walk read it, in its current shape or sealed, is
never written over: the walk stops before it and reads it again on its next pass.

An answer remembered under an [`idempotencyKey`](/guides/idempotent-commands/) is sealed under the
marks of its day, which said whom it quotes and in which categories: one sealed under other marks
Expand Down Expand Up @@ -1800,7 +1815,11 @@ recipient is presumed to hold every category it declares of every person whose d
each event of theirs names it with `presumed: true`, until a forgetting told it to erase them; and
the worker tells it, presumed, in order, of every forgetting, restriction, lift and redaction the
store recorded before it was declared, a batch of events per pass, while its later notices wait.
`bounda status` says so for as long as it lasts.
`bounda status` says so for as long as it lasts. What a presumed recipient holds is read from the
person's stream: while that stream holds an event a newer Bounda wrote, a forgetting, restriction,
lift or redaction of the person that would tell one fails with
[`SCHEMA_AHEAD`](/reference/errors/#codes), writing nothing, and what the worker would tell it
waits, giving up nothing, until that code runs.

A port renamed keeps what its recipient holds when the new one lists the old key, `"<aggregate or
read model>.<port>"`, in `recipient.formerly`: the app moves its disclosures to the new port when it
Expand Down
3 changes: 2 additions & 1 deletion docs/src/content/docs/reference/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ does files that start with `_` or `.`, tests (`*.test.ts`, `*.test-d.ts`), decla
- a command or a policy that is a directory, a process that is a file, a process directory without
`index.ts`, a read model without `view.ts`, a projection or a query that is a directory;
- a projection outside a folder named after an aggregate of the app, or after `bounda` with a
name that is no event of the runtime's; an aggregate named `bounda`;
name that is no event of the runtime's; an aggregate named `bounda`; an event named
`ScheduledCommandFailed`, which the runtime appends to a stream itself;
- a process handler not named `on-<event>.ts` or `at-<deadline>.ts`, or one for an event its
aggregate does not have;
- a module at an aggregate's root that exports an event's function (`payload`, `begin`, `evolve`)
Expand Down
15 changes: 15 additions & 0 deletions docs/src/content/docs/reference/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ dead letter with `errorType: "retriable_exhausted"`.
knows: `scope` (`storage`, `checkpoints` or `forget-ledger`), its `version`, and `supported`,
the newest this version knows. The app does not start and nothing is written: Bounda never takes
a schema back. Run that version or a later one, or restore a backup taken before it migrated.
- **`SCHEMA_AHEAD`** (`SchemaAheadError`, `CategoryAheadError`; waited out, never given up). A
read met an event newer code wrote after the app started, in a schema version past the one this
code reads or of a type it has no module for
([what the runtime checks](/guides/changing-events/#what-the-runtime-checks)): `SchemaAheadError`,
with `eventId`, `aggregateType`, `aggregateId`, `type`, `position`, `schemaVersion`, and `known`,
the version this code reads, `null` when it has no module for the type. Or a forget would take
keys newer code gave in a personal-data category this code does not declare
([changing a category](/guides/personal-data/#changing-a-category)): `CategoryAheadError`, with
`category`. A command, a forget of a person as a whole or a tenant's forget fails with it; a
policy, process, scheduled command, deadline or delayed policy tries again every
`dispatcher.backoff.baseDelay`, spending none of its attempts, and never becomes a dead letter
for it; the retention and the forget ledger's reconciliation hold and try again later, spending
none either; a projection or other subscriber stops before the event; a rebuild or an export
stops whole. Deploy the newer code on every instance: roll forward, never back.
- **`CLAIM_LOST`** (`ClaimLostError`; handled by the runtime). A reaction's inbox claim expired and
another instance took it over. The attempt writes nothing; the other instance's run is the one
that counts.
Expand Down Expand Up @@ -188,6 +202,7 @@ The hosts map the codes a client can cause to a status:
| `TENANT_FROZEN` | 423 | 423 |
| `READ_MODEL_RESTORING` | 503 | rethrown |
| `RESTORE_UNRECONCILED` | 503 | rethrown |
| `SCHEMA_AHEAD` | 503 | 503 |
| `IDEMPOTENCY_KEY_CONFLICT` | 500, code `INTERNAL`, logged | 422 |
| `IDEMPOTENT_ANSWER_ERASED` | 500, code `INTERNAL`, logged | 410 |
| `CONCURRENCY_CONFLICT` | 409 | rethrown |
Expand Down
Loading
Loading