-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.go
More file actions
472 lines (431 loc) · 13.9 KB
/
Copy pathworker.go
File metadata and controls
472 lines (431 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package flow
import (
"bytes"
"context"
"errors"
"fmt"
"slices"
"sort"
"time"
"github.com/goware/flow/internal/canonical"
"github.com/goware/flow/internal/definition"
retrypolicy "github.com/goware/flow/internal/retry"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type Tx interface {
Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
Query(context.Context, string, ...any) (pgx.Rows, error)
QueryRow(context.Context, string, ...any) pgx.Row
}
type Commit[A, R any] struct {
Args A
Result R
Info CommandInfo
}
// Work is the attempt-local command scope passed to a worker. It represents
// one invocation of one claimed command, not the whole Run and not the
// immutable Command definition. Args contains the command's typed arguments;
// Info returns its durable run, command, and attempt identity. The private
// scope backs Enqueue, Emit, and GetEventValue for the decision being built by
// this invocation.
//
// A fresh Work is created for every command attempt. It is valid only during
// the worker call and must not be retained or used concurrently.
type Work[A any] struct {
Args A
info CommandInfo
scope *scopeState
}
// Info returns immutable identity and timing information for the claimed
// command and its current attempt.
func (w *Work[A]) Info() CommandInfo {
if w == nil {
return CommandInfo{}
}
return w.info
}
func (w *Work[A]) flowScope() *scopeState {
if w == nil {
return nil
}
return w.scope
}
type WorkerOption[A, R any] interface {
applyWorker(*workerOptionState[A, R])
}
type workerOptionState[A, R any] struct {
commit func(context.Context, Tx, Commit[A, R]) error
commitSet bool
errs []error
}
type workerOptionFunc[A, R any] func(*workerOptionState[A, R])
func (f workerOptionFunc[A, R]) applyWorker(state *workerOptionState[A, R]) { f(state) }
func WithCommit[A, R any](fn func(context.Context, Tx, Commit[A, R]) error) WorkerOption[A, R] {
return workerOptionFunc[A, R](func(state *workerOptionState[A, R]) {
if state.commitSet {
state.errs = append(state.errs, errors.New("commit function configured more than once"))
return
}
state.commitSet = true
if fn == nil {
state.errs = append(state.errs, errors.New("commit function must not be nil"))
return
}
state.commit = fn
})
}
type Registration interface {
flowRegistration() registrationData
}
type registrationData struct {
name string
version int
value any
validation error
}
type erasedWorker struct {
command *definition.Command
defaults commandDefaults
invoke func(context.Context, *workScope) (any, error)
commit func(context.Context, Tx, any, any, CommandInfo) error
}
type workScope struct {
args any
info CommandInfo
state scopeState
}
type workerRegistration struct {
data registrationData
}
func (r workerRegistration) flowRegistration() registrationData { return r.data }
func Handle[A, R any](
cmd Command[A, R],
worker func(context.Context, *Work[A]) (R, error),
opts ...WorkerOption[A, R],
) Registration {
state := workerOptionState[A, R]{}
for _, option := range opts {
if option == nil {
state.errs = append(state.errs, errors.New("nil worker option"))
continue
}
option.applyWorker(&state)
}
validation := errors.Join(cmd.err, errors.Join(state.errs...))
if cmd.def == nil {
validation = errors.Join(validation, errors.New("zero command definition"))
}
if worker == nil {
validation = errors.Join(validation, errors.New("worker function must not be nil"))
}
erased := erasedWorker{command: cmd.def, defaults: cmd.defaults}
if worker != nil {
erased.invoke = func(ctx context.Context, scope *workScope) (any, error) {
args, ok := scope.args.(A)
if !ok {
return nil, newError(ErrInvalid, "invoke", "command", scope.info.CommandKey, "argument type mismatch")
}
work := &Work[A]{Args: args, info: scope.info, scope: &scope.state}
return worker(ctx, work)
}
}
if state.commit != nil {
erased.commit = func(ctx context.Context, tx Tx, args, result any, info CommandInfo) error {
typedArgs, argsOK := args.(A)
typedResult, resultOK := result.(R)
if !argsOK || !resultOK {
return newError(ErrInvalid, "commit", "command", info.CommandKey, "durable type mismatch")
}
return state.commit(ctx, tx, Commit[A, R]{Args: typedArgs, Result: typedResult, Info: info})
}
}
name, version := "", 0
if cmd.def != nil {
name, version = cmd.def.Name, cmd.def.Version
}
return workerRegistration{data: registrationData{
name: name,
version: version,
value: erased,
validation: validation,
}}
}
type scopeState struct {
firstError error
decision decisionState
eventInputs map[string]eventInputSnapshot
}
type eventInputSnapshot struct {
position int64
payload []byte
}
type stagedEvent struct {
definition *definition.Event
key string
payload canonical.Value
}
type stagedCommand struct {
definition *definition.Command
defaults commandDefaults
key string
args canonical.Value
startAfter time.Duration
waits []commandEventWait
within time.Duration
}
type commandEventWait struct {
eventReference
key string
}
type decisionState struct {
events map[string]stagedEvent
eventOrder []string
commands map[string]stagedCommand
commandOrder []string
}
func (s *scopeState) poison(err error) {
if s != nil && s.firstError == nil {
s.firstError = err
}
}
// Emit stages an application event in a worker decision. It
// performs no database work and becomes durable only when the enclosing
// decision settles successfully.
func Emit[W, T any](work *Work[W], event Event[T], key string, payload T) error {
state, err := usableWork(work, "emit")
if err != nil {
return err
}
if event.def == nil || event.def.Namespace != "application" || event.err != nil {
err = newError(ErrInvalid, "emit", "event", key, "invalid application event definition")
state.poison(err)
return err
}
if err = validateStableKey(key, maxCommandKeyBytes, "event"); err != nil {
state.poison(err)
return err
}
encoded, err := encodeDefinitionValue(event.def.Payload, payload, maxApplicationEventBytes, "event payload")
if err != nil {
state.poison(err)
return err
}
identity := event.def.Name + "\x00" + key
if state.decision.events == nil {
state.decision.events = make(map[string]stagedEvent)
}
if prior, exists := state.decision.events[identity]; exists {
if bytes.Equal(prior.payload.Bytes, encoded.Bytes) {
return nil
}
err = newError(ErrConflict, "emit", "event", key, "event identity was staged with different content")
state.poison(err)
return err
}
if len(state.decision.events) >= maxStagedApplicationEvents {
err = newError(ErrInvalid, "emit", "event", key, "decision exceeds the 256 staged-event limit")
state.poison(err)
return err
}
state.decision.events[identity] = stagedEvent{definition: event.def, key: key, payload: encoded}
state.decision.eventOrder = append(state.decision.eventOrder, identity)
return nil
}
// Enqueue requests a command from a worker. It never invokes
// the worker inline; the command is staged in the enclosing durable decision.
func Enqueue[W, A, R any](work *Work[W], key string, cmd Command[A, R], args A) *StagedCommand {
state, err := usableWork(work, "enqueue")
node := &StagedCommand{scope: state, key: key}
if err != nil {
return node
}
if cmd.def == nil || cmd.err != nil {
err = newError(ErrInvalid, "enqueue", "command", key, "invalid command definition")
state.poison(err)
return node
}
if err = validateStableKey(key, maxCommandKeyBytes, "command"); err != nil {
state.poison(err)
return node
}
encoded, err := encodeDefinitionValue(cmd.def.Args, args, maxCommandArgumentBytes, "command arguments")
if err != nil {
state.poison(err)
return node
}
staged := stagedCommand{definition: cmd.def, defaults: cmd.defaults, key: key, args: encoded}
if state.decision.commands == nil {
state.decision.commands = make(map[string]stagedCommand)
}
if prior, exists := state.decision.commands[key]; exists {
if equivalentStagedCommandIdentity(prior, staged) {
return node
}
err = newError(ErrConflict, "enqueue", "command", key, "command key was staged with a different declaration")
state.poison(err)
return node
}
state.decision.commands[key] = staged
state.decision.commandOrder = append(state.decision.commandOrder, key)
return node
}
func equivalentStagedCommandIdentity(a, b stagedCommand) bool {
left, right := a, b
left.startAfter, right.startAfter = 0, 0
left.waits, right.waits = nil, nil
left.within, right.within = 0, 0
return equivalentStagedCommand(left, right)
}
func usableWork[W any](work *Work[W], operation string) (*scopeState, error) {
if work == nil || work.flowScope() == nil {
return nil, newError(ErrInvalidState, operation, "work", "", "work is unavailable")
}
state := work.flowScope()
if state.firstError != nil {
return state, state.firstError
}
return state, nil
}
func equivalentStagedCommand(a, b stagedCommand) bool {
if a.definition == nil || b.definition == nil {
return false
}
if a.definition.Name != b.definition.Name || a.definition.Version != b.definition.Version ||
a.startAfter != b.startAfter || a.within != b.within ||
!equivalentCommandDefaults(a.defaults, b.defaults) ||
!bytes.Equal(a.args.Bytes, b.args.Bytes) || !slices.Equal(a.waits, b.waits) {
return false
}
return true
}
func equivalentCommandDefaults(a, b commandDefaults) bool {
if a.queue != b.queue || a.attemptTimeout != b.attemptTimeout || a.recoveryLease != b.recoveryLease {
return false
}
left, leftErr := retrypolicy.CanonicalPublic(a.retryPolicy)
right, rightErr := retrypolicy.CanonicalPublic(b.retryPolicy)
return leftErr == nil && rightErr == nil && bytes.Equal(left.Bytes, right.Bytes)
}
func addCommandEventWait(waits []commandEventWait, wait commandEventWait) []commandEventWait {
if slices.Contains(waits, wait) {
return waits
}
waits = append(waits, wait)
sort.Slice(waits, func(i, j int) bool {
if waits[i].namespace != waits[j].namespace {
return waits[i].namespace < waits[j].namespace
}
if waits[i].name != waits[j].name {
return waits[i].name < waits[j].name
}
return waits[i].key < waits[j].key
})
return waits
}
func validateDecisionCommands(state decisionState) error {
for _, command := range state.commands {
if command.within > 0 && len(command.waits) == 0 {
return newError(ErrInvalid, "enqueue", "within", command.key, "Within requires WaitFor")
}
if len(command.waits) > maxCommandEventWaits {
return newError(ErrInvalid, "enqueue", "wait", command.key, "command exceeds the 256 event-wait limit")
}
}
return nil
}
func (state *decisionState) orderedEvents() []stagedEvent {
if state == nil || len(state.events) == 0 {
return nil
}
keys := append([]string(nil), state.eventOrder...)
sort.Strings(keys)
result := make([]stagedEvent, 0, len(keys))
for _, key := range keys {
result = append(result, state.events[key])
}
return result
}
func (state *decisionState) orderedCommands() []stagedCommand {
if state == nil || len(state.commands) == 0 {
return nil
}
keys := append([]string(nil), state.commandOrder...)
sort.Strings(keys)
result := make([]stagedCommand, 0, len(keys))
for _, key := range keys {
result = append(result, state.commands[key])
}
return result
}
// GetEventValue returns the typed value attached to an exact event gate
// materialized for the current command. The value is already in memory when
// the worker starts; this function does not wait or query the database.
// found=false reports ordinary absence without poisoning the worker decision.
func GetEventValue[W, T any](work *Work[W], event Event[T], key string) (T, bool, error) {
var zero T
state, err := usableWork(work, "get event value")
if err != nil {
return zero, false, err
}
if event.def == nil || event.def.Namespace != "application" || event.err != nil {
err = newError(ErrInvalid, "get", "event value", key, "invalid application event definition")
state.poison(err)
return zero, false, err
}
if err = validateStableKey(key, maxCommandKeyBytes, "event"); err != nil {
state.poison(err)
return zero, false, err
}
input, ok := state.eventInputs[event.def.Name+"\x00"+key]
if !ok {
return zero, false, nil
}
decoded, err := event.def.Payload.Decode(input.payload)
if err != nil {
err = newError(ErrInvalidState, "get", "event value", key, "stored event payload cannot be decoded")
state.poison(err)
return zero, false, err
}
result, ok := decoded.(T)
if !ok {
err = newError(ErrInvalidState, "get", "event value", key, "stored event payload has an incompatible type")
state.poison(err)
return zero, false, err
}
return result, true, nil
}
func ResultOf[A, R any](trace RunTrace, key string, cmd Command[A, R]) (R, error) {
var zero R
value, err := lookupTraceResult(trace, key, cmd.def)
if err != nil {
return zero, NoRetry(err)
}
decoded, err := cmd.def.Result.Decode(value.Result)
if err != nil {
return zero, NoRetry(newError(ErrInvalidState, "result", "command", key, "stored result cannot be decoded"))
}
result, ok := decoded.(R)
if !ok {
return zero, NoRetry(newError(ErrInvalidState, "result", "command", key, "stored result has an incompatible type"))
}
return result, nil
}
func lookupTraceResult(trace RunTrace, key string, command *definition.Command) (TraceCommand, error) {
if command == nil {
return TraceCommand{}, newError(ErrInvalid, "read", "command", key, "invalid command definition")
}
for _, value := range trace.Commands {
if value.Key != key {
continue
}
if value.Name != command.Name || value.Version != command.Version {
return TraceCommand{}, newError(ErrConflict, "read", "command", key, fmt.Sprintf("definition differs from %s/%d", value.Name, value.Version))
}
if value.Status != CommandStatusSucceeded {
return TraceCommand{}, newError(ErrInvalidState, "read", "command", key, "command has no successful result")
}
return value, nil
}
return TraceCommand{}, newError(ErrNotFound, "read", "command", key, "command does not exist in the trace")
}