Skip to content
27 changes: 19 additions & 8 deletions docs/src/content/docs/durable/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ implement the `Store` interface out of tree.
- **Deterministic replay.** A recovered instance is byte-identical to one that
never crashed, because recovery replays the recorded driving events and
nondeterministic results rather than re-executing their sources.
- **Exactly-once effects.** A domain effect is applied exactly once over the
- **Exactly-once effects.** A domain effect from a step (not an initial entry, which is at-least-once, or a parent transition an actor drives, which is not dispatched) is applied exactly once over the
instance's lifetime (the live run plus any number of recoveries), even though
the replay loop is at-least-once. Each effect carries a deterministic
`EffectID` and is deduplicated through the `Store`'s dispatch set.
the replay loop is at-least-once. The exception is a crash or failed mark after the
handler returned, or a handoff between owners, where it can apply twice, so keep
handlers idempotent on `EffectInstance` plus the `EffectID`. Each effect carries a
deterministic `EffectID` and is deduplicated through the `Store`'s dispatch set.
The handler needs a `Store` that implements `DispatchStore`, as `MemStore` and
`FileStore` do: with any other `Store`, `Start`, `Recover` and the stateless
`Runner.Fire` refuse the configuration with `ErrNoDispatchStore`, a permanent
configuration error that doesn't match `ErrEffectDispatch`, so retrying doesn't help.
- **Durability across restart.** Every `Fire` step is write-ahead appended to the
`Store` before it is acknowledged, so a crash after a successful `Fire` never
loses the step. Periodic checkpoints bound the tail that recovery replays.
Expand Down Expand Up @@ -74,7 +80,7 @@ the first time, and replayed verbatim on recovery:
| **Clock** (timers) | `WithRunnerClock` | `JournalClockRead`, replayed so timers fire at the same recorded instants, wall-clock-independent; armed deadlines survive checkpoint compaction |
| **Invoked services** | `WithServiceRegistry` + `Handle.RunService` | `JournalServiceResult`; a recorded result replays through the kernel's settle seam without re-running the service, and an unrecorded one (a context canceled during the service or its settle, a crash, or a failed `Append` whose write didn't land) runs again when the host calls `RunService` |
| **Child-machine actors** | `WithActorPalette` + `Handle.DeliverToActor` | `JournalActorMessage`; recovery re-fires a recorded parent transition; a settled or stopped actor doesn't re-run; a running `InvokeActor` actor restarts from its initial state on any `Recover`, and a `Spawn` actor doesn't return |
| **Domain effects** | `WithEffectHandler` | dispatch set, applied exactly once via deterministic `EffectID` dedup |
| **Domain effects** | `WithEffectHandler` | dispatch set, a step's effects applied exactly once via deterministic `EffectID` dedup, with the exceptions under Exactly-once effects |

Use `WithCheckpointEvery(n)` to tune how often a full snapshot is written: a
shorter interval bounds recovery replay, a longer one cuts checkpoint cost.
Expand Down Expand Up @@ -153,15 +159,20 @@ failed the Handle returns `ErrHandleFailed` saying it was recorded; `Recover` se
its effects. To move a healthy Handle's instance to a new Handle, stop calling the old one
and let its calls return first: a step can start after `Drain` returns. `Recover` a new
Handle with the same options to continue, unless the cause is `ErrFenced`: then a newer
owner holds the instance, so stop.
owner holds the instance, so stop. `Recover` retries an unapplied effect, and returns
`ErrEffectDispatch` while the handler still fails.

## A note on serialized payloads

