-
Notifications
You must be signed in to change notification settings - Fork 15
/
diff_parallel.go
458 lines (396 loc) · 9.96 KB
/
diff_parallel.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
package amt
import (
"bytes"
"context"
"fmt"
"sync"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)
func ParallelDiff(ctx context.Context, prevBs, curBs cbor.IpldStore, prev, cur cid.Cid, workers int64, opts ...Option) ([]*Change, error) {
prevAmt, err := LoadAMT(ctx, prevBs, prev, opts...)
if err != nil {
return nil, xerrors.Errorf("loading previous root: %w", err)
}
prevCtx := &nodeContext{
bs: prevBs,
bitWidth: prevAmt.bitWidth,
height: prevAmt.height,
}
curAmt, err := LoadAMT(ctx, curBs, cur, opts...)
if err != nil {
return nil, xerrors.Errorf("loading current root: %w", err)
}
// TODO: remove when https://github.com/filecoin-project/go-amt-ipld/issues/54 is closed.
if curAmt.bitWidth != prevAmt.bitWidth {
return nil, xerrors.Errorf("diffing AMTs with differing bitWidths not supported (prev=%d, cur=%d)", prevAmt.bitWidth, curAmt.bitWidth)
}
curCtx := &nodeContext{
bs: curBs,
bitWidth: curAmt.bitWidth,
height: curAmt.height,
}
// edge case of diffing an empty AMT against non-empty
if prevAmt.count == 0 && curAmt.count != 0 {
return addAll(ctx, curCtx, curAmt.node, 0)
}
if prevAmt.count != 0 && curAmt.count == 0 {
return removeAll(ctx, prevCtx, prevAmt.node, 0)
}
out := make(chan *Change)
differ, ctx := newDiffScheduler(ctx, workers, &task{
prevCtx: prevCtx,
curCtx: curCtx,
prev: prevAmt.node,
cur: curAmt.node,
offset: 0,
})
differ.startScheduler(ctx)
differ.startWorkers(ctx, out)
var changes []*Change
done := make(chan struct{})
go func() {
for change := range out {
changes = append(changes, change)
}
close(done)
}()
err = differ.grp.Wait()
close(out)
<-done
return changes, err
}
func parallelAddAll(ctx context.Context, nc *nodeContext, node *node, offset uint64, out chan *Change) error {
return node.forEachAt(ctx, nc.bs, nc.bitWidth, nc.height, 0, offset, func(index uint64, deferred *cbg.Deferred) error {
out <- &Change{
Type: Add,
Key: index,
Before: nil,
After: deferred,
}
return nil
})
}
func parallelRemoveAll(ctx context.Context, nc *nodeContext, node *node, offset uint64, out chan *Change) error {
return node.forEachAt(ctx, nc.bs, nc.bitWidth, nc.height, 0, offset, func(index uint64, deferred *cbg.Deferred) error {
out <- &Change{
Type: Remove,
Key: index,
Before: deferred,
After: nil,
}
return nil
})
}
func parallelDiffLeaves(prev, cur *node, offset uint64, out chan *Change) error {
if len(prev.values) != len(cur.values) {
return fmt.Errorf("node leaves have different numbers of values (prev=%d, cur=%d)", len(prev.values), len(cur.values))
}
for i, prevVal := range prev.values {
index := offset + uint64(i)
curVal := cur.values[i]
if prevVal == nil && curVal == nil {
continue
}
if prevVal == nil && curVal != nil {
out <- &Change{
Type: Add,
Key: index,
Before: nil,
After: curVal,
}
continue
}
if prevVal != nil && curVal == nil {
out <- &Change{
Type: Remove,
Key: index,
Before: prevVal,
After: nil,
}
continue
}
if !bytes.Equal(prevVal.Raw, curVal.Raw) {
out <- &Change{
Type: Modify,
Key: index,
Before: prevVal,
After: curVal,
}
}
}
return nil
}
type task struct {
prevCtx, curCtx *nodeContext
prev, cur *node
offset uint64
}
func newDiffScheduler(ctx context.Context, numWorkers int64, rootTasks ...*task) (*diffScheduler, context.Context) {
grp, ctx := errgroup.WithContext(ctx)
s := &diffScheduler{
numWorkers: numWorkers,
stack: rootTasks,
in: make(chan *task, numWorkers),
out: make(chan *task, numWorkers),
grp: grp,
}
s.taskWg.Add(len(rootTasks))
return s, ctx
}
type diffScheduler struct {
// number of worker routine to spawn
numWorkers int64
// buffer holds tasks until they are processed
stack []*task
// inbound and outbound tasks
in, out chan *task
// tracks number of inflight tasks
taskWg sync.WaitGroup
// launches workers and collects errors if any occur
grp *errgroup.Group
}
func (s *diffScheduler) enqueueTask(task *task) {
s.taskWg.Add(1)
s.in <- task
}
func (s *diffScheduler) startScheduler(ctx context.Context) {
s.grp.Go(func() error {
defer func() {
close(s.out)
// Because the workers may have exited early (due to the context being canceled).
for range s.out {
s.taskWg.Done()
}
// Because the workers may have enqueued additional tasks.
for range s.in {
s.taskWg.Done()
}
// now, the waitgroup should be at 0, and the goroutine that was _waiting_ on it should have exited.
}()
go func() {
s.taskWg.Wait()
close(s.in)
}()
for {
if n := len(s.stack) - 1; n >= 0 {
select {
case <-ctx.Done():
return ctx.Err()
case newJob, ok := <-s.in:
if !ok {
return nil
}
s.stack = append(s.stack, newJob)
case s.out <- s.stack[n]:
s.stack[n] = nil
s.stack = s.stack[:n]
}
} else {
select {
case <-ctx.Done():
return ctx.Err()
case newJob, ok := <-s.in:
if !ok {
return nil
}
s.stack = append(s.stack, newJob)
}
}
}
})
}
func (s *diffScheduler) startWorkers(ctx context.Context, out chan *Change) {
for i := int64(0); i < s.numWorkers; i++ {
s.grp.Go(func() error {
for task := range s.out {
if err := s.work(ctx, task, out); err != nil {
return err
}
}
return nil
})
}
}
func (s *diffScheduler) work(ctx context.Context, todo *task, results chan *Change) error {
defer s.taskWg.Done()
prev := todo.prev
prevCtx := todo.prevCtx
cur := todo.cur
curCtx := todo.curCtx
offset := todo.offset
if prev == nil && cur == nil {
return nil
}
if prev == nil {
return parallelAddAll(ctx, curCtx, cur, offset, results)
}
if cur == nil {
return parallelRemoveAll(ctx, prevCtx, prev, offset, results)
}
if prevCtx.height == 0 && curCtx.height == 0 {
return parallelDiffLeaves(prev, cur, offset, results)
}
if curCtx.height > prevCtx.height {
subCount := curCtx.nodesAtHeight()
for i, ln := range cur.links {
if ln == nil || ln.cid == cid.Undef {
continue
}
subCtx := &nodeContext{
bs: curCtx.bs,
bitWidth: curCtx.bitWidth,
height: curCtx.height - 1,
}
subn, err := ln.load(ctx, subCtx.bs, subCtx.bitWidth, subCtx.height)
if err != nil {
return err
}
offs := offset + (uint64(i) * subCount)
if i == 0 {
s.enqueueTask(&task{
prevCtx: prevCtx,
curCtx: subCtx,
prev: prev,
cur: subn,
offset: offs,
})
} else {
err := parallelAddAll(ctx, subCtx, subn, offs, results)
if err != nil {
return err
}
}
}
return nil
}
if prevCtx.height > curCtx.height {
subCount := prevCtx.nodesAtHeight()
for i, ln := range prev.links {
if ln == nil || ln.cid == cid.Undef {
continue
}
subCtx := &nodeContext{
bs: prevCtx.bs,
bitWidth: prevCtx.bitWidth,
height: prevCtx.height - 1,
}
subn, err := ln.load(ctx, subCtx.bs, subCtx.bitWidth, subCtx.height)
if err != nil {
return err
}
offs := offset + (uint64(i) * subCount)
if i == 0 {
s.enqueueTask(&task{
prevCtx: subCtx,
curCtx: curCtx,
prev: subn,
cur: cur,
offset: offs,
})
} else {
err := parallelRemoveAll(ctx, subCtx, subn, offs, results)
if err != nil {
return err
}
}
}
return nil
}
// sanity check
if prevCtx.height != curCtx.height {
return fmt.Errorf("comparing non-leaf nodes of unequal heights (%d, %d)", prevCtx.height, curCtx.height)
}
if len(prev.links) != len(cur.links) {
return fmt.Errorf("nodes have different numbers of links (prev=%d, cur=%d)", len(prev.links), len(cur.links))
}
if prev.links == nil || cur.links == nil {
return fmt.Errorf("nodes have no links")
}
subCount := prevCtx.nodesAtHeight()
for i := range prev.links {
// Neither previous or current links are in use
if prev.links[i] == nil && cur.links[i] == nil {
continue
}
// Previous had link, current did not
if prev.links[i] != nil && cur.links[i] == nil {
if prev.links[i].cid == cid.Undef {
continue
}
subCtx := &nodeContext{
bs: prevCtx.bs,
bitWidth: prevCtx.bitWidth,
height: prevCtx.height - 1,
}
subn, err := prev.links[i].load(ctx, subCtx.bs, subCtx.bitWidth, subCtx.height)
if err != nil {
return err
}
offs := offset + (uint64(i) * subCount)
err = parallelRemoveAll(ctx, subCtx, subn, offs, results)
if err != nil {
return err
}
continue
}
// Current has link, previous did not
if prev.links[i] == nil && cur.links[i] != nil {
if cur.links[i].cid == cid.Undef {
continue
}
subCtx := &nodeContext{
bs: curCtx.bs,
bitWidth: curCtx.bitWidth,
height: curCtx.height - 1,
}
subn, err := cur.links[i].load(ctx, subCtx.bs, subCtx.bitWidth, subCtx.height)
if err != nil {
return err
}
offs := offset + (uint64(i) * subCount)
err = parallelAddAll(ctx, subCtx, subn, offs, results)
if err != nil {
return err
}
continue
}
// Both previous and current have links to diff
if prev.links[i].cid == cur.links[i].cid {
continue
}
prevSubCtx := &nodeContext{
bs: prevCtx.bs,
bitWidth: prevCtx.bitWidth,
height: prevCtx.height - 1,
}
prevSubn, err := prev.links[i].load(ctx, prevSubCtx.bs, prevSubCtx.bitWidth, prevSubCtx.height)
if err != nil {
return err
}
curSubCtx := &nodeContext{
bs: curCtx.bs,
bitWidth: curCtx.bitWidth,
height: curCtx.height - 1,
}
curSubn, err := cur.links[i].load(ctx, curSubCtx.bs, curSubCtx.bitWidth, curSubCtx.height)
if err != nil {
return err
}
offs := offset + (uint64(i) * subCount)
s.enqueueTask(&task{
prevCtx: prevSubCtx,
curCtx: curSubCtx,
prev: prevSubn,
cur: curSubn,
offset: offs,
})
if err != nil {
return err
}
}
return nil
}