Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions docs/src/content/docs/durable/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,23 @@ owner holds the instance, so stop. `Recover` retries an unapplied effect, and re
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 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
result is decoded from its JSON form live as well as on replay, so a reducer gets the same
value both ways. When `WithEventDataDecoder` covers the invocation's `onDone` event, the
result arrives as the decoder's type, for example `durable.DecodeAs[User]`. Otherwise it
arrives in JSON's generic form: a struct as a map and an integer as a `float64`, which
loses precision above 2^53. A decoder that fails, or that returns a type other than the
one the service returned, parks the result: `RunService` returns `ErrEventDataEncoding`
and no `onDone` or `onError` reducer runs, but the journal records the result as the
service returned it, and the Handle stays healthy. The service stays in flight and never
runs again. The decoder is part of the Runner's options, so the Handle that parked the
result never sees a fix: `Recover` with the fixed decoder, then call `RunService`, and it
settles the parked result. Until then `RunService` keeps returning
`ErrEventDataEncoding`. A checkpoint keeps a parked result, and a `Fire` or timer that
leaves the state that declares the service stops the service and discards it. A service error stays message-only, an error with the same message, which `errors.Is` no
longer matches. Register the decoder before you record results: replay decodes the journal
with the recovering Runner's decoder, so a decoder added or changed after results were
recorded changes what replay hands the reducer, and one that can't decode a settled result
fails `Recover` and `StateAt` with `ErrEventDataEncoding`; a parked one doesn't fail them. 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
Expand Down
136 changes: 103 additions & 33 deletions durable/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.

### Added

