-
Notifications
You must be signed in to change notification settings - Fork 9
/
continuous.go
482 lines (405 loc) · 12.3 KB
/
continuous.go
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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package compute
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/executor"
"namespacelabs.dev/foundation/std/tasks"
"namespacelabs.dev/go-ids"
)
// ErrDoneSinking is used as a return value from Updated to indicate that
// no more action will be taken and Sink() should return. It is not
// returned as an error by any function.
var ErrDoneSinking = errors.New("done sinking")
type ObserveNote string
const (
ObserveContinuing ObserveNote = "observe.continuing"
ObserveDone ObserveNote = "observe.done"
)
type Sinkable interface {
Inputs() *In
Updated(context.Context, Resolved) error
Cleanup(context.Context) error
}
type Versioned interface {
Observe(context.Context, func(ResultWithTimestamp[any], ObserveNote)) (func(), error)
}
type continuousKey string
const _continuousKey = continuousKey("fn.compute.continuous")
func fromCtx(ctx context.Context) *sinkInvocation {
v := ctx.Value(_continuousKey)
if v == nil {
return nil
}
return v.(*sinkInvocation)
}
func Stop(ctx context.Context, err error) {
if inv := fromCtx(ctx); inv != nil {
inv.eg.Go(func(ctx context.Context) error {
return err
})
} else {
panic("not under a Continuously() scope")
}
}
type TransformErrorFunc func(error) error
// Continuously computes `sinkable` and recomputes it on any tansitive change to `sinkable`'s inputs.
//
// `transformErr` (if not nil) allows to transform (e.g. ignore) encountered errors.
func Continuously(baseCtx context.Context, sinkable Sinkable, transformErr TransformErrorFunc) error {
g := &sinkInvocation{}
if transformErr != nil {
g.transformErr = transformErr
} else {
// For nil-safety - use a identity transform function.
g.transformErr = func(err error) error { return err }
}
ctx := context.WithValue(baseCtx, _continuousKey, g)
// We want all executions under the executor to be able to obtain the current invocation.
eg := executor.New(ctx, "compute.continuously")
g.eg = eg
g.sink(ctx, sinkable.Inputs(), sinkable.Updated)
err := eg.Wait()
if err := sinkable.Cleanup(ctx); err != nil {
fmt.Fprintf(console.Warnings(ctx), "clean failed: %v\n", err)
}
if errors.Is(err, ErrDoneSinking) {
return nil
}
return err
}
func SpawnCancelableOnContinuously(ctx context.Context, f func(context.Context) error) func() {
return fromCtx(ctx).eg.GoCancelable(f)
}
type sinkInvocation struct {
eg executor.ExecutorLike
mu sync.Mutex
globals map[string]*observable
transformErr TransformErrorFunc
}
func (g *sinkInvocation) sink(ctx context.Context, in *In, updated func(context.Context, Resolved) error) {
var requiredKeys []string
for _, kv := range in.ins {
if _, isComputable := kv.Value.(UntypedComputable); isComputable {
requiredKeys = append(requiredKeys, kv.Name)
}
}
if len(requiredKeys) == 0 {
g.eg.Go(func(ctx context.Context) error {
err := updated(ctx, Resolved{})
if err == ErrDoneSinking {
return nil
}
return err
})
return
}
var mu sync.Mutex
var done bool
invalidations := make(chan observableUpdate)
rebuilt := func(key string, rwt ResultWithTimestamp[any]) bool {
mu.Lock()
wasDone := done
mu.Unlock()
if !wasDone {
invalidations <- observableUpdate{key, rwt}
}
return !wasDone // Return true to continue.
}
for _, kv := range in.ins {
c, isComputable := kv.Value.(UntypedComputable)
if !isComputable {
continue
}
instance := c.prepareCompute(c)
if instance.IsGlobal {
g.ensureObserver(c, instance, "", kv.Name, rebuilt)
} else if instance.IsPrecomputed {
o := g.newObserver(c, kv.Name, rebuilt)
g.eg.Go(o.Loop)
} else {
// Non-global observers follow a similar model: if the inputs are
// fully described, then we treat it as a global computation. If
// not, we maintain a computation keyed by a stable unique ID bound
// to the computable instance.
g.ensureObserver(c, instance, instance.State.ensureUniqueID(), kv.Name, rebuilt)
}
}
g.eg.Go(func(ctx context.Context) error {
var last, pending map[string]ResultWithTimestamp[any]
var lastRevisions, pendingRevisions map[string]uint64
var invokeUpdateCh chan bool // will be written to by a Go-routine spawned to call updated.
defer func() {
mu.Lock()
done = true
mu.Unlock()
close(invalidations)
}()
for {
select {
case <-ctx.Done():
return ctx.Err()
case newV, ok := <-invalidations:
if !ok {
return nil
}
if pending == nil {
if previous, ok := last[newV.key]; ok && previous.Digest.IsSet() && previous.Digest.Equals(newV.result.Digest) {
// Value didn't change.
continue
}
pending = map[string]ResultWithTimestamp[any]{}
pendingRevisions = map[string]uint64{}
// Initialize the next invalidation with the same base values.
for k, v := range last {
pending[k] = v
pendingRevisions[k] = lastRevisions[k]
}
}
if previous, ok := pending[newV.key]; !ok || newV.result.revision > previous.revision ||
(previous.Digest.IsSet() && !previous.Digest.Equals(newV.result.Digest)) {
pending[newV.key] = newV.result
}
case done := <-invokeUpdateCh:
invokeUpdateCh = nil
if done {
return ErrDoneSinking
}
}
if invokeUpdateCh != nil {
// Already handling a change, continue listening for updates.
continue
}
// We're complete if all keys we're waiting for are present.
if !hasAllKeys(pending, requiredKeys) {
continue
}
// We use a channel to signal back that the `Updated` invocation has finished.
invokeUpdateCh = make(chan bool)
r := Resolved{results: pending}
last = pending
lastRevisions = pendingRevisions
pending = nil
pendingRevisions = nil
g.eg.Go(func(ctx context.Context) error {
defer close(invokeUpdateCh)
if err := updated(ctx, r); err != nil {
if errors.Is(err, ErrDoneSinking) {
invokeUpdateCh <- true
return nil
}
return err
}
return nil
})
}
})
}
func hasAllKeys(m map[string]ResultWithTimestamp[any], keys []string) bool {
for _, key := range keys {
if _, ok := m[key]; !ok {
return false
}
}
return true
}
type rebuiltFunc func(string, ResultWithTimestamp[any]) bool
func (g *sinkInvocation) ensureObserver(c UntypedComputable, instance computeInstance, instanceKey string, obskey string, rebuilt rebuiltFunc) {
g.eg.Go(func(ctx context.Context) error {
inputs, err := c.Inputs().computeDigest(ctx, c, true)
if err != nil {
return err
}
globalKey := inputs.Digest.String()
if !inputs.Digest.IsSet() {
if instanceKey == "" {
panic(fmt.Sprintf("%s: global node that doesn't have stable inputs", reflect.TypeOf(c).String()))
}
globalKey = instanceKey
}
g.mu.Lock()
obs := g.globals[globalKey]
if obs == nil {
obs = &observable{inv: g, computable: instance}
if g.globals == nil {
g.globals = map[string]*observable{}
}
g.globals[globalKey] = obs
g.eg.Go(obs.Loop)
}
g.mu.Unlock()
// We never handle cancelations because the assumption is that the graph
// does not change for the duration of the Sink().
obs.mu.Lock()
latest := obs.latest
obs.observers = append(obs.observers, onResult{
ID: ids.NewRandomBase62ID(8),
Handle: func(rwt ResultWithTimestamp[any]) bool {
return rebuilt(obskey, rwt)
}},
)
obs.mu.Unlock()
// It's OK to do this without holding obs.mu.Lock as the parent will keep
// track of the latest revision it has observed, and drop updates with old
// versions.
if latest.revision > 0 {
rebuilt(obskey, latest)
}
return nil
})
}
func (g *sinkInvocation) newObserver(c UntypedComputable, key string, rebuilt rebuiltFunc) *observable {
obs := &observable{inv: g, computable: c.prepareCompute(c)}
obs.observers = append(obs.observers, onResult{
ID: ids.NewRandomBase62ID(8),
Handle: func(rwt ResultWithTimestamp[any]) bool {
return rebuilt(key, rwt)
},
})
return obs
}
type observable struct {
inv *sinkInvocation
computable computeInstance
mu sync.Mutex
observers []onResult
revision uint64
latest ResultWithTimestamp[any]
listenerCancel func() // The value is `Versioned`, and we're listening to new versions.
}
type observableUpdate struct {
key string
result ResultWithTimestamp[any]
}
func (o *observable) Loop(ctx context.Context) error {
deplessInputs, err := o.computable.Inputs().computeDigest(ctx, o.computable.Computable, true)
if err != nil {
return err
}
orch := On(ctx)
cacheable, shouldCache := o.computable.CacheInfo()
p := makePromise[any](o.computable.Computable, tasks.NewActionID().String())
hit := checkCache(ctx, orch, o.computable, cacheable, shouldCache, deplessInputs, p)
if hit.VerifiedHit {
o.newValue(ctx, p.resolved.value)
}
sinkInputs := Inputs()
depCount := len(deplessInputs.computable)
for key, c := range deplessInputs.computable {
sinkInputs = sinkInputs.Computable(key, c)
}
o.inv.sink(ctx, sinkInputs, func(ctx context.Context, resolved Resolved) error {
if o.computable.IsPrecomputed {
v, err := o.computable.Compute(ctx, Resolved{})
if err != nil {
return err
}
var r ResultWithTimestamp[any]
r.Value = v
o.newValue(ctx, r)
return nil
}
var inputs *computedInputs
err := o.computable.Action().RunWithOpts(ctx, tasks.RunOpts{
Wait: func(ctx context.Context) (bool, error) {
var err error
inputs, err = o.computable.Inputs().computeDigest(ctx, o.computable.Computable, true)
if err != nil {
return false, err
}
if err := inputs.Finalize(resolved.results); err != nil {
return false, err
}
p := makePromise[any](o.computable.Computable, tasks.NewActionID().String())
if hit, err := checkLoadCache(ctx, "cache.load.post", orch, o.computable, cacheable, inputs.PostComputeDigest, p); err == nil && hit.VerifiedHit {
o.newValue(ctx, p.resolved.value)
// Continue listening.
return true, nil
}
return false, nil
},
Run: func(ctx context.Context) error {
if res, err := compute(ctx, orch, p.actionID, o.computable, cacheable, shouldCache, inputs, resolved); err != nil {
if err = o.inv.transformErr(err); err != nil {
return err
}
} else {
o.newValue(ctx, res)
}
return nil
},
})
if err == nil && depCount == 0 {
return ErrDoneSinking // No more computables we depend on.
}
return err
})
return nil
}
func (o *observable) newValue(ctx context.Context, latest ResultWithTimestamp[any]) {
o.mu.Lock()
if versioned, ok := latest.Value.(Versioned); ok {
if o.listenerCancel != nil {
o.listenerCancel()
o.listenerCancel = nil
}
newListener, err := versioned.Observe(ctx, o.newVersion)
if err == nil {
// XXX report errors back
o.listenerCancel = newListener
} else {
fmt.Fprintln(console.Stderr(ctx), "failed to observe changes to value",
reflect.TypeOf(versioned).String(), latest.Digest.String(), err)
}
}
broadcast := o.doUpdate(latest)
o.mu.Unlock()
broadcast()
}
func (o *observable) newVersion(result ResultWithTimestamp[any], node ObserveNote) {
o.mu.Lock()
// XXX new versions are not cached.
broadcast := o.doUpdate(result)
if node == ObserveDone {
o.listenerCancel = nil
}
o.mu.Unlock()
broadcast()
}
func (o *observable) doUpdate(result ResultWithTimestamp[any]) func() {
o.revision++
result.revision = o.revision
o.latest = result
observers := make([]onResult, len(o.observers))
copy(observers, o.observers) // Make a copy so we can safely iterate when there are concurrent changes.
// This func should be called without holding `mu`.
return func() {
var handledObservers []onResult
for _, f := range observers {
if f.Handle(result) {
handledObservers = append(handledObservers, f)
}
}
// We update observers if any `Handle` func returned false.
if len(handledObservers) != len(observers) {
o.mu.Lock()
o.observers = handledObservers
if len(o.observers) == 0 && o.listenerCancel != nil {
// No more observers, cancel the listener.
o.listenerCancel()
o.listenerCancel = nil
}
o.mu.Unlock()
}
}
}
type onResult struct {
ID string
Handle func(ResultWithTimestamp[any]) bool
}