-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrace.go
More file actions
315 lines (302 loc) · 10.7 KB
/
Copy pathtrace.go
File metadata and controls
315 lines (302 loc) · 10.7 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
package flow
import (
"context"
"encoding/json"
"sort"
"time"
"github.com/goware/flow/internal/durable"
"github.com/goware/flow/internal/replay"
"github.com/goware/flow/internal/store"
"github.com/jackc/pgx/v5"
)
// Run is a durable run state snapshot returned by GetRun, AwaitRun, and other
// inspection reads. Enqueue and ReplaceCurrentRun return compact operation
// results instead of snapshots.
type Run struct {
ID RunID
RootCommandName string
RootCommandVersion int
RunKey string
RootCommandID CommandID
Status RunStatus
MaxCommands int
CommandCount int
OpenCommands int
DeadlineAt *time.Time
Failure *Failure
CreatedAt time.Time
UpdatedAt time.Time
StatusAt time.Time
FinishedAt *time.Time
}
type TraceAttempt struct {
ID AttemptID
Attempt int
StartedAt time.Time
FinishedAt *time.Time
Worker string
Classification string
ConsumedBudget bool
ConsumedAttempts int
NextAttemptAt *time.Time
Failure *Failure
}
type TraceCommand struct {
ID CommandID
Key string
Name string
Version int
ParentCommandID CommandID
Status CommandStatus
Args json.RawMessage
Result json.RawMessage
Queue string
InitialDelay time.Duration
BudgetStartedAt *time.Time
NextAttemptAt *time.Time
Within time.Duration
Waits []TraceEventWait
CreatedPosition JournalPosition
TerminalPosition *JournalPosition
Failure *Failure
LastError *Failure
UnsatisfiedWaits int
AttemptOrdinal int
ConsumedAttempts int
WaitStartedAt *time.Time
WaitDeadlineAt *time.Time
DeliveryState QueueState
LeaseOwner string
LeaseStartedAt *time.Time
LeaseExpiresAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
StatusAt time.Time
FinishedAt *time.Time
Attempts []TraceAttempt
}
type TraceEventWait struct {
Name string
Key string
SatisfiedPosition *JournalPosition
}
type TraceEvent struct {
ID EventID
Position JournalPosition
Namespace string
Name string
Key string
Class string
TerminalStatus TerminalStatus
CommandID CommandID
RecordedAt time.Time
CausationPosition *JournalPosition
Body json.RawMessage
}
type RunTrace struct {
Run Run
Commands []TraceCommand
Events []TraceEvent
History []HistoryEntry
}
const maxTraceEntries = 100_000
// Trace reconstructs one run and overlays its current operational
// projections. A Runtime client gets one Flow-owned Repeatable Read snapshot.
// A transaction-scoped client inherits the caller's isolation; callers needing
// a coherent multi-statement snapshot must use Repeatable Read or Serializable.
func Trace(ctx context.Context, c Client, id RunID) (RunTrace, error) {
runID, err := parseRunID(id)
if err != nil {
return RunTrace{}, err
}
client, err := resolveClient(c)
if err != nil {
return RunTrace{}, err
}
var ownedTx pgx.Tx
if client.tx == nil {
ownedTx, err = client.runtime.db.Conn.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
if err != nil {
return RunTrace{}, store.MapError("begin trace snapshot", err)
}
defer ownedTx.Rollback(context.WithoutCancel(ctx))
client.tx = ownedTx
}
rows := make([]store.JournalRow, 0, 64)
var after uint64
for len(rows) < maxTraceEntries {
page, err := client.runtime.store.HistoryInTx(ctx, client.tx, runID, after, store.MaxHistoryLimit)
if err != nil {
return RunTrace{}, err
}
rows = append(rows, page...)
if len(page) < store.MaxHistoryLimit {
break
}
after = uint64(page[len(page)-1].Position)
}
if len(rows) == 0 {
return RunTrace{}, newError(ErrNotFound, "trace", "run", string(id), "run does not exist")
}
if len(rows) >= maxTraceEntries {
return RunTrace{}, newError(ErrInvalid, "trace", "run", string(id), "trace exceeds the initial bounded history limit")
}
state, err := replay.Fold(rows)
if err != nil {
return RunTrace{}, newError(ErrInvalidState, "trace", "run", string(id), "retained history cannot be replayed")
}
live, err := client.runtime.store.GetRunInTx(ctx, client.tx, runID)
if err != nil {
return RunTrace{}, err
}
run, err := runFromStore(live)
if err != nil {
return RunTrace{}, err
}
history, err := historyEntries(rows)
if err != nil {
return RunTrace{}, err
}
result := RunTrace{Run: run, History: history}
operational, err := client.runtime.store.TraceOperationalInTx(ctx, client.tx, runID)
if err != nil {
return RunTrace{}, err
}
operationalCommands := make(map[string]store.TraceCommandRow, len(operational.Commands))
for _, command := range operational.Commands {
operationalCommands[command.ID.String()] = command
}
operationalWaits := make(map[string]*int64, len(operational.Waits))
for _, wait := range operational.Waits {
operationalWaits[wait.CommandID.String()+"\x00"+wait.Name+"\x00"+wait.Key] = wait.SatisfiedPosition
}
result.Commands = make([]TraceCommand, 0, len(state.Commands))
for _, command := range state.Commands {
status, err := commandStatusFromString(command.State)
if err != nil {
return RunTrace{}, newError(ErrInvalidState, "trace", "command status", command.State, "replayed status is unknown")
}
item := TraceCommand{
ID: CommandID(command.ID.String()), Key: command.Key, Name: command.Name, Version: command.Version,
Status: status,
Args: json.RawMessage(append([]byte(nil), command.Args...)), Result: json.RawMessage(append([]byte(nil), command.Result...)),
Queue: command.Queue, CreatedPosition: JournalPosition(command.CreatedPosition),
BudgetStartedAt: cloneTimePointer(command.BudgetStartedAt), NextAttemptAt: cloneTimePointer(command.NextAttemptAt),
Failure: cloneFailure(command.Failure),
}
if command.ParentCommandID != nil {
item.ParentCommandID = CommandID(command.ParentCommandID.String())
}
if command.InitialDelayMS != nil {
item.InitialDelay, err = durable.MillisecondsDuration("replayed command initial delay", *command.InitialDelayMS)
if err != nil {
return RunTrace{}, newError(ErrInvalidState, "trace", "initial delay", "", "replayed duration is out of range")
}
}
if command.WithinMS != nil {
item.Within, err = durable.MillisecondsDuration("replayed command within", *command.WithinMS)
if err != nil {
return RunTrace{}, newError(ErrInvalidState, "trace", "within", "", "replayed duration is out of range")
}
}
for _, wait := range command.Waits {
traceWait := TraceEventWait{Name: wait.Name, Key: wait.Key}
if position := operationalWaits[command.ID.String()+"\x00"+wait.Name+"\x00"+wait.Key]; position != nil {
value := JournalPosition(*position)
traceWait.SatisfiedPosition = &value
}
item.Waits = append(item.Waits, traceWait)
}
if command.TerminalPosition != nil {
position := JournalPosition(*command.TerminalPosition)
item.TerminalPosition = &position
}
if current, ok := operationalCommands[command.ID.String()]; ok {
item.Status, err = commandStatusFromString(current.State)
if err != nil {
return RunTrace{}, newError(ErrInvalidState, "trace", "command status", current.State, "stored status is unknown")
}
item.BudgetStartedAt = cloneTimePointer(current.BudgetStartedAt)
item.NextAttemptAt = cloneTimePointer(current.NextAttemptAt)
item.LastError = cloneFailure(current.LastError)
item.UnsatisfiedWaits = current.UnsatisfiedWaits
item.AttemptOrdinal, item.ConsumedAttempts = current.AttemptOrdinal, current.ConsumedAttempts
item.WaitStartedAt, item.WaitDeadlineAt = cloneTimePointer(current.WaitStartedAt), cloneTimePointer(current.WaitDeadlineAt)
if current.DeliveryState != "" {
item.DeliveryState, err = queueStateFromString(current.DeliveryState)
if err != nil {
return RunTrace{}, newError(ErrInvalidState, "trace", "queue state", current.DeliveryState, "stored state is unknown")
}
}
item.LeaseOwner = current.LeaseOwner
item.LeaseStartedAt, item.LeaseExpiresAt = cloneTimePointer(current.LeaseStartedAt), cloneTimePointer(current.LeaseExpiresAt)
item.CreatedAt, item.UpdatedAt, item.StatusAt = current.CreatedAt, current.UpdatedAt, current.StatusAt
item.FinishedAt = cloneTimePointer(current.FinishedAt)
}
item.Attempts = make([]TraceAttempt, len(command.Attempts))
for index, attempt := range command.Attempts {
item.Attempts[index] = TraceAttempt{
ID: AttemptID(attempt.ID.String()), Attempt: attempt.Ordinal, StartedAt: attempt.StartedAt,
FinishedAt: cloneTimePointer(attempt.FinishedAt), Worker: attempt.Worker,
Classification: attempt.Classification, ConsumedBudget: attempt.ConsumedBudget,
ConsumedAttempts: attempt.ConsumedAttempts, NextAttemptAt: cloneTimePointer(attempt.NextAttemptAt),
Failure: cloneFailure(attempt.Failure),
}
}
result.Commands = append(result.Commands, item)
}
sort.Slice(result.Commands, func(i, j int) bool { return result.Commands[i].Key < result.Commands[j].Key })
result.Events = make([]TraceEvent, 0, len(state.Events))
historyByPosition := make(map[JournalPosition]HistoryEntry, len(result.History))
for _, entry := range result.History {
historyByPosition[entry.Position] = entry
}
for _, event := range state.Events {
entry := historyByPosition[JournalPosition(event.Position)]
var terminalStatus TerminalStatus
if event.TerminalStatus != "" {
terminalStatus, err = terminalStatusFromString(event.TerminalStatus)
if err != nil {
return RunTrace{}, newError(ErrInvalidState, "trace", "terminal status", event.TerminalStatus, "replayed status is unknown")
}
}
item := TraceEvent{
ID: EventID(event.ID.String()), Position: JournalPosition(event.Position), Namespace: event.Namespace,
Name: event.Name, Key: event.Key, Class: event.Class,
TerminalStatus: terminalStatus, RecordedAt: entry.RecordedAt,
CausationPosition: cloneJournalPosition(entry.CausationPosition),
Body: json.RawMessage(append([]byte(nil), event.Body...)),
}
if event.CommandID != nil {
item.CommandID = CommandID(event.CommandID.String())
}
result.Events = append(result.Events, item)
}
if ownedTx != nil {
if err := ownedTx.Commit(ctx); err != nil {
return RunTrace{}, store.MapError("commit trace snapshot", err)
}
}
return result, nil
}
func cloneJournalPosition(value *JournalPosition) *JournalPosition {
if value == nil {
return nil
}
copy := *value
return ©
}
func cloneIntPointer(value *int) *int {
if value == nil {
return nil
}
copy := *value
return ©
}
func cloneTimePointer(value *time.Time) *time.Time {
if value == nil {
return nil
}
copy := *value
return ©
}