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
15 changes: 9 additions & 6 deletions durable/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,10 @@ func splitActorEntries(rec *Record) (before, rest []decodedActorEntry, err error
// the recorded entry cannot be decoded or the re-fired transition fails (a corrupt or
// mis-ordered journal), or the recorded done-data won't decode for its event, as the
// live settle refused it. aliases carries the Runner's WithResultTypeAlias renames,
// which the recorded check honors, as a recorded service result's replay does.
func replayActor[S comparable, E comparable, C any](ctx context.Context, inst *state.Instance[S, E, C], codec EventCodec[E], decode EventDataDecoder[E], aliases map[string]string, entry decodedActorEntry) ([]state.Effect, error) {
// which the recorded check honors, as a recorded service result's replay does. names
// is the Runner's cache the recorded type check spells through; the Runner-less
// StateAt passes nil and spells uncached (see typeNames).
func replayActor[S comparable, E comparable, C any](ctx context.Context, inst *state.Instance[S, E, C], codec EventCodec[E], decode EventDataDecoder[E], aliases map[string]string, names *typeNames, entry decodedActorEntry) ([]state.Effect, error) {
mp := entry.mp
// A Fire refused before the kernel saw it (middleware that denied it) has no event
// to re-fire; replay is refused the same way.
Expand All @@ -367,7 +369,7 @@ func replayActor[S comparable, E comparable, C any](ctx context.Context, inst *s
}
var res state.FireResult[S]
if mp.HasData && !mp.Refused {
data, derr := replayActorData(mp, event, decode, aliases)
data, derr := replayActorData(mp, event, decode, aliases, names)
if derr != nil {
return nil, fmt.Errorf("durable: decoding recorded actor %q done-data: %w", entry.actorID, derr)
}
Expand Down Expand Up @@ -403,8 +405,9 @@ func replayActor[S comparable, E comparable, C any](ctx context.Context, inst *s
// service result is; one that won't decode fails the replay, as the live settle
// refused it. A payload that fails to decode is surfaced as an error rather than
// silently dropped, so a corrupt journal fails replay loudly instead of re-firing the
// parent with nil data it never saw on the live path.
func replayActorData[E comparable](mp actorMessagePayload, event E, decode EventDataDecoder[E], aliases map[string]string) (any, error) {
// parent with nil data it never saw on the live path. names is the Runner's cache
// the recorded type check spells through; nil spells uncached (see typeNames).
func replayActorData[E comparable](mp actorMessagePayload, event E, decode EventDataDecoder[E], aliases map[string]string, names *typeNames) (any, error) {
if mp.Error != nil {
return errors.New(*mp.Error), nil
}
Expand All @@ -413,7 +416,7 @@ func replayActorData[E comparable](mp actorMessagePayload, event E, decode Event
}
// The transform decodes the recorded JSON for the onDone event, as it did live,
// and holds the decoder to the recorded type. An abort is the decode's refusal.
out, _, abort := replayableOutcome(decode, aliases, nil, "actor done-data", "actor error")(event, recordedResult{raw: mp.Data, typ: mp.Type}, nil)
out, _, abort := replayableOutcome(decode, aliases, names, nil, "actor done-data", "actor error")(event, recordedResult{raw: mp.Data, typ: mp.Type}, nil)
if abort != nil {
return nil, abort
}
Expand Down
8 changes: 4 additions & 4 deletions durable/actor_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestReplayActorData(t *testing.T) {
errMsg := "boom"

t.Run("error", func(t *testing.T) {
got, err := replayActorData(actorMessagePayload{Error: &errMsg}, "childDone", decodeAny[string], nil)
got, err := replayActorData(actorMessagePayload{Error: &errMsg}, "childDone", decodeAny[string], nil, cacheRunner().names)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -25,7 +25,7 @@ func TestReplayActorData(t *testing.T) {
})

t.Run("valid data", func(t *testing.T) {
got, err := replayActorData(actorMessagePayload{Data: json.RawMessage(`{"k":1}`)}, "childDone", decodeAny[string], nil)
got, err := replayActorData(actorMessagePayload{Data: json.RawMessage(`{"k":1}`)}, "childDone", decodeAny[string], nil, cacheRunner().names)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -36,7 +36,7 @@ func TestReplayActorData(t *testing.T) {
})

t.Run("no data", func(t *testing.T) {
got, err := replayActorData(actorMessagePayload{}, "childDone", decodeAny[string], nil)
got, err := replayActorData(actorMessagePayload{}, "childDone", decodeAny[string], nil, cacheRunner().names)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -46,7 +46,7 @@ func TestReplayActorData(t *testing.T) {
})

t.Run("corrupt data surfaces error", func(t *testing.T) {
got, err := replayActorData(actorMessagePayload{Data: json.RawMessage(`{not json`), Type: "int"}, "childDone", decodeAny[string], nil)
got, err := replayActorData(actorMessagePayload{Data: json.RawMessage(`{not json`), Type: "int"}, "childDone", decodeAny[string], nil, cacheRunner().names)
if err == nil {
t.Fatal("want an error for undecodable done-data, got nil")
}
Expand Down
28 changes: 20 additions & 8 deletions durable/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,25 @@ type Runner[S comparable, E comparable, C any] struct {
machine *state.Machine[S, E, C]
store Store
cfg runnerConfig[S, E, C]
// names is this Runner's cache of each result type's full spelling (see
// typeNames): every settle, park and replay type check a Handle of this Runner
// asks — Start and Recover included — spells through it, and two Runners share
// no entry. It is unexported state, built by NewRunner.
names *typeNames
}

// NewRunner binds a machine and a Store into a durable Runner. Behavior is tuned
// with functional options — the checkpoint policy (WithCheckpointEvery) and the
// event codec (WithEventCodec) — each additive and defaulting to a working
// baseline (no periodic checkpoint, JSON event encoding).
// baseline (no periodic checkpoint, JSON event encoding). Each Runner owns its own
// cache of result types' full spellings (see typeNames), so no two Runners share
// state.
func NewRunner[S comparable, E comparable, C any](m *state.Machine[S, E, C], st Store, opts ...Option[S, E, C]) *Runner[S, E, C] {
return &Runner[S, E, C]{
machine: m,
store: st,
cfg: resolveRunner(opts...),
names: newTypeNames(),
}
}

Expand Down Expand Up @@ -758,7 +766,7 @@ func (r *Runner[S, E, C]) newServiceRunner(inst *state.Instance[S, E, C]) (*stat
return nil, nil
}
settled := &settledJSON{}
return state.NewServiceRunner(inst, r.cfg.serviceReg).WithResultTransform(replayableOutcome(r.cfg.eventDataDecoder, r.cfg.resultAliases, settled, "service result", "service error")), settled
return state.NewServiceRunner(inst, r.cfg.serviceReg).WithResultTransform(replayableOutcome(r.cfg.eventDataDecoder, r.cfg.resultAliases, r.names, settled, "service result", "service error")), settled
}

// newActorSystem builds the kernel host driver for child-machine actors bound to
Expand Down Expand Up @@ -792,7 +800,7 @@ func (r *Runner[S, E, C]) newActorSystem(inst *state.Instance[S, E, C]) (*state.
// to the settle still in flight — notes no abort and fails no Handle.
WithSettleAbortObserver(func(sa state.SettleAbort[E]) { fires.aborted(sa.Abort) })
settleJSON := &settledJSON{}
outcome := replayableOutcome(r.cfg.eventDataDecoder, r.cfg.resultAliases, settleJSON, "actor done-data", "actor error")
outcome := replayableOutcome(r.cfg.eventDataDecoder, r.cfg.resultAliases, r.names, settleJSON, "actor done-data", "actor error")
sys.WithSettleTransform(func(event E, output any, err error) (any, error, error) {
out, outErr, abort := outcome(event, output, err)
if abort == nil {
Expand Down Expand Up @@ -2020,7 +2028,7 @@ func (r *Runner[S, E, C]) recover(ctx context.Context, id InstanceID) (*Handle[S
return nil, fmt.Errorf("durable: recovering %q: %w", id, err)
}
reads += clockReadCount(rec)
stepEffects, err := replayRecord(ctx, inst, &r.cfg, sched, svc, rec, mirror, armsActorFires(rec))
stepEffects, err := replayRecord(ctx, inst, &r.cfg, r.names, sched, svc, rec, mirror, armsActorFires(rec))
if err != nil {
return nil, fmt.Errorf("durable: recovering %q: %w", id, err)
}
Expand Down Expand Up @@ -2495,6 +2503,7 @@ func replayRecord[S comparable, E comparable, C any](
ctx context.Context,
inst *state.Instance[S, E, C],
cfg *runnerConfig[S, E, C],
names *typeNames,
sched *state.Scheduler[S, E, C],
svc *state.ServiceRunner[S, E, C],
rec *Record,
Expand All @@ -2509,7 +2518,7 @@ func replayRecord[S comparable, E comparable, C any](
// Parent Fires a Start's or Recover's spawns drove ran before the step's own work.
// Their effects aren't returned: no live path dispatches them (see the
// Start-spawned note above).
if _, err := replayActors(ctx, inst, cfg, sched, svc, rec, before, mirror, actorArms); err != nil {
if _, err := replayActors(ctx, inst, cfg, names, sched, svc, rec, before, mirror, actorArms); err != nil {
return stepEffects, err
}
switch {
Expand Down Expand Up @@ -2586,7 +2595,7 @@ func replayRecord[S comparable, E comparable, C any](
// drove after it, such as a spawned actor's onError. Their effects follow the
// step's own, in fire order, as the live step named them, so Recover re-dispatches
// a driven Fire's domain effects under the ids live stamped.
driven, derr := replayActors(ctx, inst, cfg, sched, svc, rec, rest, mirror, actorArms)
driven, derr := replayActors(ctx, inst, cfg, names, sched, svc, rec, rest, mirror, actorArms)
if derr != nil {
return stepEffects, derr
}
Expand All @@ -2599,11 +2608,14 @@ func replayRecord[S comparable, E comparable, C any](
// When actorArms, the version that wrote rec armed the timers and services an
// actor-driven Fire declares, so each one arms them, as the live run did. An older
// version armed none, and took no
// clock read for them, so they're left alone, as that run left them.
// clock read for them, so they're left alone, as that run left them. names is the
// Runner's cache the re-fired transitions' recorded type checks spell through; the
// Runner-less StateAt passes nil and spells uncached (see typeNames).
func replayActors[S comparable, E comparable, C any](
ctx context.Context,
inst *state.Instance[S, E, C],
cfg *runnerConfig[S, E, C],
names *typeNames,
sched *state.Scheduler[S, E, C],
svc *state.ServiceRunner[S, E, C],
rec *Record,
Expand All @@ -2613,7 +2625,7 @@ func replayActors[S comparable, E comparable, C any](
) ([]state.Effect, error) {
var driven []state.Effect
for _, entry := range entries {
effects, err := replayActor(ctx, inst, cfg.eventCodec, cfg.eventDataDecoder, cfg.resultAliases, entry)
effects, err := replayActor(ctx, inst, cfg.eventCodec, cfg.eventDataDecoder, cfg.resultAliases, names, entry)
if err != nil {
return nil, fmt.Errorf("at step %d: %w", rec.Step, err)
}
Expand Down
5 changes: 4 additions & 1 deletion durable/runner_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ func WithResultTypeAlias[S comparable, E comparable, C any](recorded string, cur
return func(c *runnerConfig[S, E, C]) {
var currentName string
if current != nil {
currentName = typeName(current)
// The option runs before any Runner exists to own a cache, so the type is
// spelled uncached (see typeNames): the spelling is pure in the type alone,
// so it is the one the Runner's cache will return once built.
currentName = (*typeNames)(nil).typeName(current)
}
if recorded == "" || current == nil || recorded == currentName {
return
Expand Down
Loading
Loading