Publish and Consume Streams
A context shares what happened in it by publishing its aggregates' event streams. Other contexts read them as they read their own: a handler names the aggregate, takes the peer's event classes, and keeps its position in its own checkpoints. This page publishes the ordering's order and reads it in the payment context. Between Contexts compares this with the other ways contexts talk.
What a context publishes
Every aggregate publishes its state events by default: one record per commit, with the commit's events and the state after it. The Manifest lists them as the context's published streams, and the context's Kafka sink writes them to the topic wow.<context>.<aggregate>.state. @AggregateRoot changes that per aggregate:
| Setting | Effect |
|---|---|
published = false | The aggregate is not published |
publishState = false | Its events are published without the state, for a state too large to send |
topic = "…" | Another topic name |
The ordering publishes its order as it is, on wow.eshop-ordering.order.state. A published stream is part of the context's contract: its events and its state's fields are what peers read, and the Manifest's compatibility check treats a removed or retyped field as breaking.
Publishing needs Kafka: Kafka describes the topics and the envelope.
Read another context's stream
A handler takes that context's event class and the view of its state, both from its api module. The service's Manifest finds the aggregate from the context's contract; an event two of its aggregates share is named with @OnEvent(aggregates = [<Context>Contract.<Aggregate>.QUALIFIED_NAME]):
@OnEvent
fun onOrderStockConfirmed(
event: StateTypedEvent<OrderStateView, OrderStockConfirmed>
): CommandRequest<RequestPayment> =
RequestPayment(event.ref.id, event.state.totalAmount).toCommandRequest(event.ref.id)- The event classes.
OrderStockConfirmedis the ordering api's class. The payment's domain module asks for the ordering api's contract,implementation(wow.contract(project(":eshop-ordering-api"))), for the generatedOrderStateView; the payment's service finds the ordering's contract (META-INF/wow/contract.json) without a line. - The state.
OrderStateViewis generated into the ordering api from its contract, one view per aggregate whose stream carries its state, named after the state class withViewappended. It holds the state's fields: those the ordering requires are non-null, the others nullable. A class of the api (OrderItem,OrderStatus) is referenced; a type of the ordering's domain is copied into the view as a nested class; free-shape content is anObjectNode(a free value aJsonNode). Fields the view does not have are ignored, so the ordering adding one breaks no reader. When the upstream publishes without the state (publishState = false), its api generates no view of it, and the handler declares its state nullable: a non-nullable state of a stream without state fails when the node starts. - The access attributes. Each published record's
accessholds the aggregate's access attributes after the commit, owner and space among them, as the upstream published them, with the state or without it; it isnullwhen the stream was not stamped. - The aggregates. All the aggregates in
aggregatesbelong to one context, writtencontext.aggregate. - The kinds. A saga, a projection and a reactor read a published stream the same way.
Events the subscriber has no class for
A handler that takes a StateTypedEvent<S, E> is called only for the events that are an E. A handler that takes the whole StateEvent<S> gets each event as the api's class when the event's revision is the one the api's contract declares, and as a PublishedEvent otherwise: its name (eshop-ordering.order.OrderShipped), its revision and its JSON body. So a handler of the whole stream reads every revision the upstream publishes, and decides for itself what to do with a body its api has no class for.
A handler that takes a StateTypedEvent<S, E> does not pass over an E it cannot read. When a record holds an event the handler takes at a revision the api's contract does not declare, the handling fails: the subscription retries and then parks the aggregate, and the parked entry names the event, the revision found and the contract's revision (Failures). This is decided once a commit, before the handler is called, so a commit is handled whole or not at all. The parked aggregate holds back its later commits for that subscription until it is retried or skipped; other aggregates go on. So when the upstream raises an event's revision:
- Deploy the upstream first, then the subscribers on its new contract, then retry what parked. The records at the new revision park until a subscriber holds the new contract, and are handled when retried.
- A typed handler on the new contract that is deployed before the upstream, or reads the topic from the beginning, parks at each aggregate whose records hold the event at the earlier revision: what the upstream's nodes of the earlier code publish, and the topic's history. A retry does not help, since no later contract reads an earlier revision. The ways out are to skip each such entry, which goes on without that commit for the subscription, or to read the event with a handler of the whole stream. Nothing in 10.0 publishes a stream's history again at the current revision: the transport sink has no generation, unlike the ClickHouse and Elasticsearch sinks.
- A handler of the whole stream takes every revision, the older ones as a
PublishedEvent: the way to read any revision on purpose.
An event the upstream renamed with @Alias is read the same way under its former name, on its own aggregate's stream. The topic keeps records under the former name: what was published before the rename, and what a node of the upstream's earlier code publishes until it is upgraded. Such a record is the api's class when its revision is the contract's. At another revision it is what a record under the current name at another revision is: a PublishedEvent of the former name to a handler of the whole stream, and a parked aggregate to a handler of the event's class. So take the upstream's new contract before the upstream renames the event. A handler of the event's class then receives every record of it at the contract's revision, under either name (Evolve Events). A subscriber still on the earlier contract does not know the new name, and passes its records over as those of an event it has no class for. The node does not start when two events of one aggregate claim one name by their names or former ones, in a contract on its class path or in the service's own Manifest; it names both.
The node does not start when a handler takes an event class the contract on its class path does not declare.
Where it starts
A subscription to another context's stream that has no checkpoint yet starts by its kind:
- A projection starts at the beginning of the topic, so it fills its read model with what was published before.
- A saga's or a reactor's handler starts where its service joins the stream. When no other subscription of the service has read the topic yet, that is the beginning, so a service that joins later still gets what was published before. When one has, it is the end, so a handler added later to a service that already reads the stream does not act on its history again: a new saga handler does not send commands for events its service read long ago. This is decided partition by partition, and a partition added to the topic later is read from its beginning whatever the start (Subscriptions). The Manifest writes this start as
JOIN.
@OnEvent(start = OnEvent.Start.BEGINNING) or END fixes it, and the host's wow.subscriptions.[<context>.<processor.function>].start overrides both, with beginning, end or an instant. An instant starts at the first record of each partition, in offset order, committed at or after it, and every record after that one is handled, whatever its commit time; at the end when none is. Its first start in a partition reads the retained records from the first to find it, skipping those it cannot decode. After its first checkpoint it goes on from there.
The service reads the topic without a Kafka consumer group: its offsets are in its own checkpoint store, beside its other subscriptions' positions.
History imported from 9.x
A service migrated from 9.x publishes the history migrate-data imported into its log too, so its peers' projections can build their read models from it. Each record up to the cut-over the import recorded is marked imported (imported in the state event). A saga or a reactor passes over such a record as it passes over an event it does not handle, since 9.x already acted on it; a projection, and any system reading the topic, takes it. A service that was never migrated publishes no record so marked.
Answer the upstream
A saga that reacts to a peer's event often answers with a command to that peer: the payment pays the order once its gateway approves the payment.
@OnEvent
fun onPaymentSucceeded(
event: StateTypedEvent<PaymentState, PaymentSucceeded>
): CommandRequest<PayOrder> = PayOrder(event.ref.id, event.state.amount).toCommandRequest(event.state.orderId)body.toCommandRequest(id) finds the ordering's order from the ordering api's contract, and the command goes to the ordering's command topic, wow.eshop-ordering.commands. A rejection comes back as a receipt, to the saga's @OnRejected handler:
/** An order that refuses its payment has the payment refunded. */
@OnRejected
fun onPayRejected(rejected: CommandReceipt.Rejected<PayOrder>): CommandRequest<RefundPayment> =
RefundPayment("The order ${rejected.ref.id} refused the payment: ${rejected.error.message}")
.toCommandRequest(rejected.source.id)Write a Saga covers derived commands, receipts and rejections in full.
Sensitive values
A published record carries the @Sensitive values of its state and of its events' bodies masked, as a read masks them: every CONFIDENTIAL value, and every DISPLAY value unless the ordering declares that the stream reveals them. The ordering's transport sink masks them as it writes the record. So a password hash or a phone number in the ordering's state reaches neither the payment's handler nor any other reader of the topic, and the topic does not hold it. The ordering's own subscriptions read its log, and get the values as they were committed.
- What a reader gets: the same fields and the same schema fingerprint, with the mask in place of the value. A mask is still a string, but a value type of the contract that checks its content when it is constructed may refuse it.
- A stream that reveals its
DISPLAYvalues: the ordering declares it on the aggregate,@AggregateRoot(revealsDisplay = true), when its peers need them raw, such as the phone number a payment sends a text to. ItsCONFIDENTIALvalues stay masked. The Manifest'spublishedand the peer contract carry the declaration, so a consumer knows what it receives; turning it off later is a breaking change of the contract. The build refuses it on an aggregate whose published state and events hold noDISPLAYvalue, and the compiler on one that is not published. - How it differs from a revealing read:
- the ordering says the word, not the reader;
- every reader of the topic gets the raw values, external consumers included: the topic's ACL is the boundary, and no read of it is audited;
- the values stay in the topic for its retention.
- The ordering's switch:
wow.query.sensitivity.display-sharedoff masks a revealing stream'sDISPLAYvalues all the same, as it refuses a peer's revealing read. - Records published before the upgrade: they keep every value as it was written (Upgrade a Deployment).
During a rolling upgrade
The node that owns an aggregate's partition writes its records, and while a new version rolls out that is as often a node of the older code. It publishes a newer node's event as stored, at the revision it knows: the properties its schema does not declare go out as they are, masked only where its own schema marks a value. So a @Sensitive value a new version adds to an event of a published stream needs a new revision, which checkWowManifest holds (Event Evolution): an older node does not publish an event of a revision it does not know, it retries the entry and parks the aggregate until a newer node owns it. A break of that check recorded in the baseline accepts that the older nodes may publish the value raw until the last of them stops. Any other property a new version adds goes out as it is from the older nodes, as it does from the newer ones.
A scheduled command is the one body a node trims. The command of wow.schedule.ScheduleSet was serialised by whichever node scheduled it, and a command has no revision. So every node publishes it with only what its own schema of the command type declares, as a trace and an events query answer it. An older node leaves out every property a new version adds to a scheduled command, a @Sensitive one among them, and no revision is needed for it. The timer still sends the command as it was scheduled.
External consumers
A system that is not a Wow service reads the same topics. Each record is an envelope keyed by the aggregate, so one aggregate's records stay in order: Wire Protocols describes it.
Test it
A test hands the handler a commit of the upstream's aggregate with the test DSL's stateEvent, built from the api's event class and its state view: Test Domain Code.