Events, service done-data, actor done-data, and actor messages are recorded as their
JSON form. A parent reducer that type-asserts a non-JSON Go type from `AssignCtx.Event`
observes the JSON-decoded shape on a replayed `onDone`. A `WithEventData` payload is
decoded from its JSON form live as well as on replay, so a reducer gets the same value
both ways. `WithEventDataDecoder` decodes it as the type the caller passed, chosen per
observes the JSON-decoded shape of actor done-data on a replayed `onDone`. A service's
result is JSON-decoded live as well as on replay, so a reducer gets the same value both
ways: a struct is a map and an integer a `float64`, which loses precision above 2^53, and
an error an error with the same message, which `errors.Is` and `errors.As` no longer
match. A
`WithEventData` payload is decoded from its JSON form live as well as on replay, so a
reducer gets the same value both ways. `WithEventDataDecoder` decodes it as the type the caller passed, chosen per
event, for example `durable.DecodeAs[Order]`. Without one, a payload decodes as JSON's own
form, so `Handle.Fire` refuses a struct or an integer rather than hand the reducer a map
or a `float64`. An error stays an error with the same message, with any invalid UTF-8
Expand Down
123 changes: 118 additions & 5 deletions durable/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
onError was refused because a context ended, so `Recover` can tell them
from the actors that were running. A Store must keep an empty set distinct from nil.
- **`Record.Format`** records the journal format of the version that wrote the Record,
which is 1 from this version. Replay arms the timers and services an actor-driven
parent Fire's state declares only for a Record of format 1 or later.
which is 2 from this version. Replay arms the timers and services an actor-driven
parent Fire's state declares only for a Record of format 1 or later. `Recover` sends a
tick's and a service settle's effects, and replay hands a tick's timer transitions'
effects to the service runner, only for a Record of format 2 or later.
**For Store authors:** a Store must keep `Format`, `EndedActors` and
`UnstartedActors`, and keep an empty set distinct from nil. A Store that drops them, or
maps an empty set to nil as an SQL array column or a protobuf repeated field can,
makes `Recover` spawn ended actors again, and replay arm nothing for a Record without
its `Format`. MemStore and FileStore keep all three.

- **`EffectInstance`** reads the instance from the ctx an `EffectHandler` receives, because
effect ids repeat across instances.
- **Fencing.** `WithFence` on `Store.Append` and `WithCheckpointFence` on
`Store.Checkpoint` carry a caller-issued token, and a Store refuses a token lower
than the highest it has seen for the instance with `ErrFenced`, writing nothing.
Expand Down Expand Up @@ -64,6 +68,19 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.

### Changed

