-
Notifications
You must be signed in to change notification settings - Fork 179
/
cleaner.go
87 lines (77 loc) · 2.34 KB
/
cleaner.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
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package badger
import (
"math/rand"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/module"
)
type Cleaner struct {
log zerolog.Logger
db *badger.DB
metrics module.CleanerMetrics
enabled bool
ratio float64
freq int
calls int
}
// NewCleaner returns a cleaner that runs the badger value log garbage collection once every `frequency` calls
// if a frequency of zero is passed in, we will not run the GC at all
func NewCleaner(log zerolog.Logger, db *badger.DB, metrics module.CleanerMetrics, frequency int) *Cleaner {
// NOTE: we run garbage collection frequently at points in our business
// logic where we are likely to have a small breather in activity; it thus
// makes sense to run garbage collection often, with a smaller ratio, rather
// than running it rarely and having big rewrites at once
c := &Cleaner{
log: log.With().Str("component", "cleaner").Logger(),
db: db,
metrics: metrics,
ratio: 0.2,
freq: frequency,
enabled: frequency > 0, // Disable if passed in 0 as frequency
}
// we don't want the entire network to run GC at the same time, so
// distribute evenly over time
if c.enabled {
c.calls = rand.Intn(c.freq)
}
return c
}
func (c *Cleaner) RunGC() {
if !c.enabled {
return
}
// only actually run approximately every frequency number of calls
c.calls++
if c.calls < c.freq {
return
}
// we add 20% jitter into the interval, so that we don't risk nodes syncing
// up on their GC calls over time
c.calls = rand.Intn(c.freq / 5)
// run the garbage collection in own goroutine and handle sentinel errors
go func() {
started := time.Now()
err := c.db.RunValueLogGC(c.ratio)
if err == badger.ErrRejected {
// NOTE: this happens when a GC call is already running
c.log.Warn().Msg("garbage collection on value log already running")
return
}
if err == badger.ErrNoRewrite {
// NOTE: this happens when no files have any garbage to drop
c.log.Debug().Msg("garbage collection on value log unnecessary")
return
}
if err != nil {
c.log.Error().Err(err).Msg("garbage collection on value log failed")
return
}
runtime := time.Since(started)
c.log.Debug().
Dur("gc_duration", runtime).
Msg("garbage collection on value log executed")
c.metrics.RanGC(runtime)
}()
}