generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 98
/
reprovider.go
556 lines (464 loc) · 14.7 KB
/
reprovider.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package provider
import (
"context"
"errors"
"fmt"
"math"
"strconv"
"sync"
"time"
"github.com/ipfs/boxo/provider/internal/queue"
"github.com/ipfs/boxo/verifcid"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
logging "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multihash"
)
const (
// MAGIC: how long we wait before reproviding a key
DefaultReproviderInterval = time.Hour * 22 // https://github.com/ipfs/kubo/pull/9326
// MAGIC: If the reprovide ticker is larger than a minute (likely), provide
// once after we've been up a minute. Don't provide _immediately_ as we
// might be just about to stop.
defaultInitialReprovideDelay = time.Minute
// MAGIC: how long we wait between the first provider we hear about and
// batching up the provides to send out
pauseDetectionThreshold = time.Millisecond * 500
// MAGIC: how long we are willing to collect providers for the batch after
// we receive the first one
maxCollectionDuration = time.Minute * 10
)
var log = logging.Logger("provider.batched")
type reprovider struct {
ctx context.Context
close context.CancelFunc
closewg sync.WaitGroup
reprovideInterval time.Duration
initalReprovideDelay time.Duration
initialReprovideDelaySet bool
allowlist verifcid.Allowlist
rsys Provide
keyProvider KeyChanFunc
q *queue.Queue
ds datastore.Batching
reprovideCh chan cid.Cid
noReprovideInFlight chan struct{}
maxReprovideBatchSize uint
statLk sync.Mutex
totalProvides, lastReprovideBatchSize uint64
avgProvideDuration, lastReprovideDuration time.Duration
throughputCallback ThroughputCallback
// throughputProvideCurrentCount counts how many provides has been done since the last call to throughputCallback
throughputProvideCurrentCount uint
// throughputDurationSum sums up durations between two calls to the throughputCallback
throughputDurationSum time.Duration
throughputMinimumProvides uint
keyPrefix datastore.Key
}
var _ System = (*reprovider)(nil)
type Provide interface {
Provide(context.Context, cid.Cid, bool) error
}
type ProvideMany interface {
ProvideMany(ctx context.Context, keys []multihash.Multihash) error
}
type Ready interface {
Ready() bool
}
// Option defines the functional option type that can be used to configure
// BatchProvidingSystem instances
type Option func(system *reprovider) error
var (
lastReprovideKey = datastore.NewKey("/reprovide/lastreprovide")
DefaultKeyPrefix = datastore.NewKey("/provider")
)
// New creates a new [System]. By default it is offline, that means it will
// enqueue tasks in ds.
// To have it publish records in the network use the [Online] option.
// If provider casts to [ProvideMany] the [ProvideMany.ProvideMany] method will
// be called instead.
//
// If provider casts to [Ready], it will wait until [Ready.Ready] is true.
func New(ds datastore.Batching, opts ...Option) (System, error) {
s := &reprovider{
allowlist: verifcid.DefaultAllowlist,
reprovideInterval: DefaultReproviderInterval,
maxReprovideBatchSize: math.MaxUint,
keyPrefix: DefaultKeyPrefix,
reprovideCh: make(chan cid.Cid),
noReprovideInFlight: make(chan struct{}),
}
for _, o := range opts {
if err := o(s); err != nil {
return nil, err
}
}
// Setup default behavior for the initial reprovide delay
if !s.initialReprovideDelaySet && s.reprovideInterval > defaultInitialReprovideDelay {
s.initalReprovideDelay = defaultInitialReprovideDelay
s.initialReprovideDelaySet = true
}
if s.keyProvider == nil {
s.keyProvider = func(ctx context.Context) (<-chan cid.Cid, error) {
ch := make(chan cid.Cid)
close(ch)
return ch, nil
}
}
s.ds = namespace.Wrap(ds, s.keyPrefix)
s.q = queue.NewQueue(s.ds)
// This is after the options processing so we do not have to worry about leaking a context if there is an
// initialization error processing the options
ctx, cancel := context.WithCancel(context.Background())
s.ctx = ctx
s.close = cancel
if s.rsys != nil {
if _, ok := s.rsys.(ProvideMany); !ok {
s.maxReprovideBatchSize = 1
}
s.run()
}
return s, nil
}
func Allowlist(allowlist verifcid.Allowlist) Option {
return func(system *reprovider) error {
system.allowlist = allowlist
return nil
}
}
func ReproviderInterval(duration time.Duration) Option {
return func(system *reprovider) error {
system.reprovideInterval = duration
return nil
}
}
func KeyProvider(fn KeyChanFunc) Option {
return func(system *reprovider) error {
system.keyProvider = fn
return nil
}
}
// DatastorePrefix sets a prefix for internal state stored in the Datastore.
// Defaults to [DefaultKeyPrefix].
func DatastorePrefix(k datastore.Key) Option {
return func(system *reprovider) error {
system.keyPrefix = k
return nil
}
}
// ThroughputReport will fire the callback synchronously once at least limit
// multihashes have been advertised, it will then wait until a new set of at least
// limit multihashes has been advertised.
// While ThroughputReport is set batches will be at most minimumProvides big.
// If it returns false it wont ever be called again.
func ThroughputReport(f ThroughputCallback, minimumProvides uint) Option {
return func(system *reprovider) error {
system.throughputCallback = f
system.throughputMinimumProvides = minimumProvides
return nil
}
}
type ThroughputCallback = func(reprovide bool, complete bool, totalKeysProvided uint, totalDuration time.Duration) (continueWatching bool)
// Online will enable the router and make it send publishes online.
// nil can be used to turn the router offline.
// You can't register multiple providers, if this option is passed multiple times
// it will error.
func Online(rsys Provide) Option {
return func(system *reprovider) error {
if system.rsys != nil {
return errors.New("trying to register two provider on the same reprovider")
}
system.rsys = rsys
return nil
}
}
func initialReprovideDelay(duration time.Duration) Option {
return func(system *reprovider) error {
system.initialReprovideDelaySet = true
system.initalReprovideDelay = duration
return nil
}
}
func (s *reprovider) run() {
provCh := s.q.Dequeue()
s.closewg.Add(1)
go func() {
defer s.closewg.Done()
m := make(map[cid.Cid]struct{})
// setup stopped timers
maxCollectionDurationTimer := time.NewTimer(time.Hour)
pauseDetectTimer := time.NewTimer(time.Hour)
stopAndEmptyTimer(maxCollectionDurationTimer)
stopAndEmptyTimer(pauseDetectTimer)
// make sure timers are cleaned up
defer maxCollectionDurationTimer.Stop()
defer pauseDetectTimer.Stop()
resetTimersAfterReceivingProvide := func() {
firstProvide := len(m) == 0
if firstProvide {
// after receiving the first provider start up the timers
maxCollectionDurationTimer.Reset(maxCollectionDuration)
pauseDetectTimer.Reset(pauseDetectionThreshold)
} else {
// otherwise just do a full restart of the pause timer
stopAndEmptyTimer(pauseDetectTimer)
pauseDetectTimer.Reset(pauseDetectionThreshold)
}
}
for {
performedReprovide := false
complete := false
batchSize := s.maxReprovideBatchSize
if s.throughputCallback != nil && s.throughputMinimumProvides < batchSize {
batchSize = s.throughputMinimumProvides
}
// at the start of every loop the maxCollectionDurationTimer and pauseDetectTimer should be already be
// stopped and have empty channels
for uint(len(m)) < batchSize {
var noReprovideInFlight chan struct{}
if len(m) == 0 {
noReprovideInFlight = s.noReprovideInFlight
}
select {
case c := <-provCh:
resetTimersAfterReceivingProvide()
m[c] = struct{}{}
case c := <-s.reprovideCh:
resetTimersAfterReceivingProvide()
m[c] = struct{}{}
performedReprovide = true
case <-pauseDetectTimer.C:
// if this timer has fired then the max collection timer has started so let's stop and empty it
stopAndEmptyTimer(maxCollectionDurationTimer)
complete = true
goto AfterLoop
case <-maxCollectionDurationTimer.C:
// if this timer has fired then the pause timer has started so let's stop and empty it
stopAndEmptyTimer(pauseDetectTimer)
goto AfterLoop
case <-s.ctx.Done():
return
case noReprovideInFlight <- struct{}{}:
// if no reprovide is in flight get consumer asking for reprovides unstuck
}
}
stopAndEmptyTimer(pauseDetectTimer)
stopAndEmptyTimer(maxCollectionDurationTimer)
AfterLoop:
if len(m) == 0 {
continue
}
keys := make([]multihash.Multihash, 0, len(m))
for c := range m {
delete(m, c)
// hash security
if err := verifcid.ValidateCid(s.allowlist, c); err != nil {
log.Errorf("insecure hash in reprovider, %s (%s)", c, err)
continue
}
keys = append(keys, c.Hash())
}
// in case after removing all the invalid CIDs there are no valid ones left
if len(keys) == 0 {
continue
}
if r, ok := s.rsys.(Ready); ok {
ticker := time.NewTicker(time.Minute)
for !r.Ready() {
log.Debugf("reprovider system not ready")
select {
case <-ticker.C:
case <-s.ctx.Done():
return
}
}
ticker.Stop()
}
log.Debugf("starting provide of %d keys", len(keys))
start := time.Now()
err := doProvideMany(s.ctx, s.rsys, keys)
if err != nil {
log.Debugf("providing failed %v", err)
continue
}
dur := time.Since(start)
totalProvideTime := time.Duration(s.totalProvides) * s.avgProvideDuration
recentAvgProvideDuration := dur / time.Duration(len(keys))
s.statLk.Lock()
s.avgProvideDuration = time.Duration((totalProvideTime + dur) / (time.Duration(s.totalProvides) + time.Duration(len(keys))))
s.totalProvides += uint64(len(keys))
log.Debugf("finished providing of %d keys. It took %v with an average of %v per provide", len(keys), dur, recentAvgProvideDuration)
if performedReprovide {
s.lastReprovideBatchSize = uint64(len(keys))
s.lastReprovideDuration = dur
s.statLk.Unlock()
// Don't hold the lock while writing to disk, consumers don't need to wait on IO to read thoses fields.
if err := s.ds.Put(s.ctx, lastReprovideKey, storeTime(time.Now())); err != nil {
log.Errorf("could not store last reprovide time: %v", err)
}
if err := s.ds.Sync(s.ctx, lastReprovideKey); err != nil {
log.Errorf("could not perform sync of last reprovide time: %v", err)
}
} else {
s.statLk.Unlock()
}
s.throughputDurationSum += dur
s.throughputProvideCurrentCount += uint(len(keys))
if s.throughputCallback != nil && s.throughputProvideCurrentCount >= s.throughputMinimumProvides {
if more := s.throughputCallback(performedReprovide, complete, s.throughputProvideCurrentCount, s.throughputDurationSum); !more {
s.throughputCallback = nil
}
s.throughputProvideCurrentCount = 0
s.throughputDurationSum = 0
}
}
}()
s.closewg.Add(1)
go func() {
defer s.closewg.Done()
var initialReprovideCh, reprovideCh <-chan time.Time
// If reproviding is enabled (non-zero)
if s.reprovideInterval > 0 {
reprovideTicker := time.NewTicker(s.reprovideInterval)
defer reprovideTicker.Stop()
reprovideCh = reprovideTicker.C
// if there is a non-zero initial reprovide time that was set in the initializer or if the fallback has been
if s.initialReprovideDelaySet {
initialReprovideTimer := time.NewTimer(s.initalReprovideDelay)
defer initialReprovideTimer.Stop()
initialReprovideCh = initialReprovideTimer.C
}
}
for s.ctx.Err() == nil {
select {
case <-initialReprovideCh:
case <-reprovideCh:
case <-s.ctx.Done():
return
}
err := s.reprovide(s.ctx, false)
// only log if we've hit an actual error, otherwise just tell the client we're shutting down
if s.ctx.Err() == nil && err != nil {
log.Errorf("failed to reprovide: %s", err)
}
}
}()
}
func stopAndEmptyTimer(t *time.Timer) {
if !t.Stop() {
<-t.C
}
}
func storeTime(t time.Time) []byte {
val := []byte(strconv.FormatInt(t.UnixNano(), 10))
return val
}
func parseTime(b []byte) (time.Time, error) {
tns, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, tns), nil
}
func (s *reprovider) Close() error {
s.close()
err := s.q.Close()
s.closewg.Wait()
return err
}
func (s *reprovider) Provide(cid cid.Cid) error {
return s.q.Enqueue(cid)
}
func (s *reprovider) Reprovide(ctx context.Context) error {
return s.reprovide(ctx, true)
}
func (s *reprovider) reprovide(ctx context.Context, force bool) error {
if !s.shouldReprovide() && !force {
return nil
}
kch, err := s.keyProvider(ctx)
if err != nil {
return err
}
reprovideCidLoop:
for {
select {
case c, ok := <-kch:
if !ok {
break reprovideCidLoop
}
select {
case s.reprovideCh <- c:
case <-ctx.Done():
return ctx.Err()
case <-s.ctx.Done():
return errors.New("failed to reprovide: shutting down")
}
case <-ctx.Done():
return ctx.Err()
case <-s.ctx.Done():
return errors.New("failed to reprovide: shutting down")
}
}
// Wait until the underlying operation has completed
select {
case <-s.noReprovideInFlight:
return nil
case <-ctx.Done():
return ctx.Err()
case <-s.ctx.Done():
return errors.New("failed to reprovide: shutting down")
}
}
func (s *reprovider) getLastReprovideTime() (time.Time, error) {
val, err := s.ds.Get(s.ctx, lastReprovideKey)
if errors.Is(err, datastore.ErrNotFound) {
return time.Time{}, nil
}
if err != nil {
return time.Time{}, errors.New("could not get last reprovide time")
}
t, err := parseTime(val)
if err != nil {
return time.Time{}, fmt.Errorf("could not decode last reprovide time, got %q", string(val))
}
return t, nil
}
func (s *reprovider) shouldReprovide() bool {
t, err := s.getLastReprovideTime()
if err != nil {
log.Debugf("getting last reprovide time failed: %s", err)
return false
}
if time.Since(t) < s.reprovideInterval {
return false
}
return true
}
type ReproviderStats struct {
TotalProvides, LastReprovideBatchSize uint64
AvgProvideDuration, LastReprovideDuration time.Duration
}
// Stat returns various stats about this provider system
func (s *reprovider) Stat() (ReproviderStats, error) {
s.statLk.Lock()
defer s.statLk.Unlock()
return ReproviderStats{
TotalProvides: s.totalProvides,
LastReprovideBatchSize: s.lastReprovideBatchSize,
AvgProvideDuration: s.avgProvideDuration,
LastReprovideDuration: s.lastReprovideDuration,
}, nil
}
func doProvideMany(ctx context.Context, r Provide, keys []multihash.Multihash) error {
if many, ok := r.(ProvideMany); ok {
return many.ProvideMany(ctx, keys)
}
for _, k := range keys {
if err := r.Provide(ctx, cid.NewCidV1(cid.Raw, k), true); err != nil {
return err
}
}
return nil
}