- **An effect handler needs a `DispatchStore`.** This is a behavior change. On main, a
Runner with `WithEffectHandler` and a Store that doesn't implement `DispatchStore`
silently skipped every effect: `Fire`'s, and `Recover`'s re-send, never reached the
handler. Now `Start`, `Recover` and the stateless `Runner.Fire` refuse that
configuration with the new `ErrNoDispatchStore`, before they read or write the Store
or call the handler. A stateless `Runner.Fire` on a custom Store without
`DispatchStore` used to succeed with its effects skipped; it now returns
`ErrNoDispatchStore`. It is a permanent configuration error and doesn't match
`ErrEffectDispatch`, so a caller that retries `Recover` on a dispatch failure doesn't
loop on it. A Store with no handler wired is unaffected, and
`MemStore` and `FileStore` implement `DispatchStore`. A Store author whose backend
doesn't track dispatched effects either implements `Dispatched` and
`MarkDispatched` or drops the handler.
- **A journal this version writes isn't for an older one.** An actor entry can now say
its parent Fire was refused, which an older `durable` fails `Recover` on, or, for an
entry with no event, replays as the zero event. An older `durable` also ignores the
Expand Down Expand Up @@ -98,7 +115,37 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
- **A failed effect dispatch fails the Handle**, and so does an effect handler that
panics, which now returns `ErrEffectDispatch` instead of panicking out of `Fire`. The
effect stays unmarked in the journal, so no later checkpoint compacts it away, and
`Recover` dispatches it again.
`Recover` dispatches it again, failing with `ErrEffectDispatch` while the handler still
does. A handler that panics in `Start` or `Recover` returns `ErrEffectDispatch` too, and
so does a `DispatchStore` whose `Dispatched` or `MarkDispatched` panics or returns an
error, in a step, `Recover` or the stateless `Runner.Fire`, with the Store's error in
the chain. A failed `MarkDispatched` fails the Handle too, and `Recover` applies that
effect again. The handler's error now matches through `errors.Is`.
- **A `KindedEffect` that can't marshal no longer fails a step.** A step records it by
its id and kind with no payload, as it records an effect that isn't a `KindedEffect`,
and its `EffectEnvelope.Meta` holds the marshal error's message under
`crucible.marshalError`; a `MarshalJSON` that panics is recorded the same way, with the
panic's message there. Replay re-derives the effect's value, so it dispatches as any
effect does: once live with a handler wired, once on the `Recover` that retries a
failed dispatch, and once on a `Recover` whose handler was wired only then. This
relaxes `Fire`, and the stateless `Runner.Fire`, which failed with the marshal error
before; `Tick` and `RunService` now record such an effect too.
- **A service result reaches `Assign` decoded, live as well as on replay,** so a retried
effect can't differ from the first attempt: a struct arrives as a map, an integer as a
`float64`, which loses precision above 2^53, and an error as an error with the same
message, any invalid UTF-8 replaced, which `errors.Is` and `errors.As` no longer match.
So onError's `Assign` and the `ServiceRunner`'s `LastError` see a plain error even for
an unbound service's `*state.UnboundRefError`. An `Error` method that panics settles
with a message saying the service error panicked, its invalid UTF-8 replaced too. The journal records this decoded value. A result whose JSON
form won't decode, such as a number beyond `float64`, fails `RunService` with
`ErrHandleFailed` and records nothing, as a result that won't encode does, so
`Recover` doesn't fail on it. Either is refused only after its settle applied: the
onDone `Assign` runs once on an internal stand-in for the result (for one with no
JSON form, the raw result), and the state onDone enters starts its actors and
services, before `RunService` fails; after `Recover` the service runs again, and its
`Assign` with it. So keep a service's result JSON round-trippable, and have an onDone
`Assign` check the payload's type rather than assume it. This may change, to refuse
such a result before any `Assign` runs.
- **A step that may not have been recorded fails the Handle.** That call and every later
one return `ErrHandleFailed`, wrapping the cause. A step that was recorded while
another goroutine failed the Handle also returns `ErrHandleFailed`, saying the step was
Expand Down Expand Up @@ -142,6 +189,34 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.

### Fixed

