Rebuild and Replay
The log is the only fact. Snapshots, read models, the warehouse and search indexes are derived from it, so any of them can be rebuilt from it, without a migration script and while the service runs. This page lists the ways, from the most common.
A read model: a new subscription
A projection's position is its subscription's checkpoint, so a read model is rebuilt by giving the projection a subscription without one:
- Deploy the changed projection under a new name (a new class, or
@Name), writing to a new table or index. A projection without a checkpoint starts at the beginning of the log and fills its store with the history;wow.subscriptions.[<context>.<processor.function>].startcan start it at an instant instead: at the first entry of each partition committed at or after it, handling every entry after that one, whatever its commit time. Finding it reads the partition from the beginning once, on the first start, without handling what it reads. - Wait until it has caught up: its
wow.subscription.backlogfalls to zero andwow.subscription.progressreaches 1. - Switch the reads to the new store.
- Remove the old projection, and drop its store.
The old and the new read model run side by side until the switch, so the reads never see a half-built model. A reactor or a saga is never replayed this way: a new one starts at the end of the log, so deploying it does not repeat effects or commands for the past.
Snapshots: raise the state revision
When the stored snapshots no longer read as the state, or no longer hold what the state now derives, raise the aggregate's @AggregateRoot(stateRevision = n) and deploy (Evolve Events). The service rebuilds the stored snapshots in place, in the background:
- The nodes of the new revision claim the snapshot table at it. During a rolling upgrade, the nodes of the old revision keep writing at theirs, and the store keeps them from overwriting the new revision's snapshots. Their rebuilds, rebuild requests and access walks read the table's claim before they rewrite a snapshot and again after: one rewritten while the table was claimed at the new revision is handed to its rebuild, so none stays at the old revision.
- One node at a time rewrites the table's snapshots from the log, page by page, and marks the table rebuilt when none is left.
- Once no snapshot is left to rewrite, the node marks the table rebuilt and scans it from the first once more. A snapshot pending then that is not parked was written before the mark, by a node of the old revision, so the table is marked rebuilding again and the rebuild goes on (
… was pending once its table was marked rebuilt …: the table is marked rebuilding again.). A failure or a stop during that scan marks the table rebuilding again too. The rebuild is done when that scan finds nothing but parked snapshots; the node then logsThe snapshots of <type> in <store> are rebuilt at state revision <n>.A node whose round read the table between the two marks answers the type's queries withoutWow-Rebuilding: trueuntil its next round, up to a maintenance interval after the second mark. - A snapshot it cannot rewrite does not hold the table back. One whose history no longer replays (an event the model cannot read any more, a sourcing function that throws), whose read or rewrite the store refuses for its own data, or whose log holds no stream of it, is parked in the failure ledger under
wow.snapshot.rewrite, and the rebuild goes on with the others. A store that is down, busy or slow is not such a failure: the run stops and the next one resumes where it stopped. Once only parked snapshots are left, the table is marked rebuilt and queries stop answeringWow-Rebuilding: true. A parked snapshot stays as it was stored, of the earlier revision and still pending a rewrite: a query returns it as it returns any snapshot not rewritten yet, with no mark of its own, and a command to the aggregate replays its log as it would with no snapshot, so it fails where the rebuild did. Each parked snapshot is logged once (The snapshot-rewrite of <type> skipped <aggregate> (<reason>)), counted inwow.maintenance.skipped(kindsnapshot-rewrite), and listed with the other parked entries (Parked aggregates). Nothing retries it automatically: after a deployment that reads its history again, retry the entry, which rewrites the snapshot from the log at the table's revision and resolves it (on a node of another revision it is left for that revision's nodes, whose rebuild resolves it). Skipping the entry resolves it and leaves the snapshot as it is, still pending: nothing claims it was rewritten, and the count of skips stays.
Snapshots: rebuild some on request
A snapshot that is wrong for another reason, a defect fixed since, say, is rewritten from the log on request:
curl -X POST "http://<service>/<context>/<aggregate>/wow.snapshot.rebuild-snapshot" \
-H "Content-Type: application/json" \
-d '{"ids": ["SO-261031-1", "SO-261031-2"]}'The route sends the system command wow.snapshot.RebuildSnapshot to the context's built-in rebuild aggregate, through admission like any command, so the authorizer decides who may send it; a service refuses it on the public port (403 wow.auth.forbidden) unless an authorizer decides Wow's operational commands, never allowing it by default (Authorize commands). A built-in subscription then rewrites each named snapshot from the log, in every target store. An id whose history no longer replays is parked under wow.snapshot.rewrite, as a snapshot the in-place rebuild cannot rewrite is (Snapshots: raise the state revision), and the request's other ids are rewritten.
A new snapshot store
A store a type's snapshots are first written to is filled from the beginning of the log. That is a type's own store changed to another (wow.snapshot.store, or wow.aggregates.[<context>.<aggregate>].snapshot.store), or a copy its queries read (.query.snapshot.store), an Elasticsearch index beside PostgreSQL, say. A new store's own snapshot subscription starts at the beginning. For a store whose subscription is already past it, the default store's included, a subscription of its own does, and stops once the store has caught up. Meanwhile the type's snapshot queries answer rebuilding. A changed store is so a backfill from the log: the commands that load an aggregate before its snapshot is written there replay its history, and the old store is left as it was.
A snapshot table removed under its record
A store creates an aggregate type's snapshot table (an Elasticsearch index, a PostgreSQL table, a MongoDB collection) before it records the table in wow_snapshot_table, and never creates it again after that. A table missing while its record is kept was removed under it: it holds nothing the log committed before, so it is never served empty.
- Its queries are refused with
wow.query.dataset-unavailable(409), which names the type and the store and says why. Nothing is answered from it. - Its loads read the log, so the type's commands and state reads go on, each replaying the aggregate's history.
- Nothing is written to it. The store's snapshot subscription passes the type's entries over and parks none: they are the log's. A rebuild request or a raised state revision writes nothing to it either, and never marks it rebuilt.
- The node logs an error at its start and when it finds the table removed, naming the type, the store and the key whose raise fills it again (below). The table's record says
removedfrom then on, for every node. - Putting the table back does not serve it, from a backup or by hand: once recorded, it stays removed until its generation is raised, which fills it again from the log's beginning.
A store finds it at its first use of the table after a start. On a running node, Elasticsearch and PostgreSQL find it at the first statement that misses the index or the table, whichever comes first: a write, which then writes nothing and fails nothing, a query, which is then refused, and on PostgreSQL a load too; a load that misses reads the log on both. No aggregate is parked for it, and nothing is created for it. MongoDB finds it at the next read of the table's record: a maintenance round's (wow.snapshot.maintenance-interval), a rebuild request's, or one a page of the access walk or of the in-place rebuild makes. Until then a write recreates the collection, as MongoDB does for any write, and the type's queries answer from what was written since. On MongoDB the mark of a collection is its owner index, owner_ix, which the store creates with the collection before it records the table: a collection whose owner_ix alone was dropped, its documents kept, is taken as removed too (MongoDB). A store under ddl: none cannot tell, and concludes nothing; a table emptied in place (a delete by query, a TRUNCATE, a deleteMany) is not missing.
Fill it again: raise its generation
Each snapshot table has a generation in its record, 1 from its first claim. Raising it fills the table again from the log's beginning, by a backfill of that generation, wow.snapshot[:<store>]#<aggregate>@<n> (generation 1's is …#<aggregate>):
- Delete the table, or leave it deleted. On Elasticsearch, delete its concrete index
<name>--1, which Elasticsearch deletes through no alias (Elasticsearch). A kept table is not emptied: the backfill writes every aggregate of the log at its latest version over an older row, and leaves as it is a row the log does not exceed or one of an aggregate the log does not hold. Delete it when it may hold what the log does not: a log restored to an earlier point, a table copied from another environment. One restored from a backup is not a way out on its own: the nodes passed the type's entries over while it was refused (Backup and restore). - Raise its key above the generation in the error:
wow.aggregates.[<context>.<aggregate>].snapshot.generationfor the type's own table,.query.snapshot.generationfor its query copy. Both are 1 by default, so the first time it is 2. - Restart the nodes one by one. The first node that starts at the raised generation makes the table again, as a first claim does, then resets its record: not removed, filled by a backfill, at the new generation. The revision, and whether it is rebuilding, stay as they were. Until the backfill catches up, the type's queries answer
rebuilding. - The table answers again once its backfill is marked caught up. The procedure is finished when every node runs at the raised generation. A node still configured lower says so in its own log, once, at its start or at its next round after another node renewed the table: the record's generation stands, naming both.
What it assumes and what it leaves:
- The nodes share
wow.snapshot.maintenance-interval. A node not restarted yet still holds the table as removed. Its snapshot subscription passes the type's entries over until its next maintenance round reads the record; from then on it writes them. The backfill covers that window: a node does not mark the table caught up before the second round after the one in which it first saw the new generation, and checks it against log heads read in a round after that one. Every node on the same interval has read the record by then, unless its round runs long (below), so the backfill reads past every entry a node passed over. - A node whose next round comes later than that, on a longer interval, paused, or busy in a long round, can pass entries over after those heads. A round runs long while the node rebuilds a raised state revision in place (above): the rebuild runs inside the round, up to
wow.snapshot.rebuild.max-pagespages ofwow.snapshot.rebuild.page-sizerewrites for each table it rebuilds, one table after another, and the node reads the record again only at its next round. The aggregates of those entries are stale in the table until their next event or a rebuild request by id. - So raise a generation while no state revision is rebuilt in place: once each state revision a release raised is rebuilt (its node logs
The snapshots of <type> in <store> are rebuilt at state revision <n>.), or in a release of its own. A table renewed during an in-place rebuild is repaired by raising its generation once more after the rebuild: every node serves the table by then, so none passes an entry over, and the new backfill writes every aggregate of the log at its latest version. - Loads are never affected: a missing or older snapshot is completed from the log.
- The earlier generation's backfill checkpoints stay. A start names them while the new backfill runs, as it names a sink's earlier generation.
- A lowered generation is not refused. The record's stands and the table is not filled again; each such node warns once, at its start or at its next round after a renewal, naming both. Only raise it.
- A node that finds the table missing while another renews it does not record it removed over the renewal: a store writes
removedonly on the record it checked, while that record is still at the same generation and not removed. The node serves the renewed table. - A store that cannot renew a table (
SnapshotRebuildStore.renew) leaves its record as it is, and the node warns once that the key is not applied. Wow's stores renew. - A table first claimed under a raised key, after a store changed while the key stays raised, is claimed at 1, then renewed: one more pass of a backfill over the type's log, which writes what the store's subscription writes.
- A table emptied in place (a delete by query, a
TRUNCATE, adeleteMany) is filled again by the same step: the store cannot tell it from an empty one, so nothing tells you to. - Under
ddl: validatethe renewal refuses a missing table as a first use does, naming how to create it; create it, then restart. Underddl: nonethe store makes nothing: create the table before you raise the key.
Access attributes
Each stream records the aggregate's access attributes after its commit, its owner and space among them, and so do the snapshots and the Sinks' records. When a commit changes them, the built-in subscription wow.access stamps the new ones over the aggregate's earlier streams, so its whole history is authorized by what the aggregate is now. An Elasticsearch store's events index follows them the same way: its Sink stamps a change over the documents it wrote before, and the walks below stamp the index with the log. Every copy keeps the attributes of the later version (accessVersion), whatever order the stamps arrive in.
Streams an import from 9.x appended carry none: under an access policy, an events query leaves those streams out until they are stamped, and so does an events query by a caller acting as an owner or in a space. The access walk of each aggregate type stamps them:
- When it runs. A node claims the walk of a type when the type's log records none, or records one of another access schema (the attributes the model declares, the type of each and whether it holds many, and the model's access revision) and no node of that schema is left. So a rolling deployment that declares a new attribute walks the log once the last node of the previous version has stopped. A type the log holds no stream of yet has no walk, and the walk creates no collection or table for it. The step that creates them, the type's first append or the query indexes a node makes as it starts, records the type's walk as completed for that node's schema, with nothing stamped: every stream a store this version created holds was committed with its attributes, so a new aggregate type or a new service is never walked, on any node. A store created during a rolling deployment that changes the type's access schema is walked once the other version's nodes are gone. A store an import or a tool created records none, and the first run after its first append claims its walk. A node reads whether a type without a store has one now after one run, then after twice as many runs each time, up to 16 runs, about 80 seconds at the default interval. An import claims a type's walk once its first streams of that type are in, in place of one that stamped nothing, such as the walk the store's creation recorded, so the nodes walk them when they start or are activated from standby. On a node that is already running,
wow.accessstamps each of them when it reads it, within its lag: a walk recorded at creation has no heads, so it leaves every stream to the follower. After a migration,migrate-data snapshotstamps the imported history itself and records each type's walk as completed, so the nodes do not walk it (The access attributes). - What it does. One node at a time pages the type's aggregates and, one aggregate after another, stamps each one's whole history with the attributes of its current state and rewrites its snapshot, each in short statements of its own: the walk holds no lock or connection from one aggregate to the next, and at most one connection of the log store's at a time. It reaches the streams up to the log's heads when it was claimed;
wow.accessstamps the aggregate of a stream without attributes after them, from an import, when it reads it. - Each copy. The walk stamps the log's streams, rewrites the type's snapshots in every store they are written to, an Elasticsearch index among them, and stamps an Elasticsearch store's events index with the log. A ClickHouse sink's rows are no walk target: a row keeps the attributes it was written with until its aggregate commits again, which may be never. Once the walk has completed, drop the sink's tables and raise its generation, which fills them again from the stamped log (Rebuild the warehouse). Until a copy is brought in line, a record written before an attribute was declared holds no key of it, and a rule that reads it,
isNull()included, grants none of them. - Its pace. The walk is background work on the store the commands write to, so it takes a bounded share of it, whatever the load. After each aggregate it waits so that its stamping took at most
wow.access.walk-shareof the time (25% by default): an aggregate that took 3 ms is followed by a 9 ms wait. When the store is busy, from any node, the walk's own statements take longer and its pace slows with them. Once a second it reads whether the log store's writes are pressed, as the store's adaptive query bound reads them (a caller waiting for a connection, or appends well above their usual latency): while they are, the share halves, down to an eighth ofwalk-share, and it comes back by an eighth a second once they are not. It reads each history under the log store's query bulkhead, with the queries and the state rebuilds; a read the bulkhead refuses counts as pressure, and the walk reads that aggregate again a second later. It never stamps more thanwow.access.walk-rateaggregates a second (500 by default). A store whose query bound is fixed (wow.stores.<name>.query.max-concurrentorwow.query.max-concurrent) gives no pressure to read: the walk keeps its share. - How long it takes. At its share, a walk stamps about
walk-sharedivided by the time one aggregate takes. On the benchmark's 4-core runner, with 200 commands a second and aggregates of 10 streams, the default share stamped about 150 aggregates a second on PostgreSQL and about 40 on MongoDB: 100,000 aggregates take about 11 and 40 minutes, 10 million about 19 hours and 3 days. While the writes press it slows to an eighth of that, and still ends. The streams it has not reached keep the attributes they were stamped with, or none: an events query under an access policy reads them by those. One stamped with none it leaves out, and an attribute declared since is absent from the others, so a rule that reads it,isNull()included, grants none of them until the walk stamps them: less than the aggregates' attributes would, never more. - Tuning. To walk faster in a maintenance window, raise
wow.access.walk-share, up to1: the walk then runs as fast as the store andwalk-ratelet it, and still gives way while the writes press. Put it back afterwards: only an operator knows that the whole cluster is idle, which no node can see. The two stores differ. On PostgreSQL, in the same benchmark, a walk at full speed left the commands' p99 latency within its noise, so raising the share costs mostly CPU. On MongoDB, be careful: a walk at full speed about doubled the commands' median latency, which the default share avoids. Lower it when the commands' latency rises while a walk runs (wow.access.walk.pendingreads 1). - On MongoDB, after declaring a new attribute. Each aggregate's first commit afterwards has
wow.accessstamp its earlier streams, whichwalk-sharedoes not bound, so write latency stays raised until the aggregates being written are stamped (about seven times the usual median in the benchmark's worst case, where every command reaches an aggregate not yet stamped): make such a change outside peak hours. - Resuming. After each page the walk records how far it is in the log's registry, so a restart, or another node, goes on from there. A page holds as many aggregates as the walk stamps in about five seconds at its pace, at most
wow.access.walk-batch(100 by default). - An aggregate it cannot read does not stop the walk. One whose history no longer replays, or whose read or stamps the store refuses for its own data, is parked in the failure ledger under
wow.access.walk, and the walk goes on; a store that is down, busy or slow stops the run instead, and the next one resumes at the cursor. The walk completes without it, andwow.access.walk.walkedcounts only the aggregates it stamped. The parked aggregate's history keeps what it carried: a stream with no attributes stays without them, and an events query under an access policy, or by a caller acting as an owner or in a space, leaves it out, as it leaves out any stream not stamped yet. A stream stamped under an earlier schema keeps that schema's attributes, and an attribute the schema lacked is absent from it: a rule that reads that attribute,isNull()included, grants it neither way until the aggregate is stamped. Each parked aggregate is logged once, counted inwow.maintenance.skipped(kindaccess-walk) and listed with the other parked entries; retrying the entry, after a deployment that reads the history again, stamps it and resolves it, and skipping it resolves it and leaves the history as it is. - Watching it.
wow.access.walk.pendingreads 1 for a type until the walk of the node's access schema completes, and is not reported for a type with no walk yet; it reads 1 too while the log records another schema's walk, completed or not, which a rolling deployment that changes the schema keeps until the last node of the old schema stops, when the new schema's walk starts.wow.access.walk.walkedreads how many aggregates the walk of the node's schema has stamped, as the log's registry records it, the same on every node of that schema, and 0 while the log records another schema's walk;wow.access.walkedcounts the aggregates this node walked, andwow.access.stampedthe streams stamped, by why (Metrics). No metric says how many are left, since the walk does not count the type's aggregates. To estimate it, take the type's aggregates from acountquery of its snapshots ({ "kind": "count", "filter": { "op": "match-all" } }, Querying) or from the log itself (on PostgreSQLSELECT count(*) FROM wow_log__<context>__<aggregate> WHERE version = 1, on MongoDBcountDocuments({ version: 1 })on that collection), subtractwow.access.walk.walked, and divide by the walk's rate of the last minutes (rate(wow_access_walked_total[5m])summed over the nodes) for the time it still takes. The registry holds each walk, its schema, heads, cursor, count and completion: on PostgreSQLSELECT context, aggregate, access_walk FROM wow_log_aggregate, on MongoDB theaccess_walkfield of thewow_log_aggregatedocuments. The walking node logsThe access walk of <type> is completed: <n> aggregates stamped.when it is done.
After a changed derivation. An attribute derived another way, its name, type and multiplicity unchanged, leaves the access schema as it was: no walk is claimed, and the history keeps the earlier values until each aggregate commits again. A rule compares those for an events query, a snapshot query or a dataset, while a state point read compares what the state derives now under the node's model, from a snapshot the node reads back as its state, and answers the state as stored. A snapshot it does not read back, of another state revision or schema, keeps the attributes it stored (Read State). The version that changes a derivation raises the aggregate's @AggregateRoot(accessRevision = n) (Declare access attributes), which is part of the schema (department:string;revision=2 in the registry): its walk runs as a new attribute's does, once the last node of the version before has stopped, and stamps the streams, the snapshots and the Elasticsearch events index with what each aggregate's state derives now. A rollback walks again, once the last node of the raised revision has stopped. ClickHouse rows are not walked: they are brought in line as after any walk, once it has completed (Each copy, above). Wow cannot see that a derivation changed: a version that leaves the revision as it was changes nothing in the registry or the metrics, and its history keeps the earlier values.
The warehouse
The ClickHouse sink is a subscription too: its tables are derived from the log, and it keeps them exact when a commit reaches it twice (ClickHouse).
A table the sink did not make in its present shape (its comment does not say the sink's format, it has other columns, or it lacks a computed column) fails the sink's writes, naming it (Schema management). It holds nothing the log does not, so drop it and raise the sink's generation, whose new subscriptions fill it again from the log (Rebuild the warehouse).
Parked aggregates
An aggregate a subscription parked is not lost: resuming it reads its stream again from the version it stopped at, and its handler sees every event it missed, in order (Failures and Retries).