read model: ETS rebuilt from the log, and no rocksdb (macula-ci-images#19) #26

Manually merged
terra merged 2 commits from juno/19-off-rocksdb into main 2026-11-24 03:10:44 +00:00
Member

Refs macula-io/macula-ci-images#19 — the mcl-mail step of "port them off, then delete".

What changes

The letter read model stops being a barrel_docdb database on the data volume and becomes an ETS ordered_set owned by a process, rebuilt from the event store at every boot.

  • mailboxes_read_model is a gen_server owning two tables: the letters, keyed {CitizenDid, -DepositedAt, LetterId} so a forward walk reads newest first, and a small index for a fetch by id. The API is unchanged (upsert_deposited/2, carried/3, mark_replied/2, mark_archived/2, find/2, list_unarchived/3); open/1 and db/0 are gone with the store.
  • mailboxes_read_model_replay fills it at every start: pages evoq_event_store:read_all_global from position 0 in order, applies each event through the projection's own project/4 (same shape, same metadata as the live feed), and stops only on a SHORT page — the store answering "nothing at that offset". A failed read, a raise, or a failed apply is a failure, never a short page: the owner drops both tables and starts again from 0.
  • A readiness gate. get_mailbox and get_letter answer {error, replaying} until that replay reaches the end of the log, because an empty table would tell a citizen their mailbox is empty while it fills. The flag belongs to the table (design r=1 R2): it is created and dies with the process that owns the table, and project_mailboxes_sup is rest_for_one with the owner first, so a restarted owner takes the projection with it and never opens over an empty table.
    • The gate believes its own driver, nothing else. A report carries the reporting process's pid, and the owner opens reads only for the driver it started; a report from anything else (a driver left over from an earlier life of the owner, which ends normally and so is not killed with it) is logged and ignored. A failed replay drops the tables and starts a new driver with the gate shut, and the owner traps exits, so a driver that dies mid-page is a table dropped and refilled rather than the owner's own death.
  • No rocksdb anywhere. barrel_docdb and the rocksdb override are gone from rebar.config; barrel_docdb/project_mailboxes are gone from config/sys.config.src; the image builds in macula-ci-otp and runs on macula-pq-runtime, both pinned by digest on the forge registry, as the siblings do. README, architecture/README.md and the C4 diagrams (SVGs regenerated) say the same. Version 0.7.0.

Proof

Gates on the tip, in the pinned macula-ci-otp@sha256:3e202cf7… image: compile rc=0, rebar3 lint (MCL_LINT=1) rc=0, rebar3 eunit: All 148 tests passed, rebar3 dialyzer rc=0. (rebar3 xref is red on mcl_mail_store:ensure/2,/5, unused exports, the same on main; it is not a gate here.)

New tests: the store tests now run on the ETS model, including the two readiness cases (a read refused until caught up, and a restarted model replaying before it answers); two more pin the gate itself — a report from another process leaves reads refused, and a failed replay leaves the owner answering replaying; and the service tests replace the rocksdb pin guards with the plain-pair pins and "no rocksdb dependency or override".

The replay at ~12 MB of letters

3,000 deposits of 4 KB through the real CMD desks into a store of 82 MB on disk, one owner started, the clock from start_link/0 to the first answered read: letters_read=3000 of 3,000, table=3000, a fetch by id ok, replay_ms=110 — with the store's read index emptied first, so the replay pays the scan-and-sort a restart pays; with that index already built (as a boot's own subscription catch-up leaves it) the same replay is 65 ms. The measurement is a script, not part of the repository; the numbers are in the handoff.

ci= for the tip is none on the forge for a reason that is not this branch's: both pull-request runs queued and no job on any repository has started for over half an hour (the runner pool), so the proof above is the same four gates run by hand in the pinned image.

