-
Notifications
You must be signed in to change notification settings - Fork 175
/
pruner.go
209 lines (174 loc) · 5.92 KB
/
pruner.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
package pruner
import (
"context"
"fmt"
"time"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/component"
"github.com/onflow/flow-go/module/executiondatasync/tracker"
"github.com/onflow/flow-go/module/irrecoverable"
"github.com/onflow/flow-go/module/util"
)
const (
defaultHeightRangeTarget = uint64(400_000)
defaultThreshold = uint64(100_000)
)
// Pruner is a component responsible for pruning data from
// execution data storage. It is configured with the following
// parameters:
// - Height range target: The target number of most recent blocks
// to store data for. This controls the total amount of data
// stored on disk.
// - Threshold: The number of block heights that we can exceed
// the height range target by before pruning is triggered. This
// controls the frequency of pruning.
//
// The Pruner consumes a stream of tracked height notifications,
// and triggers pruning once the difference between the tracked
// height and the last pruned height reaches the height range
// target + threshold.
// A height is considered fulfilled once it has both been executed,
// tracked, and sealed.
type Pruner struct {
storage tracker.Storage
pruneCallback func(ctx context.Context) error
// channels used to send new fulfilled heights and config changes to the worker thread
fulfilledHeights chan uint64
thresholdChan chan uint64
heightRangeTargetChan chan uint64
lastFulfilledHeight uint64
lastPrunedHeight uint64
// the height range is the range of heights between the last pruned and last fulfilled
// heightRangeTarget is the target minimum value for this range, so that after pruning
// the height range is equal to the target.
heightRangeTarget uint64
// threshold defines the maximum height range and how frequently pruning is performed.
// once the height range reaches `heightRangeTarget+threshold`, `threshold` many blocks
// are pruned
threshold uint64
logger zerolog.Logger
metrics module.ExecutionDataPrunerMetrics
component.Component
cm *component.ComponentManager
}
type PrunerOption func(*Pruner)
// WithHeightRangeTarget is used to configure the pruner with a custom
// height range target.
func WithHeightRangeTarget(heightRangeTarget uint64) PrunerOption {
return func(p *Pruner) {
p.heightRangeTarget = heightRangeTarget
}
}
// WithThreshold is used to configure the pruner with a custom threshold.
func WithThreshold(threshold uint64) PrunerOption {
return func(p *Pruner) {
p.threshold = threshold
}
}
func WithPruneCallback(callback func(context.Context) error) PrunerOption {
return func(p *Pruner) {
p.pruneCallback = callback
}
}
// NewPruner creates a new Pruner.
func NewPruner(logger zerolog.Logger, metrics module.ExecutionDataPrunerMetrics, storage tracker.Storage, opts ...PrunerOption) (*Pruner, error) {
lastPrunedHeight, err := storage.GetPrunedHeight()
if err != nil {
return nil, fmt.Errorf("failed to get pruned height: %w", err)
}
fulfilledHeight, err := storage.GetFulfilledHeight()
if err != nil {
return nil, fmt.Errorf("failed to get fulfilled height: %w", err)
}
fulfilledHeights := make(chan uint64, 32)
fulfilledHeights <- fulfilledHeight
p := &Pruner{
logger: logger.With().Str("component", "execution_data_pruner").Logger(),
storage: storage,
pruneCallback: func(ctx context.Context) error { return nil },
fulfilledHeights: fulfilledHeights,
thresholdChan: make(chan uint64),
heightRangeTargetChan: make(chan uint64),
lastFulfilledHeight: fulfilledHeight,
lastPrunedHeight: lastPrunedHeight,
heightRangeTarget: defaultHeightRangeTarget,
threshold: defaultThreshold,
metrics: metrics,
}
p.cm = component.NewComponentManagerBuilder().
AddWorker(p.loop).
Build()
p.Component = p.cm
for _, opt := range opts {
opt(p)
}
return p, nil
}
// NotifyFulfilledHeight notifies the Pruner of the latest fulfilled height.
func (p *Pruner) NotifyFulfilledHeight(height uint64) {
if util.CheckClosed(p.cm.ShutdownSignal()) {
return
}
select {
case p.fulfilledHeights <- height:
default:
}
}
// SetHeightRangeTarget updates the Pruner's height range target.
// This may block for the duration of a pruning operation.
func (p *Pruner) SetHeightRangeTarget(heightRangeTarget uint64) error {
select {
case p.heightRangeTargetChan <- heightRangeTarget:
return nil
case <-p.cm.ShutdownSignal():
return component.ErrComponentShutdown
}
}
// SetThreshold update's the Pruner's threshold.
// This may block for the duration of a pruning operation.
func (p *Pruner) SetThreshold(threshold uint64) error {
select {
case p.thresholdChan <- threshold:
return nil
case <-p.cm.ShutdownSignal():
return component.ErrComponentShutdown
}
}
func (p *Pruner) loop(ctx irrecoverable.SignalerContext, ready component.ReadyFunc) {
ready()
for {
select {
case <-ctx.Done():
return
case height := <-p.fulfilledHeights:
if height > p.lastFulfilledHeight {
p.lastFulfilledHeight = height
}
p.checkPrune(ctx)
case heightRangeTarget := <-p.heightRangeTargetChan:
p.heightRangeTarget = heightRangeTarget
p.checkPrune(ctx)
case threshold := <-p.thresholdChan:
p.threshold = threshold
p.checkPrune(ctx)
}
}
}
func (p *Pruner) checkPrune(ctx irrecoverable.SignalerContext) {
if p.lastFulfilledHeight > p.heightRangeTarget+p.threshold+p.lastPrunedHeight {
pruneHeight := p.lastFulfilledHeight - p.heightRangeTarget
p.logger.Info().Uint64("prune_height", pruneHeight).Msg("pruning storage")
start := time.Now()
if err := p.storage.PruneUpToHeight(pruneHeight); err != nil {
ctx.Throw(fmt.Errorf("failed to prune: %w", err))
}
if err := p.pruneCallback(ctx); err != nil {
ctx.Throw(err)
}
duration := time.Since(start)
p.logger.Info().Dur("duration", duration).Msg("pruned storage")
p.metrics.Pruned(pruneHeight, duration)
p.lastPrunedHeight = pruneHeight
}
}