-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_runtime.go
More file actions
573 lines (547 loc) · 19.8 KB
/
Copy pathcommand_runtime.go
File metadata and controls
573 lines (547 loc) · 19.8 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package flow
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"sync"
"time"
"github.com/google/uuid"
"github.com/goware/flow/internal/canonical"
"github.com/goware/flow/internal/failure"
"github.com/goware/flow/internal/fault"
retrypolicy "github.com/goware/flow/internal/retry"
"github.com/goware/flow/internal/store"
"github.com/goware/flow/internal/store/journalcodec"
"github.com/jackc/pgx/v5"
)
const (
commandProbeFactor = 4
maxCommandProbe = 256
maxCommandResultBytes = 256 << 10
settlementAttempts = 3
)
func (r *Runtime) runCommandScheduler(ctx context.Context) {
slots := newCommandSlots(r.workerConcurrency, r.queueConcurrency)
queueTurn := 0
keys := r.registry.workerKeys()
kinds := make([]store.CommandKind, len(keys))
for index, key := range keys {
kinds[index] = store.CommandKind{Name: key.name, Version: key.version}
}
for {
if ctx.Err() != nil {
return
}
seen := r.wake.snapshot()
free := slots.free()
if free == 0 || len(kinds) == 0 {
r.wake.wait(ctx, seen, r.pollInterval)
continue
}
limit := min(maxCommandProbe, max(free, free*commandProbeFactor))
started := time.Now()
candidates, err := r.store.ProbeCommands(ctx, kinds, limit)
if err == nil {
err = r.faults.Hit(ctx, fault.ProbeReturn)
}
r.observe(ctx, Observation{
Kind: ObservationClaim, Operation: "probe", Outcome: outcomeForError(err),
Count: int64(len(candidates)), Duration: time.Since(started), Worker: r.replicaName(),
})
if err != nil {
r.wake.wait(ctx, seen, r.pollInterval)
continue
}
progress := false
selected := make([]store.CommandCandidate, 0, free)
for _, candidate := range fairQueueCandidates(candidates, &queueTurn) {
if slots.reserve(candidate.Queue) {
selected = append(selected, candidate)
}
if slots.free() == 0 {
break
}
}
if ctx.Err() != nil {
for _, candidate := range selected {
slots.release(candidate.Queue)
}
return
}
for _, group := range groupCandidatesByExecution(selected) {
claimStarted := time.Now()
result, claimErr := r.store.ClaimCommands(ctx, group, r.commandLease, r.replicaName(), r.faults)
if claimErr != nil && len(result.Commands) > 0 {
confirmed := result.Commands[:0]
for _, command := range result.Commands {
ownership, resolveErr := r.store.ResolveCommandAttempt(ctx, command.CommandID, command.AttemptID, command.LeaseToken)
if resolveErr == nil && ownership == store.AttemptOwnershipStillOwned {
confirmed = append(confirmed, command)
}
}
result.Commands = confirmed
if len(confirmed) > 0 {
claimErr = nil
}
}
r.observe(ctx, Observation{
Kind: ObservationClaim, Operation: "claim", Outcome: outcomeForError(claimErr),
ExecutionID: ExecutionID(group[0].ExecutionID.String()), Count: int64(len(result.Commands)),
Duration: time.Since(claimStarted), Worker: r.replicaName(),
})
claimedIDs := make(map[uuid.UUID]struct{}, len(result.Commands))
for _, command := range result.Commands {
claimedIDs[command.CommandID] = struct{}{}
}
for _, candidate := range group {
if _, claimed := claimedIDs[candidate.CommandID]; !claimed {
slots.release(candidate.Queue)
}
}
if result.Progressed {
progress = true
}
if claimErr != nil || len(result.Commands) == 0 {
continue
}
for _, command := range result.Commands {
worker, ok := r.registry.worker(command.Name, command.Version)
if !ok {
slots.release(command.Queue)
continue
}
progress = true
r.workerGroup.Add(1)
go r.executeClaim(worker, command, slots)
}
}
if !progress {
r.wake.wait(ctx, seen, r.pollInterval)
}
}
}
type commandSlots struct {
global chan struct{}
mu sync.Mutex
limits map[string]int
active map[string]int
}
func newCommandSlots(global int, limits map[string]int) *commandSlots {
return &commandSlots{
global: make(chan struct{}, global), limits: cloneIntMap(limits), active: make(map[string]int),
}
}
func (slots *commandSlots) free() int { return cap(slots.global) - len(slots.global) }
func (slots *commandSlots) reserve(queue string) bool {
slots.mu.Lock()
defer slots.mu.Unlock()
if limit := slots.limits[queue]; limit > 0 && slots.active[queue] >= limit {
return false
}
select {
case slots.global <- struct{}{}:
slots.active[queue]++
return true
default:
return false
}
}
func (slots *commandSlots) release(queue string) {
slots.mu.Lock()
if slots.active[queue] > 1 {
slots.active[queue]--
} else {
delete(slots.active, queue)
}
slots.mu.Unlock()
<-slots.global
}
func fairQueueCandidates(candidates []store.CommandCandidate, turn *int) []store.CommandCandidate {
if len(candidates) < 2 {
return candidates
}
byQueue := make(map[string][]store.CommandCandidate)
queues := make([]string, 0)
for _, candidate := range candidates {
if _, exists := byQueue[candidate.Queue]; !exists {
queues = append(queues, candidate.Queue)
}
byQueue[candidate.Queue] = append(byQueue[candidate.Queue], candidate)
}
if len(queues) < 2 {
return candidates
}
sort.Strings(queues)
start := 0
if turn != nil {
start = *turn % len(queues)
*turn = (start + 1) % len(queues)
}
ordered := make([]store.CommandCandidate, 0, len(candidates))
for offset := 0; len(ordered) < len(candidates); offset++ {
for queueOffset := range len(queues) {
queue := queues[(start+queueOffset)%len(queues)]
if offset < len(byQueue[queue]) {
ordered = append(ordered, byQueue[queue][offset])
}
}
}
return ordered
}
func groupCandidatesByExecution(candidates []store.CommandCandidate) [][]store.CommandCandidate {
groups := make([][]store.CommandCandidate, 0, len(candidates))
indexes := make(map[uuid.UUID]int, len(candidates))
for _, candidate := range candidates {
index, exists := indexes[candidate.ExecutionID]
if !exists {
index = len(groups)
indexes[candidate.ExecutionID] = index
groups = append(groups, nil)
}
groups[index] = append(groups[index], candidate)
}
return groups
}
func (r *Runtime) executeClaim(worker erasedWorker, claim store.ClaimedCommand, slots *commandSlots) {
defer r.workerGroup.Done()
baseCtx, cancelCause := context.WithCancelCause(context.Background())
workerCtx := baseCtx
cancelDeadline := func() {}
if remaining, ok := commandAttemptRemaining(claim); ok {
var cancel context.CancelFunc
workerCtx, cancel = context.WithTimeoutCause(baseCtx, max(0, remaining), errAttemptTimeout)
cancelDeadline = cancel
}
localLeaseExpiry := time.Now().Add(max(0, claim.LeaseExpiresAt.Sub(claim.DBNow)))
r.active.register(activeCommand{
commandID: claim.CommandID, attemptID: claim.AttemptID, token: claim.LeaseToken,
localExpiry: localLeaseExpiry, cancel: cancelCause,
})
r.mu.RLock()
stopping := r.lifecycle == runtimeStopping || r.lifecycle == runtimeStopped
r.mu.RUnlock()
if stopping {
cancelCause(errRuntimeShutdown)
}
defer func() {
cancelDeadline()
cancelCause(nil)
r.active.unregister(claim.CommandID, claim.AttemptID)
slots.release(claim.Queue)
r.wake.signal()
}()
args, err := worker.command.Args.Decode(claim.Args)
if err != nil {
r.concludeClaim(workerCtx, claim, classifiedConclusion{
class: retrypolicy.ClassPermanent, code: "argument_decode", message: "stored command arguments do not match the registered definition",
})
return
}
info := CommandInfo{
ExecutionID: ExecutionID(claim.ExecutionID.String()), CommandID: CommandID(claim.CommandID.String()),
CommandKey: claim.CommandKey, Name: claim.Name, Version: claim.Version,
CreatedAt: claim.CreatedAt, BudgetStartedAt: claim.BudgetStartedAt,
Attempt: claim.Attempt, AttemptStartedAt: claim.DBNow,
}
inputs, err := r.store.LoadCommandInputs(workerCtx, claim.CommandID)
if err != nil {
r.concludeClaim(context.Background(), claim, classifiedConclusion{
class: retrypolicy.ClassInterrupted, code: "dependency_load", message: "dependency inputs could not be loaded",
})
return
}
scope := &workScope{args: args, info: info}
scope.state.results = workerResultSource(inputs)
workerCtx = withAttemptScope(workerCtx, &scope.state)
if err := r.faults.Hit(workerCtx, fault.HandlerStart); err != nil {
r.concludeClaim(workerCtx, claim, classifiedConclusion{class: retrypolicy.ClassInterrupted, code: "handler_start_interrupted", message: "handler start was interrupted"})
return
}
started := time.Now()
result, workerErr, panicked := invokeWorker(workerCtx, worker, scope)
if hookErr := r.faults.Hit(workerCtx, fault.HandlerReturn); hookErr != nil {
workerErr = hookErr
}
r.observe(context.Background(), Observation{
Kind: ObservationAttempt, Operation: "handler", Outcome: outcomeForError(workerErr),
ExecutionID: info.ExecutionID, CommandID: info.CommandID, CommandKey: info.CommandKey,
Name: info.Name, Version: info.Version, Queue: claim.Queue, Worker: r.replicaName(), Duration: time.Since(started),
})
if cause := context.Cause(workerCtx); cause != nil {
r.concludeClaim(context.Background(), claim, classifyWorkerError(cause, false))
return
}
if panicked || workerErr != nil || scope.state.firstError != nil {
if scope.state.firstError != nil {
workerErr = scope.state.firstError
}
r.concludeClaim(context.Background(), claim, classifyWorkerError(workerErr, panicked))
return
}
encoded, err := worker.command.Result.Encode(result, maxCommandResultBytes)
if err != nil {
r.concludeClaim(context.Background(), claim, classifiedConclusion{
class: retrypolicy.ClassPermanent, code: "result_encode", message: "worker result is invalid or exceeds the result limit",
})
return
}
events, children, err := prepareWorkerDecision(scope, claim)
if err != nil {
r.concludeClaim(context.Background(), claim, classifiedConclusion{
class: retrypolicy.ClassPermanent, code: "invalid_decision", message: safeErrorMessage(err),
})
return
}
commit := func(tx pgx.Tx) error { return nil }
if worker.commit != nil {
commit = func(tx pgx.Tx) error {
commitErr := worker.commit(workerCtx, tx, args, result, info)
if scope.state.firstError != nil {
return scope.state.firstError
}
return commitErr
}
} else {
commit = nil
}
for attempt := 0; attempt < settlementAttempts; attempt++ {
_, settleErr := r.store.SettleCommandSuccess(context.Background(), store.CommandSuccess{
Claim: claim, Result: encoded, Events: events, Children: children, Commit: commit,
}, r.faults)
if settleErr == nil {
for _, event := range events {
r.observe(context.Background(), Observation{
Kind: ObservationEvent, Operation: "settle", Outcome: "accepted",
ExecutionID: info.ExecutionID, CommandID: info.CommandID, CommandKey: info.CommandKey,
Name: event.Name, Worker: r.replicaName(),
})
}
r.observe(context.Background(), Observation{
Kind: ObservationAttempt, Operation: "settle", Outcome: "succeeded",
ExecutionID: info.ExecutionID, CommandID: info.CommandID, CommandKey: info.CommandKey,
Name: info.Name, Version: info.Version, Queue: claim.Queue, Worker: r.replicaName(), Count: int64(len(events)),
})
return
}
var commitErr *store.CommitFunctionError
if errors.As(settleErr, &commitErr) {
if errors.Is(commitErr.Err, ErrConflict) || errors.Is(commitErr.Err, ErrInvalid) ||
errors.Is(commitErr.Err, ErrInvalidState) || errors.Is(commitErr.Err, ErrPayloadTooLarge) {
r.concludeClaim(context.Background(), claim, classifiedConclusion{
class: retrypolicy.ClassPermanent, code: "invalid_decision", message: safeErrorMessage(commitErr.Err),
})
return
}
r.concludeClaim(context.Background(), claim, classifyWorkerError(commitErr.Err, false))
return
}
if errors.Is(settleErr, ErrConflict) || errors.Is(settleErr, ErrInvalid) ||
errors.Is(settleErr, ErrInvalidState) || errors.Is(settleErr, ErrPayloadTooLarge) {
r.concludeClaim(context.Background(), claim, classifiedConclusion{
class: retrypolicy.ClassPermanent, code: "invalid_decision", message: safeErrorMessage(settleErr),
})
return
}
ownership, resolveErr := r.store.ResolveCommandAttempt(context.Background(), claim.CommandID, claim.AttemptID, claim.LeaseToken)
if resolveErr == nil && ownership == store.AttemptOwnershipConcluded {
return
}
if resolveErr == nil && ownership == store.AttemptOwnershipLost || errors.Is(settleErr, ErrLeaseLost) || errors.Is(settleErr, ErrTerminal) {
return
}
if attempt+1 < settlementAttempts {
time.Sleep(time.Duration(attempt+1) * 10 * time.Millisecond)
}
}
}
func workerResultSource(inputs []store.CommandInput) resultSourceState {
state := resultSourceState{restricted: true, values: make(map[string]resultSourceValue, len(inputs))}
for _, input := range inputs {
value := resultSourceValue{
name: input.Name, version: input.Version, status: commandStatus(input.State),
result: append([]byte(nil), input.Result...),
}
if input.Failure != nil {
value.failure = &CommandFailure{Code: input.Failure.Code, Message: input.Failure.Message}
} else if value.status != "" && value.status != StatusSucceeded {
value.failure = &CommandFailure{Code: input.State, Message: "command ended " + input.State}
}
state.values[input.Key] = value
}
return state
}
func commandStatus(state string) CommandStatus {
switch CommandStatus(state) {
case StatusSucceeded, StatusFailed, StatusCancelled, StatusExpired, StatusSkipped:
return CommandStatus(state)
default:
return ""
}
}
func prepareWorkerDecision(scope *workScope, claim store.ClaimedCommand) ([]store.ApplicationEvent, []store.CommandCreate, error) {
stagedEvents := scope.state.decision.orderedEvents()
events := make([]store.ApplicationEvent, 0, len(stagedEvents))
for _, staged := range stagedEvents {
body, err := canonical.Marshal(journalcodec.ApplicationEventBody{
V: 1, Payload: json.RawMessage(staged.payload.BytesCopy()),
}, 0)
if err != nil {
return nil, nil, newError(ErrInvalid, "settle", "event", staged.key, "event body cannot be journaled")
}
events = append(events, store.ApplicationEvent{
ID: uuid.New(), Name: staged.definition.Name, Key: staged.key, Body: body,
})
}
stagedCommands := scope.state.decision.orderedCommands()
children := make([]store.CommandCreate, 0, len(stagedCommands))
for _, staged := range stagedCommands {
child, err := prepareCommand(uuid.New(), staged.key, staged.definition, staged.defaults, staged.args, "worker_child")
if err != nil {
return nil, nil, err
}
child.ParentCommandID = cloneUUIDPointer(claim.CommandID)
child.Required = staged.required
if staged.startAfter > 0 {
child.ScheduleKind = "execute_delay"
child.InitialDelay = staged.startAfter
}
declaration, err := canonical.Marshal(struct {
V int `json:"v"`
Key string `json:"key"`
Name string `json:"name"`
Version int `json:"version"`
Args json.RawMessage `json:"args"`
Origin string `json:"origin"`
Parent string `json:"parent"`
Required bool `json:"required"`
StartAfterMS int64 `json:"start_after_ms,omitempty"`
}{
V: 1, Key: child.Key, Name: child.Name, Version: child.Version,
Args: json.RawMessage(child.Args.BytesCopy()), Origin: child.Origin,
Parent: claim.CommandID.String(), Required: child.Required,
StartAfterMS: child.InitialDelay.Milliseconds(),
}, 0)
if err != nil {
return nil, nil, newError(ErrInvalid, "settle", "command", child.Key, "declaration cannot be canonicalized")
}
child.DeclarationFingerprint = declaration.Digest
children = append(children, child)
}
return events, children, nil
}
func cloneUUIDPointer(value uuid.UUID) *uuid.UUID {
copy := value
return ©
}
func invokeWorker(ctx context.Context, worker erasedWorker, scope *workScope) (result any, err error, panicked bool) {
defer func() {
if recover() != nil {
result = nil
err = errors.New("worker panicked")
panicked = true
}
}()
result, err = worker.invoke(ctx, scope)
return result, err, false
}
type classifiedConclusion struct {
class retrypolicy.ErrorClass
explicitDelay *time.Duration
code string
message string
}
func classifyWorkerError(err error, panicked bool) classifiedConclusion {
if panicked {
return classifiedConclusion{class: retrypolicy.ClassPanic, code: "panic", message: "worker panicked"}
}
switch {
case errors.Is(err, ErrLeaseLost):
return classifiedConclusion{class: retrypolicy.ClassLeaseLost, code: "lease_lost", message: "command lease was lost"}
case errors.Is(err, errRuntimeShutdown):
return classifiedConclusion{class: retrypolicy.ClassInterrupted, code: "shutdown", message: "runtime shutdown interrupted the attempt"}
case errors.Is(err, errAttemptTimeout), errors.Is(err, context.DeadlineExceeded):
return classifiedConclusion{class: retrypolicy.ClassTimeout, code: "attempt_timeout", message: "command attempt timed out"}
case failure.IsPermanent(err):
return classifiedConclusion{class: retrypolicy.ClassPermanent, code: "permanent", message: safeErrorMessage(err)}
}
if delay, ok := failure.RetryDelay(err); ok {
if delay <= 0 {
return classifiedConclusion{class: retrypolicy.ClassPermanent, code: "invalid_retry_after", message: "retry delay must be positive"}
}
return classifiedConclusion{class: retrypolicy.ClassRetryAfter, explicitDelay: &delay, code: "retry_after", message: safeErrorMessage(err)}
}
if errors.Is(err, context.Canceled) {
return classifiedConclusion{class: retrypolicy.ClassInterrupted, code: "interrupted", message: "command attempt was interrupted"}
}
return classifiedConclusion{class: retrypolicy.ClassRetryable, code: "worker_error", message: safeErrorMessage(err)}
}
func (r *Runtime) concludeClaim(ctx context.Context, claim store.ClaimedCommand, conclusion classifiedConclusion) {
for attempt := 0; attempt < settlementAttempts; attempt++ {
result, err := r.store.SettleCommandConclusion(ctx, store.CommandConclusion{
Claim: claim, Classification: conclusion.class, ExplicitDelay: conclusion.explicitDelay,
ErrorCode: conclusion.code, ErrorMessage: conclusion.message,
}, r.faults)
if err == nil {
if result.Retry {
r.wake.signal()
}
r.observe(context.Background(), Observation{
Kind: ObservationAttempt, Operation: "conclude", Outcome: result.Status,
ExecutionID: ExecutionID(claim.ExecutionID.String()), CommandID: CommandID(claim.CommandID.String()),
CommandKey: claim.CommandKey, Name: claim.Name, Version: claim.Version, Queue: claim.Queue, Worker: r.replicaName(),
})
return
}
ownership, resolveErr := r.store.ResolveCommandAttempt(context.Background(), claim.CommandID, claim.AttemptID, claim.LeaseToken)
if resolveErr == nil && ownership != store.AttemptOwnershipStillOwned {
return
}
if errors.Is(err, ErrLeaseLost) || errors.Is(err, ErrTerminal) {
return
}
if attempt+1 < settlementAttempts {
time.Sleep(time.Duration(attempt+1) * 10 * time.Millisecond)
}
}
}
func commandAttemptRemaining(claim store.ClaimedCommand) (time.Duration, bool) {
var deadline time.Time
if claim.AttemptTimeout > 0 {
deadline = claim.DBNow.Add(claim.AttemptTimeout)
}
if claim.RetryMaxElapsed != nil {
candidate := claim.BudgetStartedAt.Add(*claim.RetryMaxElapsed)
if deadline.IsZero() || candidate.Before(deadline) {
deadline = candidate
}
}
if claim.ExecutionDeadline != nil && (deadline.IsZero() || claim.ExecutionDeadline.Before(deadline)) {
deadline = *claim.ExecutionDeadline
}
if deadline.IsZero() {
return 0, false
}
return deadline.Sub(claim.DBNow), true
}
func safeErrorMessage(err error) string {
if err == nil {
return "worker returned an error"
}
message := err.Error()
if len(message) > 1024 {
message = message[:1024]
}
return message
}
func outcomeForError(err error) string {
if err == nil {
return "ok"
}
return "error"
}
func (r *Runtime) wakeCommands() { r.wake.signal() }
func unexpectedWorkerError(name string, version int) error {
return fmt.Errorf("worker %s/%d is not registered", name, version)
}