Behaviour changes, in the CHANGELOG

  • Two letters deposited in the same millisecond now list by letter_id ascending (the table's key order) instead of descending. The order stays total and flag-free, which is what a page cursor needs; no cursor outlives a release.
  • A letter read once under 0.6.1 has one carry fact in the log, so after the cutover it gets the one grace read and is then forgotten. The old store's documents — and the read => true shape the purge rule used to recognise — are not carried over: the model has no history but the log.

Not in the suite

The replay against a real store: a test's reads reach evoq's own .app default store id, not the service's, because sys.config is what sets it at a release boot; the store itself starts. That gap is filed as mcl-mail#27.

Refs macula-io/macula-ci-images#19 — the mcl-mail step of "port them off, then delete". ## What changes The letter read model stops being a barrel_docdb database on the data volume and becomes an ETS `ordered_set` owned by a process, rebuilt from the event store at every boot. - **`mailboxes_read_model`** is a gen_server owning two tables: the letters, keyed `{CitizenDid, -DepositedAt, LetterId}` so a forward walk reads newest first, and a small index for a fetch by id. The API is unchanged (`upsert_deposited/2`, `carried/3`, `mark_replied/2`, `mark_archived/2`, `find/2`, `list_unarchived/3`); `open/1` and `db/0` are gone with the store. - **`mailboxes_read_model_replay`** fills it at every start: pages `evoq_event_store:read_all_global` from position 0 in order, applies each event through the projection's own `project/4` (same shape, same metadata as the live feed), and stops only on a SHORT page — the store answering "nothing at that offset". A failed read, a raise, or a failed apply is a failure, never a short page: the owner drops both tables and starts again from 0. - **A readiness gate.** `get_mailbox` and `get_letter` answer `{error, replaying}` until that replay reaches the end of the log, because an empty table would tell a citizen their mailbox is empty while it fills. The flag belongs to the table (design r=1 R2): it is created and dies with the process that owns the table, and `project_mailboxes_sup` is `rest_for_one` with the owner first, so a restarted owner takes the projection with it and never opens over an empty table. - **The gate believes its own driver, nothing else.** A report carries the reporting process's pid, and the owner opens reads only for the driver it started; a report from anything else (a driver left over from an earlier life of the owner, which ends normally and so is not killed with it) is logged and ignored. A failed replay drops the tables and starts a new driver with the gate shut, and the owner traps exits, so a driver that dies mid-page is a table dropped and refilled rather than the owner's own death. - **No rocksdb anywhere.** `barrel_docdb` and the `rocksdb` override are gone from `rebar.config`; `barrel_docdb`/`project_mailboxes` are gone from `config/sys.config.src`; the image builds in `macula-ci-otp` and runs on `macula-pq-runtime`, both pinned by digest on the forge registry, as the siblings do. README, `architecture/README.md` and the C4 diagrams (SVGs regenerated) say the same. Version 0.7.0. ## Proof Gates on the tip, in the pinned `macula-ci-otp@sha256:3e202cf7…` image: compile rc=0, `rebar3 lint` (MCL_LINT=1) rc=0, **`rebar3 eunit`: All 148 tests passed**, `rebar3 dialyzer` rc=0. (`rebar3 xref` is red on `mcl_mail_store:ensure/2,/5`, unused exports, the same on main; it is not a gate here.) New tests: the store tests now run on the ETS model, including the two readiness cases (a read refused until caught up, and a restarted model replaying before it answers); two more pin the gate itself — a report from another process leaves reads refused, and a failed replay leaves the owner answering `replaying`; and the service tests replace the rocksdb pin guards with the plain-pair pins and "no rocksdb dependency or override". ### The replay at ~12 MB of letters 3,000 deposits of 4 KB through the real CMD desks into a store of 82 MB on disk, one owner started, the clock from `start_link/0` to the first answered read: **`letters_read=3000` of 3,000, `table=3000`, a fetch by id `ok`, `replay_ms=110`** — with the store's read index emptied first, so the replay pays the scan-and-sort a restart pays; with that index already built (as a boot's own subscription catch-up leaves it) the same replay is 65 ms. The measurement is a script, not part of the repository; the numbers are in the handoff. `ci=` for the tip is `none` on the forge for a reason that is not this branch's: both pull-request runs queued and no job on any repository has started for over half an hour (the runner pool), so the proof above is the same four gates run by hand in the pinned image. ## Behaviour changes, in the CHANGELOG - Two letters deposited in the same millisecond now list by `letter_id` ascending (the table's key order) instead of descending. The order stays total and flag-free, which is what a page cursor needs; no cursor outlives a release. - A letter read once under 0.6.1 has one carry fact in the log, so after the cutover it gets the one grace read and is then forgotten. The old store's documents — and the `read => true` shape the purge rule used to recognise — are not carried over: the model has no history but the log. ## Not in the suite The replay against a real store: a test's reads reach evoq's own `.app` default store id, not the service's, because `sys.config` is what sets it at a release boot; the store itself starts. That gap is filed as mcl-mail#27.
read model: ETS rebuilt from the log, and no rocksdb
Some checks failed
c4 model / c4-model-1 (pull_request) Successful in 3s
c4 model / c4-model (pull_request) Successful in 0s
lint-and-test / check (pull_request) Successful in 3m42s
lint-and-test / The tested lock arrives whole (pull_request) Has been cancelled
58cff2d1cd
The letter read model was a barrel_docdb database on the data volume, which put
the erlang rocksdb binding -- and a system librocksdb -- under a service whose
letters are already durable in reckon-db and which replayed that log in full at
every boot anyway.

mailboxes_read_model now owns one ordered_set keyed
{CitizenDid, -DepositedAt, LetterId}, plus a small index for a fetch by id.
Nothing of it is written to disk: mailboxes_read_model_replay reads the whole
event store through the projection's own project/4 at every start, in order,
from position 0, and stops only on a short page -- so the replay and the live
feed cannot disagree about what an event means, and every apply is idempotent
the way the model already was.

A read is refused, by name (replaying), until that replay reaches the end of the
log: an empty table would answer "your mailbox is empty" while it fills. The
flag belongs to the table -- it is created and dies with the process that owns
it, and rest_for_one restarts the projection with it -- so a restarted model is
never open over an empty table.

barrel_docdb and the rocksdb override are gone with it; the image builds in
macula-ci-otp and runs on macula-pq-runtime, both pinned by digest on the forge
registry. Two behaviour changes, both in the CHANGELOG: same-millisecond ties
now list by letter id ascending, and a letter read once under 0.6.1 gets the one
grace read after the cutover.

Refs macula-ci-images#19
Read model gate: a report counts only from its own replay driver
All checks were successful
c4 model / c4-model-1 (pull_request) Successful in 4s
c4 model / c4-model (pull_request) Successful in 0s
lint-and-test / check (pull_request) Successful in 3m36s
lint-and-test / The tested lock arrives whole (pull_request) Successful in 1s
3d296acefd
The gate opened on any `replayed` report that reached the owner, because the
report carried no sender. The owner is called by name, and a driver that ends
normally is not killed with its owner, so a leftover driver could open a later
owner's gate over a table nobody filled. The replay now reports with its own
pid, and the owner believes only the driver it started -- for the failure
report too. After a report it is no longer the driver, so the exit that
follows is not treated as a failure.

A failed replay also left the owner's state as its own `{noreply, State}'
return value, so every later read answered `{error, unknown_request}' -- a
refusal that does not look like `replaying', so nothing upstream noticed. And
`handle_info/2`'s driver-exit clause could never run, because the owner did
not trap exits: it does now, so a driver that dies mid-page is a table dropped
and filled again rather than the owner's own death.

Two tests, both without a store: a report from another process leaves reads
refused, and a failed replay leaves the owner answering `replaying'.

Refs macula-ci-images#19
terra manually merged commit 80f80fb1df into main 2026-11-24 03:10:44 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
macula-services/mcl-mail!26
No description provided.