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
7 changes: 5 additions & 2 deletions docs/src/content/docs/durable/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ 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 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
- **Exactly-once effects.** A domain effect from a step, including a parent
transition an actor drives (not an initial entry, which is at-least-once, or a
parent transition driven by an actor that `Start` or `Recover` spawned, which is
not dispatched yet: #269), 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. 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
Expand Down
13 changes: 13 additions & 0 deletions durable/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.

### Fixed

- **A parent transition an actor drives dispatches its domain effects.** A domain
effect a parent `Fire` emitted, where an actor drove that Fire — a failed spawn's
onError during a `Fire`, a `Tick`, or a service settle's transition, or a settle or
message a `DeliverToActor` delivery drove — never reached the `EffectHandler`: the
step that drove the Fire journaled it but named only its own effects, so the effect
was never dispatched, live or on `Recover`, and an effect id re-derived by replay
deduplicated nothing. Now every such step — `Fire`, `Tick`, `DeliverToActor` and
`RunService` — names the driven Fires' effects after its own, in fire order, under
the same deterministic ids live and on replay, so `Recover` re-dispatches one a
crash kept from landing, exactly once. An actor delivery now dispatches after
releasing the step lock, as `Fire` does, so an effect handler can call back into the
same `Handle`. The parent Fires an actor spawned by `Start` or `Recover` drives run
before any step and still dispatch none of their effects.
- **A settle a respawn drops runs no transform.** The drop a host's respawn replay
makes (`AbsorbRespawn`) was decided after the settle transform ran, so a respawned
nested child whose output differed from its first spawn's — a time, a random id,
Expand Down
7 changes: 5 additions & 2 deletions durable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ backends 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 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
- **Exactly-once effects**: a domain effect from a step, including a parent
transition an actor drives (not an initial entry, which is at-least-once, or a
parent transition driven by an actor that `Start` or `Recover` spawned, which is
not dispatched yet: #269), 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. 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
Expand Down
74 changes: 61 additions & 13 deletions durable/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ type actorMessagePayload struct {

// DeliverToActor routes event into the running actor identified by ref, runs the
// actor (settling it when it reaches its final state), records each parent
// transition the delivery drove, and persists the produced step. The actor
// transition the delivery drove, persists the produced step, and dispatches the
// domain effects those transitions emitted once it is recorded, as Fire dispatches
// its own. A failed dispatch fails the Handle with ErrHandleFailed; the step stays
// recorded, and Recover sends its effects. The actor
// behavior is resolved and run against the palette supplied with WithActorPalette;
// the done-data, error, or parent-driving message it routes is journaled as a
// JournalActorMessage correlated by the actor id so recovery replays it without
Expand Down Expand Up @@ -155,13 +158,35 @@ func (h *Handle[S, E, C]) DeliverToActor(ctx context.Context, ref state.ActorRef
if err := h.acquire(ctx); err != nil {
return false, err
}
delivered, rec, des, err := h.lockedDeliverStep(ctx, ref, event)
if rec == nil {
return delivered, err
}
// Dispatch after releasing the step lock, as Fire does, so an effect handler can
// call back into this Handle. The Record already names every effect (write-ahead),
// so a crash here leaves it for recovery to redispatch, deduped by id.
return delivered, errors.Join(err, h.finishStep(ctx, rec, des))
}

// lockedDeliverStep runs deliverStep and releases the step lock however it returns.
// A panic in host code fails the Handle first, since the kernel may have applied a
// parent Fire the delivery drove.
func (h *Handle[S, E, C]) lockedDeliverStep(ctx context.Context, ref state.ActorRef, event any) (bool, *Record, []dispatchableEffect, error) {
defer h.release()
defer h.failOnPanic()
return h.deliverStep(ctx, ref, event)
}

// deliverStep is the part of DeliverToActor that holds the step lock: it runs the
// delivery and records the step, naming the domain effects of the parent Fires it
// drove, before any is dispatched. It returns a nil Record when nothing was
// recorded. ctx is the step's marked context.
func (h *Handle[S, E, C]) deliverStep(ctx context.Context, ref state.ActorRef, event any) (bool, *Record, []dispatchableEffect, error) {
if err := h.refuse(ctx); err != nil {
return false, err
return false, nil, nil, err
}
if h.actors == nil {
return false, fmt.Errorf("durable: no actor palette wired for %q (supply WithActorPalette)", h.id)
return false, nil, nil, fmt.Errorf("durable: no actor palette wired for %q (supply WithActorPalette)", h.id)
}

stepCtx := h.stepContext(ctx)
Expand All @@ -173,14 +198,14 @@ func (h *Handle[S, E, C]) DeliverToActor(ctx context.Context, ref state.ActorRef
}()
fires := h.actorFires.take()
if !delivered {
return false, nil
return false, nil, nil, nil
}
// A context that ended during the delivery can make the kernel refuse the actor's
// own Fire, losing the event, or a parent Fire it drove. A refused Fire rolls back,
// so live never applied it, but journaling the delivery would replay it under a live
// context, where it applies. Record nothing.
if err := stepCanceled(ctx, parentFireErrs(fires)...); err != nil {
return true, h.fail(fmt.Errorf("durable: delivering to actor %q for %q: %w", ref.ID, h.id, err))
return true, nil, nil, h.fail(fmt.Errorf("durable: delivering to actor %q for %q: %w", ref.ID, h.id, err))
}
// The actor's done-data wouldn't decode for its onDone event, or decodes as another
// type than the actor produced: the settle aborted, firing nothing, so no Assign ran
Expand All @@ -189,22 +214,45 @@ func (h *Handle[S, E, C]) DeliverToActor(ctx context.Context, ref state.ActorRef
// but the actor can run again. Fail the Handle and record nothing, so a recovered
// Handle runs the actor from its initial state and the delivery again.
if abort := h.actorFires.takeAborts(); abort != nil {
return true, h.fail(fmt.Errorf("durable: delivering to actor %q for %q: %w", ref.ID, h.id, abort))
return true, nil, nil, h.fail(fmt.Errorf("durable: delivering to actor %q for %q: %w", ref.ID, h.id, abort))
}

entries, err := recordParentFires(fires, false)
if err != nil {
return true, h.fail(fmt.Errorf("durable: recording actor %q result for %q: %w", ref.ID, h.id, err))
return true, nil, nil, h.fail(fmt.Errorf("durable: recording actor %q result for %q: %w", ref.ID, h.id, err))
}

// A parent Fire the delivery drove can arm a timer, which reads the clock. Its effects
// aren't dispatched, so the step names none, and finishStep only offers its
// checkpoint.
// A parent Fire the delivery drove can arm a timer, which reads the clock, and can
// emit a domain effect, which the step dispatches as its own: the delivery itself
// produces no parent transition, so the driven Fires' effects are all it names.
rec := &Record{Step: h.nextStep, Entries: append(h.drainClock(), entries...)}
if _, err := h.recordStep(ctx, rec, nil); err != nil {
return true, err
des, err := h.recordStep(ctx, rec, drivenFireEffects(nil, fires))
if err != nil {
return true, nil, nil, err
}
return true, rec, des, nil
}

// drivenFireEffects appends the effects of the parent Fires an actor system drove, in
// fire order, after a step's own effects, so the driving step names a parent
// transition an actor drove among the effects it dispatches: the step's own first,
// then each driven Fire's, in fire order. The stamped ids follow that order
// (dispatchableEffects numbers every dispatchable effect of one step), and replay
// reproduces it, re-firing the recorded Fires after the step's own work (see
// replayActors), so live and Recover stamp and dispatch the same ids. A refused Fire
// applied nothing and carries no effects; Fires that drove none leave the step's own
// effects unchanged.
func drivenFireEffects[S comparable](own []state.Effect, fires []notedFire[S]) []state.Effect {
var driven []state.Effect
for _, nf := range fires {
driven = append(driven, nf.pf.Result.Effects...)
}
if len(driven) == 0 {
return own
}
return true, h.finishStep(ctx, rec, nil)
out := make([]state.Effect, 0, len(own)+len(driven))
out = append(out, own...)
return append(out, driven...)
}

// DeliverToActorByID is DeliverToActor keyed by raw actor id, for a host that
Expand Down
Loading
Loading