Saga

@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class Saga

A saga: a processor whose OnEvent functions derive commands from committed events. It replaces 9.x's @StatelessSaga, and compensation that 9.x wrote with @OnError is written as a saga.

Each handler is compiled into its own subscription, named processor.function (or its Name). A handler takes the committed stream, or each of its events of a type, with the state after it, and optionally the me.ahoo.wow.api.subscribe.SagaContext, and returns the commands it derives as requests: one me.ahoo.wow.api.client.CommandRequest, or several (an Iterable, a Sequence, a Flow; empty or null for none), each the command's toCommandRequest with the id of its aggregate. me.ahoo.wow.api.subscribe.SagaContext says how each becomes a command.

import me.ahoo.wow.api.client.toCommandRequest

@Saga
class PaymentSaga {
@OnEvent
fun onOrderPlaced(event: StateTypedEvent<OrderState, OrderPlaced>): CommandRequest<RequestPayment> =
RequestPayment(event.event.amount).toCommandRequest(event.ref.id)
}

Each subscription keeps its own checkpoint, so nothing orders two handlers of one saga, for one aggregate either: after a backlog (a restart, a deployment, a handler that retries or has parked the aggregate) the command one handler derives from a later event can reach its target before the command another handler derives from an earlier event. Steps at one target that depend on each other are derived by one handler: its subscription handles an aggregate's commits in version order and takes the next one only once the commands derived from the one before are committed (or, across contexts, durably accepted).

A handler may be suspend or return a Reactor Mono or Flux. Derived commands have deterministic request ids, so handling a commit again derives the same requests. The saga is done when its commands are committed (or, across contexts, durably accepted); a derived command that is rejected goes to the saga's OnRejected handler of its type, and parks the aggregate in the failure ledger only when the saga has none. A saga that must act on a command's success declares an OnCommitted handler of its type: the receipt of the commit comes back to it, from another context too. The processor's dependencies are its constructor's parameters.

A processor is of exactly one kind: Projection, Reactor or Saga.