- **A timer transition starts and stops services and actors.** `Tick` handed a fired
timer transition's effects only to the scheduler, so a timer that left a state left
its service in flight and its actor running, and one that entered a state started no
service and spawned no actor. A service left in flight could then settle after a
checkpoint that no longer held it, journaling a settle replay couldn't apply, so every
`Recover` failed with "no in-flight service to settle". Now `Tick` hands every fired
transition's effects to the service runner, in fire order, and then to the actor
system, as a `Fire` that produced them all does, live and on replay, so a parent Fire
an actor spawned for one timer drives sees the services every timer of the tick
started, and stops one it leaves rather than leave it in flight: a timer that leaves a
state stops its service and its actor, and one that enters a state starts its service
and spawns its actor. The tick's Record journals the parent Fires an actor it spawned
drove, replayed after its timers, and a parent Fire a context's ending refused fails
the Handle and records nothing, as for `Fire`. Only such a parent Fire fails it once
its timers have fired: a message to an actor that the actor refused because the
tick's `ctx` ended is lost, as actor deliveries are across `Recover`, and the tick
records. Replay does this for a tick of journal
format 2 or later; an older version's tick replays as it ran, with its services left
in flight. This is the Tick-driver fix #236 tracked. The services and actors a timer
starts run under the tick's step context, as a `Fire`'s do, so an actor's behavior or
a parent Fire's middleware that calls back into the same Handle gets
`ErrReentrantStep` at once rather than deadlock on the step lock the `Tick` holds.
- **A checkpoint without `WithServiceRegistry` keeps the settled services.** A Handle
recovered without a service registry restored them from its checkpoint but wrote its
next checkpoint without them, so a later `Recover` with the registry found the service
not settled, and `RunService` ran its `ServiceFn` again. A checkpoint now keeps them
with or without a registry. `Recover` without a registry still refuses a journal tail
that holds a service's settle.
- **`Recover` and `StateAt` fail when a Store loses `Record.Format`.** The Start baseline
and every checkpoint record the journal format too, and a Record after one with a lower
format fails replay, where it used to replay as an older journal's and silently skip
Expand All @@ -168,6 +243,18 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
the step that tried it. A retry the Store refuses as not advancing means the first
write landed, and counts as written. Any other checkpoint failure is returned, and
after that only the next step the policy makes due tries again.
- **Effects reach the handler from `Start`, `Tick` and `RunService`.** They were only
dispatched from `Fire`. `Start` applies the initial entry's effects after encoding the
baseline and before writing it, and only if the entry succeeded. Those effects are
at-least-once: a handler failure, a failed baseline write or overlapping `Start` calls
re-apply them, so key any dedupe on `EffectInstance` plus the effect id. Effects from a
parent transition an actor drives, including during `Fire`, are still not dispatched.
`Recover` re-dispatches what replaying a step produces, as it did for a `Fire`: for a
`Tick` or service step, only in a Record of format 2 or later, so it doesn't send
effects from an older version's `Tick` and service steps, which never dispatched
them. It goes by the Record's format, not its `Effects` envelopes, so a Store that
doesn't keep `Record.Effects` loses no effect a crash left unsent. As for a `Fire`'s,
it loads the Store's dispatched set once for each Record with effects to send.
- **A Tick, RunService or DeliverToActor whose context ended partway isn't journaled.** A canceled `RunService`, `Tick`
or `DeliverToActor` used to record a result the live instance never applied, which
recovery then committed, or for an actor, failed on. Each now returns `ErrHandleFailed`
Expand All @@ -192,8 +279,34 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
state spawned failed to start and a context's ending refused its onError, the Fire was
recorded, and `Recover` then applied the onError live never applied. Now `Fire` returns
`ErrHandleFailed` and records nothing, though its event applied: fire it again on the
recovered Handle. A Fire that drove no Fire a context refused still records, even when its
context ends once the kernel has applied the event.
recovered Handle. Only a parent Fire a context refused fails it: a Fire that drove none
still records, even when its context ends once the kernel has applied the event. A
message to an actor, or an actor's start, that the actor refused because the context
ended is lost, as actor deliveries are across `Recover`: failing the step would have
the retry run the actors' behaviors, and their side effects, again.
- **A parent's `forwardTo` forwards its event.** A `forwardTo` on a transition a `Fire`
or a `Tick`'s timer drove forwarded nothing, as the actor system was handed the
effects without the event; this was already so on main for `Fire`. Now `Fire`
forwards its event, and `Tick` each timer's event, rebuilt with the Runner's event
codec. As for any actor delivery, the forwarded event isn't replayed: `Recover`
starts the actor fresh. A `forwardTo` on a service settle's transition forwards
nothing. One on a transition an actor's message to the parent drove still hands the
child a nil event: the actor system absorbs that Fire's effects without its event
(#260). `Tick` decodes a
timer's event only for a `forwardTo` an actor system can carry. When the codec can't
decode it, `Tick` forwards nothing and returns the codec's error with the timers'
results. The tick is still recorded and its effects dispatched, and the Handle carries
on, as when an actor can't take a message: failing it would only have `Recover` fire
the timers again and meet the same error.
- **A step spawns no actor for a state it has already left.** When an actor a step
spawned sent the parent a message that left another state the step had entered, such
as a parallel region's, that state's actor still spawned after, and ran for a state
that wasn't active, where `Recover` never spawned it. Now the step skips that spawn,
for one `Fire` and for timers that fire in one `Tick`. `Start` and `Recover` skip it
too, for an actor that starts when they do and sends such a message. When `Recover`
respawns the actors that were running and one fails with an onError that leaves
another of their states, it stops that state's actor. A `Tick` also skips the spawn
of a state that a later timer of the same tick leaves.
- **An actor's output replays with the Fire that carried it.** When one delivery drove
several parent Fires, such as an onDone entering a state whose actor then failed to
start, the output was recorded with the last Fire, so replay gave it to the wrong
Expand Down
Loading
Loading