-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdurable.go
More file actions
277 lines (245 loc) · 7.93 KB
/
durable.go
File metadata and controls
277 lines (245 loc) · 7.93 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
package ojs
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"sync"
"time"
)
// DurableContext provides deterministic execution support within a job handler.
// Non-deterministic operations (time, randomness, external calls) are recorded
// on first execution and replayed from the checkpoint on retry, ensuring
// idempotent re-execution after crashes.
type DurableContext struct {
mu sync.Mutex
parent context.Context
transport *transport
jobID string
attempt int
entries []sideEffectEntry
cursor int
replaying bool
}
type sideEffectEntry struct {
Seq int `json:"seq"`
Type string `json:"type"`
Key string `json:"key,omitempty"`
Result json.RawMessage `json:"result"`
}
type checkpointState struct {
StepIndex int `json:"step_index"`
Metadata map[string]string `json:"metadata,omitempty"`
State json.RawMessage `json:"state"`
}
// newDurableContext creates a DurableContext, loading any existing checkpoint.
// The parent context is used for checkpoint operations to ensure cancellation
// and distributed tracing propagate correctly.
func newDurableContext(parent context.Context, transport *transport, jobID string, attempt int) *DurableContext {
dc := &DurableContext{
parent: parent,
transport: transport,
jobID: jobID,
attempt: attempt,
}
// Try to load existing replay log from server
var resp struct {
HasCheckpoint bool `json:"has_checkpoint"`
Checkpoint *struct {
Metadata map[string]string `json:"metadata,omitempty"`
} `json:"checkpoint,omitempty"`
}
ctx, cancel := context.WithTimeout(parent, 5*time.Second)
defer cancel()
err := transport.get(ctx, fmt.Sprintf("%s/checkpoints/%s/resume", basePath, jobID), &resp)
if err == nil && resp.HasCheckpoint && resp.Checkpoint != nil {
if logData, ok := resp.Checkpoint.Metadata["_replay_log"]; ok {
var entries []sideEffectEntry
if json.Unmarshal([]byte(logData), &entries) == nil && len(entries) > 0 {
dc.entries = entries
dc.replaying = true
}
}
}
return dc
}
// Now returns the current time deterministically.
// On first execution the real time is recorded; on replay the recorded time is returned.
func (dc *DurableContext) Now() time.Time {
dc.mu.Lock()
defer dc.mu.Unlock()
if dc.replaying && dc.cursor < len(dc.entries) {
entry := dc.entries[dc.cursor]
if entry.Type == "time" {
var t time.Time
if json.Unmarshal(entry.Result, &t) == nil {
dc.cursor++
if dc.cursor >= len(dc.entries) {
dc.replaying = false
}
return t
}
}
}
t := time.Now()
data, _ := json.Marshal(t)
dc.entries = append(dc.entries, sideEffectEntry{
Seq: len(dc.entries), Type: "time", Key: "now", Result: data,
})
dc.replaying = false
return t
}
// Random returns a deterministic random hex string of the specified byte length.
func (dc *DurableContext) Random(numBytes int) string {
dc.mu.Lock()
defer dc.mu.Unlock()
if dc.replaying && dc.cursor < len(dc.entries) {
entry := dc.entries[dc.cursor]
if entry.Type == "random" {
var s string
if json.Unmarshal(entry.Result, &s) == nil {
dc.cursor++
if dc.cursor >= len(dc.entries) {
dc.replaying = false
}
return s
}
}
}
buf := make([]byte, numBytes)
rand.Read(buf)
s := hex.EncodeToString(buf)
data, _ := json.Marshal(s)
dc.entries = append(dc.entries, sideEffectEntry{
Seq: len(dc.entries), Type: "random", Result: data,
})
dc.replaying = false
return s
}
// SideEffect executes fn deterministically. On first execution, fn is called and
// the result is recorded. On replay, the recorded result is returned without
// calling fn. The result must be JSON-serializable.
//
// Example:
//
// price, err := dc.SideEffect("fetch-price", func() (any, error) {
// return externalAPI.GetPrice(productID)
// })
func (dc *DurableContext) SideEffect(key string, fn func() (any, error)) (json.RawMessage, error) {
dc.mu.Lock()
defer dc.mu.Unlock()
if dc.replaying && dc.cursor < len(dc.entries) {
entry := dc.entries[dc.cursor]
if entry.Type == "call" {
// Strict key matching during replay: if both keys are set, they must match.
// A mismatch means the code changed between the checkpoint save and replay,
// which would silently return the wrong data.
if key != "" && entry.Key != "" && entry.Key != key {
return nil, fmt.Errorf("ojs: durable replay key mismatch at position %d: checkpoint has %q but code called %q — handler logic may have changed since checkpoint was saved", dc.cursor, entry.Key, key)
}
dc.cursor++
if dc.cursor >= len(dc.entries) {
dc.replaying = false
}
return entry.Result, nil
}
}
dc.replaying = false
result, err := fn()
if err != nil {
return nil, err
}
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("ojs: marshal side effect %q result: %w", key, err)
}
dc.entries = append(dc.entries, sideEffectEntry{
Seq: len(dc.entries), Type: "call", Key: key, Result: data,
})
return data, nil
}
// Checkpoint saves the current execution state to the server.
// Call this after completing an important step to enable resume from this point.
func (dc *DurableContext) Checkpoint(stepIndex int, state any) error {
dc.mu.Lock()
entriesCopy := make([]sideEffectEntry, len(dc.entries))
copy(entriesCopy, dc.entries)
dc.mu.Unlock()
logData, err := json.Marshal(entriesCopy)
if err != nil {
return fmt.Errorf("ojs: marshal replay log: %w", err)
}
stateData, err := json.Marshal(state)
if err != nil {
return fmt.Errorf("ojs: marshal checkpoint state: %w", err)
}
req := struct {
State json.RawMessage `json:"state"`
StepIndex int `json:"step_index"`
Metadata map[string]string `json:"metadata,omitempty"`
}{
State: stateData,
StepIndex: stepIndex,
Metadata: map[string]string{
"_replay_log": string(logData),
"attempt": fmt.Sprintf("%d", dc.attempt),
},
}
ctx, cancel := context.WithTimeout(dc.parent, 5*time.Second)
defer cancel()
return dc.transport.do(ctx, "PUT",
fmt.Sprintf("%s/checkpoints/%s", basePath, dc.jobID), req, nil)
}
// Complete clears the checkpoint after successful job completion.
func (dc *DurableContext) Complete() error {
ctx, cancel := context.WithTimeout(dc.parent, 5*time.Second)
defer cancel()
return dc.transport.delete(ctx,
fmt.Sprintf("%s/checkpoints/%s", basePath, dc.jobID), nil)
}
// IsReplaying returns true if the context is currently replaying from a checkpoint.
func (dc *DurableContext) IsReplaying() bool {
dc.mu.Lock()
defer dc.mu.Unlock()
return dc.replaying && dc.cursor < len(dc.entries)
}
// --- DurableHandlerFunc ---
// DurableHandlerFunc is a job handler that receives a DurableContext for
// checkpoint-based durable execution.
type DurableHandlerFunc func(ctx JobContext, dc *DurableContext) error
// RegisterDurable registers a job handler with durable execution support.
// The handler receives a DurableContext that provides deterministic wrappers
// for non-deterministic operations.
//
// Example:
//
// worker.RegisterDurable("etl.process", func(ctx ojs.JobContext, dc *ojs.DurableContext) error {
// // Step 1: Fetch data (result is recorded for replay)
// data, err := dc.SideEffect("fetch-data", func() (any, error) {
// return fetchFromAPI(ctx.Job.Args["url"].(string))
// })
// if err != nil {
// return err
// }
// dc.Checkpoint(1, map[string]any{"fetched": true})
//
// // Step 2: Transform
// transformed := transform(data)
// dc.Checkpoint(2, map[string]any{"transformed": true})
//
// // Step 3: Load
// if err := loadIntoDB(transformed); err != nil {
// return err
// }
//
// dc.Complete()
// ctx.SetResult(map[string]any{"records": 100})
// return nil
// })
func (w *Worker) RegisterDurable(jobType string, handler DurableHandlerFunc) {
w.Register(jobType, func(ctx JobContext) error {
dc := newDurableContext(ctx.Context(), w.transport, ctx.Job.ID, ctx.Attempt)
return handler(ctx, dc)
})
}