-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinder.go
More file actions
293 lines (243 loc) · 7.11 KB
/
binder.go
File metadata and controls
293 lines (243 loc) · 7.11 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
package taskgraph
import (
"errors"
"fmt"
"sync"
set "github.com/deckarep/golang-set/v2"
)
// ErrDuplicateBinding is returned when Binder.Store is called with a binding whose ID has already
// been stored (which implies that the graph being executed contains multiple tasks producing
// bindings for the same Key).
var ErrDuplicateBinding = errors.New("duplicate binding")
// BindStatus represents the tristate of a Binding.
type BindStatus int
const (
// Pending represents where the key is unbound (i.e. that no task has yet provided a binding for
// the key, and no input binding was provided).
Pending BindStatus = iota
// Absent represents where the key is explicitly unbound (i.e. that the task which provides it was
// unable to provide a value). The binding will contain an error, which is ErrIsAbsent by default
// but can be another error to propagate information between tasks. This allows for errors which
// do not terminate the execution of a graph.
Absent
// Present represents where the key is bound to a valid value (i.e. the task was able to provide a
// value, or the value was bound as an input).
Present
)
func (bs BindStatus) String() string {
return map[BindStatus]string{
Pending: "PENDING",
Absent: "ABSENT",
Present: "PRESENT",
}[bs]
}
// A Binding is a tristate wrapper around a key ID and an optional value or error. See the
// documentation for BindStatus for details of the 3 states. Bindings are produced by calling the
// Bind, BindAbsent, or BindError methods on a Key.
type Binding interface {
// ID returns the ID of the key which is bound by this Binding.
ID() ID
// Status returns the status of this binding.
Status() BindStatus
// Value returns the value bound to the key. This should only be called if Status() returns Present.
Value() any
// Value returns the error bound to the key. This should only be called if Status() returns Absent.
Error() error
}
type binding struct {
id ID
status BindStatus
value any
err error
}
func (b *binding) ID() ID {
return b.id
}
func (b *binding) Status() BindStatus {
return b.status
}
func (b *binding) Value() any {
return b.value
}
func (b *binding) Error() error {
return b.err
}
func (b *binding) String() string {
if b.status == Present {
return fmt.Sprintf("%s(%s -> %v)", b.status, b.id, b.value)
}
return fmt.Sprintf("%s(%s)", b.status, b.id)
}
// bind a value to a key ID.
func bind(id ID, value any) Binding {
return &binding{
id: id,
status: Present,
value: value,
}
}
// bindAbsent produces an absent binding for the given Key ID.
func bindAbsent(id ID) Binding {
return bindAbsentWithError(id, ErrIsAbsent)
}
// bindAbsent produces an absent binding for the given Key ID.
func bindAbsentWithError(id ID, err error) Binding {
return &binding{
id: id,
status: Absent,
err: err,
}
}
// bindPending produces a pending binding; this is only ever used when calling Get on a Binder.
func bindPending(id ID) Binding {
return &binding{
id: id,
status: Pending,
}
}
// A Binder is the state store for tasks in a graph.
type Binder interface {
// Store adds bindings to the binder that can be retrieved with Get().
Store(...Binding) error
// Returns whether the given IDs have all been bound (as Present or Absent).
Has(...ID) bool
// Get a previously stored binding. If no binding with the given ID has yet been stored, a binding with Status() = Pending is generated.
Get(ID) Binding
// GetAll returns all stored bindings. This is typically used only for tests.
GetAll() []Binding
}
type binder struct {
// Protects against concurrent access to the map
sync.RWMutex
bindings map[ID]Binding
}
func (b *binder) Store(bs ...Binding) error {
b.Lock()
defer b.Unlock()
for _, binding := range bs {
if _, ok := b.bindings[binding.ID()]; ok {
return wrapStackErrorf("%w: %q", ErrDuplicateBinding, binding.ID())
}
b.bindings[binding.ID()] = binding
}
return nil
}
func (b *binder) Has(ids ...ID) bool {
b.RLock()
defer b.RUnlock()
for _, id := range ids {
if _, ok := b.bindings[id]; !ok {
return false
}
}
return true
}
func (b *binder) Get(id ID) Binding {
b.RLock()
defer b.RUnlock()
if binding, ok := b.bindings[id]; ok {
return binding
}
return bindPending(id)
}
func (b *binder) GetAll() []Binding {
b.RLock()
defer b.RUnlock()
res := make([]Binding, 0, len(b.bindings))
for _, binding := range b.bindings {
res = append(res, binding)
}
return res
}
// NewBinder returns a new binder.
func NewBinder() Binder {
return &binder{
bindings: map[ID]Binding{},
}
}
// overlayBinder implements Binder to provide an overlay over an existing binder, such that newly
// stored keys are added to the overlay only, but bindings can still be read from the base. This
// still does not allow duplicate bindings (attempting to Store() a binding already present in the
// base will return an error)
type overlayBinder struct {
base, overlay Binder
}
func (ob *overlayBinder) Store(bindings ...Binding) error {
for _, b := range bindings {
if ob.base.Has(b.ID()) {
return wrapStackErrorf("%w: %q", ErrDuplicateBinding, b.ID())
}
}
return ob.overlay.Store(bindings...)
}
func (ob *overlayBinder) Has(ids ...ID) bool {
for _, id := range ids {
if !ob.overlay.Has(id) && !ob.base.Has(id) {
return false
}
}
return true
}
func (ob *overlayBinder) Get(id ID) Binding {
if b := ob.overlay.Get(id); b.Status() != Pending {
return b
}
return ob.base.Get(id)
}
func (ob *overlayBinder) GetAll() []Binding {
return append(ob.base.GetAll(), ob.overlay.GetAll()...)
}
// NewOverlayBinder creates a new overlay binder.
func NewOverlayBinder(base, overlay Binder) Binder {
return &overlayBinder{
base: base,
overlay: overlay,
}
}
// graphTaskBinder implements Binder to run a Graph as a task. Any bindings for keys that should be
// exposed are immediately added to the Binder of the parent graph, so that dependent tasks outside
// this graph do not have to wait for every task in this graph to complete.
type graphTaskBinder struct {
internal, external Binder
exposeKeys set.Set[ID]
}
func (gtb *graphTaskBinder) Store(bindings ...Binding) error {
for _, binding := range bindings {
if gtb.exposeKeys.Contains(binding.ID()) {
if err := gtb.external.Store(binding); err != nil {
return err
}
} else {
if err := gtb.internal.Store(binding); err != nil {
return err
}
}
}
return nil
}
func (gtb *graphTaskBinder) Has(ids ...ID) bool {
for _, id := range ids {
if !gtb.internal.Has(id) && !gtb.external.Has(id) {
return false
}
}
return true
}
func (gtb *graphTaskBinder) Get(id ID) Binding {
if ib := gtb.internal.Get(id); ib.Status() != Pending {
return ib
}
return gtb.external.Get(id)
}
func (gtb *graphTaskBinder) GetAll() []Binding {
return append(gtb.internal.GetAll(), gtb.external.GetAll()...)
}
// TestOnlyNewGraphTaskBinder creates a new graph task binder. This is exported for testing, and
// should not be called in production code.
func TestOnlyNewGraphTaskBinder(internal, external Binder, exposeKeys set.Set[ID]) Binder {
return &graphTaskBinder{
internal: internal,
external: external,
exposeKeys: exposeKeys,
}
}