-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparallel.go
509 lines (456 loc) · 10.5 KB
/
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package routinghelpers
import (
"bytes"
"context"
"reflect"
"sync"
multierror "github.com/hashicorp/go-multierror"
cid "github.com/ipfs/go-cid"
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
record "github.com/libp2p/go-libp2p-record"
routing "github.com/libp2p/go-libp2p-routing"
ropts "github.com/libp2p/go-libp2p-routing/options"
)
// Parallel operates on the slice of routers in parallel.
type Parallel struct {
Routers []routing.IpfsRouting
Validator record.Validator
}
// Helper function that sees through router composition to avoid unnecessary
// go routines.
func supportsKey(vs routing.ValueStore, key string) bool {
switch vs := vs.(type) {
case Null:
return false
case *Compose:
return vs.ValueStore != nil && supportsKey(vs.ValueStore, key)
case Parallel:
for _, ri := range vs.Routers {
if supportsKey(ri, key) {
return true
}
}
return false
case Tiered:
for _, ri := range vs.Routers {
if supportsKey(ri, key) {
return true
}
}
return false
case *LimitedValueStore:
return vs.KeySupported(key) && supportsKey(vs.ValueStore, key)
default:
return true
}
}
func supportsPeer(vs routing.PeerRouting) bool {
switch vs := vs.(type) {
case Null:
return false
case *Compose:
return vs.PeerRouting != nil && supportsPeer(vs.PeerRouting)
case Parallel:
for _, ri := range vs.Routers {
if supportsPeer(ri) {
return true
}
}
return false
case Tiered:
for _, ri := range vs.Routers {
if supportsPeer(ri) {
return true
}
}
return false
default:
return true
}
}
func supportsContent(vs routing.ContentRouting) bool {
switch vs := vs.(type) {
case Null:
return false
case *Compose:
return vs.ContentRouting != nil && supportsContent(vs.ContentRouting)
case Parallel:
for _, ri := range vs.Routers {
if supportsContent(ri) {
return true
}
}
return false
case Tiered:
for _, ri := range vs.Routers {
if supportsContent(ri) {
return true
}
}
return false
default:
return true
}
}
func (r Parallel) filter(filter func(routing.IpfsRouting) bool) Parallel {
cpy := make([]routing.IpfsRouting, 0, len(r.Routers))
for _, ri := range r.Routers {
if filter(ri) {
cpy = append(cpy, ri)
}
}
return Parallel{Routers: cpy, Validator: r.Validator}
}
func (r Parallel) put(do func(routing.IpfsRouting) error) error {
switch len(r.Routers) {
case 0:
return routing.ErrNotSupported
case 1:
return do(r.Routers[0])
}
var wg sync.WaitGroup
results := make([]error, len(r.Routers))
wg.Add(len(r.Routers))
for i, ri := range r.Routers {
go func(ri routing.IpfsRouting, i int) {
results[i] = do(ri)
wg.Done()
}(ri, i)
}
wg.Wait()
var (
errs []error
success bool
)
for _, err := range results {
switch err {
case nil:
// at least one router supports this.
success = true
case routing.ErrNotSupported:
default:
errs = append(errs, err)
}
}
switch len(errs) {
case 0:
if success {
// No errors and at least one router succeeded.
return nil
}
// No routers supported this operation.
return routing.ErrNotSupported
case 1:
return errs[0]
default:
return &multierror.Error{Errors: errs}
}
}
func (r Parallel) search(ctx context.Context, do func(routing.IpfsRouting) (<-chan []byte, error)) (<-chan []byte, error) {
switch len(r.Routers) {
case 0:
return nil, routing.ErrNotFound
case 1:
return do(r.Routers[0])
}
ctx, cancel := context.WithCancel(ctx)
out := make(chan []byte)
var errs []error
var wg sync.WaitGroup
for _, ri := range r.Routers {
vchan, err := do(ri)
switch err {
case nil:
case routing.ErrNotFound, routing.ErrNotSupported:
continue
default:
errs = append(errs, err)
}
wg.Add(1)
go func() {
var sent int
defer wg.Done()
for {
select {
case v, ok := <-vchan:
if !ok {
if sent > 0 {
cancel()
}
return
}
select {
case out <- v:
sent++
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}()
}
go func() {
wg.Wait()
close(out)
cancel()
}()
return out, nil
}
func (r Parallel) get(ctx context.Context, do func(routing.IpfsRouting) (interface{}, error)) (interface{}, error) {
switch len(r.Routers) {
case 0:
return nil, routing.ErrNotFound
case 1:
return do(r.Routers[0])
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
results := make(chan struct {
val interface{}
err error
})
for _, ri := range r.Routers {
go func(ri routing.IpfsRouting) {
value, err := do(ri)
select {
case results <- struct {
val interface{}
err error
}{
val: value,
err: err,
}:
case <-ctx.Done():
}
}(ri)
}
var errs []error
for range r.Routers {
select {
case res := <-results:
switch res.err {
case nil:
return res.val, nil
case routing.ErrNotFound, routing.ErrNotSupported:
continue
}
// If the context has expired, just return that error
// and ignore the other errors.
if ctx.Err() != nil {
return nil, ctx.Err()
}
errs = append(errs, res.err)
case <-ctx.Done():
return nil, ctx.Err()
}
}
switch len(errs) {
case 0:
return nil, routing.ErrNotFound
case 1:
return nil, errs[0]
default:
return nil, &multierror.Error{Errors: errs}
}
}
func (r Parallel) forKey(key string) Parallel {
return r.filter(func(ri routing.IpfsRouting) bool {
return supportsKey(ri, key)
})
}
func (r Parallel) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
return r.forKey(key).put(func(ri routing.IpfsRouting) error {
return ri.PutValue(ctx, key, value, opts...)
})
}
func (r Parallel) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
vInt, err := r.forKey(key).get(ctx, func(ri routing.IpfsRouting) (interface{}, error) {
return ri.GetValue(ctx, key, opts...)
})
val, _ := vInt.([]byte)
return val, err
}
func (r Parallel) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
resCh, err := r.forKey(key).search(ctx, func(ri routing.IpfsRouting) (<-chan []byte, error) {
return ri.SearchValue(ctx, key, opts...)
})
if err != nil {
return nil, err
}
valid := make(chan []byte)
var best []byte
go func() {
defer close(valid)
for v := range resCh {
if best != nil {
n, err := r.Validator.Select(key, [][]byte{best, v})
if err != nil {
continue
}
if n != 1 {
continue
}
}
if bytes.Equal(best, v) && len(v) != 0 {
continue
}
best = v
select {
case valid <- v:
case <-ctx.Done():
return
}
}
}()
return valid, err
}
func (r Parallel) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error) {
vInt, err := r.
forKey(routing.KeyForPublicKey(p)).
get(ctx, func(ri routing.IpfsRouting) (interface{}, error) {
return routing.GetPublicKey(ri, ctx, p)
})
val, _ := vInt.(ci.PubKey)
return val, err
}
func (r Parallel) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
vInt, err := r.filter(func(ri routing.IpfsRouting) bool {
return supportsPeer(ri)
}).get(ctx, func(ri routing.IpfsRouting) (interface{}, error) {
return ri.FindPeer(ctx, p)
})
pi, _ := vInt.(pstore.PeerInfo)
return pi, err
}
func (r Parallel) Provide(ctx context.Context, c cid.Cid, local bool) error {
return r.filter(func(ri routing.IpfsRouting) bool {
return supportsContent(ri)
}).put(func(ri routing.IpfsRouting) error {
return ri.Provide(ctx, c, local)
})
}
func (r Parallel) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan pstore.PeerInfo {
routers := r.filter(func(ri routing.IpfsRouting) bool {
return supportsContent(ri)
})
switch len(routers.Routers) {
case 0:
ch := make(chan pstore.PeerInfo)
close(ch)
return ch
case 1:
return routers.Routers[0].FindProvidersAsync(ctx, c, count)
}
out := make(chan pstore.PeerInfo)
ctx, cancel := context.WithCancel(ctx)
providers := make([]<-chan pstore.PeerInfo, len(routers.Routers))
for i, ri := range routers.Routers {
providers[i] = ri.FindProvidersAsync(ctx, c, count)
}
go func() {
defer cancel()
defer close(out)
if len(providers) > 8 {
manyProviders(ctx, out, providers, count)
} else {
fewProviders(ctx, out, providers, count)
}
}()
return out
}
// Unoptimized many provider case. Doing this with reflection is a bit slow but
// definitely simpler. If we start having more than 8 peer routers running in
// parallel, we can revisit this.
func manyProviders(ctx context.Context, out chan<- pstore.PeerInfo, in []<-chan pstore.PeerInfo, count int) {
found := make(map[peer.ID]struct{}, count)
selectCases := make([]reflect.SelectCase, len(in))
for i, ch := range in {
selectCases[i] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ch),
}
}
for count > 0 && len(selectCases) > 0 {
chosen, val, ok := reflect.Select(selectCases)
if !ok {
// Remove the channel
selectCases[chosen] = selectCases[len(selectCases)-1]
selectCases = selectCases[:len(selectCases)-1]
continue
}
pi := val.Interface().(pstore.PeerInfo)
if _, ok := found[pi.ID]; ok {
continue
}
select {
case out <- pi:
found[pi.ID] = struct{}{}
count--
case <-ctx.Done():
return
}
}
}
// Optimization for few providers (<=8).
func fewProviders(ctx context.Context, out chan<- pstore.PeerInfo, in []<-chan pstore.PeerInfo, count int) {
if len(in) > 8 {
panic("case only valid for combining fewer than 8 channels")
}
found := make(map[peer.ID]struct{}, count)
cases := make([]<-chan pstore.PeerInfo, 8)
copy(cases, in)
// Oh go, what would we do without you!
nch := len(in)
var pi pstore.PeerInfo
for nch > 0 && count > 0 {
var ok bool
var selected int
select {
case pi, ok = <-cases[0]:
selected = 0
case pi, ok = <-cases[1]:
selected = 1
case pi, ok = <-cases[2]:
selected = 2
case pi, ok = <-cases[3]:
selected = 3
case pi, ok = <-cases[4]:
selected = 4
case pi, ok = <-cases[5]:
selected = 5
case pi, ok = <-cases[6]:
selected = 6
case pi, ok = <-cases[7]:
selected = 7
}
if !ok {
cases[selected] = nil
nch--
continue
}
if _, ok = found[pi.ID]; ok {
continue
}
select {
case out <- pi:
found[pi.ID] = struct{}{}
count--
case <-ctx.Done():
return
}
}
}
func (r Parallel) Bootstrap(ctx context.Context) error {
var me multierror.Error
for _, b := range r.Routers {
if err := b.Bootstrap(ctx); err != nil {
me.Errors = append(me.Errors, err)
}
}
return me.ErrorOrNil()
}
var _ routing.IpfsRouting = Parallel{}