- **`WithResultTypeAlias`** tells the Runner that a service result type the journal
recorded under one full Go type name now goes by another, so a result settled or
parked under the old name still replays after a pure refactor moves or renames the
type: `Recover`, `StateAt` and a `RunService` that settles a recorded result accept
the decoded value when its type is the one the alias names, and every other recorded
name is held to strictly, exactly as before. A parked result the Handle itself holds
is still held to its own type, as live. The option takes the recorded name and a
value of the type the name now means, can be repeated for several renames, and maps
one hop, never chaining through another alias. One recorded name given for two
different types drops that name's alias entirely, whichever order the registrations
arrive in, and a later registration, for any type, can't revive it, so the strict
check stands for that name; an empty recorded name, a nil value or a name aliased to
the type's own current name is ignored, like the other Runner options, leaving the
strict check in place. A refusal names the recorded type and, when an alias covers
the name, the aliased type to decode as.
- **`Record.EndedActors` and `ActorSet`** record the declared child-machine actors that
ran and have ended, settled or stopped, so `Recover` doesn't spawn them again. A Store
must keep an empty set distinct from nil.
Expand Down Expand Up @@ -133,21 +148,66 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
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.
effect can't differ from the first attempt. The result is decoded through the
`WithEventDataDecoder` decoder for the invocation's onDone event, as a `WithEventData`
payload is: a decoder for that event, such as `DecodeAs[Order]`, hands `Assign` that
type, and without one a struct arrives as a map and an integer as a `float64`, which
loses precision above 2^53. A decoder may always return JSON's generic form, including
a `json.Number` from one that decodes with `UseNumber`; a `WithEventData` payload
must still come back as the type its caller passed. A decoder that fails, or returns a type other than the
service returned, or a result whose JSON form won't decode, such as a number beyond
`float64`, parks the result (see below). `Recover` and `StateAt` fail with
`ErrEventDataEncoding`, running no `Assign`, on a settled result their decoder can't
decode. Every successful settle records the Go type the service returned, with its
import path, and replay holds the decoder to it as the live settle did: `Recover` and
`StateAt` fail with `ErrEventDataEncoding`, running no `Assign`, when the decoder
returns another type, such as a pointer to it. JSON's generic form, and nil, are always
accepted. Replay decodes the journal with the recovering Runner's decoder, so register
a decoder before results are recorded: changing the type a decoder returns for an event
whose results are recorded fails `Recover` on them, unless it returns the generic form,
and a decoder added later for the type the service returned changes what replay hands
`Assign` from the generic form to that type. A typed nil result, such as a nil
pointer, goes through the decoder as JSON's `null`, so `DecodeAs[*T]` hands `Assign` a
nil `*T` live and on replay alike; on main, `Assign` saw the typed nil live and an
untyped nil on replay. With no decoder it arrives as an untyped nil, and an untyped nil
result never reaches the decoder. An error stays message-only:
an error with the same message, any invalid UTF-8 replaced, which `errors.Is` and
`errors.As` no longer match, live or on replay. 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 the JSON the live settle
decoded, so replay decodes the same bytes, even for a decoder whose type doesn't
survive a round trip through its own encoding. A result with no JSON form fails the
Handle, as before, but now before any `Assign` runs.
- **A service result that won't decode is parked.** `RunService` returns
`ErrEventDataEncoding`, not `ErrHandleFailed`, before any onDone or onError `Assign`
runs. The decode aborts the settle through `ServiceRunner.WithResultTransform`'s abort
return, so no event fires. The journal records the result as the service returned it,
with its Go type and import path, in a Record of its own that settles nothing. The
Handle stays healthy and the service stays in flight, but its `ServiceFn` never runs
again. The Handle that parked it never sees a fixed decoder: `Recover` with the fixed
decoder, then call `RunService`, and it settles the parked result, which the decoder
must return as the type the service returned, or in JSON's generic form, as live.
The type is matched by import path and name, so after the type is moved or renamed
only a decoder that returns the generic form settles it: settle parked results before
such a move. A
ctx that ended during the service parks nothing: `RunService` fails the Handle and
records nothing, as for a result that decodes, and the service runs again after
`Recover`. Until then each `RunService` returns
`ErrEventDataEncoding` and records nothing more. `Recover` and `StateAt` replay a
parked result as parked, so they don't fail on it, and `StateAt` at that step shows
the instance as it was before. A checkpoint carries the parked results in a new
`parkedServices` field of its envelope, so compacting the Record that parked one
doesn't lose it. A Fire or timer that leaves the state that declares the service stops
it and discards the parked result; entering the state again runs the `ServiceFn` for
the new activation. The Record that settles a parked result records its Go type too, and replay holds the decoder to it,
as that settle did: `Recover` and `StateAt` fail with `ErrEventDataEncoding`, running no `Assign`, when the decoder returns another type. The
type is the full name, with every named type's import path, also inside a struct
literal, func or chan type, whose fields' tags are part of it. Moving or renaming the
type after a park leaves the result parked under a decoder for its new name, and, once
settled, fails replay under one; a decoder that returns JSON's generic form for that
event settles or replays it. A parked Record or checkpoint is new to this version: an
older `durable` replays a parked Record as a settled result.
- **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 @@ -196,29 +256,39 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
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
`Recover` failed with "no in-flight service to settle"; and a parked result the timer
discarded made `RunService` run the `ServiceFn` again. 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
state stops its service,
discarding a parked result, 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.
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 parked settle holds the decoder to the result's own type on the Handle that
parked it.** The live settle compares the decoder's type with the `ServiceFn`'s, but
the parked settle compared only their names, so a decoder that returned another type of
the same name and package, such as another function-local type, was refused live, and
its result parked, then accepted by the next `RunService` on the same Handle. That
`RunService` now refuses it too. After a `Recover` only the recorded name is left, so a
result of a function-local type is told from another of the same name only until then.
- **A checkpoint without `WithServiceRegistry` keeps the services' parked and settled
results.** 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 in flight with no parked result, or 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 or park.
- **`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 Down
23 changes: 19 additions & 4 deletions durable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,25 @@ owner holds the instance, so stop. `Recover` retries an unapplied effect, and re
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 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
result is decoded from its JSON form live as well as on replay, so a reducer gets the same
value both ways. When `WithEventDataDecoder` covers the invocation's `onDone` event, the
result arrives as the decoder's type, for example `durable.DecodeAs[User]`. Otherwise it
arrives in JSON's generic form: a struct as a map and an integer as a `float64`, which
loses precision above 2^53. A decoder that fails, or that returns a type other than the
one the service returned, parks the result: `RunService` returns `ErrEventDataEncoding`
and no `onDone` or `onError` reducer runs, but the journal records the result as the
service returned it, and the Handle stays healthy. The service stays in flight and never
runs again. The decoder is part of the Runner's options, so the Handle that parked the
result never sees a fix: `Recover` with the fixed decoder, then call `RunService`, and it
settles the parked result. Until then `RunService` keeps returning
`ErrEventDataEncoding`. A checkpoint keeps a parked result, and a `Fire` or timer that
leaves the state that declares the service stops the service and discards it. A service error stays message-only, an error with the same message, which `errors.Is` no
longer matches. Register the decoder before you record results: replay decodes the journal
with the recovering Runner's decoder, so a decoder added or changed after results were
recorded changes what replay hands the reducer, and one that can't decode a settled result
fails `Recover` and `StateAt` with `ErrEventDataEncoding`; a parked one doesn't fail them. A
type moved or renamed after its results were recorded replays under its new name once
told to the Runner with `WithResultTypeAlias`. 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
Expand Down
15 changes: 15 additions & 0 deletions durable/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ type pendingTimerWire struct {
Event json.RawMessage `json:"event,omitempty"`
}

// parkedServiceWire is a parked service result as a checkpoint envelope carries it: the
// service id, the result's JSON and its Go type, as the parked journal entry records
// them.
type parkedServiceWire struct {
ID string `json:"id"`
Result json.RawMessage `json:"result"`
Type string `json:"type,omitempty"`
}

// checkpointEnvelope is the durable layer's wrapper around the bytes a Store keeps
// for a checkpoint: the opaque state.Snapshot bytes, the pending-timer table armed at
// the checkpoint instant, and the next step ordinal. The durable layer fully owns the checkpoint
Expand Down Expand Up @@ -98,6 +107,12 @@ type checkpointEnvelope struct {
// settled isn't one. None, as in a Start baseline or an older checkpoint, means
// Recover starts every declared service.
EndedServices []string `json:"endedServices,omitempty"`
// ParkedServices are the results parked at the checkpoint, by service id, sorted:
// each didn't decode for its onDone event, so its service is in flight and not
// settled, and a later RunService settles the recorded result rather than run the
// ServiceFn again. A checkpoint compacts the Record that parked it, so it carries
// them. None, as in a Start baseline or an older checkpoint, means none is parked.
ParkedServices []parkedServiceWire `json:"parkedServices,omitempty"`
// Format is the journal format of the version that wrote the checkpoint, as
// Record.Format is. Zero means a checkpoint from before this field. A Record after
// it with a lower Format means the Store lost the field, and replay fails.
Expand Down
Loading
Loading